Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

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 an array of squares, each containing the square they are connected to; and a player array 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 array will make it easier to code. How things are visualised may be different to how they can be stored in memory.

You could write the program using a 2D array, 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 reads 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.


------------------------------------
…
🆘 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
static void Play(int[] board, int[] players)
{
---
    int current_player = 0;
    bool game_won = false;
    // Play until the game has been won
    while (!game_won)
    {
---
        int player_square = players[current_player];
        Console.WriteLine();
        Console.WriteLine("------------------------------------");
        Console.WriteLine($"Player {current_player + 1} it's your turn.");
        Console.WriteLine($"You are on square {player_square}");
        Console.WriteLine("Press Enter to roll the dice.");
        Console.ReadLine();
        int dice = random_generator.Next(1, 7);
        Console.WriteLine($"You rolled a {dice}");
        player_square += dice;
        // The player cannot move beyond the last square
        if (player_square > board.Length - 1)
        {
---
            player_square = board.Length - 1;
---
        }
---
        Console.WriteLine($"You moved to square {player_square}");
        int board_square = board[player_square];
        // Player landed on a snake
        if (board_square < player_square)
        {
---
            Console.WriteLine("Oh no, you landed on a snake.");
            player_square = board_square;
            Console.WriteLine($"You are now on square {player_square}");
---
        }
---
        // Player landed on a ladder
        else if (board_square > player_square)
        {
---
            Console.WriteLine("Yay, you landed on a ladder.");
            player_square = board_square;
            Console.WriteLine($"You are now on square {player_square}");
---
        }
---
        // If the player is on the last square they win
        if (player_square >= board.Length - 1)
        {
---
            game_won = true;
            Console.WriteLine($"Player {current_player + 1} wins the game!");
---
        }
---
        // Otherwise record the player's new square and change the player
        else
        {
---
            players[current_player] = player_square;
            Console.WriteLine("Press Enter for the next player to take their turn.");
            Console.ReadLine();
            current_player = (current_player + 1) % players.Length;
---
        }
---
    }
}
initialise_board
// Set up the board
static int[] initialise_board(int squares, int special_squares)
{
---
    int[] board = new int[squares + 1];
    List square_used = new List();
    int random_start = 0;
    int random_end = 0;
---
    // Create each square
    for (int square = 0; square <= squares; square++)
    {
---
        board[square] = square;
---
    }

---
    // Assign snakes and ladders
    // Add the number of snakes and ladders
    for (int count = 0; count < special_squares; count++)
    {
---
        // A snake or ladder must start and end on a unique square that is not the same square
        while (square_used.Contains(random_start) || square_used.Contains(random_end) || random_start == random_end)
        {
---
            // Choose a start and end position of the snake or ladder between 2 and the penultimate square
            random_start = random_generator.Next(2, squares);
            random_end = random_generator.Next(2, squares);
---
        }
---
        board[random_start] = random_end;
        square_used.Add(random_start);
        square_used.Add(random_end);
---
    }
---
    return board;
}
</pre>
</dw-parsons-puzzle>

Initialise_players
// Set up the players
static int[] Initialise_players()
{
---
    Console.WriteLine("How many players? :");
    int num_players = Convert.ToInt32(Console.ReadLine());
    int[] players = new int[num_players];
---
    // Initialise all players to start at square 1
    for (int player = 0; player < num_players; player++)
    {
---
        players[player] = 1;
---
    }
---
    return players;
}
</details>