Exercise 5

More while loop (nested) and third dimension

We'll play with 3D: y

So we’ll create first a chess Board.

If you look carefully at a board you’ll notice that there is one color when the parity is the same for x and z, and the other color when parity is different. See :
Chess Board : same parity - same color Chess board, one color if parity is the same, another color if parity is different

import mcpi.minecraft as minecraft
import mcpi.block as block
mc = minecraft.Minecraft()
mc.postToChat("Checkmate ! ")
#Here we get the player position
x,y,z = mc.player.getPos()
x = x+1
y = y
z = z+1

    
size = 8
#We will have 2 loop, so we need to integer.
i= j = 0
material = 0
#We will loop on x with i
while i < size:
    i = i+1
#We initiate counter for nested loop
    j= 0
#We will loop on z with j
    while j < size:
        j = j+1
#Chess Board simple
#Here we use the modulo 2 to figure  parity. If it equal 0 then it's even otherwise it's odd.
#In our case we check that both have same parity and affect a materials...otherwise setting another one
        if (i%2 == j%2):
#49 is obsidian : black
            material = 49
        else:
#155 is quartz : white.
            material = 155
        mc.setBlock(x+i,y,z+j,material)

And here is what you should get :
Chess Board in Minecraft Chessboard, with python in Minecraft

You can then play with the modulo and see the result. Here is one example :

import mcpi.minecraft as minecraft
import mcpi.block as block
mc = minecraft.Minecraft()
#Here we get the player position
x,y,z = mc.player.getPos()
x = x+1
y = y
z = z+1

    
size = 17
#We will have 2 loops, so we'll need two integers.
i = 0
material = 0
#We will loop on x with i
while i < size:
    i = i+1
#We initiate counter for nested loop
    j= 0
#We will loop on z with j
    while j < size:
        j = j+1
#Chess Board fun --  Next line is the only modification compared to previous loop - We use a modulo 4 and play with conditions
        if (((i%4) < 3 and(j%4) <3 ) or ((i%4) > 2 and(j%4) > 2 )):
#49 is obsidian : black
            material = 49
        else:
#155 is quartz : white.
            material = 155
        mc.setBlock(x+i,y,z+j,material)

Which will gives you :
ChessBoard - Other conditions Chessboard..sort of… with python in Minecraft

Now we will add a dimension and have a cube of alternated chessboard

import mcpi.minecraft as minecraft
import mcpi.block as block
mc = minecraft.Minecraft()
#Here we get the player position
x,y,z = mc.player.getPos()
x = x+1
y = y
z = z+1

    
size = 8
#We will have 3 loops, so we'll need three integers.
i = 0
material = 0
#We will loop on x with i
while i < size:
    i = i+1
#We initiate counter for nested loop
    j = 0
#We will loop on z with j
    while j < size:
        j = j+1
        k = 0
        while k < size:
            k = k+1
#Here we use the modulo 2 to figure  parity. If it equal 0 then it's even otherwise it's odd.
#In our case we check that both have same parity and affect a materials...otherwise setting another one
            if (i%2 == j%2):
#then we choose one color if our k is even or the other
                if (k%2 ==0 ):
#49 is obsidian : black
                    material = 49
                else:
#155 is quartz : white.
                    material = 155
#Then we have to do the opposite for same k when i and j does not have same parity
            else:
                if (k%2 ==0 ):
                    material = 155
                else:
                    material = 49
            mc.setBlock(x+i,y+k,z+j,material)

Which will gives you :
ChessBoard Cube mode Cube of alternate material

If you change obsidian (49) for air (0) result is quite nice :
ChessBoard Cube mode with air Cube of air and quartz

Here is now the code to generate a colored cube :

import mcpi.minecraft as minecraft
import mcpi.block as block
import time
mc = minecraft.Minecraft()
#Here we get the player position
x,y,z = mc.player.getPos()
x = x+1
z = z+1

    
size = 12

k=0
while k < size:
    j=0 
    while j < size:
        i=0
        while i < size:
#The color will be dependant on the 3 coordinates. You can play a lot with the color 
# and see what is happening when you make variation on one or two coordinates
            mc.setBlock(x+j,y+k,z+i,35,(i+j+k)%16 )
            i=i+1
        j=j+1
    k = k+1

And here is our colored cube:
Colored cube Colored cube