On completion of this page you will learn how to use tuples, lists and nested loops to navigate around a snakes and ladders board
Introduction
Below is an image of the Snakes and Ladders 'board' that we shall be using for our game example. It is slightly different to the board that you would by in a toy store. The difference is that we have added the yellow section to the right of the main board to show a representation of the dice that would be used in traditional boards. Here we show an image of a dice that we click on. This clicking will result producing a random number between 1 and 5 inclusive; the same as what a physical dice would produce when rolled.
The left hand portion shows an example of a real Snakes and Ladders board.
Snakes and Ladders is a game of pure chance; there is no strategy involved. Success or failure depends on the roll if the dice. The game was initially an allegory of life: the rolls of the dice represent the steps a person takes towards a goal, the ladders represent opportunities encountered and the snakes represent setbacks.
With this little nugget of history at the back of our minds let us look at developing a programme that plays Snakes and Ladders on your computer screen.
Programme Code
Initial Setup
As the subtitle above indicates this block of code is where we import our resources and set up our clock. It is not necessary to discuss it as it should be familiar to you alaready.
Of the code above only the bottom line needs an explanation. As its name implies the variable random_number will hold the random number in the range 1 - 6 inclusive that will be generated when the user clicks the dice image.
No new concepts are introduced here. Line 24 sets up the game window and line 25 adds a caption to it. The rest of the code involves loading and positioning the .PNG images for the game board, the dice and the button. All of this code is identical to the code for positioning an image on the page Images on the page Startup. The only difference is that the images being loaded above are scaled to fit the requirements of the game.
We have now loaded all of our resources and are ready for the next step, which is building up a map which guides the button through the grid that forms the game board until it reaches the top right hand corner.
Above we have a 10 X 10 board of the type that our Snakes and Ladders are based on. It is laid out according to the computing version of the Cartesian plane. The x-axis moves from left to right and the y-axes moves from top to bottom, as opposed to mathematics where the y-axis moves from bottom to top.
On a Snakes and Ladders board the button starts at the bottom row and moves from right to left. It then moves to the row above and moves from left to right. It continues with this zigzag movement until it reaches the top right hand corner. This is not compliant with the coordinate system shown in Fig 2. In order to overcome this problem we need to map the movements of the Snakes and Ladders board onto the system shown in Fig 2 above.
Fig 3 above is not a table; it is simply a list of tuples - hence the brackets. The tuples in the list are organised to guide the button to start at the bottom right hand corner, move step by step to the bottom left corner, move up to the next row and move from left to right and continue in this manner until it reaches the top right hand corner.
The code below allows us to populate such a list.
At line 57 a list board_squares is initialised as being blank. This list will contain the 'map' for navigating the Snakes and Ladders board from start to finish.
The for loop that spans lines 58 - 64 is involved in populating this list.
At line 58 the outer loop starts. The loop counter row moves from 9 to 0 in steps of 1.
The body of the loop is an if..else construct. At line 59 thr counter row is tested for being even. On the first iteration of the loop this will not be the case since 9 is an odd number. Since this is the case lines 60 and 61 are skipped and lines 63 and 64 are executed.
Like line 58 the loop at line 63 iterates from 9 to 0 in units of 1 step. Thus on completion of the first iteration of the main loop the list board_squares will have the following values in it:
(9,0) (8,0) (7,0) (6,0) (5,0) (4,0) (3,0) (2,0) (1,0) (0,0)Go to top