0% found this document useful (0 votes)
6 views

ladder genius sheet for problem

The document outlines a Java implementation of a Snake and Ladder game, including classes for the game board, players, snakes, ladders, and dice. It features services for managing the game logic and player interactions, allowing multiple players to compete until one reaches the end of the board. The main class initiates the game by running a demo with predefined players.

Uploaded by

Rajat Chauhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

ladder genius sheet for problem

The document outlines a Java implementation of a Snake and Ladder game, including classes for the game board, players, snakes, ladders, and dice. It features services for managing the game logic and player interactions, allowing multiple players to compete until one reaches the end of the board. The main class initiates the game by running a demo with predefined players.

Uploaded by

Rajat Chauhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Model.

Board
package model;

import java.util.ArrayList;
import java.util.List;

public class Board {


private final int boardSize = 100;
private final List<Snake> snakes;
private final List<Ladder> ladders;

public Board(){
snakes = new ArrayList<>();
ladders = new ArrayList<>();
initializeSnakeAndLadder();
}
private void initializeSnakeAndLadder(){
// Initialize snakes
snakes.add(new Snake(16, 6));
snakes.add(new Snake(48, 26));
snakes.add(new Snake(64, 60));
snakes.add(new Snake(93, 73));
// Initialize ladders
ladders.add(new Ladder(1, 38));
ladders.add(new Ladder(4, 14));
ladders.add(new Ladder(9, 31));
ladders.add(new Ladder(21, 42));
ladders.add(new Ladder(28, 84));
ladders.add(new Ladder(51, 67));
ladders.add(new Ladder(80, 99));
}

public int getBoardSize() {


return boardSize;
}
public List<Snake> getSnakes(){
return snakes;
}
public List<Ladder> getLadders(){
return ladders;
}

DICE
package model;

public class Dice {


final static int MIN_LIMIT = 1;
final static int MAX_LIMIT = 6;

public static int getMinLimit() {


return MIN_LIMIT;
}

public static int getMaxLimit() {


return MAX_LIMIT;
}
}

LADDER
package model;

public class Ladder {


private final int start;
private final int end;
public Ladder(int start, int end){
this.start = start;
this.end = end;
}
public int getStart(){
return start;
}
public int getEnd(){
return end;
}
}
PLAYER
package model;

public class Player {


private final String name;
private int position;

public Player(String name){


this.name = name;
this.position = 0;
}
public String getName(){
return name;
}
public int getPosition(){
return position;
}
public void setPosition(int position){
this.position = position;
}
}

SNAKE
package model;

public class Snake {


private final int start;
private final int end;
public Snake(int start, int end){
this.start = start;
this.end= end;
}
public int getStart(){
return start;
}
public int getEnd(){
return end;
}
}

SERVICES:

BOARDSERVICE
package service;

import model.Board;
import model.Ladder;
import model.Snake;

public class BoardService {


private final Board board;

public BoardService(Board board) {


this.board = board;
}

public int getNewPositionAfterSnakeOrLadder(int position) {


for (Snake snake : board.getSnakes()) {
if (snake.getStart() == position) {
return snake.getEnd();
}
}

for (Ladder ladder : board.getLadders()) {


if (ladder.getStart() == position) {
return ladder.getEnd();
}
}

return position;
}
}

DICESERVICE
package service;

import model.Dice;

public class DiceService {


public int roll() {
int minLimit = Dice.getMinLimit();
int maxLimit = Dice.getMaxLimit();
return (int) (Math.random() * (maxLimit - minLimit + 1) +
minLimit);
}
}

IGAME
package service;

public interface IGame {


void play();
}

IGAMEMANAGER
package service;

import java.util.List;

public interface IGameManager {


void startNewGame(List<String> players);
}

GAMEMANAGER
package service;

import java.util.List;

public class GameManager implements IGameManager{


private static GameManager instance;

private GameManager() {
}

public static synchronized GameManager getInstance() {


if (instance == null) {
instance = new GameManager();
}
return instance;
}
@Override
public void startNewGame(List<String> players) {
IGame game = new SnakeAndLadderGame(players);
game.play();
}
}

SNAKE&LADDER GAME
package service;

import model.Board;
import model.Dice;
import model.Player;

import java.util.ArrayList;
import java.util.List;

public class SnakeAndLadderGame implements IGame {


private final Board board;
private final List<Player> players;
private final Dice dice;
private final DiceService diceService;
private final BoardService boardService;
private int currentPlayerIndex;

public SnakeAndLadderGame(List<String> playerNames) {


board = new Board();
dice = new Dice();
diceService = new DiceService();
boardService = new BoardService(board);
players = new ArrayList<>();
for (String playerName : playerNames) {
players.add(new Player(playerName));
}
currentPlayerIndex = 0;
}

@Override
public void play() {
while (!isGameOver()) {
Player currentPlayer = players.get(currentPlayerIndex);
int diceRoll = diceService.roll();
int newPosition = currentPlayer.getPosition() + diceRoll;
if (newPosition <= board.getBoardSize()) {

currentPlayer.setPosition(boardService.getNewPositionAfterSnakeOrLadder(new
Position));
System.out.println(currentPlayer.getName() + " rolled a " +
diceRoll +
" and moved to position " +
currentPlayer.getPosition());
}

if (currentPlayer.getPosition() == board.getBoardSize()) {
System.out.println(currentPlayer.getName() + " wins!");
break;
}

currentPlayerIndex = (currentPlayerIndex + 1) % players.size();


}
}

private boolean isGameOver() {


for (Player player : players) {
if (player.getPosition() == board.getBoardSize()) {
return true;
}
}
return false;
}
}

SNAKEAND LADDERDEMO
package service;

import java.util.Arrays;
import java.util.List;

public class SnakeAndLadderDemo {


public static void run() {
IGameManager gameManager = GameManager.getInstance();

// Start game 1
List<String> players1 = Arrays.asList("Player 1", "Player 2",
"Player 3");
gameManager.startNewGame(players1);

// Start game 2
List<String> players2 = Arrays.asList("Player 4", "Player 5");
gameManager.startNewGame(players2);
}
}

main:
import service.SnakeAndLadderDemo;
public class Main {
public static void main(String[] args) {

SnakeAndLadderDemo.run();
}
}

You might also like