diff --git a/Board.java b/Board.java new file mode 100644 index 00000000..76456274 --- /dev/null +++ b/Board.java @@ -0,0 +1,42 @@ +import java.util.List; + +public class Board { + + int size; + Listcells; + List snakeLadders; + + public Board (int size, List cells, List 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 dices; + List players; + + Board board; + + public Game (Board board, Listplayers, List 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; + } + +} diff --git a/Player.java b/Player.java new file mode 100644 index 00000000..1d426869 --- /dev/null +++ b/Player.java @@ -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; + } + +} diff --git a/SnakeLadder.java b/SnakeLadder.java new file mode 100644 index 00000000..b0784439 --- /dev/null +++ b/SnakeLadder.java @@ -0,0 +1,11 @@ +public class SnakeLadder { + + int start; + int end; + + public SnakeLadder( int start, int end) + { + this.start = start; + this.end = end; + } +} diff --git a/main.java b/main.java new file mode 100644 index 00000000..f72f62f9 --- /dev/null +++ b/main.java @@ -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 cells = new ArrayList<>(); + + for(int i=0; i snakeLadders = new ArrayList<>(); + + System.out.println("Enter " + snakesLaddersCount + " lines containing start and end coordinates of snakes and ladders: "); + + for(int i=0; i 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 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;iboard.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; + } + + } + + +}