June 18, 2020

How to implement Snakes and Ladders using Java?

Let's create a SnakeNLadder.java class. Define constant WINPOINT with value 100. Each player has to start the journey from 0, roll the dice and try to reach WINPOINT.

For snake and ladder, I create a static Hashmap. Snake Map has the key as a starting point of snake and value as tailing point of snake. Similarly, ladder map has key as a lower point of ladder and value as upper point of ladder. I have initialized values of both snake and ladder map in static block.

GIT URL: How to implement Snakes and Ladders using Java?

Methods:
1). rollDice() method will generate a random number between 1 to 6.

2). calculatePlayerValue() method will calculate the position of the player based on his current position after rolling the dice.

We will first check if the new position value is greater than WINPOINT, then it will again set it to old position. In SnakeNLadder to win the race, your final position value must match the WINPOINT. It can't be less or more.

Then it will check if there is a snake or ladder at the new position, if yes then it will change the new position of player accordingly.

3). isWin() returns true, if the player reached to the WINPOINT.

4). startGame() method will initiate the game. This method keeps running until any player reaches to the WINPOINT. FYI, players need to press 'r' key to roll the dice, also the currentPlayer keeps on changing after each dice roll.

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
public class SnakeLadderTest {
public static void main(String[] args) {
SnakeNLadder snakeLadder = new SnakeNLadder();
snakeLadder.startGame();
}
}
class SnakeNLadder {
final static int WINPOINT = 100;
static Map<Integer, Integer> snake = new HashMap<>();
static Map<Integer, Integer> ladder = new HashMap<>();
{
snake.put(99, 54);
snake.put(70, 55);
snake.put(52, 42);
snake.put(25, 2);
snake.put(95, 72);
ladder.put(6, 25);
ladder.put(11, 40);
ladder.put(60, 85);
ladder.put(46, 90);
ladder.put(17, 69);
}
//this method will generate random number from 1-6
public int rollDice() {
int n = 0;
Random r = new Random();
n = r.nextInt(7);
return (n == 0 ? 1 : n);
}
/*calculatePlayerValue() method will calculate
* the position of the player based on his current
* position after rolling the dice.
*/
public int calculatePlayerValue(int playerPosition, int diceValue) {
int playerNewPosition=playerPosition+diceValue;
if (playerNewPosition > WINPOINT)
return playerPosition;
if (null !=snake.get(playerNewPosition)) {
System.out.println("Oops..swallowed by the snake..");
playerNewPosition=snake.get(playerNewPosition);
}
if (null !=ladder.get(playerNewPosition)) {
System.out.println("YAY! climbing the ladder..");
playerNewPosition=ladder.get(playerNewPosition);
}
return playerNewPosition;
}
public boolean isWin(int playerPosition) {
return WINPOINT==playerPosition;
}
public void startGame() {
int player1Position=0, player2Position=0;
int currentPlayer=-1;
Scanner scan= new Scanner(System.in);
String rPressed;
int diceValue = 0;
do {
System.out.println(currentPlayer == -1
? "\n\nFirst player's turn" : "\n\nSecond player's turn");
System.out.println("Press 'r' to roll Dice");
rPressed=scan.next();
diceValue=rollDice();
if (currentPlayer==-1) {
player1Position=calculatePlayerValue(player1Position, diceValue);
System.out.println("First Player Position:"+player1Position);
System.out.println("Second Player Position:"+player2Position);
System.out.println("-------------------------");
if (isWin(player1Position)) {
System.out.println("Congratulations! First player won");
return;
}
} else {
player2Position = calculatePlayerValue(player2Position, diceValue);
System.out.println("First Player Position:"+player1Position);
System.out.println("Second Player Position:"+player2Position);
System.out.println("-------------------------");
if (isWin(player2Position)) {
System.out.println("Congratulations! Second player won");
return;
}
}
currentPlayer = -currentPlayer;
} while ("r".equals(rPressed));
}
}


Happy Coding :)

-K Himaanshu Shuklaa..

No comments:

Post a Comment