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
232 lines (220 loc) · 8.5 KB
/
script.js
File metadata and controls
232 lines (220 loc) · 8.5 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
// There are 2 players and players take turns.
var ENTERPLAYERNAME = `player enters name`;
var ROLLDICE = `player rolls dice`;
var DICEORDER = `player chooses dice order`;
var COMPARENUMBERS = `compare final nunmbers`;
var REPLAY = `play again`;
var gameMode = ENTERPLAYERNAME;
var playerNames = [];
var playerDiceNum = [];
var playerFinalNum = [];
var isPlayer1turn = true;
var afterFirstRound = false;
var winCount = [0, 0];
//players enter name
var enterPlayerName = function (inputUserName) {
var message = ``;
playerNames.push(inputUserName);
console.log("starting:" + gameMode);
console.log(playerNames);
if (gameMode == ENTERPLAYERNAME && isPlayer1turn == true) {
message =
`Hello <b>${playerNames[0]}</b>, let's play Beat That!` +
"<br><br>Press the <b>Submit</b> button to roll the two dice.";
gameMode = ROLLDICE;
console.log("after player 1 name :" + gameMode);
return message;
} else if (gameMode == ENTERPLAYERNAME && isPlayer1turn == false) {
message =
`Hello <b>${playerNames[1]}</b>, let's see if you can beat ${playerNames[0]}!` +
"<br><br>Press the <b>Submit</b> button to roll the two dice.";
gameMode = ROLLDICE;
console.log("after player 2 name :" + gameMode);
return message;
}
};
// When player clicks Submit, the game rolls 2 dice and shows the dice rolls, for example 3 and 6.
var rollDice = function () {
var randomDecimal = Math.random() * 6;
var randomInteger = Math.floor(randomDecimal);
var number = randomInteger + 1;
return number;
};
var getTwoDiceNum = function () {
var message = ``;
if (gameMode == ROLLDICE && isPlayer1turn == true) {
var diceNum1 = rollDice();
var diceNum2 = rollDice();
console.log(`dice num1: ${diceNum1}, dice num2: ${diceNum2}`);
message =
`${playerNames[0]}, you rolled a <b>${diceNum1}</b> for Dice 1 and a <b>${diceNum2} </b>for Dice 2.` +
"<br><br><u>Choose the order of the dice: </u><br>Enter <b>1</b> to put dice 1 first. <br>Enter <b>2</b> to put dice 2 first.";
gameMode = DICEORDER;
playerDiceNum.push(diceNum1);
playerDiceNum.push(diceNum2);
console.log(`player1 dice: ` + playerDiceNum[0] + ", " + playerDiceNum[1]);
return message;
} else if (gameMode == ROLLDICE && isPlayer1turn == false) {
var diceNum1 = rollDice();
var diceNum2 = rollDice();
console.log(`dice num1: ${diceNum1}, dice num2: ${diceNum2}`);
message =
`${playerNames[1]}, you rolled a <b>${diceNum1}</b> for Dice 1 and a <b>${diceNum2} </b>for Dice 2.` +
"<br><br><u>Choose the order of the dice: </u><br>Enter <b>1</b> to put dice 1 first. <br>Enter <b>2</b> to put dice 2 first.";
gameMode = DICEORDER;
playerDiceNum.push(diceNum1);
playerDiceNum.push(diceNum2);
console.log(`player2 dice: ` + playerDiceNum[2] + ", " + playerDiceNum[3]);
return message;
}
};
// 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.
var orderDice = function (userChoice) {
var message = ``;
var playerChoice = ``;
if (
gameMode == DICEORDER &&
isPlayer1turn == true &&
afterFirstRound == false
) {
if (!(userChoice == `1` || userChoice == `2`)) {
message =
`Please enter <b>1</b> or <b>2</b>.` +
"<br><br>" +
`In case you forgot, you rolled a <b>${playerDiceNum[0]}</b> for dice 1 and a <b>${playerDiceNum[1]} </b>for dice 2. `;
return message;
} else {
if (userChoice == `1`) {
playerChoice = `${playerDiceNum[0]}` + `${playerDiceNum[1]}`;
} else if (userChoice == `2`) {
playerChoice = `${playerDiceNum[1]}` + `${playerDiceNum[0]}`;
}
playerFinalNum.push(playerChoice);
message =
`<b>${playerNames[0]}</b>, your number is <b>${playerFinalNum[0]}</b>. ` +
"<br><br>It's the next player's turn now! <br><br>Player 2, please enter your <b>name</b>.";
console.log(`player 1 num: ` + playerFinalNum[0]);
gameMode = ENTERPLAYERNAME;
console.log("after play 1 choice: " + gameMode);
isPlayer1turn = false;
return message;
}
} else if (
gameMode == DICEORDER &&
isPlayer1turn == true &&
afterFirstRound == true
) {
if (!(userChoice == `1` || userChoice == `2`)) {
message =
`Please enter <b>1</b> or <b>2</b>.` +
"<br><br>" +
`In case you forgot, you rolled a <b>${playerDiceNum[0]}</b> for dice 1 and a <b>${playerDiceNum[1]} </b>for dice 2. `;
return message;
} else {
if (userChoice == `1`) {
playerChoice = `${playerDiceNum[0]}` + `${playerDiceNum[1]}`;
} else if (userChoice == `2`) {
playerChoice = `${playerDiceNum[1]}` + `${playerDiceNum[0]}`;
}
playerFinalNum.push(playerChoice);
message =
`<b>${playerNames[0]}</b>, your number is <b>${playerFinalNum[0]}</b>. ` +
"<br><br>" +
`It's <b>${playerNames[1]}</b>'s turn now, press <b>submit</b> to roll the two dice.`;
console.log(`player 1 num: ` + playerFinalNum[0]);
gameMode = ROLLDICE;
console.log("after play 1 choice: " + gameMode);
isPlayer1turn = false;
return message;
}
} else if (gameMode == DICEORDER && isPlayer1turn == false) {
if (!(userChoice == `1` || userChoice == `2`)) {
message =
`Please enter <b>1</b> or <b>2</b>.` +
"<br><br>" +
`In case you forgot, you rolled a <b>${playerDiceNum[2]}</b> for dice 1 and a <b>${playerDiceNum[3]} </b>for dice 2. `;
return message;
} else {
if (userChoice == `1`) {
playerChoice = `${playerDiceNum[2]}` + `${playerDiceNum[3]}`;
} else if (userChoice == `2`) {
playerChoice = `${playerDiceNum[3]}` + `${playerDiceNum[2]}`;
}
playerFinalNum.push(playerChoice);
message =
`<b>${playerNames[1]}</b>, your number is <b>${playerFinalNum[1]}</b>.` +
"<br><br>Press <b>submit</b> to see who has won!";
console.log(`player 2 num: ` + playerFinalNum[1]);
gameMode = COMPARENUMBERS;
console.log("after player 2 choice: " + gameMode);
return message;
}
}
};
// After both players have rolled and chosen dice order, the player with the higher combined number wins.
var compareFinalNum = function () {
var message = ``;
if (gameMode == COMPARENUMBERS) {
if (playerFinalNum[0] > playerFinalNum[1]) {
winCount[0] += 1;
message = `Congrats <b>${playerNames[0]}</b>, you won!`;
} else if (playerFinalNum[1] > playerFinalNum[0]) {
winCount[1] += 1;
message = `Congrats <b>${playerNames[1]}</b>, you won!`;
} else if (playerFinalNum[1] == playerFinalNum[0]) {
message = `Unbelievable! It's a draw!`;
}
result =
"<b>RESULTS:</b> <br><br>" +
`${playerNames[0]}'s number: ${playerFinalNum[0]} ` +
"<br>" +
`${playerNames[1]}'s number: ${playerFinalNum[1]}` +
"<br><br>";
score =
"<br><br><u>Current score:</u><br>" +
`${playerNames[0]}'s score: ${winCount[0]} ` +
"<br>" +
`${playerNames[1]}'s score: ${winCount[1]}` +
"<br><br>";
playAgainMessage = "<br><br>Play again? <b>yes 🥳</b> or <b>no 😔</b>";
gameMode = REPLAY;
console.log(`after round ends: ${gameMode}`);
return result + message + score + playAgainMessage;
}
};
// Option to play again
var replayGame = function (userChoice) {
var message = ``;
if (gameMode == REPLAY && userChoice == `yes`) {
message =
`Alright!! Let's go!!` +
"<br><br>" +
`<b>${playerNames[0]}</b>, press the <b>Submit</b> button to roll the two dice.`;
playerDiceNum = [];
playerFinalNum = [];
isPlayer1turn = true;
afterFirstRound = true;
gameMode = ROLLDICE;
console.log(`after player chooses to replay: ${gameMode}`);
console.log(`playerDiceNum: ${playerDiceNum}`);
console.log(`playerFinalNum: ${playerFinalNum}`);
} else if (gameMode == REPLAY && userChoice == `no`) {
message = `Aww, alright.... Close this window and hope to see you again! Byeeeee!`;
}
return message;
};
var main = function (input) {
var myOutputValue = ``;
if (gameMode == ENTERPLAYERNAME) {
myOutputValue = enterPlayerName(input);
} else if (gameMode == ROLLDICE) {
myOutputValue = getTwoDiceNum();
} else if (gameMode == DICEORDER) {
myOutputValue = orderDice(input);
} else if (gameMode == COMPARENUMBERS) {
myOutputValue = compareFinalNum();
} else if (gameMode == REPLAY) {
myOutputValue = replayGame(input);
}
return myOutputValue;
};