Low Level Design Snake & Ladder Game

vipul pachauri
2 min readJun 8, 2023

--

Data Model

  1. Game -> Board,Dice,Players
  2. Board -> Cells
  3. Cell -> Jump
  4. Jump -> start, end
  5. Player -> currentPosition
  6. Dice -> number

Main.java :

public class Main {

public static void main(String[] args) {
Game game = new Game();
game.startGame();
}
}

Game.java :

package systemDesign.snakeAndLadderGame;

import java.util.Deque;
import java.util.LinkedList;

public class Game {

private Board board;
private Dice dice;
private Player winner;
private Deque<Player> playerList = new LinkedList<>();

public Game(){
intializeGame();
}

private void intializeGame() {
board = new Board(10,5,4);
dice = new Dice(1);
winner = null;
addPlayers();

}

private void addPlayers() {
Player p1 = new Player(0);
Player p2 = new Player(0);
playerList.add(p1);
playerList.add(p2);
}

public void startGame(){
System.out.println("Game Begins......");
while (winner == null){
// Find Player
Player currentPlayer = findCurrentPlayer();
int diceNumber = dice.rollDice();
int nextPosition = diceNumber + currentPlayer.getCurrentPosition();
int finalPosition = checkIfJumpPossible(nextPosition);
currentPlayer.setCurrentPosition(finalPosition);
if(finalPosition > board.getCells().length * board.getCells().length -1){
winner = currentPlayer;
System.out.println("Game Over...");
}


}
}

private int checkIfJumpPossible(int nextPosition) {
System.out.println("Checking for snake or ladder...");
Cell cell = board.getCell(nextPosition);
if(cell.getJump() != null && cell.getJump().start == nextPosition){
return cell.getJump().end;
}
return nextPosition;

}

private Player findCurrentPlayer() {
System.out.println("Selecting player to play...");
Player currentPlayer = playerList.removeFirst();
playerList.addLast(currentPlayer);
return currentPlayer;
}
}

Board.java :

package systemDesign.snakeAndLadderGame;

import java.util.concurrent.ThreadLocalRandom;

public class Board {

private Cell[][] cells;

public Cell[][] getCells() {
return cells;
}

public Board(int boardSize, int snakes, int ladders) {
intializeCells(boardSize);
addSnakeAndLadders(snakes,ladders);

}

private void intializeCells(int boardSize) {
cells = new Cell[boardSize][boardSize];
for(int i = 0;i<boardSize;i++){
for(int j = 0;j<boardSize;j++){
Cell cell = new Cell();
cells[i][j] = cell;
}
}
}

private void addSnakeAndLadders(int snakes, int ladders) {

while (snakes > 0){
int start = ThreadLocalRandom.current().nextInt(1,cells.length*cells.length-1);
int end = ThreadLocalRandom.current().nextInt(1,cells.length*cells.length-1);
if(start <= end){
continue;
}

Jump jump = new Jump(start,end);
Cell cell = getCell(start);
cell.setJump(jump);
snakes--;


}

while (ladders > 0){
int start = ThreadLocalRandom.current().nextInt(1,cells.length*cells.length-1);
int end = ThreadLocalRandom.current().nextInt(1,cells.length*cells.length-1);
if(start <= end){
continue;
}

Jump jump = new Jump(start,end);
Cell cell = getCell(start);
cell.setJump(jump);
ladders--;
}
}

public Cell getCell(int playerPosition) {
int start = playerPosition / cells.length;
int end = playerPosition % cells.length;
return cells[start][end];
}
}

Cell.java :

package systemDesign.snakeAndLadderGame;

public class Cell {

private Jump jump;

public Jump getJump() {
return jump;
}

public void setJump(Jump jump) {
this.jump = jump;
}
}

Dice.java :

package systemDesign.snakeAndLadderGame;

import java.util.concurrent.ThreadLocalRandom;

public class Dice {

private int count;
private int max = 6;
private int min = 1;

public Dice(int count) {
this.count = count;
}

public int rollDice(){
System.out.println("Dice rolling...");
int diceUsed = 0;
int result = 0;

while (diceUsed < count){
result += ThreadLocalRandom.current().nextInt(min,max);
diceUsed++;
}

return result;
}
}

Jump.java :

package systemDesign.snakeAndLadderGame;

public class Jump {

int start;
int end;

public Jump(int start, int end) {
this.start = start;
this.end = end;
}
}

Player.java :

package systemDesign.snakeAndLadderGame;

public class Player {

private int currentPosition;

public Player(int currentPosition) {
this.currentPosition = currentPosition;
}

public int getCurrentPosition() {
return currentPosition;
}

public void setCurrentPosition(int currentPosition) {
this.currentPosition = currentPosition;
}
}

Thanks for reading. Happy Learning !!

--

--

vipul pachauri
vipul pachauri

Written by vipul pachauri

Senior Software Backend Engineer

No responses yet