Posts

Conditionals and loops

Detect and Inspect

These three instructions are useful to decide what or if there is a block near the turtle.

The detect command detects if there is an existing block ahead, up or below the turtle. If there is not, the turtle can move. Else, you may use the dig command to destroy the block.

The following program will make the turtle move 10 blocks. If there is an obstacle, the turtle will destroy it.

The rep command stands for repeat.

Repeat is a block which must be written as following:

rep <number> do

<instructions>

end

Inside a repeat loop you can nest other instructions as well.

In the next example the turtle will check 10 times if there is a block ahead; if it is true, it will break the block; in any case it will move 10 blocks forward.

The if statement is a conditional. It is checked once. If it is true it will execute whatever inside the command. An if statement must be written as following:

if <condition> then

<instructions>

end

The next program is an example of the inspect block. While the turtle inspects the block ahead and it is not a block of cobblestone, the turtle moves.

The turtle will stop moving when face a cobblestone block.

The while loop is the second iterative control statement.

A while loop must be written as following:

while <condition> do

<instructions>

end

In the while loop the instructions inside are executed as long as the condition is true. The moment the condition turns false there will be an exit from the loop.

In a repeat loop you must state in advance the number of repetitions. In a while loop this number is unknown as the condition may turn false at any point.

Build a bridge with the while loop

The following code will make the turtle build a bridge, provided that the turtle moves in the air.

In particular, the turtle detects if there is a block below.

If not, the turtle places a block from the first slot of its inventory.

It is required that there are some block items (stone or dirt etc) in the turtle’s inventory.

A fully functional program is the following.

The turtle will keep moving until detects a block.

If below the turtle is nothing, a block will be placed.

This way the turtle will build a bridge while walking in the air.

Of course, if there is nothing ahead the turtle will keep moving forward forever.

 

 

 

Download the lesson in pdf

Program a mining turtle

To program a turtle is an awesome way to learn the fundamental concepts of programming while playing your favorite game.

You will need a mining turtle and a remote control.

To craft a turtle, you will need 7 iron ingots, 1 computer and 1 chest:

To turn your turtle to a mining turtle you will need a pickaxe:

To control your turtle, you will need a remote control. Right click on the turtle and get ready to code your first program in Minecraft!

There are a few tabs you can use when you right click on a turtle:

The last one is the remote; you can make basic moves (up, down, forward, back, turn, dig, place).

The second tab is used to customize your turtle’s appearance.

The first is the program tab; here you can drag and drop your programming blocks.

There is a huge list in your disposal.

You may use a control statement (if, while, repeat), move or turn commands and a list of actions as well (detect, compare, inspect etc).

The third one is the turtle’s inventory. You will need items if you want the turtle to place blocks.

You can exchange items between your own and the turtle’s inventory.

You can now program your turtle using appropriate instructions. In the following code the turtle will move forward 10 blocks.

As you can see, you may drag and drop the instructions from the right part of the programming tool and place them in the grid to the left part. If the program has no errors, it will be executed and see the results.

You can switch from visual to code editor to see the actual code running. The language used by MinecraftEdu is Lua.

 

Download the lesson in pdf

ComputerCraft

What ComputerCraft is about?

It is a mod you can download and install

  • Mods (short for modifications) are anything that changes Minecraft’s game content from what it originally was.

ComputerCraft adds computers that you can program in Lua

  • Lua is a powerful, efficient, lightweight, embeddable scripting language.
  • “Lua” (pronounced LOO-ah) means “Moon” in Portuguese. As such, it is neither an acronym nor an abbreviation, but a noun.

ComputerCraft also adds Turtles – little robots

How to craft a computer?

Recipe for a computer:

Advanced computer (mouse support & colors):

This is what your computer should look like

This is called the terminal or command line interface.

We can type into this and the computer will receive the keystrokes.

Commands to practice

list

The list program shows the files and folders in the current directory

list <name>

We can see the contents of the <name> folder

cd <name>

Change our current directory to the <name> folder

cd ..

Changes the directory to be one level higher than the current directory

lua

The “lua” program starts another command-line interface where we can run lua code directly

Let’s run little bits of actual lua code

Type in ‘x = 1 + 1’ and hit Enter

Type ‘x’

Type ‘print(“hello world”)’

exit() function to exit the lua prompt

… and that’s it!

Create/edit a file

edit startup

Tells the edit program to edit the file named “startup” in the current directory. Since that file doesn’t exist, it will open up a blank editor.

When we save the file in the editor, it will automatically create the file since it did not exist already.

The edit program is one of the programs in ComputerCraft that requires an argument when you start it.

edit myFile

Type ‘print(“Hello minecrafters”)

Ctrl > Save > Exit

Type ‘myFile

How to print, do math, run a program

How to print stuff to a computer

print()

print(“Hello”)

How to do math with a computer

print(5+5)

How to run a program

Just type the name of the file/program

Declare Variables

local variables

local a=10

print(a^2)

Get input from user

print(“type your name”)

Declare the variable name to store the answer of the user

local name=read()

print(name)

print(“name”)

The “if” statement

The general form of the if statement is the following:

if <condition> then

<commands>

else

<commands>

end

Create a program to ask your name, read it and if it is valid print “Hello”, if not, print “Who are you?”

if (name==“Christos”) then

print (“Hello”)

else

print (“Who are you?”)

end

Craft a disk drive and a disk

Recipe for disk drive (device needed to read floppy disks):

Floppy disk (a storage media to copy files):

Use a floppy disk

Place the disk drive directly adjacent to the computer

Right click the disk drive to open its inventory interface and place the floppy disk into the drive

If you attach successfully a disk drive to a computer, by typing “list” you see that there is a folder named “disk”.

You can now create, edit, move or copy files into the disk and exchange files with other players or computers.

Assign a label to a disk

You can assign the disk a label

Use the same label command you used to set the computer’s label

You have to include the name of the side that the disk drive is on, in order to set the disk’s label rather than the computer’s.

If your disk drive is on the right side of the computer, you should use the command `label set right test-disk` to set the disk’s label to “test-disk“

Exercise 1: Create a program in a disk

This program should:

  • Ask for your name
  • While the name you type is not the correct one, ask again
  • The loop should end when you type the correct name (your name)

Solution

Reviewing the if statement

The if statement is a Control Structure

Control structures effect the flow of the program

The if statement is executed once

if <condition> then

<commands>

else

<commands>

end

Example of an if statement

x = 1
print(“Let’s test if x > 0 …”)
if (x > 0) then
print(“It is”)
else
print(“It is not”)
end
print(“Test is over”)

— Result:
— Let’s test if x > 0 …
— It is.
— Test is over

Let’s explain the above code:

The while loop

The while statement is a control function

It keeps running until the condition is false

A condition could be either true or false

while <condition> do

<commands>

end

Example of a while loop

–Example of while control structure
x = 1
print(“Let’s count up to 5“)
while x <= 5 do
print(x,“…”)
x = x + 1
end
— result:
— Let’s count up to 5
— 1…
— 2…
— 3…
— 4…
— 5…

Exercise 2: Create a password protected door

Create an iron door

Place a computer next to it or connect the computer to the door using redstone

Create a program in the computer and name it startup (to run every time the computer reboots)

The program must ask for the password (“haef”) and grant access or not

Solution

print(“Give the password”)
a = read()
if a == “minecraft” then
print(“Access granted”)
redstone.setOutput(“right”,true)
sleep(5)
redstone.setOutput(“right”,false)
else
print(“Access denied”)
sleep(2)
end
reboot()

minecraft in education

The fundamental concepts of programming through Minecraft

Christos KatsoulasComputer Science teacher katsoulas.info