Skip to content
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
42 changes: 42 additions & 0 deletions Board.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import java.util.List;

public class Board {

int size;
List<Cell>cells;
List<SnakeLadder> snakeLadders;

public Board (int size, List<Cell> cells, List<SnakeLadder> snakeLadders)
{
this.size = size;
this.cells = cells;
this.snakeLadders = snakeLadders;
}

public int handleSnakesAndLadders(Player player)
{
int currPosition= player.currPosition;
while(true)
{
boolean updated= false;
for(int i=0;i<snakeLadders.size();i++)
{
int start=snakeLadders.get(i).start;
int end=snakeLadders.get(i).end;

if(start == currPosition)
{
updated= true;
currPosition = end;
break;
}

}
if(updated==false)
{
break;
}
}
return currPosition;
}
}
8 changes: 8 additions & 0 deletions Cell.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public class Cell {
int cellNo;

public Cell (int cellNo)
{
this.cellNo = cellNo;
}
}
11 changes: 11 additions & 0 deletions Dice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import java.util.Random;

class Dice
{
Random rand = new Random();

int rollDice() {
return rand.nextInt(6) + 1; // Generates 1 to 6
}

}
29 changes: 29 additions & 0 deletions Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.List;

public class Game {

List<Dice> dices;
List<Player> players;

Board board;

public Game (Board board, List<Player>players, List<Dice> dices)
{
this.board = board;
this.players =players;
this.dices= dices;
}

public boolean hasWon(Player player)
{
if(player.currPosition == board.size)
return true;
return false;
}

int newPosOfPlayer(Player player, int count)
{
return player.currPosition + count;
}

}
16 changes: 16 additions & 0 deletions Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class Player {

String name;
int currPosition;

public Player (String name)
{
this.name= name;
currPosition = 0;
}
void changePosition(int diff)
{
currPosition+=diff;
}

}
11 changes: 11 additions & 0 deletions SnakeLadder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class SnakeLadder {

int start;
int end;

public SnakeLadder( int start, int end)
{
this.start = start;
this.end = end;
}
}
129 changes: 129 additions & 0 deletions main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class main {

public static void main(String[] args) {

int size=0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter the size of the board: ");
size = sc.nextInt();

List<Cell> cells = new ArrayList<>();

for(int i=0; i <size;i++)
{
cells.add(new Cell(i + 1));
}

int snakesLaddersCount=0;

System.out.println("Enter the count of snakes and ladders: ");

snakesLaddersCount = sc.nextInt();

List<SnakeLadder> snakeLadders = new ArrayList<>();

System.out.println("Enter " + snakesLaddersCount + " lines containing start and end coordinates of snakes and ladders: ");

for(int i=0; i <snakesLaddersCount;i++)
{
int l = sc.nextInt();
int r = sc.nextInt();

snakeLadders.add(new SnakeLadder(l,r));
}

//System.out.println(cells.getClass());
// System.out.println(snakeLadders.getClass());

//sc.close();
Board board = new Board(size, cells, snakeLadders);

int playerCount = 0;

System.out.println("Enter count of players");
playerCount = sc.nextInt();

List<Player> players = new ArrayList<>();

sc.nextLine(); // fix: consume the newline
System.out.println("Enter " + playerCount + " lines containing names of players: ");

for (int i = 0; i < playerCount; i++) {
String name = sc.nextLine();
players.add(new Player(name));
}

int diceCount = 0;
System.out.println("Enter count of dice");

diceCount = sc.nextInt();

List<Dice> dices = new ArrayList<>();

for (int i = 0; i < diceCount; i++) {
dices.add(new Dice());
}

//System.out.println(board.getClass());
// System.out.println(players.getClass());
// System.out.println(dices.getClass());

Game game = new Game(board, players, dices);

int turn =0;
int hasturn=0;
while(true)
{
turn++;
if(turn ==500000 ) break;
Player player = players.get(hasturn);
int nowpos= player.currPosition;

//System.out.println("enter to throw dice!!" + player.name);

int count = 0;
for(int i=0;i<diceCount;i++)
{
count+=dices.get(i).rollDice();
}

//System.out.println("number rolled in dice" + count);

int newpos= game.newPosOfPlayer(player,count);

if(newpos>board.size)
{
System.out.println("Player " + player.name + "rolled a " + count + " and moved from " + nowpos + " to " + player.currPosition);
System.out.println("Player " + player.name + " has gone outisze the board. Skipping ts move");
hasturn++;
hasturn%=playerCount;
continue;
}

player.currPosition = newpos;

int finall= board.handleSnakesAndLadders(player);
player.currPosition= finall;

System.out.println("Player " + player.name + " rolled a " + count + " and moved from " + nowpos + " to " + player.currPosition);

if(game.hasWon(player))
{
System.out.println(" Player " + player.name + " has won ");
break;
}


hasturn++;
hasturn%=playerCount;
}

}


}