Skip to content

Please review code for snake-ladder #156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions SnakeLadderApplication/src/Board.java
Original file line number Diff line number Diff line change
@@ -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<Integer,Integer> snakeMap;
private final Map<Integer,Integer> laddersMap;
private final List<List<Integer>> 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<Integer> row = new ArrayList<>(columns);
for(int j=0;j<columns;j++) {
row.add(0);
}
values.add(row);
}
setupValues();
snakeMap = new HashMap<>();
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<rows;i++) {
if(i%2==0) {
for (int j = 0; j < columns; j++) {
values.get(i).set(j, start--);
}
}
else {
for (int j = columns-1; j >=0; j--) {
values.get(i).set(j, start--);
}
}
}
}

public Map<Integer, Integer> getSnakeMap() {
return snakeMap;
}

public Map<Integer, Integer> getLaddersMap() {
return laddersMap;
}

public void display() {
System.out.println("\n-----------------------Board State--------------------\n");
for(int i=0;i<rows;i++) {
for(int j=0;j<columns;j++) {
System.out.print(values.get(i).get(j));
System.out.print(" ");
}
System.out.println();
}
System.out.println("\n-----------------------END------------------------------\n");
}
}
21 changes: 21 additions & 0 deletions SnakeLadderApplication/src/Dice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import java.util.concurrent.ThreadLocalRandom;

public class Dice {
private final int minValue;
private final int maxValue;

public Dice(int minValue, int maxValue) {
this.minValue = minValue;
this.maxValue = maxValue;
}

public int roll(int diceCount) {
int totalValue = 0;
while(diceCount > 0) {
int value = ThreadLocalRandom.current().nextInt(maxValue - minValue) + 1;
totalValue += value;
diceCount--;
}
return totalValue;
}
}
55 changes: 55 additions & 0 deletions SnakeLadderApplication/src/Game.java
Original file line number Diff line number Diff line change
@@ -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<Player> 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<Player> 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);
}
}
45 changes: 45 additions & 0 deletions SnakeLadderApplication/src/GameUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.util.*;

public class GameUtil {
private static Integer destination;
private static Map<Player,List<Integer>> playerMoves;

public static void start(List<Player> players, int dices,Dice dice, Board board) {
Deque<Player> 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<Integer> 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);
}
}
34 changes: 34 additions & 0 deletions SnakeLadderApplication/src/Main.java
Original file line number Diff line number Diff line change
@@ -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<players;i++) {
String playerName = scanner.nextLine();
game.getPlayersList().add(new Player(playerName));
}
scanner.close();
game.start();
}
}
17 changes: 17 additions & 0 deletions SnakeLadderApplication/src/Move.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Move {
private final int initialPos;
private final int finalPos;

public Move(int initialPos, int finalPos) {
this.initialPos = initialPos;
this.finalPos = finalPos;
}

public int getFinalPos() {
return finalPos;
}

public int getInitialPos() {
return initialPos;
}
}
11 changes: 11 additions & 0 deletions SnakeLadderApplication/src/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class Player {
private final String playerName;

public Player(String playerName) {
this.playerName = playerName;
}

public String getPlayerName() {
return playerName;
}
}