Abstraction in a classic children's game.

Snakes and ladders

Try

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

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:

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!

Restricted automated feedback

Automated feedback for this assignment is still under construction. Submitted programs are checked for syntax errors and their source code is checked for potential errors, bugs, stylistic issues, and suspicious constructs. However, no checks are performed yet to see if the program correctly implements the behaviour specified in the assignment.

🆘 If you're really stuck, use this Parsons code sorting exercise
play
# Play snakes and ladders on any size board with any number of players
def play(board, players):
---
    current_player = 0
    game_won = False
---
    # Play until the game has been won
    while not game_won:
---
        player_square = players[current_player]
---
        print()
        print("------------------------------------")
        print("Player", current_player + 1, "it's your turn.")
        print("You are on square", player_square)
        print("Press Enter to roll the dice.")
---
        wait = input()
---
        dice = random.randint(1, 6)
---
        print("You rolled a", dice)
---
        player_square = player_square + dice
---
        # The player cannot move beyond the last square
        if player_square > len(board) - 1:
---
            player_square = len(board) - 1
---
        print("You moved to square", player_square)
---
        board_square = board[player_square]
---
        # Player landed on a snake
        if board_square < player_square:
---
            print("Oh no, you landed on a snake.")
            player_square = board_square
            print("You are now on square", player_square)
---
        # Player landed on a ladder
        elif board_square > player_square:
---
            print("Yay, you landed on a ladder.")
            player_square = board_square
            print("You are now on square", player_square)
---
        # If the player is on the last square they win
        if player_square >= len(board) - 1:
---
            game_won = True
            print("Player", current_player + 1, "wins the game!")
---
        # Otherwise record the player's new square and change the player
        else:
---
            players[current_player] = player_square
---
            print("Press Enter for the next player to take their turn.")
---
            wait = input()
---
            current_player = (current_player + 1) % len(players)
initialise_board
# Set up the board
def initialise_board(squares, special_squares):
---
    random.seed()
    board = []
---
    # Create each square
    for square in range(squares + 1):
---
        board.append(square)
---
    
    # Assign snakes and ladders
    random_start = 0
    random_end = 0
    square_used = []
    # Add the number of snakes and ladders
    for count in range(special_squares):
---
        # A snake or ladder must start and end on a unique square that is not the same square
        while random_start in square_used or random_end in square_used or random_start == random_end:
---
            # Choose a start and end position of the snake or ladder between 2 and the penultimate square
            random_start = random.randint(2, squares - 1)
            random_end = random.randint(2, squares - 1)
---
        square_used.append(random_start)
        square_used.append(random_end)
---
        board[random_start] = random_end
---
    return board
initialise_players
# Set up the players
def initialise_players():
---
    num_players = int(input("How many players? :"))
---
    players = [1 for player in range(num_players)]
---
    return players