Skip to content

Instantly share code, notes, and snippets.

@rohanjai777
Created November 9, 2022 06:03
Show Gist options
  • Save rohanjai777/6965994e846b4a37a10e56360f71366e to your computer and use it in GitHub Desktop.
Save rohanjai777/6965994e846b4a37a10e56360f71366e to your computer and use it in GitHub Desktop.
import java.util.*;
class Player{
String name;
int currentPosition;
Player(String name, int currentPosition){
this.name = name;
this.currentPosition = currentPosition;
}
}
//------------------
class Dice{
int diceCount;
int min = 1;
int max = 6;
Dice(int diceCount){
this.diceCount = diceCount;
}
public int rollDice(){
int totalSum = 0;
int diceUsed = 0;
while(diceUsed < diceCount){
int value = (int)Math.random()*(max-min+1) + min;
totalSum+=value;
diceUsed++;
}
return totalSum;
}
}
//-----------------------
class Jump{
int start;
int end;
}
//------------------------
class Cell{
Jump jump;
}
//------------------------
class Board{
Cell[][] cells;
Board(int size, int numberOfSnakes, int numberOfLadder){
initializeCells(size);
initializeSnakesAndLadders(numberOfSnakes,numberOfLadder);
}
private void initializeCells(int size){
cells = new Cell[size][size];
for(int i=0;i<size;i++){
for(int j=0;j<size;j++){
cells[i][j] = new Cell();
}
}
}
private void initializeSnakesAndLadders(int numberOfSnakes, int numberOfLadder){
int min = 1;
int max = cells.length*cells.length-1;
while(numberOfSnakes>0){
int start = (int)(Math.random()*(max-min+1)+min);
int end = (int)(Math.random()*(max-min+1)+min);
if(start<=end){
continue;
}
Jump jump = new Jump();
jump.start = start;
jump.end = end;
Cell cell = getCell(start);
cell.jump = jump;
numberOfSnakes--;
}
while(numberOfLadder>0){
int start = (int)(Math.random()*(max-min+1)+min);
int end = (int)(Math.random()*(max-min+1)+min);
if(start>=end){
continue;
}
Jump jump = new Jump();
jump.start = start;
jump.end = end;
Cell cell = getCell(start);
cell.jump = jump;
numberOfLadder--;
}
}
public Cell getCell(int position){
int row = position / cells.length;
int col = position % cells.length;
return cells[row][col];
}
}
//-----------------------------
class Game{
Board board;
Dice dice;
Deque<Player> players = new LinkedList<>();
Player winner;
public Game(){
initialize();
}
public void initialize(){
board = new Board(10,5,4);
System.out.println("Board initialized");
dice = new Dice(1);
System.out.println("Dice initialized");
winner = null;
addPlayers();
}
public void addPlayers(){
Player p1 = new Player("rohan",0);
Player p2 = new Player("janu",0);
players.add(p1);
players.add(p2);
}
public void startGame(){
while(winner == null){
//check whose turn now
Player currentPlayer = checkTurn();
System.out.println(currentPlayer.name+" turn ");
//dice rolled
int diceNumber = dice.rollDice();
System.out.println(currentPlayer.name+" "+diceNumber);
//new position
int newPosition = currentPlayer.currentPosition + diceNumber;
//check jump at new position
newPosition = checkJump(newPosition);
currentPlayer.currentPosition = newPosition;
System.out.println(currentPlayer.name+" "+currentPlayer.currentPosition);
if(newPosition >= board.cells.length * board.cells.length-1){ //we get the winner, game stop
winner = currentPlayer;
}
}
System.out.println("Winner is "+winner.name);
}
private Player checkTurn(){
Player player = players.removeFirst();
players.addLast(player);
return player;
}
private int checkJump(int position){
if(position > board.cells.length * board.cells.length-1){
return position;
}
Cell cell = board.getCell(position);
if(cell.jump != null && cell.jump.start == position){
return cell.jump.end;
}
return position;
}
}
//----------------------------------------
public class client{
public static void main(String[] args){
Game game = new Game();
game.startGame();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment