-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLadderAndSnake.java
339 lines (288 loc) · 10.6 KB
/
LadderAndSnake.java
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import java.util.Random;
import java.util.Scanner;
public class LadderAndSnake {
/**
* create a 5-player obj array
* create a 10 x 10 map obj array
*/
private int numberOfPlayers;
Scanner scanner = new Scanner(System.in);
private Players[] playerArray = new Players[5];
private Map[][] map = new Map[10][10];
//=============================================
/**
* create a map method with an obj array map, which contains string symbo (default value "-", when occupied by a player it will be a player name); and an integer for the player location
*/
public void map() {
int i = 0, j = 0, k = 1;
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++, k++)
map[i][j] = new Map("-", k);
}
}
//====================================================================================
/**
* a method to print out the map. after the user movement, symbo will be signed as players name. After the next movement, obj signed with players name will be changed back to the default "-"; when printing out the map, if the symbo value is "-" will be printed the location number; while the obj with the players name will be printed as the players name;
* @param player = player's name
* @param currentPosition = player's location number
*/
public void printMap(String player, int currentPosition) {
int i = 0, j = 0;
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
if (map[i][j].getSymbo().equals(player)) {
map[i][j].setSymbo("-");
}
}
}
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
if (map[i][j].getLocation() == currentPosition) {
map[i][j].setSymbo(player);
}
}
}
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
if (map[i][j].getLocation() == currentPosition)
System.out.print(map[i][j].getSymbo() + " ");
else if (map[i][j].getSymbo().equals("-"))
System.out.print(map[i][j].getLocation() + " ");
else if (!map[i][j].getSymbo().equals("-"))
System.out.print(map[i][j].getSymbo() + " ");
}
System.out.println();
}
}
//========================================================================
/**
* constructor
* @param numberOfPlayers
*/
public LadderAndSnake(int numberOfPlayers) {
this.numberOfPlayers = numberOfPlayers;
System.out.println("number of players: " + numberOfPlayers);
}
//=====================================================================
/**
* a method to declare each player in an obj array. create an extra array element as a temporary element for later to sort the player order
*/
public void setPlayerArray() {
playerArray[0] = new Players("player#1", 0, 0, false, false);
playerArray[1] = new Players("player#2", 0, 0, false, false);
playerArray[2] = new Players("player#3", 0, 0, false, false);
playerArray[3] = new Players("player#4", 0, 0, false, false);
playerArray[4] = new Players("temp", 0, 0, false, false);
}
// ===================================================================
/**
* a method to make users play term by term with the sequence of the players array. while loop will be discontinued once there is a winner
*/
public void play() {
printOrder();
while (!playerArray[0].isWinner() && !playerArray[1].isWinner() && !playerArray[2].isWinner()
&& !playerArray[3].isWinner()) {
int i = 0, j = 0;
for (i = 0; i < numberOfPlayers && !playerArray[0].isWinner() && !playerArray[1].isWinner()
&& !playerArray[2].isWinner() && !playerArray[3].isWinner(); i++) {
System.out.println("\n\n" + playerArray[i].getName());
System.out.println("Please type any buttom to role the dice");
String enter = scanner.nextLine();
j = flipDice();
playerArray[i].playerMove(j);
playerArray[i].setPosition(snakeNLadder(playerArray[i].getPosition()));//check if the position hit a ladder or a snake, so the position will be further changed accordingly
if (playerArray[i].getPosition() == 100) {
playerArray[i].setWinner(true);
}
printMap(playerArray[i].getName(), playerArray[i].getPosition());
}
}
scanner.close();
}
//===============================================================================
/**
* a method to ask the player roll the dice to determine the order of playing turns
* @param numberOfPlayers
*/
public void decidOrder(int numberOfPlayers) {
setPlayerArray();
System.out.println("Before starting, Please role the dice to determine the order of players");
int counter = 1;
for (int i = 1; i < numberOfPlayers + 1; i++) {
System.out.println("player#" + counter + " turn");
System.out.println("please type any buttom to role the dice:");
String enter = scanner.nextLine();
System.out.print("player#" + counter + " got: ");
//Random rand = new Random();
int randomNum = flipDice();
//rand.nextInt(6) + 1;
System.out.println("*" + randomNum + "*\n");
if (i == 1) {
playerArray[0].setDiceNm(randomNum);
} else if (i == 2) {
playerArray[1].setDiceNm(randomNum);
} else if (i == 3) {
playerArray[2].setDiceNm(randomNum);
} else if (i == 4) {
playerArray[3].setDiceNm(randomNum);
}
counter++;
}
}
//=======================================================================================================
/**
* a method to check the order of playing turns. the first for loop to sort the array order (players with higher dice number put at earlier turns); the second for loop to determine if there is a tie. If number of ties>3 will ask all players to roll the dice again, otherwise will user a tie method
*/
public void checkOder() {
int sameDice = 0;
//boolean tie = false;
for (int i = 0; i < numberOfPlayers; i++) {
for (int j = i + 1; j < numberOfPlayers; j++) {
if (playerArray[i].getDiceNm() < playerArray[j].getDiceNm()) {
playerArray[4] = playerArray[i];
playerArray[i] = playerArray[j];
playerArray[j] = playerArray[4];
}
}
}
for (int i = 0; i < numberOfPlayers; i++) {
for (int j = i + 1; j < numberOfPlayers; j++) {
if (playerArray[i].getDiceNm() == playerArray[j].getDiceNm()) {
sameDice++;
System.out.println("******* "+playerArray[i].getName() + " and " + playerArray[j].getName() + " have a tie *******");
playerArray[i].setRepeatDice(true);
playerArray[j].setRepeatDice(true);
//tie = true;
}
}
}
if (sameDice >= 2) {
System.out.println("You all need to re-role!\n");
decidOrder(numberOfPlayers);
} else if (sameDice == 1) {
tie();
}
}
// ========================================================================================
/**
* a method to solve the tie issue. the first for loop to ask the user to roll the dice. the second for loop to sort the order. if another tie is reached call the tie method again.
*/
public void tie() {
for (int i = 0; i < numberOfPlayers; i++) {
if (playerArray[i].isRepeatDice()) {
System.out.println("\n"+playerArray[i].getName() + " turn\n" + "Please type any buttom to roll the dice");
String enter = scanner.nextLine();
playerArray[i].setDiceNm(flipDice());
System.out.println(playerArray[i].getName() + " got: *"
+ playerArray[i].getDiceNm() + "*\n");
}
}
for (int i = 0; i < numberOfPlayers; i++) {
for (int j = i + 1; j < numberOfPlayers; j++) {
if (playerArray[i].isRepeatDice() && playerArray[j].isRepeatDice()) {
if (playerArray[i].getDiceNm() < playerArray[j].getDiceNm()) {
playerArray[4] = playerArray[i];
playerArray[i] = playerArray[j];
playerArray[j] = playerArray[4];
} else if (playerArray[i].getDiceNm() == playerArray[j].getDiceNm()) {
System.out.println("Tie again!");
tie();
}
}
}
}
}
//======================================================================================================
/**
* a method to print out the order of playing turns
*/
public void printOrder() {
System.out.println("\nThe order is: ");
for (int i = 0; i < numberOfPlayers; i++) {
System.out.print(playerArray[i].getName() + " ");
}
}
/**
*
* @return a random value from 1-6
*/
public int flipDice() {
int a;
return a = 1 + (int) (Math.random() * 6);
}
// =========================================================================================
/**
* a method to detect ladders and snakes. if the player location match a ladder or a snake location the player's location will be changed according to the rule
*
* @param userPosition
* @return player's location after encounter a ladder or a snake
*/
public int snakeNLadder(int userPosition) {
if (userPosition == 16) {
System.out.println("Got bit by a snake! Go down! You are now at 6");
userPosition = 6;
}
if (userPosition == 48) {
System.out.println("Got bit by a snake! Go down! You are now at 30");
userPosition = 30;
}
if (userPosition == 62) {
System.out.println("Got bit by a snake! Go down! You are now at 19");
userPosition = 19;
}
if (userPosition == 63) {
System.out.println("Got bit by a snake! Go down! You are now at 60");
userPosition = 60;
}
if (userPosition == 98) {
System.out.println("Got bit by a snake! Go down! You are now at 78");
userPosition = 78;
}
if (userPosition == 97) {
System.out.println("Got bit by a snake! Go down! You are now at 76");
userPosition = 76;
}
if (userPosition == 95) {
System.out.println("Got bit by a snake! Go down! You are now at 24");
userPosition = 24;
}
if (userPosition == 93) {
System.out.println("Got bit by a snake! Go down! You are now at 68");
userPosition = 68;
}
if (userPosition == 4) {
System.out.println("Reach a ladder! Go up! You are now at 14");
userPosition = 14;
}
if (userPosition == 9) {
System.out.println("Reach a ladder! Go up! You are now at 31");
userPosition = 31;
}
if (userPosition == 20) {
System.out.println("Reach a ladder! Go up! You are now at 38");
userPosition = 38;
}
if (userPosition == 40) {
System.out.println("Reach a ladder! Go up! You are now at 42");
userPosition = 42;
}
if (userPosition == 36) {
System.out.println("Reach a ladder! Go up! You are now at 44");
userPosition = 44;
}
if (userPosition == 28) {
System.out.println("Reach a ladder! Go up! You are now at 84");
userPosition = 84;
}
if (userPosition == 71) {
System.out.println("Reach a ladder! Go up! You are now at 91");
userPosition = 91;
}
if (userPosition == 80) {
System.out.println("Reach a ladder! Go up! You are now at 100");
userPosition = 100;
}
return userPosition;
}
//===============================================================================
}