diff --git a/SnakeLadderApplication/src/Board.java b/SnakeLadderApplication/src/Board.java new file mode 100644 index 00000000..3ebd0af3 --- /dev/null +++ b/SnakeLadderApplication/src/Board.java @@ -0,0 +1,73 @@ +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class Board { + private final int rows; + private final int columns; + + private final Map snakeMap; + private final Map laddersMap; + private final List> values; + + public Board(int rows, int columns) { + this.rows = rows; + this.columns = columns; + values = new ArrayList<>(rows); + for(int i=0;i< rows;i++){ + List row = new ArrayList<>(columns); + for(int j=0;j(); + laddersMap = new HashMap<>(); + } + + public int getRows() { + return rows; + } + + public int getColumns() { + return columns; + } + + private void setupValues() { + int start= rows * columns; + for(int i=0;i=0; j--) { + values.get(i).set(j, start--); + } + } + } + } + + public Map getSnakeMap() { + return snakeMap; + } + + public Map getLaddersMap() { + return laddersMap; + } + + public void display() { + System.out.println("\n-----------------------Board State--------------------\n"); + for(int i=0;i 0) { + int value = ThreadLocalRandom.current().nextInt(maxValue - minValue) + 1; + totalValue += value; + diceCount--; + } + return totalValue; + } +} diff --git a/SnakeLadderApplication/src/Game.java b/SnakeLadderApplication/src/Game.java new file mode 100644 index 00000000..fc7c55fc --- /dev/null +++ b/SnakeLadderApplication/src/Game.java @@ -0,0 +1,55 @@ +import java.util.ArrayList; +import java.util.List; + +public class Game { + private final int dices; + private final Board board; + private final List playersList; + + private final Dice dice; + private static int snakes; + private static int ladders; + private Game(int rows,int columns,int dices) { + playersList = new ArrayList<>(); + board = new Board(rows,columns); + this.dices = dices; + dice = new Dice(1,6); + } + + private static Game game; + public static Game getInstance(int rows,int columns,int dices) { + if(game == null) + game = new Game(rows,columns,dices); + return game; + } + + public static int getSnakes() { + return snakes; + } + + public static void setSnakes(int snakes) { + Game.snakes = snakes; + } + + public static int getLadders() { + return ladders; + } + + public static void setLadders(int ladders) { + Game.ladders = ladders; + } + + public List getPlayersList() { + return playersList; + } + + public Board getBoard() { + return board; + } + + public void start() { + System.out.println("\n----------Starting game---------------\n"); + board.display(); + GameUtil.start(playersList,dices,dice,board); + } +} diff --git a/SnakeLadderApplication/src/GameUtil.java b/SnakeLadderApplication/src/GameUtil.java new file mode 100644 index 00000000..0b16bf7d --- /dev/null +++ b/SnakeLadderApplication/src/GameUtil.java @@ -0,0 +1,45 @@ +import java.util.*; + +public class GameUtil { + private static Integer destination; + private static Map> playerMoves; + + public static void start(List players, int dices,Dice dice, Board board) { + Deque playerTurns = new LinkedList<>(); + playerMoves = new HashMap<>(); + for(Player player: players) { + playerTurns.addLast(player); + playerMoves.put(player, new ArrayList<>()); + playerMoves.get(player).add(0); + } + destination = board.getRows() * board.getColumns(); + boolean isWinner = false; + while (!isWinner) { + Player player = playerTurns.removeFirst(); + int diceNumber = dice.roll(dices); + Move move = getMove(player,diceNumber,board); + playerMoves.get(player).add(move.getFinalPos()); + System.out.println(player.getPlayerName() + " rolled a " + diceNumber + " and move from " + move.getInitialPos() + " to " + move.getFinalPos() ); + if(move.getFinalPos() == destination ) { + System.out.println(player.getPlayerName() + " wins the game"); + isWinner = true; + } + else { + playerTurns.addLast(player); + } + } + } + + private static Move getMove(Player player,int diceNumber,Board board) { + List moves = playerMoves.get(player); + int currentPos = moves.get(moves.size()-1); + int newPos = currentPos + diceNumber; + if(newPos > destination) + return new Move(currentPos,currentPos); + if(board.getLaddersMap().containsKey(newPos)) + return new Move(currentPos,board.getLaddersMap().get(newPos)); + if(board.getSnakeMap().containsKey(newPos)) + return new Move(currentPos,board.getSnakeMap().get(newPos)); + return new Move(currentPos,newPos); + } +} \ No newline at end of file diff --git a/SnakeLadderApplication/src/Main.java b/SnakeLadderApplication/src/Main.java new file mode 100644 index 00000000..2bc36c31 --- /dev/null +++ b/SnakeLadderApplication/src/Main.java @@ -0,0 +1,34 @@ +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Game game = Game.getInstance(10,10,1); + Scanner scanner = new Scanner(System.in); + int snakes = scanner.nextInt(); + scanner.nextLine(); + Game.setSnakes(snakes); + for(int i = 0; i< Game.getSnakes(); i++) { + String[] snakePosition = scanner.nextLine().split(" "); + int pos1 = Integer.parseInt(snakePosition[0]); + int pos2 = Integer.parseInt(snakePosition[1]); + game.getBoard().getSnakeMap().put(pos1,pos2); + } + int ladders = scanner.nextInt(); + scanner.nextLine(); + Game.setLadders(ladders); + for(int i = 0; i< Game.getLadders(); i++) { + String[] ladderPosition = scanner.nextLine().split(" "); + int pos1 = Integer.parseInt(ladderPosition[0]); + int pos2 = Integer.parseInt(ladderPosition[1]); + game.getBoard().getLaddersMap().put(pos1,pos2); + } + int players = scanner.nextInt(); + scanner.nextLine(); + for(int i = 0;i