forked from rocketacademy/basics-beat-that
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
299 lines (247 loc) · 8.49 KB
/
script.js
File metadata and controls
299 lines (247 loc) · 8.49 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
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
// OBJECTIVES
// There are 2 players and players take turns.
// When a player clicks Submit, the game rolls 2 dice and shows the dice rolls, for example 3 and 6.
// The player picks the order of the dice they want. For example, if they wanted the number 63, they would specify that the 2nd dice goes first. You can choose how the player specifies dice order.
// After both players have rolled and chosen dice order, the player with the higher combined number wins.
// Define default game mode.
var gameMode = "start";
// Store player's diceroll and number formed.
var diceRollP1 = [];
var diceRollP2 = [];
var createdNumberP1 = "";
var createdNumberP2 = "";
var createdNumberP1Log = [];
var createdNumberP2Log = [];
// Track player's win-lose record
var player1wins = 0;
var player2wins = 0;
var playertie = 0;
// Adds all number in 'createdNumberP1Log' array for Player 1
// Adds all number in 'createdNumberP2Log' array for Player 2
// var totalScoreP2 = function (createdNumberP2Log) {
// var reducer = function () {
// accumulator, currentValue;
// return accumulator + currentValue;
// };
// return createdNumberP1Log.reduce(reducer());
// };
// Generate random dice roll between 1 to 6.
var diceRoll = function () {
var randomDiceRoll = Math.ceil(Math.random() * 6);
return randomDiceRoll;
};
// Generate 2 dice rolls when Player 1 clicks submit, then switches to number select mode for dice 1.
var player1Turn = function () {
var firstDiceP1 = diceRoll();
var secondDiceP1 = diceRoll();
console.log(firstDiceP1, secondDiceP1);
diceRollP1.push(firstDiceP1, secondDiceP1);
console.log(diceRollP1);
gameMode = "selectDiceOrder";
console.log(gameMode);
};
// Generate 2 dice roll when Player 2 clicks submit, then switches to number select mode for dice 2.
var player2Turn = function () {
var firstDiceP2 = diceRoll();
var secondDiceP2 = diceRoll();
console.log(firstDiceP2, secondDiceP2);
diceRollP2.push(firstDiceP2, secondDiceP2);
console.log(diceRollP2);
gameMode = "selectDiceOrder2";
console.log(gameMode);
};
// Restart the game cycle, saves refreshes player's created numbers in the round.
var resetgame = function () {
gameMode = "player1";
createdNumberP1Log.push(createdNumberP1);
createdNumberP2Log.push(createdNumberP2);
console.log(createdNumberP1Log);
console.log(createdNumberP2Log);
diceRollP1.pop();
diceRollP1.pop();
diceRollP2.pop();
diceRollP2.pop();
console.log(diceRollP1);
console.log(diceRollP2);
console.log(createdNumberP1);
console.log(createdNumberP2);
};
var main = function (input) {
// Starting Game Mode
if (gameMode == "start") {
myOutputValue =
"You are now playing [Beat It]. <br><br>Click on submit to roll dice!";
gameMode = "player1";
return myOutputValue;
}
// Player 1 Game Mode
if (gameMode == "player1") {
player1Turn();
myOutputValue =
"Welcome Player 1, <br><br>" +
"[Dice 1] = " +
diceRollP1[0] +
"<br>[Dice 2] = " +
diceRollP1[1] +
"<br><br>In the field above:<br>Enter 1 to use " +
diceRollP1[0] +
" as your first number. <br>Enter 2 to use " +
diceRollP1[1] +
" as your first number.";
return myOutputValue;
}
// Player 1 Dice Placement Game Mode
if (gameMode == "selectDiceOrder") {
if (input == "1") {
myOutputValue =
"[Player 1] <br><br> You have selected Dice " +
input +
" to be your first number. <br><br> Your combined number is " +
diceRollP1[0] +
diceRollP1[1] +
"." +
"<br><br> ..........................................................................<br><br>It is now Player 2's turn! <br><br> Please click on submit to roll dice.<br><br> ..........................................................................";
createdNumberP1 = diceRollP1[0] + "" + diceRollP1[1];
console.log(createdNumberP1);
gameMode = "player2";
return myOutputValue;
}
if (input == "2") {
myOutputValue =
"[Player 1] <br><br> You have selected Dice " +
input +
" to be your first number. <br><br> Your combined number is " +
diceRollP1[1] +
diceRollP1[0] +
"." +
"<br><br> ..........................................................................<br><br>It is now Player 2's turn! <br><br> Please click on submit to roll dice.<br><br> ..........................................................................";
createdNumberP1 = diceRollP1[1] + "" + diceRollP1[0];
console.log(createdNumberP1);
gameMode = "player2";
return myOutputValue;
}
}
// Player 2 Game Mode
if (gameMode == "player2") {
player2Turn();
myOutputValue =
"Welcome Player 2, <br><br>" +
"[Dice 1] = " +
diceRollP2[0] +
"<br>[Dice 2] = " +
diceRollP2[1] +
"<br><br>In the field above:<br>Enter 1 to use " +
diceRollP2[0] +
" as your first number. <br>Enter 2 to use " +
diceRollP2[1] +
" as your first number.";
return myOutputValue;
}
// Player 2 Dice Placement Game Mode
if (gameMode == "selectDiceOrder2") {
if (input == "1") {
myOutputValue =
"[Player 2] <br><br> You have selected Dice " +
input +
" to be your first number. <br><br> Your combined number is " +
diceRollP2[0] +
diceRollP2[1] +
"." +
"<br><br> ..........................................................................<br><br> Please click on submit to view results.<br><br>..........................................................................";
createdNumberP2 = diceRollP2[0] + "" + diceRollP2[1];
console.log(createdNumberP2);
gameMode = "result";
return myOutputValue;
}
if (input == "2") {
myOutputValue =
"[Player 2] <br><br> You have selected Dice " +
input +
" to be your first number. <br><br> Your combined number is " +
diceRollP2[1] +
diceRollP2[0] +
"." +
"<br><br> ..........................................................................<br><br> Please click on submit to view results.<br><br>..........................................................................";
createdNumberP2 = diceRollP2[1] + "" + diceRollP2[0];
console.log(createdNumberP2);
gameMode = "result";
return myOutputValue;
}
}
// Determine Results Mode
if (gameMode == "result") {
if (createdNumberP1 < createdNumberP2) {
player2wins = player2wins + 1;
myOutputValue =
"Player 1 produced the number " +
createdNumberP1 +
"." +
"<br><br> Player 2 produced the number " +
createdNumberP2 +
"." +
"<br><br><br>[Results] Congratulations, Player 2 Won!<br>" +
"<br><br>" +
"[Player 1] [" +
player1wins +
" Wins]" +
"<br>Previous Rolls: " +
createdNumberP1Log +
"<br><br>[Player 2] [" +
player2wins +
" Wins]" +
"<br>Previous Rolls:" +
createdNumberP2Log +
"<br><br><br> Click on submit to play again.";
resetgame();
}
if (createdNumberP1 > createdNumberP2) {
player1wins = player1wins + 1;
myOutputValue =
"Player 1 produced the number " +
createdNumberP1 +
"." +
"<br><br> Player 2 produced the number " +
createdNumberP2 +
"." +
"<br><br><br>[Results] Congratulations, Player 1 Won!<br>" +
"<br><br>" +
"[Player 1] [" +
player1wins +
" Wins]" +
"<br>Previous Rolls: " +
createdNumberP1Log +
"<br><br>[Player 2] [" +
player2wins +
" Wins]" +
"<br>Previous Rolls:" +
createdNumberP2Log +
"<br><br><br> Click on submit to play again.";
resetgame();
}
if (createdNumberP1 == createdNumberP2) {
playertie = playertie + 1;
myOutputValue =
"Player 1 produced the number " +
createdNumberP1 +
"." +
"<br><br> Player 2 produced the number " +
createdNumberP2 +
"." +
"<br><br><br> [Results] It's a Draw!<br>" +
"<br><br>" +
"[Player 1] [" +
player1wins +
" Wins]" +
"<br>Previous Rolls: " +
createdNumberP1Log +
"<br><br>[Player 2] [" +
player2wins +
" Wins]" +
"<br>Previous Rolls:" +
createdNumberP2Log +
"<br><br><br> Click on submit to play again.";
resetgame();
}
}
return myOutputValue;
};