Object-Oriented Design: Snakes and Ladders Game

Problem Statement

How would you design a game of Snakes and Ladders

Hints

  • Think in terms of the entities involved
  • Think about the variations
  • Try to relate it to the actual board game.

Solution

  • A Game of Snakes and Ladders will consist of a board, players, and dice (1 or more)
  • A board will have
    • Positions numbered from 1 to 100.
    • Snakes: encountering snakes will move a player from position A to position B, where the number at position A > the number at position B
    • Ladders: encountering ladders will help move a player from position C to position D, where the number at position C < the number at position D
  • Players
    • players will take turns to roll a dice or more than one dice.
    • based on the number or the sum of all the numbers on dice the player will move from one position to another position on the board.
  • Dice or Dices
    • A game could have only one dice, or it could have more than one.
    • A dice could be a
      • Regular dice where getting a number on the face of a dice is equally probable
      • Weighted dice are where getting a number on the face of a dice is not equally probable.

At this point, there are a few open questions

  • How many players can play a game at the same time?
    • Let’s assume 4 players, but it should be designed so that more players can be added easily.
  • How the winner or winners are determined?
    • Let’s assume there will be only one winner.
    • Anyone who reaches the position number 100 first would be the winner.
  • How many Snakes would be there on the board?
    • Let’s assume there would be a total of 5 snakes, where one snake should be at position 99 and should end at position 36 (Just to add some thrill to the game)
  • How many Ladders would be there on the board?
    • Let’s assume there would be three ladders, but the difference between the start and the end position of a ladder should not be greater than 9.
  • How many dice would be there?
    • For now let’s assume there would be only one dice.
  • Whether the dice would be a regular or weighted?
    • For now let’s assume the dice would be a regular dice.
    • But you should design in such a way that it can be switchable at the start of the game.

Version 1

Game: Version 1

Color

Snake: Version 1

Ladder: Version 1

We can see that we are calling validate method in both Snake and Ladder class. It might be a good idea to define a Validator interface with a method named validate for performing validations.

Validator Interface

SnakePositionValidator

LadderPositionValidator

LadderPositionRangeValidator

Version 2

Snake: Version 2

Ladder: Version 2

Player

SnakeAndLadderPlayer

Dice

RegularDice

We can also add a RegularDiceValidator to check the number of faces on dice.

For example, the number of faces on the dice should be a non-negative and non-zero integer, whose value should be between 1 and 20 (both inclusive)

WeightedDice

Board

  • We don’t need to maintain a list of all the positions for the board.
  • We need to know the range of positions allowed.

BoardSnakeAndLadderValidator

BoardThrillnessValidator

Game: Version 2

Rate this post

Leave a Reply