Raspberry pi projects 2015

Page 80

Projects

WorldMags.net It can be frustrating, and sometimes Steve’s temper gets the better of him

Amazingly, that’s the guts of the code all dealt with, but we still need a means by which to input our moves, and of course we need to render the results in Minecraft world.

Master and commander We’ll use the cmd module to provide a simple command line interface to our game, all the code is available from www. pastebin.com/5zEZUfBS or www.linuxformat.com/files/ mine2048.py.zip. We shall have a command for initialising the board, four directional commands and a command to quit, Ctrl-D, aka the end of file (EOF) character or the quick way to end a Python session. We even get a help command for free (see picture, page 80). When you enter a command, for example left, the cmd module will run the function with that name prefixed by do_ hence we have a function do_left() which calls the leftMove() function above. This function is really part of our board class. The do_left() function will check if the left move is valid, and if so update the board, via the imaginatively-titled updateBoard() function, which in turn will add a new tile and decide whether or not the game is up. If it is all over, then we cheekily use raw_input() as a means to wait for the user to press Return.

The cmd module works by subclassing its Cmd class. Any class you write that instantiates this will inherit all of its functionality. We use the standard boilerplate to start things up when the program is executed: if __name__ == "__main__": command2048().cmdloop() This will instantiate our command interpreter when the program is run with: $ python mine2048.py Minecraft must be running when you do this, or you’ll run into errors. Also you’ll need to make sure you’ve copied the Python API from your Minecraft install (e.g. ~/mcpi/api/ python/mcpi) to a subdirectory called minecraft in the same directory as the mine2048.py file. But if you’ve been following our Minecraft series you’ll be au fait with all this. The command2048 class’s __init__() method sets up the basics, including setting up the mc object and using it to get the player’s position. This provides a reference point for drawing our board, which we will arbitrarily decide to be 3 blocks away in the z direction. Make sure you’re not looking at the back of the board, though, as everything will be backwards. For convenience, we also have a printTiles() function which draws the board at the command terminal.

Rules of the game A key tenet of programming anything is knowing what the rules are – knowing exactly how your program should behave in a given set of circumstances. A vague idea of what should happen often won’t cut it: we need specifics and exactitude, free of any semblance of ambiguity. With that in mind, let’s consider how the game of 2048 works. We start with a 4x4 grid which has some 2s and some 4s on it at random locations. Initially two tiles are populated. The player can

80 | Raspberry Pi Projects

make one of four moves, shifting the tiles up, down, left or right. Suppose the player chooses left, then the following algorithm applies: For every row start by moving all the numbered tiles left so that any empty tiles are on the right of the row. Now we will look at each tile’s situation and decide how the tiles will change. Starting with the tile on the left, if the tile to its right has the same value then the left-most of the pair will double in value, the rightmost of the pair will vanish, and all

WorldMags.net

tiles to the right will move one position left. Repeat this process for the second and third tiles from the left. Repeat the whole process for the second, third and fourth rows. If there is still space then another 2 or 4 tile is added randomly in one of the available empty spaces. If no move is possible after this then it’s Game Over. For other directions, the same algorithm applies with strategic substitution of row with column and right with left, up or down.


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.