Last active
December 3, 2017 11:36
-
-
Save shivamsaboo17/9b3ac74d42f96477389d5f6269a1eabd to your computer and use it in GitHub Desktop.
Snakes and ladder simulation in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package snladder; | |
import java.util.*; | |
public class Board { | |
// The board class defines the board | |
// We use Hash Maps for storing data about snakes and ladders. | |
private HashMap<Integer, Integer> board = new HashMap<>(); | |
int boardCounter; | |
Board(){ | |
generateBoard(); | |
} | |
private void generateBoard(){ | |
board.put(6, 17); | |
board.put(14, 3); | |
board.put(20, 15); | |
board.put(24, 26); | |
board.put(30, 44); | |
board.put(39, 33); | |
board.put(49, 62); | |
board.put(66, 53); | |
board.put(69, 58); | |
board.put(79, 67); | |
board.put(82, 86); | |
board.put(84, 71); | |
board.put(88, 36); | |
boardCounter = 1; | |
} | |
Player updatePlayerPos(Player player, int dice){ | |
boolean win = player.getWin(); | |
if(!win){ | |
int current_pos = player.getPos(); | |
if(current_pos + dice > 100){ | |
System.out.println("Player rolled too high"); | |
} | |
else{ | |
if(board.containsKey(current_pos + dice)){ | |
player.setPos(board.get(current_pos + dice)); | |
if(board.get(current_pos + dice) > current_pos + dice){ | |
player.ladderCount ++; | |
} | |
else{ | |
player.snakesCount++; | |
} | |
} | |
else{ | |
player.setPos(current_pos + dice); | |
} | |
} | |
} | |
player = checkWin(player); | |
return player; | |
} | |
private Player checkWin(Player player){ | |
if(player.getPos() == 100){ | |
player.setWin(true); | |
return player; | |
} | |
return null; | |
} | |
public static void main(String[] args) { | |
Player p1 = new Player("A"); | |
Player p2 = new Player("B"); | |
Board b = new Board(); | |
// Type this snippet in the button-click listener | |
if (p1.getWin() || p2.getWin()){ | |
// Display winning message | |
} | |
else{ | |
if(b.boardCounter % 2 == 1){ | |
b.updatePlayerPos(p1, (int)Math.floor(Math.random() * 5 + 1)); | |
} | |
else{ | |
b.updatePlayerPos(p2, (int)Math.floor(Math.random() * 5 + 1)); | |
} | |
b.boardCounter ++; | |
} | |
// Type code up-to here in button-click listener | |
} | |
} | |
class Player{ | |
// The Player class helps maintaining attributes | |
// of the players. | |
// Defining separate class for players helps in making | |
// multiplayer game. | |
private String playerName; | |
private int playerPos; | |
private boolean playerWin; | |
int snakesCount; | |
int ladderCount; | |
Player(String name){ | |
playerPos = 0; | |
playerWin = false; | |
playerName = name; | |
snakesCount = 0; | |
ladderCount = 0; | |
} | |
int getPos(){ | |
return playerPos; | |
} | |
boolean getWin(){ | |
return playerWin; | |
} | |
void setPos(int pos){ | |
playerPos = pos; | |
} | |
void setWin(boolean win){ | |
playerWin = win; | |
} | |
String getName(){ | |
return playerName; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment