1756571030928491-Coding With Minecraft - Unit 7 - Iteration
1756571030928491-Coding With Minecraft - Unit 7 - Iteration
Unit 7 – 4 Lessons
60 minutes per lesson
Single Student
EDUCATION.MINECRAFT.NET
1
THEME OVERVIEW
In this unit, students will explore the coding concept of iteration, or ways to make
things repeat. Programmers might repeat actions in a program to have a certain
effect, or might use repetition to accomplish the same task in a smaller number of
steps. Students will have the opportunity to learn, practice, and apply the coding
concepts through unplugged activities and guided coding activities. Then, students
will have the opportunity to demonstrate their new knowledge and skills in the
independent project.
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 Iteration
• Lesson B: Coding with Iteration
• Lesson C: Debugging with Iteration
• Lesson D: Get Creative with Iterations
LESSON OBJECTIVES
• Learn and apply the coding concept of iteration
• Explain the use of iteration in coding and synonymous terms (i.e., loops)
• Describe the different kinds of loops in MakeCode
• Use different types of loops to code and debug more efficiently
• Design an original project to apply the use of loops to create an automated
solution
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
Repeat
Run part of the program the number of
times you say.
For
Run part of the program the number of
times you say using an index variable.
While
Repeat code while a Boolean condition is
true/false.
Forever
Run a part of the program in the
background and keep running it over
again.
KEY VOCABULARY
Iteration – process wherein a set of instructions or structures are repeated in a
sequence a specified number of times or until a condition is met
Loop – structure to repeat code for a specified number of times
LESSON ACTIVITIES
Lesson A: Introduction to Iteration (Slides 1-32)
Direct Instruction (Teacher-Led; “I Do”)
3
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 iteration. (slide 1)
Today, we are going to learn about what is iteration and how iteration is applicable
in real-life, but how they are used in Minecraft. (slide 2)
For instance, in Minecraft you might want to place 10 grass blocks. A loop is a block
structure that allows you to easily repeat placing a grass block. In the Block editor,
loops are green. There are different types of loops for different situations.
You use loops to repeat code as many times as you want. When programmers talk
about repeating code, they use many other words as well. All these words basically
mean “repeat.” You will find them used, at times interchangeably, when you are
learning about code. (slide 5)
• iterate
• iteration
• iterative
• repeat
• repetition
• repetitive
• loop
Programmers use sentences like this: “Let’s iterate through the loop.” This means,
“Run the same code many times, repeating something.”
4
Other examples
What are some other examples of iteration or repeating in your daily life? What
tasks and actions are composed of repeated steps? What are those steps? (slide 6)
• Search the web for Ancient Roman aqueducts - Architects use repetition in
their building designs.
• Search the web for dog fetching ball on beach - Dogs love to play fetch as a
repetitive activity.
• Search the web for honeycomb - There are many repeated patterns found in
nature.
This activity works best with a partner. You will write instructions for your partner
to walk completely around the outside of a Minecraft house and return to the start.
You will need a chair and some way to write things down. Place the chair in the
center of the room. The chair represents a Minecraft house. Your partner should
stand next to one of the corners of the house.
Your goal is to write the instructions for your partner to walk all the way around
the house, using these two commands: (slide 8)
forward()
turn left/right()
5
forward()
turn left()
forward()
turn left()
forward()
turn left()
Programmers like to take shortcuts. A program that has fewer lines of code takes
up less room in memory, and ultimately the shorter your program is, the easier it
is to find mistakes. Whenever you have code that repeats, you have an opportunity
to use a loop to simplify your code.
You have just rewritten eight lines of code as three lines of code by using a loop.
The “repeat” command creates a loop. The code within the loop gets repeated a
certain number of times, and then the program exits the loop.
Counted Loops
Repeat N times For Index 0 to N
6
Both repeat and for loops repeat _n_ number of times. If you have 32 stairs in
your house, going upstairs or downstairs would probably involve taking one step
32 times. Other examples of activities that repeat a set number of times include
entering a building with double front doors, lacing your shoes, and turning the
pages in a picture book.
Similar to a repeat block, a for block also performs its actions the number of
times indicated, but it uses a variable called index that you can use inside the
loop to check (although index is used by default, you can change this variable). If
you need to do something on the fifth time through a loop, this might be a good
loop for you!
A while loop runs as long as the condition inside of it is true. By default, true is
always true, so in effect, a while <true> loop runs forever. Usually, though, you
replace the <true> with a condition that evaluates to true or false. For
example, while <hair is messy> brush hair would continue to repeat brushing hair
until <hair is messy> is false. Some examples of activities that might use a while
loop include while <soup remains> eat with spoon; while <energy greater than 0>
do jumping jacks; and while <she hasn’t seen me> wave furiously.
7
In Minecraft, you might have a while block that repeats as long as the Agent
detects redstone beneath it. You can think of a while block as a repeat block
combined with an if conditional statement – if there’s redstone beneath me, do
these things.
Forever loops
A forever loop starts running when the program starts and keeps going until the
program ends. Some real-life examples are breathing or the beating of your heart.
Before moving on with the lesson, briefly check for understanding with the
following questions: (slide 10)
• What’s another word for iteration?
• What are two ways that loops are beneficial when coding?
Have all students log into Minecraft: Education Edition. After students have logged
into the platform, navigate to the Coding with Minecraft subject kit. (slide 12) Once
in the Coding with Minecraft subject kit, select Unit 7: Iteration to begin the lesson.
(slide 13)
For this coding activity about loops, you will be using the Agent in Minecraft. The
Minecraft Agent is a little robot who can carry out the commands you write in
MakeCode. This activity will walk you through getting started with the agent.
(slide 14)
8
Once inside of the world, press “C” to open Code Builder. Start a new project and
name it “Agent Moves”. (slide 15)
Coding Solution:
Create some additional on chat command commands that will move the agent up
and down.
You could do this in many ways. Regardless of how you solve this challenge, it
might be easiest to create a new on chat command block and put your solution
there.
Extensions
9
There are no rules for these extensions. See what you can come up with!
10
How could you modify this? It would be more impressive if the agent destroyed
the whole cage. You could teleport the agent underground and have it dig back
out. There are tons of possibilities.
Dancing is a perfect example for loops because when you dance, you often repeat
the same moves.
You can select a different direction for each of these commands by choosing from
the drop-down menu. The agent move command moves the agent in the direction
the agent is facing.
agent turn turns the agent. This can be used multiple times to spin the agent
around 360 degrees.
11
There are also additional actions for the agent. At the end or start of these actions,
the agent does an animation. These might be interesting to add to your dance.
You have the moves; now you just need to mix them together! Feel free to mix
some of those commands in to make your dance more interesting.
Sample Code:
12
Challenges
Challenge 1 - A Second Repeat Loop
Recall that LOOPS are great for things that repeat. In the dance above, you use
one repeat loop, but actually there is a place in the code where you could use a
second repeat loop.
Extensions
Here there are no rules… Copy the code for the experiments and change things
around to see what kind of results you can create. Suggestions are given, but do as
you like!
Extension 1 - You Dance When I Say
In this code, you use different kinds of loops to stop and start the robot dancing
whenever you want. You might use the same structure to start the robot digging,
collecting, or doing anything you want. Give it a try!
Extension 2 - Instant Dance Floor
This code uses two functions to make a random dance floor. You will also use a bit
of teleporting trickery. First, you teleport your character, and then you teleport the
agent. Finally, you teleport your character back to its original spot.
How could you change this code? How could you use the two functions that make
random blocks in a different way? Could you add even more to set the mood for a
dance contest? Night time? Lights?
13
Could you find a way to keep the robot on the dance floor no matter what dance
moves he does?
LESSON CONCLUSION
Upon completion of this lesson, students should be able to answer the following
questions: (slide 22)
1. Are agent commands based on coordinates or the direction the agent faces?
Answer: Agent direction
14
2. What another word for iteration?
Answer: Any one of the following: repeat, repetition, repetitive, loop,
iterative, iterate
Conclude the lesson by reviewing the concepts covered in Lesson A. (slide 23)
After everyone has their steps written out, have the students exchange steps. After
students have exchanged their steps, someone might correctly guess that the
repeated action just described is someone using their cell phone.
15
Is this a repeated action? If so, it would be good to use a loop for this if it were in
code. You might even draw a picture of a loop block and write the name of an
example task in the middle to get students to bridge the connection to coding.
If you put these steps in the loop in the picture, how many times would you repeat
the steps?
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. Students should continue
working in the same world from Lesson A. Directions for navigating to the previous
world can be found on slide 30.
Use this starter code from the Introduction to the Agent activity. This will allow you
to easily position your agent, and then you can adapt some of it to do something
new. (slide 30)
16
• From AGENT, put agent destroy down inside on chat command "square".
• Put agent place down inside on chat command "square".
• From LOOPS, grab a repeat loop to surround the existing code in on chat
command “square".
• Adjust the repeat loop to repeat six times.
• Adding agent move forward by 1 at the end of this loop will get your agent
moving while he destroys and places blocks.
Coding Solution:
Then, you will need to publish and import the program into Code Builder.
17
Step 3: Select the green “Copy link” Step 4: Inside Code Builder, select the
button. “Import” button.
Now, we are going to return to our Minecraft world to try out our code. (slide 38)
18
Challenge 1 - Turn One Line into a Square
What is a square? In this case, let’s say four lines. How do you make a square by
using the code you have up to this point as a starting place?
The lesson is about loops, so you might need another, but you will also need one
other block.
19
Extensions
Here, there are no rules… Copy the code for the experiments and change things
around to see what kind of results you can create. Suggestions are given, but do as
you like!
Experiment 1 - Starter Castle
How far can you go with a loop inside a loop inside a… you get the idea. These are
called nested loops, and if you add another with some code, can you make a
castle?
Most castles have four towers. With this code you get two. Go get a sandwich while
your Agent does all the work. After this, you can write code to make it build the
other two towers and walls!
20
What other weird tower designs could you think up? Tinker with the code and
make it happen!
LESSON CONCLUSION
Upon completion of this lesson, students should be able to answer the following
questions: (slide 39)
The following questions can also be used as an exit ticket (formative assessment).
A printable version of these questions can be found in the Assessment Guide.
1. What type of loop does this describe: while <soup remains> eat with a
spoon?
Answer: A while loop
2. What kind of loop starts when a program starts and keeps going until the
program ends?
Answer: A forever loop
Conclude the lesson by reviewing the concepts covered in Lesson B. (slide 40)
21
Lesson C: Debugging with Iteration (Slides 42-66)
Direct Instruction (Teacher-Led; “I Do”)
Welcome back to Coding with Minecraft. We are going to continue to learn about
and practice using iteration. (slide 42)
Have all students log into Minecraft: Education Edition. Students should continue
working in the same world from Lesson B. Directions for navigating to the previous
world can be found on slide 44.
22
So far, using the current code, the agent will till the soil directly in front of it (with
its own diamond hoe) and move forward into that space. It does this six times
because of the repeat loop. Try running the code in Minecraft to see what
happens.
Notice where the agent ends up after the repeat block finishes. This is great, but
you need two rows! You need to get the agent to turn around and till the next row
over so it ends up like the following picture.
23
Independent Work (Teacher Support; “You Do”)
Students should work on debugging the problematic code. (slide 51)
Coding Solution:
Challenges
Challenge - Get Agent to Till a 5x6 Plot of Aligned Soil
Can you change the dimensions of the garden? We need more carrots! Try to
make a 5x6 garden instead of 2x6.
24
There are many ways to do this. Can you figure out a way to do it by adding a third
loop? If not, then try another approach. Positioning your agent before each loop
starts is the most important thing to consider.
Extensions
Here, there are no rules… Copy the code for the experiments and change things
around to see what kind of results you can create. Suggestions are given but do as
you like!
Experiment 1 - Creating a Help Menu for Users
Controlling the agent is not easy, and sometimes it is easy to forget all the
commands. This code has a built-in Help menu. It takes in commands as integers
from 1-6. Entering a ‘?’ gets you help or you can just enter do without an integer to
get the Help menu.
How can you change this code so it moves the agent forward based on a number
the user enters for the on chat command "do"? What if you wanted to enter
something like ‘do 1 4’, for example, to move your agent forward four? Is this
possible? What other code could you build a Help menu for?
25
Experiment 2 - Creating a Raised Farming Bed with Planted Crops
You can use this code to create a raised farming bed with crops. This is a huge
amount of code, but if you look at it block by block, you can probably figure out
how it works. You use functions to organize your code a bit.
Also, you use the builder blocks to construct your raised farming bed. Feel free to
play around with these.
Here is how you use this code:
26
2. Teleport your agent to your character’s location (chat command ‘do 6’).
3. Give your character some seeds (chat command ‘do 7’).
4. You will need to transfer seeds to the agent’s inventory so the robot can
access them when it plants. To do this, right-click on your agent. Move your
character’s seeds to your agent’s inventory as shown in the following
picture. The seeds must be put in that upper-left box, or the code will not
work.
27
7. Plant (chat command ‘do 9’)!
How would you modify this to make your farming bed bigger? Could you get your
agent to plant different crops? What about putting plants in some rows, water in
others, and decorative blocks as a third style for rows? What about getting the
agent to plant seeds in all the soil in the raised farming bed?
LESSON CONCLUSION
28
Upon completion of this lesson, review the concepts covered in this lesson. (slide
52)
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’s a benefit of iteration inAny of the following:
coding? • Fewer lines of code
• Simplified code
• Takes up less room in memory
• Shorter programs
• Makes it easier to find mistakes
2. Match the following types of loops with their definition:
29
Then provide students with a preview of the next lesson. (slide 53)
In this unit, you learned how organizing blocks of code into separate functions
makes your code more readable and saves space. Functions generally contain
sections of code that go together logically and accomplish one thing. The
function’s name usually tells you what it does.
Functions can call other functions. Although functions in MakeCode do not accept
parameters, you can use an on chat command block to pass in a parameter. You
might pass in a number to affect what the function does. Also, you could update
the value of an existing variable and then any function can access that.
One of the most valuable resources in Minecraft are diamonds. You can find
diamonds deep underground in Survival mode. Getting from the surface down to
where diamonds are found is challenging. Luckily, you can program your agent to
do this for you! For this project, come up with a way to teach your agent to dig a
tunnel down to around layer 10-13 underground, where diamonds are usually
found. After you are at that level, you can dig parallel tunnels in search of
diamonds, or even program the agent to do it for you. (slide 56)
For this independent project, choose one of the following options. You can build
any of these styles of staircases, but remember to use loops in your code to help
do it. (slide 57)
30
Option 1 - Straight Staircase (slide 58)
A straight staircase is most common, and it has the added benefit of allowing you
to run up and down quickly if you line the rock with stairs.
This challenge is a little easier if you aren’t placing stairs, but there are a few
things you should keep in mind.
1. You will need to jump up each level, and that increases the rate at which you
need to eat.
2. Also, it’s impractical to bring a horse down into the mine if you don’t have a
wide, tall staircase lined with stairs.
31
Option 2 - Spiral Staircase (slide 59)
Another type of staircase is a spiral staircase. See if you can program the agent to
dig this type of staircase for you.
Spiral Staircase
EXAMPLE
A spiral staircase involves much of the same code that you would use to create a
straight one-block-wide staircase. Think about where you might have to turn. And,
as in all the staircases, you will probably need to follow the agent to place torches
and clean up falling granite.
32
Step 2: Remove the blocks indicated by diorite.
33
Step 4: Continue in the same way, digging a diagonal tunnel.
34
Note that the agent can’t face diagonally like you can, so you will need to figure
out how to move the agent so that it can dig in the right direction. Is this even
possible?
35
• Share your project to the web and include the URL.
Grading Criteria
LOOPS
4 = Uses loops effectively in a way that is integral to the program
3 = Uses loops in a way that is integral to the program, but there are some
problems with the loop output.
2 = Uses loops effectively but in a superficial way
1 = Doesn’t use loops at all or uses loops in a superficial way and has problems
with loop output
PROJECT
4 = Staircase is complete and navigable in both directions. Ends somewhere
around layers 10-13.
3 = Staircase lacks one of the required elements.
2 = Staircase lacks two of the required elements.
1 = Staircase lacks all of the required elements.
LESSON CONCLUSION
Upon completion of this lesson, review the concepts covered in this lesson. (slide
62).
36
EDUCATIONAL STANDARDS
CSTA Standards
• 2-AP-17 Systematically test and refine programs using a range of test cases.
• 2-AP-14 Create procedures with parameters to organize code and make it easier to
reuse.
• 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.
37