Skip to content
Tutorials Center
My learning

Topics

Lesson 118 of 135 ยท Projects

Project: Snake and Ladder

Build Snake and Ladder in C in two stages: a jump table that stores every snake and ladder and maps squares to a zigzag grid, then a multi-player game with dice and exact-roll finish.

Snake and Ladder (also sold as Chutes and Ladders) has no strategy at all. Players roll a die, move that many squares, and if they land at the foot of a ladder they climb, and if they land on a snake's head they slide down. The first to reach square 100 wins. That absence of decisions is what makes it a good programming project: all the interest is in the data (where are the snakes and ladders?) and in a few details of the rules that must be handled exactly.

The project uses arrays, multidimensional thinking for drawing the board, random numbers, functions with pointer parameters and a small input check. There are two stages: the board as data, and then the game for two to six players.

Stage 1: the board is a lookup table

One array holds every snake and ladder. There are 100 squares, numbered 1 to 100, so an int jump[LAST + 1] array has a slot per square (slot 0 stays unused so that square s lives at index s). A slot is 0 for a normal square. For a square with a ladder or a snake it holds the destination. That single idea removes all the special cases from the game:

C
if (jump[square] != 0) {
    square = jump[square];
}

Whether it is a ladder or a snake depends only on the comparison: a destination higher than the start is a ladder, lower is a snake. Adding a snake to the game means adding one line of data, and the game code does not change.

Filling it from a list of pairs. An array of {from, to} pairs is short to write and easy to review against a printed board. A loop copies each pair into jump. The pairs live in a static const array so they are stored once in the program, not rebuilt every call.

Choosing the pairs involves a rule worth stating: a destination must not itself be the start of another jump. If a ladder ended on a snake's head, the player would have to move twice in one turn, and the board would be confusing. A real board never does that either. In the data above, no destination (25, 46, 5, 49, 3, 63, 69, 31, 18, 45, 92, 58, 53, 75, 41) is also a start.

Drawing a board that zigzags. On a printed board, the numbers snake back and forth: the bottom row runs left to right (1 to 10), the next row right to left (11 to 20), and so on. To draw it we need to turn a square number into a row and a column. Square s has the zero-based index s - 1, so:

  • the row, counted from the bottom, is (s - 1) / 10;
  • the position within the row is (s - 1) % 10;
  • the column is that position on even rows, and 9 - position on odd rows.

Drawing from the top means starting with row 9 and going down, so the loop counts rows downward. Each cell prints its number and, when jump[s] is non-zero, +n for a ladder up to n or -n for a snake down to n. The %-2d and %3d formats keep the columns lined up.

C Stage 1: the jump table and the zigzag board

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Find square 11 in the first table: it is row 1, column 9, directly above square 10, which is how a zigzag board looks. Find 42+63 in the picture: square 42 has a ladder to 63. 99-41 is the cruel snake right next to the finish.

Stage 2: players, dice and the finish rule

The state. For every player, the game needs one number: the square they are on, with 0 meaning "not on the board yet". An array position[MAX_PLAYERS] holds them, and a player is just an index in it. This is the simplest data structure that works. A struct per player would make sense once players had names, colors or other properties.

One turn. take_turn rolls the die, works out the target, applies the jump table, reports what happened and tells the caller whether this player has won. The position is passed as a pointer (int *position) so the function can change it, the pattern from pointers and functions.

The exact-roll rule. Many families play that you must land exactly on 100. If the roll would take you past the end, you stay where you are. Without the rule, a player on 98 wins with any roll of two or more, and the endgame is not much of a contest. The check is if (target > LAST). The rule also has a consequence for the program: a game can last quite a long time, since a player on 99 only wins with a 1. The main loop therefore has a safety limit of 500 rounds, so a bug (or a strange board) cannot make it run forever.

Rolling a die. rand() % 6 + 1 gives 1 to 6. The tiny bias of the modulo is irrelevant here (see random numbers for when it matters). The seed is fixed with srand(3) so that every run of the page shows the same game; use time(NULL) for a game that changes each time.

Several players and input. The first line of input is the number of players. atoi is fine for this, because anything that is not a number gives 0, and the check players < 2 || players > MAX_PLAYERS catches that and every other bad value, falling back to two players with a message. (For input that must be exactly right, use strtol as in string conversions.) The loop for (p = 0; p < players && winner < 0; p++) makes sure nobody takes a turn after somebody has won in the middle of a round.

C Stage 2: the full game

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Look for a snake, a ladder, and a turn where a player is stuck because the roll was too big. Change the input to 4 to add more players; the log then has four lines per round. Change srand(3) to another number and the whole game changes.

Ideas for going further

  • Extra rolls on a 6. Make take_turn loop while the roll is 6, with a limit of three in a row.
  • Names and a struct. Replace the position array with struct Player { const char *name; int square; }.
  • A summary every few rounds. Print one line showing everyone's square every five rounds, instead of one line per move.
  • Statistics. Play 10,000 games without printing and compute the average number of turns. The numbers might surprise you, and writing a function that returns the turn count is a useful change anyway.
  • Draw the players. Extend Stage 1's drawing to put a marker on the square where each player stands.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does jump[27] = 5 mean in the board table?

    Each slot holds the destination for the square with that number. 5 is lower than 27, so the player slides down; that makes it a snake.
  2. 2. A board has 10 squares per row and row 0 runs left to right. Which column holds square 14?

    Square 14 has index 13, so it is on row 1 at position 3. Row 1 runs right to left, so the column is 9 - 3 = 6. Check by counting. Square 11 is in column 9, 12 in column 8, 13 in column 7 and 14 in column 6.
  3. 3. Why does take_turn receive an int pointer for the position?

    C passes arguments by value. A function that must change the caller's variable needs its address.
  4. 4. What does the exact-roll rule change in the program?

    The check target > LAST leaves the position unchanged. A player near the end has to wait for the right number.

Summary

  • Put every special square in one lookup table (jump[square]); a non-zero entry is the destination. The game code needs no special cases, and a ladder or a snake is decided by comparing start and end.
  • Check the data: no destination should itself be a start.
  • A zigzag board maps a square to a row (s - 1) / n and a column that flips direction on odd rows.
  • One int per player is enough state; pass its address to the function that moves it.
  • The exact-roll rule is one comparison, and a round limit protects a loop that is not guaranteed to end quickly.
  • Validate the player count and fall back to a sensible default instead of failing.

Next: the library management project, where you keep a collection of records, search it and save it to a file.