Snakes and ladders

Snakes and ladders

Abstraction in a classic children's game.

Try


Select the button below to open the Python program in a new window. Run the program and read the lines of code to see if you can understand how it works. It will be helpful to arrange your display so that you can have this browser window on one side of the screen and the code on the other.

Watch this video to learn about the new concepts shown in the program:

Knowledge Organiser

Abstraction means including the necessary details and removing the unnecessary details when problem solving.


Although the board looks like a grid, it does not need to be stored this way because the squares are sequential.


A snake and ladder are essentially the same type of game object. They both have a start and an end square.


The board can be abstracted to a list of squares, each containing the square they are connected to; and a player list containing the square that each player is on.


When writing programs it is a good idea to step back and think about what data needs to be stored and the easiest way to store it before you start writing code.

Investigate


Questions to think about with this program to check your understanding:

Relation question

Explain what the index and value of each board element represents. E.g. board[4] is 7. What are the numbers 4 and 7 representing?

REVEAL ANSWER

The index (4) is the square on the board: 0-25. The data (7) is the square it is connected to. If a square is connected to itself it is an ordinary square. If it is connected to a lower square it is a snake. If it is connected to a higher square it is a ladder.

Approach question

Explain why the programmer has chosen to represent the game board as a 1D array and not a 2D array, even though the board looks like a table.

Snakes and ladders

This can be abstracted to:

REVEAL ANSWER

The board is shown as a grid, but in reality it is just 25 squares in a row if you look at how the player moves from square 5 to 6 and 10 to 11 etc. Storing the board as a 1D list will make it easier to code. How things are visualised may be different to how they can be stored in memory. In code the board can be represented as, and abstracted to:

board[0] = 0

board[1] = 1

board[2] = 2

board[3] = 3

board[4] = 7 # Ladder

board[5] = 5

board[6] = 15 # Ladder

board[7] = 7

board[8] = 8

board[9] = 9

board[10] = 10

board[11] = 11

board[12] = 12

board[13] = 13

board[14] = 14

board[15] = 15

board[16] = 16

board[17] = 17

board[18] = 23 # Ladder

board[19] = 2 # Snake

board[20] = 20

board[21] = 21

board[22] = 22

board[23] = 23

board[24] = 17 # Snake

board[25] = 25


You could write the program using a 2D list, but that would require more code to map a player's square to an x, y position on the grid.


Using a list allows the program to increase or decrease the number of squares on the board in the game. For example, the user could enter the size of the board to play on.

Make


Success Criteria

Change the program so that:

  1. The function `initialise_players` asks the user for how many players are playing the game and initialises all those players to start on square 1.
  2. The function `initialise_board` takes a second parameter that is the number of special squares in the game that contain a snake or a ladder. Snakes and ladders are placed randomly with random lengths between square 2 and the penultimate square.

The best work would also ensure the following optional conditions are met when placing snakes and ladders:

  • A snake or ladder cannot start and end on the same square.
  • A snake or ladder cannot start or end on the same square as another snake or ladder.

Typical inputs and outputs from the program would be:

How many players? :3


------------------------------------

Player 1 it's your turn.

You are on square 1

Press Enter to roll the dice.


You rolled a 2

You moved to square 3

Yay, you landed on a ladder.

You are now on square 4

Press Enter for the next player to take their turn.



------------------------------------

Player 2 it's your turn.

You are on square 1

Press Enter to roll the dice.


You rolled a 5

You moved to square 6

Yay, you landed on a ladder.

You are now on square 14

Press Enter for the next player to take their turn.



------------------------------------

Player 3 it's your turn.

You are on square 1

Press Enter to roll the dice.


You rolled a 6

You moved to square 7

Press Enter for the next player to take their turn.



------------------------------------

Player 1 it's your turn.

You are on square 4

Press Enter to roll the dice.


You rolled a 2

You moved to square 6

Yay, you landed on a ladder.

You are now on square 14

Press Enter for the next player to take their turn.



------------------------------------

Player 2 it's your turn.

You are on square 14

Press Enter to roll the dice.


You rolled a 1

You moved to square 15

Oh no, you landed on a snake.

You are now on square 10

Press Enter for the next player to take their turn.



------------------------------------

Player 3 it's your turn.

You are on square 7

Press Enter to roll the dice.


You rolled a 1

You moved to square 8

Press Enter for the next player to take their turn.



------------------------------------

Player 1 it's your turn.

You are on square 14

Press Enter to roll the dice.


You rolled a 2

You moved to square 16

Press Enter for the next player to take their turn.



------------------------------------

Player 2 it's your turn.

You are on square 10

Press Enter to roll the dice.


You rolled a 6

You moved to square 16

Press Enter for the next player to take their turn.



------------------------------------

Player 3 it's your turn.

You are on square 8

Press Enter to roll the dice.


You rolled a 5

You moved to square 13

Press Enter for the next player to take their turn.



------------------------------------

Player 1 it's your turn.

You are on square 16

Press Enter to roll the dice.


You rolled a 6

You moved to square 22

Press Enter for the next player to take their turn.



------------------------------------

Player 2 it's your turn.

You are on square 16

Press Enter to roll the dice.


You rolled a 2

You moved to square 18

Press Enter for the next player to take their turn.



------------------------------------

Player 3 it's your turn.

You are on square 13

Press Enter to roll the dice.


You rolled a 4

You moved to square 17

Press Enter for the next player to take their turn.



------------------------------------

Player 1 it's your turn.

You are on square 22

Press Enter to roll the dice.


You rolled a 5

You moved to square 25

Player 1 wins the game!

Help

Use the flowcharts if you need more support in understanding the steps required to meet the criteria. Use the Parsons code sorting exercise only if you are really stuck.

Evaluate


Run the game several times with random players to check it works as expected. It is a good idea to output the game board so that you can check the positions of the snakes and ladders are appropriate.

Registered in England and Wales: 10442992

VAT Number: 290 9845 58

Telephone: 01452 947500

Email: admin@craigndave.co.uk

Craig'n'Dave logo

Craig 'n' Dave

Bett Awards 2024 Finalist