-
Notifications
You must be signed in to change notification settings - Fork 513
Expand file tree
/
Copy pathmain.java
More file actions
129 lines (88 loc) · 3.55 KB
/
Copy pathmain.java
File metadata and controls
129 lines (88 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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;
}
}
}