Coding With Minecraft - Unit 5 - Conditionals
Coding With Minecraft - Unit 5 - Conditionals
Unit 5 – 4 Lessons
60 minutes per lesson
Single Student
EDUCATION.MINECRAFT.NET
1
THEME OVERVIEW
In this unit, students will explore the coding concept of conditionals. An important
part of programming is telling the computer when to perform a certain task.
Conditionals accomplish this task by requiring a certain condition or rule to be met
before an action is performed. Students will have the opportunity to participate in
unplugged activities, code programs using conditionals, and evaluate and improve
existing code. Students will the participate in the final activity to use collaborative
design and pair programming techniques to solve a problem players encounter in
Minecraft.
LESSON OVERVIEW
All of the following lessons are approximately 45-60 minutes, depending on how
much time students are given to explore the coding activities:
• Lesson A: Introduction to Conditionals
• Lesson B: Coding with Conditionals (this lesson is broken into 2 sessions)
• Lesson C: Get Creative with Conditionals
LESSON OBJECTIVES
• Learn and apply the coding concept of conditionals
• Create IF THEN and IF ELSE THEN conditional statements
• Explain the importance of conditionals in coding and while playing Minecraft
• Evaluate code to identify problems and debug the problem code
• Work collaboratively to design an original creative project to apply their
coding skills in new ways
MINECRAFT MECHANICS
C C
Summons the Agent and opens the MakeCode
interface
T T
2
Opens chat panel in Minecraft for commands to
be typed
ESC ESC
When a student wants to leave the game, leave
chat, or pause the game
CODING BLOCKS
On chat command
Runs the code when the student types the
chosen text in the chat window
If
Run code depending on whether a
condition is true or false
Else
Runs a specific code depending on whether
a condition is met or not met
Else if
Another conditional action to add an if to
an else for an else if
While
Repeats the code is a defined condition is
met
KEY VOCABULARY
Conditional – if a certain condition or rule must be met before an action is
performed
3
LESSON ACTIVITIES
Lesson A: Introduction to Conditionals (Slides 1-28)
Direct Instruction (Teacher-Led; “I Do”)
Welcome to back to Coding with Minecraft, a place to learn all about the basic
concepts of computer science. Over the next couple of sessions, we are going to
learn about conditionals. (slide 1)
Today, we are going to learn about what are conditionals and how we use them
not only in real-life, but how they are used in Minecraft. (slide 2)
Students are already familiar with the concept of conditionals in their daily lives.
Have they ever had a parent say any of the following?
• “If you clean your room, you can go out with your friends.” (slide 5)
• “If your homework is done, you can play video games.” (slide 6)
These are all conditionals! Conditionals follow the format of IF this, THEN that.
There are also IF THEN ELSE statements: IF (condition is met), THEN (action
performed), or ELSE (action performed).
Example: (slide 8)
IF it is snowing, THEN wear boots, ELSE wear shoes.
The ELSE portion makes sure that some action is performed regardless. Regardless
of whether it is snowing or not, you should probably wear something on your feet.
4
For the “IF it is snowing, THEN wear boots, ELSE wear shoes” conditional, notice
that without the ELSE action, your students might end up not wearing any
footwear!
Have the students share a few conditionals from their own lives with the class or
within small groups.
How to play
• Everyone stands up
• The teacher will say a series of conditional statements – some may just be
If… Then…, but some may be IF… Then… Else.
• Students will follow the instructions
Teacher tips
• Lead the activity to practice conditionals with simple instructions, yet visually
different physical poses.
• Verify that students have correctly executed the instructions based on the
criteria.
• Read the conditional statements slowly and clearly.
• Make sure students understand and are following along with each set of
statements.
• Reset the students to normal standing positions between each conditional
statement.
• Ask the class for a few volunteers to create their own conditional statements
for the rest of the class to follow along.
5
• IF the month of your birthday ends with a ‘Y’ or an ‘R’, THEN raise both your
arms
• IF you play soccer, THEN kick your feet (be careful not to kick anyone)
• IF your favorite ice cream flavor is chocolate, THEN stick out your
tongue, ELSE make rabbit ears behind your head
• IF you play a musical instrument, THEN snap your fingers, ELSE whistle
• IF your favorite monster in Minecraft is a zombie, THEN make zombie noises
and movements, ELSE jump up and down
6
Examples (slide 11)
By default, IF statements have a hexagon that says “true” in them when you first
place them. If you don’t change this, then anything that the “If… Then…” block
encloses will run every time. “True” is always true.
However, you can substitute another block, or a combination of blocks, next to the
If. The blocks you use as conditions do not have to be hexagons but ultimately
should resolve to true or false.
7
This example from the BLOCKS Drawer tests for a certain type of block at a specific
set of coordinates.
In this example, they represent the player’s exact location. This is always going to
be false because the player is standing there, so there cannot be a block where its
head or body is. However, you can specify ~0 -1 ~0 to check the block the player is
currently walking on, or even use a Position range to detect the presence of a
certain block within a given area.
In this chapter, you’ll explore some of the possibilities that the different
conditional blocks provide, by automating some common Minecraft tasks like
chopping down trees or mining. You’ll also be building things like pyramids and
making your code work a bit more intelligently so it responds to what the user
inputs. Then you’ll challenge yourself to create your own independent project
automating some other Minecraft task of your choice. Let’s go!
Before you begin this coding activity, make sure you are set-up to demonstrate
and lead the students through this coding experience. It is best if you complete
this activity on a device connected to a projector or presentation screen. Split the
screen and display the classroom presentation alongside of the Minecraft world.
Have all students log into Minecraft: Education Edition. After students have logged
into the platform, navigate to the Coding with Minecraft subject kit. (slide 14)
8
Once in the Coding with Minecraft subject kit, select Unit 5: Conditionals to begin
the lesson. (slide 15)
In this programming activity, students will practice using the Conditional if then
else block in MakeCode. They will code a program that uses a condition to
compare their friend’s age with their own, and print different messages if they are
younger, older, or the same age. (slide 16)
Steps for the Coding Activity: How Old Are You? (slides 17-24)
• Step 1: Rename the existing on chat command "run" to "age".
Click the Plus (+) sign on the on chat command "age" to create
a num1 variable parameter.
Rename num1 to FriendsAge. This makes your code more readable. Always
use meaningful names when making variables in coding. This makes finding
errors more intuitive. You can rename variables from the drop-down menu.
• Step 2: From LOGIC, drag a if then else inside on chat command "age". Click
the Plus (+) sign in the if then else to create another else if branch.
• Step 3: From LOGIC, drag a Less than, 0 < 0, comparison block into the if slot,
replacing true.
• Step 4: Again from LOGIC, drag an Equals, 0 = 0, comparison block into the else
if slot.
• Step 5: From the VARIABLES Toolbox drawer, drag two of
the FriendsAge blocks into the first slot of each of the Comparison blocks. In
the second slot of the Comparison blocks, enter your age (for example, 12).
• Step 6: From BLOCKS, drag a print "Hello" onto your workspace. Then you
can right-click print "Hello" and select Duplicate to make a copy. Place one
in each of your if, else if, and else clauses.
• Step 7: In each print "Hello", type a different message for people who are
younger than you, your same age, or older than you. Use the drop-down
menu in the print blocks to select a different block to use for each message.
• Step 8: Change the grass blocks to whatever block you like. Then, in all
the print blocks, set the Y coordinate to 10 (so these messages print in the
sky).
Run and test the program for How Old You Are? after you import the program into
Code Builder.
9
Step 1: Share your program. Step 2: Name the project, provide a
brief description, and select the
“Publish to share” button.
Step 3: Select the green “Copy link” Step 4: Inside Code Builder, select the
button. “Import” button.
Coding Solution:
Challenges
11
Challenge 1 - Compare Ages and Modify Messages
Rather than compare students at 12 years old, compare students to another age. If
you’re brave, ask your teacher how old they are and put their age in for
comparison. Modify the messages for people who are younger than your new age,
the same age, or older than your new age. Finally, modify the blocks that print.
Extensions
There are no rules for these extensions. See what you can come up with!
• Extension 1 - Try Other Types of Conversions
Use this extension to change your weight from pounds (lbs) to kilograms
(kg) and vice versa.
o Use this code to start with.
12
o If you enter weight in the chat window, the code shows you a menu
that explains the program.
o If Statements allow you to detect what the user wants to convert to.
Also, an If Statement checks whether it is ok to display an answer
message.
o How could you change this code so that the output is not in the chat
window? Maybe you could print blocks instead as an answer… What
other things could you ask people?
• Extension 2: Create Blocks and Items
o This code has a menu. Enter create in a chat window.
o The code accepts the ID for blocks and items.
o If the ID is for a block (1 - 255), the block is placed.
o If the ID is for an item (256 - 452), the item is equipped to your
character.
LESSON CONCLUSION
Upon completion of this lesson, students should be able to answer the following
questions: (slide 26)
13
Conclude the lesson by reviewing the concepts covered in Lesson A. (slide 27)
Chopping trees for wood is hard work but necessary in Minecraft if you want to
craft objects and tools in Survival mode. But you can automate this chore through
code with the help of our Agent! Let’s teach the Agent how to chop down a tree of
any height and return back down to the ground. (slide 31)
The last activity used if then else statements to check conditions and perform
actions the way you wanted them done. In this activity, you will use conditionals in
a while loop. The agent will continue moving up as long as there is a block of tree
to climb. (slide 33)
Have all students log into Minecraft: Education Edition. Students will need to
navigate to the coding tutorials from the in-game library. Directions for navigating
to the tutorials can be found on slide 34.
Once students log into the world, they will begin game play at their spawn point.
(slide 35)
14
Students should select “Blocks” as their programming language. (slide 37)
They will respawn into a new area. They will see a group of trees in front of them.
Select “C” to begin coding. (slide 38)
You should demonstrate each step of the coding activity alongside students.
Model each individual step. (slide 39)
After the code is complete, your Agent will spawn onto the gold block. However, in
order for the Agent to successfully chop down the tree, they must be standing
directly in front of the tree. You will need to position the Agent directly in front of
the tree before running the code. You (the player) need to stand in the exact
location (and direction) of where you want the Agent to teleport to. Once you are
standing in the spot, open the chat window and type “tp”. Your Agent will then
teleport to this exact spot. (slide 40)
Then, you should run and test your code to see if the Agent successfully chops
down a tree! (slide 41)
Students can continue to practice by chopping down the rest of trees in the row.
Coding Solution:
15
Extensions
Extension 1 – Increase the Size of a Hole
The Agent works hard for you! You can get it to dig for you too. Enter the on chat
command dig 4 to try out this code.
Can you get the Agent to dig and place something at the same time? Can you
increase the size of the hole? Right now, it is 2 x 2. You might get the Agent to dig a
4 x 4 hole and line it with gold as it goes down.
16
Independent Work (Teacher Support; “You Do”)
Coding Activity: All Mine (slide 40)
Import the Program:
https://minecraft.makecode.com/?ipc=1#tutorial:github:mojang/educationconten
t/CodingWithMinecraft/unit_5/lesson_B/all_mine
In addition to chopping trees for wood, you will probably want to mine the earth
for precious minerals. One good strategy for finding the rarest of minerals,
diamond, is to tunnel deep beneath the earth to levels Y12 or Y13, and then create
a series of branching tunnels off a central passageway.
In this activity, you’ll create a basic mining agent that automatically destroys
blocks in front of it and collects everything. You’ll use conditional statements to
test for precious minerals. The agent is one block tall, so you’ll send it forward 64
blocks, and then have it turn around, move up one block, and come back. If the
agent finds anything of value, it will let you know and collect the ore.
Do the activity
Create controls for the Agent
1. Create a new MakeCode project and name it All Mine.
2. Rename the existing on chat command to "do".
Because you are always doing things with your agent, you should make it easy to
command it. You can do this with a if then else statement. You can make a simple
menu to handle all the agent commands.
3. Drag a if then else into on chat command "do". Click the (+)
sign on the if then else to add a third branch for this because you will test
for three conditions.
4. Click the (+) sign on on chat command "do" and
rename num1 to AgentOrder.
17
Now just test to see what the user entered. You want the user to be able to
teleport the agent and turn it. The third branch will give the user some information
if nothing is entered.
5. From LOGIC, grab the ‘0 = 0’ comparison block and duplicate this because
you need two.
6. Place these into the if then else.
7. Adjust these so you test for what the user entered. We need to test the value
of AgentOrder. Let’s say a value of 1 means the agent teleports and a value
of 2 means the agent should turn.
Last, you just need to add blocks to the branches to make your agent do the
actions.
8. From AGENT, add agent teleport to player into the first branch of your if
then else.
9. From AGENT, add agent turn into the second branch of your if then else.
10. In the last branch, add a message to give the user directions. From PLAYER,
add a say to the third branch of your if then else.
11. Adjust the text in the say to read Enter 1 to teleport or 2 to turn.
You can only mine diamonds in Survival mode, so the first thing you should do is
make sure you change the game mode to Survival.
13. From GAMEPLAY, place change game mode into on chat command "dig".
14. Adjust change game mode by clicking the player selector drop-down menu
to select yourself @s.
From LOGIC, drag a if then else out and drop it after the change game mode block.
18
16. From AGENT, drag inspect into the first slot of the Equals, 0 = 0, replacing
the number 0. The inspect block will check the block directly in front of the
agent.
17. From BLOCKS, drag a block out and drop it into the second slot of the
Equals, 0 = 0.
18. In block, use the drop-down menu to select Diamond Ore as the block you
are checking. (You can also use the Search bar to find it.)
Get rich!
If you do find Diamond Ore, then you’ll want to print a message to the screen and
mine the ore.
19. From PLAYER, drag say and drop it into the first branch of the if then else.
20. In the say block, enter a message like "We’re rich!".
21. From AGENT, drag the following three blocks and drop them in order under
the say block: agent destroy, agent move, and agent collect all.
19
Getting down there!
Remember that you will first need to teleport your character down to where you
can mine.
Find your world position in the game (press the F1 key to see your world position
on the screen), and then enter the command: /tp X 12 Z where X and Z are your
current world coordinates. This will teleport you directly down to level 12 altitude
in your Minecraft world. Make sure you equip yourself with a pickaxe and torches
to make some room and to see down there!
Coding Solution
20
The agent might not respond the way you initially think it should. The agent will
not dig unless it is near your character when the command is issued, so you might
get a message like this:
Remember that sometimes restarting your code can be helpful if you find things
are not working as they should.
Challenges
21
Let’s set up a on chat command that will check for two conditions. If the user
enters 1, the character will be teleported down to level 12 automatically. If the user
enters 2, the user will be teleported out of the ground.
Experiments
Here, there are no rules… Copy the code for each experiment and change things
around to see what kind of results you can create. Suggestions are given, but do as
you like!
Experiment 1 - Standard Checkerboard
Have your agent build a checkerboard. Make sure slot 1 and 2 have blocks in the
agent’s inventory!
22
This uses a bit of math and a if then else to alternate blocks and give that
checkerboard look.
Use this code to start with.
How could you change this? Could you make other patterns by playing with the if
then else?
23
Could you add ideas from other code you have seen to make this different and
more interesting? What about one block up and the next down?
The agent turns randomly with the help of a if then else that checks what random
number is generated.
24
LESSON CONCLUSION
Upon completion of this lesson, students should be able to answer the following
questions: (slide 44)
1. What is the other block that can be used for conditionals but isn’t located
in the Logic toolbox drawer?
Answer: While loop block
2. What are two ways to restart your code?
Answer: Use the stop button or resave your project
Conclude the lesson by reviewing the concepts covered in Lesson B. (slide 45)
Then, students should take the quiz (found in the assessment guide). This is
intended as a low-stakes formative assessment tool to establish whether students
are prepared to take on the unit independent project.
Questions Answers
1. What is an example of an IF Responses will vary, but they need to follow
THEN statement? the format: IF _________, THEN _______
2. What’s an example of an IF Responses will vary, but they need to follow
THEN ELSE statement? the format: IF _________, THEN _______, ELSE
_________
3. In coding, conditionals tell a b. When to do it
computer to do which of the
following?
a. How to do it
b. To do it faster
c. When to do it
d. Where to do it
4. What type of blocks acts like a A while loop
like a conditional but isn’t
located in the LOGIC toolbox?
25
b. An infinite loop
c. You get the message
“Cannot issue
command, Agent is out
of range”
d. All of the above
e. Only a and c
6. Using words, describe what Something like:
the following coding blocks • When you type chop in the chat
do: window
• And your Agent detects a block in
front of it
• The height variable increases by one
block,
• The Agent destroys the block above it
• And moves up by one block
Then provide students with a preview of the next lesson. (slide 60)
26
conditions must be true, or only one of the conditions needs to be true, in order
for the whole expression to be considered true.
For the independent project, work with another student to create a project
together that uses one or more blocks from the Logic Toolbox drawer. Think about
problems you encounter in a Minecraft world and try to come up with solutions for
them. (slide 49)
If you are designing something else, think about how you might find out more
information about your problem through interviewing or observing people playing
Minecraft, with your focus being strictly on studying their behavior in-game. A
good way to focus your observations is to make a list of key questions ahead of
time:
• What do new players do first? How quickly do they decide to go
underground?
• How long do they stay there?
• Do they tend to stay in one place or migrate?
• Which characteristics make a good place to build a base?
Then start brainstorming with your partner. Talk about a variety of different ideas.
Remember that it’s okay if the ideas seem far-out or impractical. Some of the best
programs come out of seemingly outlandish ideas that can ultimately be worked
into a useful program.
Pair Programming
The concept of “pair programming” is a valuable way to have programmers
collaborate when creating programs together. Two programmers share one
computer, with one person at the keyboard acting as the driver and the other
person providing directions as the navigator. If you are collaborating with
someone else on a project, try using one computer and take turns creating code
27
and then seeing how it behaves in the Minecraft world. The main rule is, if you are
at the keyboard, you are doing what the other person is telling you to do. They are
the navigator! Remember to practice good communication with each other
throughout the entire programming process.
Debugging
Be sure to test out a number of different ideas in Minecraft. What is the easiest
way to keep track of data? You can test to see if certain conditions are actually
being detected by adding a Say block inside the If Then block. If you do something
in the world that should trigger the condition, but you don’t see the message
printed to the screen, then you know that something is wrong with your condition.
Similarly, if you are updating your variables based on certain conditions, you can
use a Say block to print out the value of the variable so you can make sure it is
changing properly. If you are using a number variable, remember to use a Join
block to join it with a word (or empty quotes) so it will print.
28
• Describe one point where you got stuck. Then discuss how you figured it
out.
• Include at least one screenshot of your agent in action.
• Share your project to the web and include the URL here.
Assessment
Competency scores: 4, 3, 2, 1
Diary
4 = Minecraft Diary addresses all prompts.
3 = Minecraft Diary entry is missing 1 of the required prompts.
2 = Minecraft Diary entry is missing 2 or 3 of the required prompts.
1 = Minecraft Diary entry is missing 4 or more of the required prompts.
Logic
4 = Uses conditional statements effectively in a way that is integral to the program.
3 = Uses conditional statements in a way that is integral to the program but some
problems with conditional statement execution.
2 = Uses conditional statements effectively but in a superficial way.
1 = Doesn’t use conditional statements at all or uses loops in a superficial way and
has problems with conditional statement execution.
Project
4 = Project solves a specific problem, efficiently and effectively..
3 = Project lacks 1 of the required elements.
2 = Project lacks 2 of the required elements.
1 = Project lacks all of the required elements.
LESSON CONCLUSION
Upon completion of this lesson, review the concepts covered in this lesson. (slide
53).
29
EDUCATIONAL STANDARDS
CSTA Standards
• 2-AP-11 Create clearly named variables that represent different data types and perform
operations on their values.
• 2-AP-10 Use flowcharts and/or pseudocode to address complex problems as
algorithms.
• 2-AP-12 Design and iteratively develop programs that combine control structures,
including nested loops and compound conditionals.
• 2-AP-13 Decompose problems and subproblems into parts to facilitate the design,
implementation, and review of programs.
• 2-AP-19 Document programs in order to make them easier to follow, test, and debug.
ISTE Standards
• 1.4.c Students develop, test and refine prototypes as part of a cyclical design process.
• 1.5.a Students formulate problem definitions suited for technology-assisted methods
such as data analysis, abstract models and algorithmic thinking in exploring and
finding solutions.
• 1.5.c Students break problems into component parts, extract key information, and
develop descriptive models to understand complex systems or facilitate problem-
solving.
• 1.6.b Students create original works or responsibly repurpose or remix digital
resources into new creations.
30