Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this question we want to simulate snakes and ladders game by writing a java program. Here are some of the rules: The game has

In this question we want to simulate snakes and ladders game by writing a java program. Here are some of the rules:

The game has 2 to 4 players.

If a player wants to enter a cell occupied by another player, the player attempting to enter the cell will be moved to the first cell. Note that we can have more that one player in the first cell.

Players take turn to roll the dice. Then, they move their piece forward the number of spaces shown on the dice.

If the player lands at the bottom of a ladder, it must move up to the top of the ladder.

If the player lands on the head of a snake, it must slide down to the bottom of the snake.

The first player to get to the last cell of the board is the winner. But there's a twist! If the player rolls too high, the piece "bounces" off the last cell and moves back to the first cell. A player can only win by rolling the exact number needed to land on the last cell.

For simplicity, you can assume that we do not have chained snakes and ladders in the game.

You can find simulation of Snakes & Ladders game (or Chutes & Ladders) Here. Below is the skeleton of the classes you need to implement:

class Game {  public Game(Board board, Player[] players); 

Creates a game with the board and players passed to constructor.

public Game(Player[] players); Creates a game with a 10-by-10 board and the players which are passed to it.

public Player currentPlayer(); Specifies the current player.

public void addPlayer(Player p); Adds a player to the list of players and the player will be placed in the first cell.

public boolean winner(); Determines whether the current player has reached the last cell.

public void movePlayer(int n); Moves current player n cells forward.

public boolean play(int moveCount); This is the main method in your game, which moves the current player to the correct cell and also checks whether the game is finished or not. If found a winner, returns true, otherwise returns false.

public Board getBoard(); Returns board of the game.

} class Player{ 

public Player(String name); This is the constructor of the player class.

public void setPosition(int position); Moves the player to the specified position.

public int getPosition(); Returns position of the player.

public String getName(); Returns players name.

public String toString(); Returns Name @ cell-number. e.g: narges @ 12.

} class Cell {

public Cell(int number); Creates a cell and assigns its number.

public void setOccupied(boolean occupied); Fills or releases the cell.

public boolean isOccupied(); Determines whether this cell is occupied.

public Ladder getLadder(); Returns the Ladder which is in the current cell. Returns null if there is no Ladder with start in this cell.

public Snake getSnake(); Returns the Snake which is in the current cell. Returns null if there is no Snake with head in this cell.

public void setLadder(Ladder ladder); Sets a Ladder with start in the current cell.

public void setSnake(Snake snake); Returns the Snake with head in the current cell.

public int getNumber(); Returns number of the cell.

} class Board { 

public Board(int n); Creates an n-by-n board.

public void setCellToLadder(int startPosition, int endPosition); Puts a ladder on the map of the game. You need to associate the ladder with the cell at startPosition.

public void setCellToSnake(int headPosition, int tailPosition); Puts a snake on the map of the game. You need to associate the snake with the cell at headPosition.

public Cell[] getCells(); Returns all cells of the board.

} class Snake { 

public Snake(int headPosition, int tailPosition ); Creates a snake with specified location of head and tail.

public int getTail(); Returns the position of the snakes tail.

public String toString(); Returns: head tail. e.g: 20 11.

} class Ladder { 

public Ladder (int startPosition, int endPosition ); Creates a ladder which connects cells with specified location of start and end of the ladder.

public int getTop(); Returns position of the top (end) of the ladder.

public String toString(); Returns: start end.. e .g: 10 18.

}

Here is a sample main method to test your program:

public static void main(String[] args){ Player[] players = new Player[3]; players [0] = new Player("abc"); players [1] = new Player("def"); players [2] = new Player("ghi"); Board myBoard = new Board(5); myBoard.setCellToLadder(7, 17); myBoard.setCellToLadder(5, 14); myBoard.setCellToSnake(19, 8); myBoard.setCellToSnake(16, 4); Game game = new Game(myBoard, players); Player p = new Player(jkl"); game.addPlayer(p); 
 boolean b = false; Random random = new Random(); int n; while (!b) { 
 n = random.nextInt(6) + 1; 
 b = game.play(n); } 
 System.out.println(game.currentPlayer().getName()); } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Know the three main dimensions of the service environment.

Answered: 1 week ago

Question

Understand the roles of signs, symbols, and artifacts.

Answered: 1 week ago