Snake and Ladder | An LLD Interview Approach | Feedback Welcome
1796

Snake and Ladder - Low Level Design

Requirements:

  1. The number of tiles the board should have is variable. Number of players is also variable.
  2. The position of the snakes and ladders is pre-determined / designed by the board designer.
  3. To win the match, the player has to roll the exact number on the dice. The number on the die is generated randomly.
  4. The design should be extensible to add any kind of mover (Ex : Jetpack)
  5. The design should be such that each player plays multiple games simultaneously.

Entities:
Board
Game
Player
Dice
Snake
Ladder

User-defined types:
MoverType - SNAKE, LADDER, JETPACK

Domain classes:

abstract class Mover {
	int startPosition;
	int endPosition;
	MoverType moverType;	

	int move(int playerPosition); // Returns the destination player position
	MoverType getType();
	int getStartPosition();
	int getEndPosition();
}

class Snake extends Mover {
	
	int move(int playerPosition) {}
	MoverType getType() {}
}

class Ladder extends Mover {
	
	int move(int playerPosition) {}
	MoverType getType() {}
}

class Jetpack extends Mover {
	
	int move(int playerPosition) {}
	MoverType getType() {}
}

class Board {
	int[] squares;
	Map<Integer, Mover> moverPositions;
	
	Board(int n) {} // Create pre-defined positions for Movers
	Board(int n, Map<Integer, Mover> moverPositions) {} // Create all the movers
	Map<Integer, Mover> getMoverPositions() {}
	boolean isMoverSquare(int playerPosition) {}
	Mover getMover(int playerPosition) {}
	boolean isWinningPosition(int playerPosition) {}
}

class Player {
	int playerNumber;
}

class Dice {
	int start;
	int end;
	
	int getFaceValue() {}
}

class Game {
	Board board;
	Queue<Player> players;
	Map<Player, Integer> playerPositionMap;
	Dice dice;
	
	void initGame(int n, Queue<Player> players, Map<Integer, Mover> moverPositions) {}
	void viewCurrentGameState() {}
	void playGame() {}
}
Comments (1)