Abstraction in a classic children's game.
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:
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.
Questions to think about with this program to check your understanding:
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?
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.
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.
This can be abstracted to:
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.
Change the program so that:
The best work would also ensure the following optional conditions are met when placing snakes and ladders:
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!
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.
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.