-
Notifications
You must be signed in to change notification settings - Fork 0
/
Driver.java
245 lines (176 loc) · 8.11 KB
/
Driver.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package SnakeAndLadder;
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;
/**
*
* @author Alizey
*/
public class Driver {
public static void main(String arg[]){
Board b = new Board();
int i, j;
Scanner sc = new Scanner(System.in);
Red R = new Red("Red");
Green G = new Green("Green");
Blue B = new Blue("Blue");
Yellow Y = new Yellow("Yellow");
Snake s = new Snake(0);
Ladder l = new Ladder(0);
int loc = 0;
// generating a random number between 1 and 6.
// upper limit is usually exclusive of range, so to make it inclusive. we add one to it.
int dice;
Piece p = null;
boolean end = false;
System.out.println("Welcome to the classic Snake&Ladder game!");
System.out.println("The game will work as follows:- \nThe Red piece will run first and then the moves of Blue, Green and Yellow will follow respectively!");
while(end == false){
System.out.println("");
for(i = 0; i <= 5; i++ ){
for(j = 0; j <= 100; j++){
if(Board.board[i][j] != null)
System.out.print(Board.board[i][j] + " ");
else if(i == 0 && Board.board[i][j] == null)
System.out.print(j + " ");
else if(i == 5 && Board.board[0][j] != null){
if(Board.board[0][j].getClass() == s.getClass()){
s = (Snake) Board.board[0][j];
int k = j - s.poison;
System.out.print(k+ " ");
}
else if(Board.board[0][j].getClass() == l.getClass()){
l = (Ladder) Board.board[0][j];
int t = j + l.climb;
System.out.print(t+" ");
}
}
else if(j < 10)
System.out.print("-"+" ");
else if(j > 9 && Board.board[0][j] != null)
System.out.print("-"+ " ");
else
System.out.print("--"+ " ");
}
System.out.println("");
}
System.out.println("");
if(Board.chance%4 == 0){
System.out.println("Red's chance!");
System.out.println("Press ENTER to roll the dice! :)");
for(i = 0; i < 101; i++){
if(Board.board[1][i] == null){
continue;
}
if(Board.board[1][i].getClass() == R.getClass()){
loc = i;
break;
}
}
sc.nextLine();
dice = ThreadLocalRandom.current().nextInt(1, 7);
System.out.println("The dice roll got us: " + dice);
p = (Piece) Board.board[1][loc];
p.move(1, loc, 1, loc+dice);
if(dice == 6){
Board.chance--;
}
}
else if(Board.chance%4 == 1){
System.out.println("Green's chance!");
System.out.println("Press ENTER to roll the dice! :)");
for(i = 0; i < 101; i++){
if(Board.board[2][i] == null){
continue;
}
if(Board.board[2][i].getClass() == G.getClass()){
loc = i;
break;
}
}
sc.nextLine();
dice = ThreadLocalRandom.current().nextInt(1, 7);
System.out.println("The dice roll got us: " + dice);
p = (Piece) Board.board[2][loc];
p.move(2, loc, 2, loc+dice);
if(dice == 6){
Board.chance--;
}
}
else if(Board.chance%4 == 2){
System.out.println("Blue's chance!");
System.out.println("Press ENTER to roll the dice! :)");
for(i = 0; i < 101; i++){
if(Board.board[3][i] == null){
continue;
}
if(Board.board[3][i].getClass() == B.getClass()){
loc = i;
break;
}
}
sc.nextLine();
dice = ThreadLocalRandom.current().nextInt(1, 7);
System.out.println("The dice roll got us: " + dice);
p = (Piece) Board.board[3][loc];
p.move(3, loc, 3, loc+dice);
if(dice == 6){
Board.chance--;
}
}
else if(Board.chance%4 == 3){
System.out.println("Yellow's chance!");
System.out.println("Press ENTER to roll the dice! :)");
for(i = 0; i < 101; i++){
if(Board.board[4][i] == null){
continue;
}
if(Board.board[4][i].getClass() == Y.getClass()){
loc = i;
break;
}
}
sc.nextLine();
dice = ThreadLocalRandom.current().nextInt(1, 7);
System.out.println("The dice roll got us: " + dice);
p = (Piece) Board.board[4][loc];
p.move(4, loc, 4, loc+dice);
if(dice == 6){
Board.chance--;
}
}
for(j = 1; j <= 4; j++){
if(Board.board[j][100] == null)
continue;
if(Board.board[j][100] != null){
p = (Piece) Board.board[j][100];
end = true;
break;
}
}
if(end == true){
System.out.println("Congratulations, the winner is: "+ p.colour);
for(i = 0; i <= 4; i++ ){
for(j = 0; j <= 100; j++){
if(Board.board[i][j] != null)
System.out.print(Board.board[i][j] + " ");
else if(i == 0 && Board.board[i][j] == null)
System.out.print(j + " ");
else if(j < 10)
System.out.print("-"+" ");
else if(j > 9 && Board.board[0][j] != null)
System.out.print("-"+ " ");
else
System.out.print("--"+ " ");
}
System.out.println("");
}
System.out.println("");
}
}
}
}