-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSnakes_and_Ladders123.java
292 lines (273 loc) · 10.9 KB
/
Snakes_and_Ladders123.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
import java.io.*;
class Snakes_and_Ladders123
{
public static void main (String [] args) throws IOException
{
int n2,n3,n4;
BufferedReader myInput2 = new BufferedReader (new InputStreamReader (System.in));
System.out.println ("\t\t\t\t\t\tWelcome To Snakes And Ladders\n\n");
System.out.println ("\t\t\t\t\t\t\tInstructions:");
System.out.println ("\t\t\t This program will simulate a regular snakes and ladders game, where you will be");
System.out.println ("\t\t\t facing the computer. You and the computer start at square 1, and the first one to");
System.out.println ("\t\t\t square 100 wins, however, there will be preset squares which will be the snakes or ladders.");
System.out.println ("\t\t\t Once you land on top of a snake you go down a few squares, and move up if you land");
System.out.println ("\t\t\t\t\t\t on the bottom of a ladder. Good Luck and Have FUN!!!");
int counter= 100,iteration=-1;
System.out.println ("-----------------------------------------------------Game Board-----------------------------------------------------------------------------");
while (counter > 0){
if (counter%10 == 0 && counter != 100){
if(iteration==-1)
{
counter-=9;
iteration=1;
}
else
{
System.out.print(counter+"\t");
counter-=10;
iteration=-1;
}
if(counter!=0)
System.out.print("\n" + counter + "\t");
}
else
System.out.print(counter + "\t");
counter+=iteration;
}
System.out.println();
System.out.println ("----------------------------------------------------------------------------------------------------------------------------------");
String sGame = "y";
System.out.print ("Do you want to play? Y or N > ");
sGame = myInput2.readLine ();
System.out.print ("\n\n\n\n\n\n");
while (sGame.equals ("y") || sGame.equals ("Y"))
{
sGame = startGame(sGame);
}
System.out.println ("\n\n\n\n\t\t\t\t\t\tSEE YA!!");
}
//-------------------------------------------------------------------------------------------------------------
public static void cls()
{
try
{
new ProcessBuilder("cmd","/c","cls").inheritIO().start().waitFor();
}
catch(Exception E)
{
System.out.println(E);
}
}
public static String startGame (String start) throws IOException
{
BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));
int userPosition = 1;
int compPosition = 1;
int diceRoll = 0;
int diceRoll2 = 0;
int userRoll = 1;
int compRoll = 1;
String playAgain = "y";
int snakesLaddersArray [] = new int [6];
snakesLaddersArray [0] = 54;
snakesLaddersArray [1] = 90;
snakesLaddersArray [2] = 99;
snakesLaddersArray [3] = 9;
snakesLaddersArray [4] = 40;
snakesLaddersArray [5] = 67;
while (playAgain.equals ("y") || playAgain.equals ("Y"))
{
userRoll = getDice(diceRoll, diceRoll2);
if(userRoll==6)
{
int n2 = getDice(diceRoll, diceRoll2);
if(n2==6)
{
int n3 = getDice(diceRoll, diceRoll2);
{
if(n3==6)
{
int n4 = getDice(diceRoll, diceRoll2);
if(n4==6)
{
userRoll = 0;
}
else
{
userRoll = 6+6+n4;
}
}
else
{
userRoll = 6+n3;
}
}
}
else
{
userRoll = n2;
}
}
compRoll = getDice(diceRoll, diceRoll2);
if(compRoll==6)
{
int n2 = getDice(diceRoll, diceRoll2);
if(n2==6)
{
int n3 = getDice(diceRoll, diceRoll2);
{
if(n3==6)
{
int n4 = getDice(diceRoll, diceRoll2);
if(n4==6)
{
userRoll = 0;
}
else
{
userRoll = 6+6+n4;
}
}
else
{
userRoll = 6+n3;
}
}
}
else
{
userRoll = n2;
}
}
System.out.println("---------------------------------------------------------------------------------------------------------------------------");
System.out.println ("\t\t\t\t\t------------------------------------------");
System.out.println ("\t\t\t\t\t|\tYou Rolled a " + userRoll + "\t\t|");
System.out.println ("\t\t\t\t\t|\tThe Computer Rolled a " + compRoll + "\t|");
System.out.println ("\t\t\t\t\t------------------------------------------");
userPosition = userPosition + userRoll;
compPosition = compPosition + compRoll;
userPosition = getP(userPosition, userRoll, snakesLaddersArray);
compPosition = compgetP(compPosition, compRoll, snakesLaddersArray, userPosition);
System.out.println("\t\t\t*************************************************************************");
System.out.println ("\t\t\t*\t\tYou are currently on square " + userPosition + "\t\t\t*");
System.out.println ("\t\t\t*\t\tThe Computer is currently on square " + compPosition + "\t\t*");
System.out.println("\t\t\t*************************************************************************");
if (userPosition == 100 || compPosition == 100)
{
userPosition = 1;
compPosition = 1;
System.out.print ("Do you want to play? Y or N > ");
playAgain = myInput.readLine ();
System.out.print ("\n\n\n\n\n\n\n\n\n\n\n\n");
}
else
{
System.out.print ("Do you want to play? Y or N > ");
playAgain = myInput.readLine ();
cls();
}
}
return playAgain;
}
public static int getDice (int diceRoll, int diceRoll2)
{
diceRoll = (int)(Math.random()*6 )+1 ;
diceRoll2 = (int)(Math.random()*6 )+1 ;
int move = diceRoll + diceRoll2;
return move;
}
public static int getP (int userPosition, int userRoll, int snakesLaddersArray []) throws IOException
{
if(userPosition == snakesLaddersArray[0])
{
userPosition = 19;
System.out.println ("\t\t\t\t~~~~~~~~~~~~~You Got Bit By A Snake, GO DOWN!!!~~~~~~~~~~~~~");
}
else if (userPosition == snakesLaddersArray[1])
{
userPosition = 48;
System.out.println ("\t\t\t\t~~~~~~~~~~~~~You Got Bit By A Snake, GO DOWN!!!~~~~~~~~~~~~~");
}
else if (userPosition == snakesLaddersArray[2])
{
userPosition = 77;
System.out.println ("\t\t\t\t~~~~~~~~~~~~~You Got Bit By A Snake, GO DOWN!!!~~~~~~~~~~~~~");
}
else if (userPosition == snakesLaddersArray[3])
{
userPosition = 34;
System.out.println ("\t\t\t\t~~~~~~~~~~~~~You Got A Ladder!! GO UP!!!~~~~~~~~~~~~~");
}
else if (userPosition == snakesLaddersArray[4])
{
userPosition = 64;
System.out.println ("\t\t\t\t~~~~~~~~~~~~~You Got A Ladder!! GO UP!!!~~~~~~~~~~~~~");
}
else if (userPosition == snakesLaddersArray[5])
{
userPosition = 86;
System.out.println ("\t\t\t\t~~~~~~~~~~~~~You Got A Ladder!! GO UP!!!~~~~~~~~~~~~~");
}
if (userPosition < 0 || userPosition > 112)
{
System.out.println ("An error has occured please reset the game!!!!!!");
}
else if (userPosition > 100)
{
userPosition = userPosition - userRoll;
System.out.println ("OHHH You cant jump, you must land on a 100");
}
else if (userPosition == 100)
{
System.out.println ("YOU WON, GOOD JOB!!!");
}
return userPosition;
}
public static int compgetP (int compPosition, int compRoll, int snakesLaddersArray [], int userPosition) throws IOException
{
if(compPosition == snakesLaddersArray[0])
{
compPosition = 19;
System.out.println ("\t\t\t\t~~~~~~~~~~~~~Computer Got Bit By A Snake, HE WENT GO DOWN!!!~~~~~~~~~~~~~");
}
else if (compPosition == snakesLaddersArray[1])
{
compPosition = 48;
System.out.println ("\t\t\t\t~~~~~~~~~~~~~Computer Got Bit By A Snake, HE WENT GO DOWN!!!~~~~~~~~~~~~~");
}
else if (compPosition == snakesLaddersArray[2])
{
compPosition = 77;
System.out.println ("\t\t\t\t~~~~~~~~~~~~~Computer Got Bit By A Snake, HE WENT GO DOWN!!!~~~~~~~~~~~~~");
}
else if (compPosition == snakesLaddersArray[3])
{
compPosition = 34;
System.out.println ("Computer Got TO A Ladder, HE WENT UP!!!");
}
else if (compPosition == snakesLaddersArray[4])
{
compPosition = 64;
System.out.println ("Computer Got TO A Ladder, HE WENT UP!!!");
}
else if (compPosition == snakesLaddersArray[5])
{
compPosition = 86;
System.out.println ("Computer Got TO A Ladder, HE WENT UP!!!");
}
if (compPosition < 0 || compPosition > 112)
{
System.out.println ("An error has occured for the computer, please reset the game!!!!!!");
}
else if (compPosition > 100)
{
compPosition = compPosition - compRoll;
System.out.println ("THE COMPUTER CAN'T JUMP!!! He must land on a 100");
}
else if (compPosition == 100 && userPosition != 100)
{
System.out.println ("THE COMPUTER WON, YOU FAILED!!!");
}
return compPosition;
}
}