-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snakes_and_Ladders.java
351 lines (261 loc) · 15.8 KB
/
Snakes_and_Ladders.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
340
341
342
343
344
345
346
347
348
349
350
351
/* Main Class for Snake and Ladder Game */
import java.io.*; // used to allow user to input data
public class Snakes_and_Ladders
{// start class
//Main
public static void main (String [] args) throws IOException
{
//Welcoming Screen
BufferedReader myInput2 = new BufferedReader (new InputStreamReader (System.in));
// Print the welcome screen and instructions
System.out.println ("\t\t\t\t\tWelcome To Snakes And Ladders\n\n");
System.out.println ("\t\t\t\t\t\tInstructions:");
System.out.println ("\t\t This program will simulate a regular snakes and ladders game, where you will be");
System.out.println ("\t\t facing the computer. You and the computer start at square 1, and the first one to");
System.out.println ("\t\t square 100 wins, however, there will be preset squares which will be the snakes or ladders.");
System.out.println ("\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 on the bottom of a ladder.");
System.out.println("\n\t\t Good Luck and Have Fun!!");
int counter= 100,iteration=-1; // sets the counter and iteration vaiables to be used in making the board
System.out.println ("-----------------------------------------------------Game Board-----------------------------------------------------");
/*
This while loop makes the board for the player to visualize what the
game looks like, it uses a counter to increment or decrement by 1.
It will also subtract by 9 or 10 when necessary to create a board
exactly like the one given.
*/
while (counter > 0){// start while
if (counter%10 == 0 && counter != 100){// checks if the counter is at a 90, or 80...
if(iteration==-1)
{
// subtract 9 from the counter
// and sets it to start adding by one
counter-=9;
iteration=1;
}
else
{
System.out.print(counter+"\t");
counter-=10;
iteration=-1; // set the counter to start subtract by ones
}
if(counter!=0)
System.out.print("\n" + counter + "\t"); // just prints out the counter with a line breck
}
else
System.out.print(counter + "\t"); // just prints out the counter
counter+=iteration; // sets counter to add by iteration
}// end while
System.out.println();
System.out.println ("-------------------------------------------------------------------------------------------");
String sGame = "y"; // decare variable used to ask user if he wants to play
System.out.print ("Do you want to play? Y or N > "); // ask user if we wants to play the game
sGame = myInput2.readLine (); // reads the user's input into the variable sGame
System.out.print ("\n\n\n\n\n\n");
// While the user says yes, go to startGame method
// startGame is fuction type method, which start the game
while (sGame.equals ("y") || sGame.equals ("Y"))
{
sGame = startGame(sGame); // give startGame a variable to work with
}
System.out.println ("\n\n\n\n\t\t\t\t\t\tThank You For Playing!!");
} //end main method
//------------------------startGame Method------------------------------------------
/*
startGame method:
This method is responsible for organizing the game, asking the user to continue playing,
and setting the important varibales. It will also return a vaule to the main method, which
will reset the game so the user can play again.
*/
public static String startGame (String start) throws IOException // Recieves data from the main method
{// start startGame method
BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));
// sets important variables for the game
// NOTE: These variables will change as the game progresses
int userPosition = 1; // sets the default loaction for user's piece to 1
int compPosition = 1; // sets the default loaction for computer's piece to 1
int diceRoll = 0; // creates the first die
int diceRoll2 = 0; // creates the second die
int userRoll = 1; // declares what the user rolled
int compRoll = 1; // declares what the computer rolled
String playAgain = "y"; // sets the play again variable
// declare all the snakes and ladders in a array
int snakesLaddersArray [] = new int [6]; // create a 6 element array
// store the snakes and ladders location in the array
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")) // loops while the playAgaim vaiable equals "y" or "Y".
{// start playAgain While
// gets the dice roll for user and computer
userRoll = getDice(diceRoll, diceRoll2); // sends data to a function type method called getDice
compRoll = getDice(diceRoll, diceRoll2); // does the same for the computer
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|"); // print the roll the user got
System.out.println ("\t\t\t\t\t|\tThe Computer Rolled a " + compRoll + "\t|"); // print the roll the computer got
System.out.println ("\t\t\t\t\t------------------------------------------");
// hold the user's last position for switching back if current position was greater than 100
userPosition = userPosition + userRoll;
// hold the computer's last position for switching back if current position was greater than 100
compPosition = compPosition + compRoll;
// check to see if user landed on top of a snake or at the bottom of a ladder
// give getP parameters to work with, and recieve a final value which can be printed out
userPosition = getP(userPosition, userRoll, snakesLaddersArray);
// The same goes for compPosition, however compgetP gets an additional
// parameter (userPosition) to check if user has already won
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*"); // print out the user's current location
System.out.println ("\t\t\t*\t\tThe Computer is currently on square " + compPosition + "\t\t*"); // print out the computer's current location
System.out.println("\t\t\t*************************************************************************");
// resets the position values, if the user or the computer won
// so that the user could play the entire game again if he/she wanted to
if (userPosition == 100 || compPosition == 100)
{
userPosition = 1;
compPosition = 1;
// asks the user if we wants to play again
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
{
// asks the user if we wants to continue playing
System.out.print ("Do you want to play? Y or N > ");
playAgain = myInput.readLine ();
}
}// end playAgain While
return playAgain; // returns prameter "playAgain" to main: if the user wants to play the game again
}// end startGame method
//-------------------------------------------------------------------geDice Method------------------------------------------------------------------------------
/*
getDice method:
This method generates two random numbers bewteen 1 and 6, then
adds them to get a final roll. Next it returns the value to be
diplayed on the screen.
*/
public static int getDice (int diceRoll, int diceRoll2)
{// start getDies class
diceRoll = (int)(Math.random()*6 )+1 ; //creates dice roll number 1
diceRoll2 = (int)(Math.random()*6 )+1 ; //creates dice roll number 2
int move = diceRoll + diceRoll2; // adds the two dice rolls to get a final move
return move; // return parameter move: this will give the final dice roll back to startGame
}// end getDice class
//-------------------------------------------------------------------getP Method------------------------------------------------------------------------------
/*
getP method:
This method is responsible for checking if the USER is on
top of a snake, or at the bottom of a ladder, and then
adjusting the user's position to mathch the snake or
ladders value.
*/
public static int getP (int userPosition, int userRoll, int snakesLaddersArray []) throws IOException // recieves two parameter from startGame
{// start getP
if(userPosition == snakesLaddersArray[0]) //if position equals snake 1
{
userPosition = 19; // set new position
System.out.println ("\t\t\t\t~~~~~~~~~~~~~You Got Bit By A Snake, GO DOWN!!!~~~~~~~~~~~~~");
}
else if (userPosition == snakesLaddersArray[1]) //if position equals snake 2
{
userPosition = 48; // set new position
System.out.println ("\t\t\t\t~~~~~~~~~~~~~You Got Bit By A Snake, GO DOWN!!!~~~~~~~~~~~~~");
}
else if (userPosition == snakesLaddersArray[2]) //if position equals snake 3
{
userPosition = 77; // set new position
System.out.println ("\t\t\t\t~~~~~~~~~~~~~You Got Bit By A Snake, GO DOWN!!!~~~~~~~~~~~~~");
}
else if (userPosition == snakesLaddersArray[3]) //if position equals ladder 1
{
userPosition = 34; // set new position
System.out.println ("\t\t\t\t~~~~~~~~~~~~~You Got A Ladder!! GO UP!!!~~~~~~~~~~~~~");
}
else if (userPosition == snakesLaddersArray[4]) //if position equals ladder 2
{
userPosition = 64; // set new position
System.out.println ("\t\t\t\t~~~~~~~~~~~~~You Got A Ladder!! GO UP!!!~~~~~~~~~~~~~");
}
else if (userPosition == snakesLaddersArray[5]) //if position equals ladder 3
{
userPosition = 86; // set new position
System.out.println ("\t\t\t\t~~~~~~~~~~~~~You Got A Ladder!! GO UP!!!~~~~~~~~~~~~~");
}
if (userPosition < 0 || userPosition > 112) // This is ab ERROR TRAP to catch any unwanted system errors that may occur by chance
{
System.out.println ("An error has occured please reset the game!!!!!!");
}
else if (userPosition > 100) // checks if user's location if greater then a 100
{
userPosition = userPosition - userRoll; // subtract userRoll from the userposition to get back old position
System.out.println ("OHHH You cant jump, you must land on a 100"); // print out the users current location
}
else if (userPosition == 100)
{
System.out.println ("YOU WON, GOOD JOB!!!"); // print out that the user won
}
return userPosition; // return the final position to starGame: this position had gone through all checks and test and can be displayed on screen
}// end getP
//-------------------------------------------------------------------compgetP Method------------------------------------------------------------------------------
/*
compgetP method:
This method is responsible for checking if the COMPUTER is on
top of a snake, or at the bottom of a ladder, and then
adjusting the compuer's position to mathch the snakes or
ladders value.
*/
public static int compgetP (int compPosition, int compRoll, int snakesLaddersArray [], int userPosition) throws IOException
{// start compgetP
// NOTE: this method is similar to getP, so the comments are the same for both!!
// Look at getP's commments if you need help
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) // ERROR TRAP to catch any unwanted system errors that may occur by chance
{
System.out.println ("An error has occured for the computer, please reset the game!!!!!!");
}
else if (compPosition > 100) // checks if computers's location if greater then a 100
{
compPosition = compPosition - compRoll; // get old position
System.out.println ("THE COMPUTER CAN'T JUMP!!! He must land on a 100"); // give message that the computer cant jump
}
else if (compPosition == 100 && userPosition != 100)
{
System.out.println ("THE COMPUTER WON, YOU FAILED!!!"); // print out that the computer won
}
return compPosition; // return the final position to starGame: this position had gone through all checks and test and can be displayed on screen
} // end compgetPy
}//end class