Skip to content

Commit ea3fcce

Browse files
authored
Create 5.3.1.old.md
1 parent c6e12c8 commit ea3fcce

1 file changed

Lines changed: 120 additions & 0 deletions

File tree

Lesson5/5.3.1/5.3.1.old.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Intermediate Coding - Lesson 5.3.1 - Loops - Using FOR loops activity
2+
3+
### @explicitHints true
4+
5+
## Step 1 @unplugged
6+
In this activity, we will use ``||LOOPS:for index from 0 to 4||`` loop blocks to add some functionality to our Minecraft game.
7+
8+
In this program we will program our agent to build a wall of blocks of a length we can pass into the chat command.
9+
10+
## Step 2
11+
From the Player drawer drag out a ``||PLAYER:on chat command||`` event block and change it to respond to the chat command **build**.
12+
#### ~ tutorialhint
13+
```blocks
14+
player.onChat("build",function () {
15+
16+
})
17+
```
18+
## Step 3
19+
From the ``||LOOPS:LOOPS||`` drawer place a ``||LOOPS:for index from 0 to 4||`` block inside the ``||PLAYER:on chat command||`` block.
20+
#### ~ tutorialhint
21+
```blocks
22+
player.onChat("build", function () {
23+
for (let index = 0; index <= 4; index++) {
24+
25+
}
26+
})
27+
```
28+
29+
## Step 4
30+
From the ``||AGENT:AGENT||`` drawer place an ``||AGENT:agent move forward by 1||`` block and an ``||AGENT:agent place back||`` block inside the for loop.
31+
32+
This will cause our agent to move forward by one and lay a block behind them.
33+
34+
If we run our code now the agent will place 5 blocks.
35+
36+
Next we will change the chat command to allow us to provide the number of blocks to lay.
37+
#### ~ tutorialhint
38+
```blocks
39+
player.onChat("build", function () {
40+
for (let index = 0; index <= 4; index++) {
41+
agent.move(FORWARD, 1)
42+
agent.place(BACK)
43+
}
44+
})
45+
```
46+
47+
## Step 5
48+
Click on the + symbol to the right of the ``||PLAYER:on chat command "build"||`` block to add a variable to pass into this command.
49+
#### ~ tutorialhint
50+
```blocks
51+
player.onChat("build", function (num1) {
52+
for (let index = 0; index <= 4; index++) {
53+
agent.move(FORWARD, 1)
54+
agent.place(BACK)
55+
}
56+
})
57+
```
58+
59+
## Step 6
60+
Click on the downward arrow of the variable num1 which was created and rename the variable to length.
61+
62+
#### ~ tutorialhint
63+
```blocks
64+
player.onChat("build", function (length) {
65+
for (let index = 0; index <= 4; index++) {
66+
agent.move(FORWARD, 1)
67+
agent.place(BACK)
68+
}
69+
})
70+
```
71+
72+
## Step 7
73+
From the variables drawer drag the length variable and place it on the number 4 in the for loop.
74+
75+
This changes the for loop to respond to the number we have passed into the build command.
76+
77+
#### ~ tutorialhint
78+
```blocks
79+
player.onChat("build", function (length) {
80+
for (let index = 0; index <= length; index++) {
81+
agent.move(FORWARD, 1)
82+
agent.place(BACK)
83+
}
84+
})
85+
```
86+
87+
## Step 8
88+
Next let's test the code.
89+
90+
Click Next for instructions on how to test your code.
91+
92+
## Step 9 @unplugged
93+
To test your code:
94+
95+
Once you have fully read this step click the Green Ok button below to close this window to return to the tutorial to perform the below steps to test your code.
96+
97+
1. Click on the **Green Start button** below to return to the game.
98+
![alt text](https://intermediatev3.codingcredentials.com/Lesson2/2.1.1/images/2.jpg?raw=true "Start")
99+
100+
101+
2. Get blue concrete blocks from the chest and give them to your agent in their inventory in slot 1.
102+
![alt text](https://intermediatev3.codingcredentials.com/Lesson5/5.3.1/images/1.jpg?raw=true "FOR")
103+
104+
105+
3. Run the chat command **build 3** and observe your agent build a wall.
106+
![alt text](https://intermediatev3.codingcredentials.com/Lesson5/5.3.1/images/2.jpg?raw=true "FOR")
107+
108+
## Step 10
109+
Follow the instructions in the previous step to test your code.
110+
111+
When your code works as expected click next to continue.
112+
113+
If it does not work as expected try to fix and test again
114+
115+
Did you notice how many blocks were placed? We specified that we wanted a wall of 3 blocks by passing the number 3 in yet the agent placed 4 blocks. Do you know why this is?
116+
117+
Remember to for loop enumerates from 0 to the number specified therefore when we pass in the number 3 the loop will enumerate when the loop control variable is 0, 1, 2 and 3 - 4 times in total!
118+
119+
## Step 11 @tutorialCompleted
120+
Return to the game and move onto the next non-player character.

0 commit comments

Comments
 (0)