Design Snake and Ladder Game | OOD

Design Snake and Ladder Game | OOD

Question:

Design Snake and Ladder Game. This is Object oriented design (OOD ) question. Many of us might have played this game. If you have not played, then check the game rules are given bellow.

Note: Usually OOD  questions will be vague. You have to discuss with interviewer and design. We don’t have a interviewer here. So we will make some assumptions about the game rules and working of game. These type of questions can be solved in many different ways. There is no right or wrong approach.  Basically these questions test how you approach and design a solution for a real world problem. Working code is not a goal here.

Rules of Game:

  • The board can have any size. By default board size is 100
  • The game will have a dice numbered from 1 to 6 (or) 12
  • Each player rolls the dice when their turn comes.
  • Based on the dice value, the player moves forward. Ex: If the dice value is 5 and the player is at position 21, the player will move to postion 26 now (21+5).
  • Player doesn’t move if dice value + current value > last position.
  • The board also contains some snakes and ladders.
  • Whenever a piece ends up at a position with the head of the snake, the piece should go down to the position of the tail of that snake.
  • Whenever a piece ends up at a position with the start of the ladder, the piece should go up to the position of the end of that ladder.
  • Game ends when a player reaches last position Ex: 100 is last if board size is 100

Play Snake and Ladder for better understanding.

Assumptions:

  1. There won’t be a snake at 100.
  2. There won’t be another snake/ladder at the tail of the snake or the end position of the ladder.
  3. Board will not contain multiple snakes/ladders at the same cell.

Approach

  1. Only one game played at a time. So we will have one Game class.
  2. In Each game we have players. There can be 1 to any number of players.
  3. Each game will have only one board. For 1 game we have 1 board
  4. Each Game will have 1 dice. This is our assumption, we can also design game with two dice each numbering from 1 to 6.
  5. Each board will have 1 to any number of cells. 1 board will have 1 to n cells. 

Slide Right->

snakeandladder1
Screenshot from 2022-01-22 21-02-47
snakeandladder2
snakeandladder3
snakeandladder14
previous arrow
next arrow

 

 

Now we have to design for snake and ladders in board.

How to Design Board?

We can design board as 2d array of cells. This will add more complexity to our design. Our goal is to reach the end. Board need not be 2d array. 1d array of cells will be easy to design and maintain.

How to Design Players?

We can keep players as Array. If maximum no. of players is 2 then array will be best. List will best when design needs to support any number of players.

How to Design Cells?

Each cell can be normal cell or can have snake or can have ladder. Last cell in board is target. So cell will be of 4 types,

  1. Snake
  2. Ladder
  3. Normal
  4. Target

 

 

Each cell will have start, end and type

class Cell
{
    int start;
    int end;
    CellType type;
    Cell(int start,int end, CellType type)
    {
        this.start = start;
        this.end = end;
        this.type = type;
    }
}
enum CellType
{
    NORMAL,SNAKE,LADDER,TARGET;
}

For a Snake, head of snake is start position. When a player move to the start position of snake. Snake bites and player goes to the tail or end position of the snake. In ladder, when player moves to start of ladder, ladder takes the player to end position. So both snake and ladder will have start and end.

Can we store snakes and ladders as list?

Of course, we can. When players moves to a position we have to check the all the list of snakes and list of ladder. It is very costly. That’s why store we store the snake and ladder as cell.


Full Code


import java.util.*;
import java.io.*;

public class myclass 
{
    public static void main(String[] arg) throws Exception
    {
        Game game = new Game();
        game.addPlayer(new Player("Ram"));
        game.addPlayer(new Player("Raju"));
        game.start();
    }
}
class Game
{
    Board board;
    List<Player> players;
    Game()
    {
        board = new Board(100);
        players = new ArrayList<Player>();
    }
    void addPlayer(Player player)
    {
         players.add(player);
    }
    void start()
    {
        boolean isGameEnded = false;
        Dice dice = new Dice(6);
        
        //Simulate Game
        while(!isGameEnded)
        {
            for(Player player : players)
            {
                int newPosition = dice.rollDice();
                isGameEnded = player.move(board , newPosition);
                if(isGameEnded)
                {
                    System.out.println(player.name +" wins");
                    break;
                }
            }
        }
    }
    
}
class Dice
{
    int max;
    Dice(int max)
    {
        this.max = max;
    }
    
    //Get random number
    int rollDice()
    {
        Random rand = new Random();
        return 1+rand.nextInt(this.max);
    }
}
class Board
{
    int boardSize;
    Cell cells[];
    Board(int size)
    {
        boardSize = size;
        cells = new Cell[boardSize+1];
        initializeBoard();
    }
    void initializeBoard()
    {
        for(int i = 1; i<boardSize; i++)
        {
            cells[i] = new Cell(i,i,CellType.NORMAL);
        }
        
        cells[boardSize] = new Cell(boardSize,boardSize,CellType.TARGET);
        
        
        addSnake(new Snake(50,10));
        /*add snakes here*/
        addLadder(new Ladder(4,30));
        /*add ladders here*/
        
    }
    void addSnake(Snake snake)
    {
       //throw error if snake is out of cells range
        cells[snake.start] = snake;
    }
    void addLadder(Ladder ladder)
    {
        //throw error if ladder is out of cells range
        cells[ladder.start] = ladder;
    }
    Cell get(int position)
    {
        //Check range
        return cells[position];
    }
}
class Cell
{
    int start;
    int end;
    CellType type;
    Cell(int start,int end, CellType type)
    {
        this.start = start;
        this.end = end;
        this.type = type;
    }
}
class Snake extends Cell
{
    Snake(int start, int end)
    {
        super(start,end,CellType.SNAKE);
    }
}
class Ladder extends Cell
{
    Ladder(int start,int end)
    {
        super(start, end, CellType.LADDER);
    }
}
enum CellType
{
    NORMAL,SNAKE,LADDER,TARGET;
}
class Player
{
    int position;
    String name;
    Player(String name)
    {
        this.name = name;
        position = 0;
    }
    boolean move(Board board, int number)
    {
       
        int newPosition = position+number;
         //System.out.println("Player "+name + "Move to "+newPosition);
        if(newPosition > board.boardSize)
            return false;
        
        Cell cell = board.get(newPosition);
        if(cell.type == CellType.TARGET)
        {
            return true;
        }
        else if(cell.type == CellType.SNAKE || cell.type == CellType.LADDER)
        {
            this.position = cell.end;
        }
        else 
        {
            this.position = newPosition;
        }
        return false;
    }
}

Alternative Approach:

Create a cells array with integer type. We can consider positive number in cells array as Ladder and negative number as snake. This approach will be easy to code when we have limited time.


Companies
:

This question is asked in Amazon, Zoho

You might be interested in,

Call Taxi Booking System | ZOHO 3rd round question

Invoice Management System | ZOHO 3rd round

 

For More Updates

Join our Telegram Channel: link

Like our Facebook Page:  link

Subscribe to our Youtube channel: link

 

Follow For Instant Updates

Join WhatsApp Group: link
Join our Telegram Channel: link
Like our Facebook Page:  link
Subscribe to our Youtube channel: link

Sree Hari Sanjeev

The founder of Wisdom Overflow. Software Developer at Zoho Corporation.
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x