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
275 lines (271 loc) · 10.6 KB
/
script.js
File metadata and controls
275 lines (271 loc) · 10.6 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
/* 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. */
// Only managed to get normal, lowest score and choice dice.
const GET_DICE_ORDER = "Game state = Dice order";
const GET_DICE_ROLL = "Game State = Dice roll";
var gamestate = GET_DICE_ROLL;
var currentPlayerDiceNumber = [];
var currentPlayer = 1;
var totalScore = [];
const COMPARESCORE = "game state = compare score";
var gameMode = "Get_game_mode";
var playerScore; //used to store for normal game mode
var lowestScore; //used to store for reverse game mode
var totalDiceValue; //used to store for choice game mode
function getPlayerScore(playerInput) {
if (gameMode == "normal") {
//check if player chooses first or second digit to be used as first number
if (playerInput == 1) {
var playerScore = Number(
String(currentPlayerDiceNumber[0]) + String(currentPlayerDiceNumber[1])
);
totalScore.push(playerScore);
currentPlayerDiceNumber = [];
return "Player " + currentPlayer + ", your Number is: " + playerScore;
} else if (playerInput == 2) {
var playerScore = Number(
String(currentPlayerDiceNumber[1]) + String(currentPlayerDiceNumber[0])
);
currentPlayerDiceNumber = [];
totalScore.push(playerScore);
return "Player " + currentPlayer + ", your Number is: " + playerScore;
} else if (playerInput != 1 && playerInput != 2) {
return `Invalid input! Please enter either '1' or '2' <br>Dice 1: ${currentPlayerDiceNumber[0]}<br>Dice 2: ${currentPlayerDiceNumber[1]}`;
}
}
if (gameMode == "reverse") {
totalScore.push(lowestScore);
currentPlayerDiceNumber = [];
return `Player ${currentPlayer}, your score is :${lowestScore}`;
}
if (gameMode == "choice") {
var isNumTotalDiceValue = Number(String(totalDiceValue)); //changing the value from string to numeric for comparison purposes
totalScore.push(isNumTotalDiceValue);
currentPlayerDiceNumber = [];
console.log(totalScore);
return `Player ${currentPlayer}, your score is : ${isNumTotalDiceValue}<br>`;
}
}
function getRandomDiceRoll() {
//get random index
var diceRoll = Number(Math.floor(Math.random() * 6 + 1));
return diceRoll;
}
function getDiceNumber(getAmountOfDice) {
if (gameMode == "normal") {
//get 2 random dice rolls
for (var counter = 0; counter < 2; counter++) {
currentPlayerDiceNumber.push(getRandomDiceRoll());
}
console.log(currentPlayerDiceNumber);
return `Hi, Player ${currentPlayer} <br> You rolled: <br>Dice 1: ${currentPlayerDiceNumber[0]}<br>Dice 2: ${currentPlayerDiceNumber[1]}<br> Please input either "1" or "2" to choose which dice number is to be used as your first digit!`;
}
if (gameMode == "reverse") {
//get 2 random dice roll and automatically calculate which permutation would be the lowest score
for (var counter = 0; counter < 2; counter++) {
currentPlayerDiceNumber.push(getRandomDiceRoll());
}
var min = Math.min.apply(Math, currentPlayerDiceNumber);
if (min == currentPlayerDiceNumber[0]) {
lowestScore =
Number(String(currentPlayerDiceNumber[0])) +
String(currentPlayerDiceNumber[1]);
} else if (min == currentPlayerDiceNumber[1]) {
lowestScore =
Number(String(currentPlayerDiceNumber[1])) +
String(currentPlayerDiceNumber[0]);
}
return `Hi, Player ${currentPlayer}<br> You rolled:<br>Dice 1: ${currentPlayerDiceNumber[0]}<br>Dice 2: ${currentPlayerDiceNumber[1]}`;
}
if (gameMode == "choice") {
var diceValue = "";
totalDiceValue = "";
for (var counter2 = 0; counter2 < getAmountOfDice; counter2++) {
// roll and stores the amount of dice user inputs into currentplayerdicenumber array
currentPlayerDiceNumber.push(getRandomDiceRoll());
}
for (var i = 0; i < currentPlayerDiceNumber.length; i++) {
// get value of each index in currentplayerdicenumber array and output what dice they rolled
diceValue += "<br>Dice" + (i + 1) + " : " + currentPlayerDiceNumber[i];
}
var sorted = currentPlayerDiceNumber; //sorting the array in descending order
sorted.sort(function (a, b) {
return b - a;
});
console.log(sorted);
for (var i = 0; i < currentPlayerDiceNumber.length; i++) {
//adding each value from the array to get the highest permutation available
totalDiceValue += currentPlayerDiceNumber[i];
}
}
console.log(totalDiceValue);
return `Hi, Player ${currentPlayer}<br> You rolled: ${diceValue}`;
}
function main(input) {
var myOutputValue = "";
if (gameMode == "Get_game_mode") {
//verify which gamemode is selected
if (input == "normal") {
gameMode = "normal";
myOutputValue =
"Game mode: Normal <br>Player with the highest number wins the game!<br>Press submit to begin.";
} else if (input == "reverse") {
gameMode = "reverse";
myOutputValue =
"Game mode: Reverse <br>Player with the lowest number wins the game!<br>Press submit to begin.";
} else if (input == "choice") {
gameMode = "choice";
myOutputValue =
"Game mode: Choice Of Dice <br> Players can choose the amount of dice they would like to roll!<br> Please input how many dice (ranging from 2 - 10 dice) you would like to roll!";
} else {
myOutputValue =
"Invalid game mode! Please input either 'normal' or 'reverse' or 'choice' !";
}
return myOutputValue;
}
if (gameMode == "normal") {
if (gamestate == GET_DICE_ROLL) {
myOutputValue = getDiceNumber();
gamestate = GET_DICE_ORDER;
return myOutputValue;
}
if (gamestate == GET_DICE_ORDER) {
myOutputValue = getPlayerScore(input);
}
if (currentPlayer == 1) {
currentPlayer = 2;
gamestate = GET_DICE_ROLL;
return myOutputValue + "<br> It is now player 2's turn!";
}
if (currentPlayer == 2) {
gamestate = COMPARESCORE;
console.log(gamestate);
currentPlayer = 3; //reason for this is so that my function will move down to comparescore
// is there a way around this? please advice 🙏
return myOutputValue + "<br> Press submit to calculate scores!";
}
if (gamestate == COMPARESCORE) {
myOutputValue = `Player 1 score: ${totalScore[0]} <br>Player 2 score: ${totalScore[1]}`;
//player1 wins
if (totalScore[0] > totalScore[1]) {
myOutputValue = myOutputValue + "<br>Player 1 Wins!";
}
//player 2 wins
else if (totalScore[1] > totalScore[0]) {
myOutputValue = myOutputValue + "<br>Player 2 Wins!";
}
//draw
else if (totalScore[0] == totalScore[1]) {
myOutputValue = myOutputValue + "<br>Its a tie!";
}
currentPlayer = 1;
gamestate = GET_DICE_ROLL;
totalScore = [];
return (
myOutputValue +
"<br>Press submit to play another round or if you would like to change game modes, please refresh your browser!"
);
}
}
if (gameMode == "reverse") {
if (gamestate == GET_DICE_ROLL) {
myOutputValue = getDiceNumber();
gamestate = GET_DICE_ORDER;
return myOutputValue;
}
if (gamestate == GET_DICE_ORDER) {
myOutputValue = getPlayerScore(input);
}
if (currentPlayer == 1) {
currentPlayer = 2;
gamestate = GET_DICE_ROLL;
return myOutputValue + "<br> It is now player 2's turn!";
}
if (currentPlayer == 2) {
gamestate = COMPARESCORE;
console.log(gamestate);
currentPlayer = 3; //reason for this is so that my function will move down to comparescore
return myOutputValue + "<br> Press submit to calculate scores!";
}
if (gamestate == COMPARESCORE) {
myOutputValue = `Player 1 score: ${totalScore[0]} <br>Player 2 score: ${totalScore[1]}`;
//player1 wins
if (totalScore[0] < totalScore[1]) {
myOutputValue = myOutputValue + "<br>Player 1 Wins!";
}
//player 2 wins
else if (totalScore[1] < totalScore[0]) {
myOutputValue = myOutputValue + "<br>Player 2 Wins!";
}
//draw
else if (totalScore[0] == totalScore[1]) {
myOutputValue = myOutputValue + "<br>Its a tie!";
}
currentPlayer = 1;
gamestate = GET_DICE_ROLL;
totalScore = [];
return (
myOutputValue +
"<br>Press submit to play another round or if you would like to change game modes, please refresh your browser!"
);
}
}
if (gameMode == "choice") {
console.log("game mode = choice");
if (gamestate == GET_DICE_ROLL) {
var getAmountOfDice = Number(input);
if (getAmountOfDice < 2 || getAmountOfDice > 10) {
myOutputValue =
"Invalid input! Please enter a number ranging from 2 - 10!";
return myOutputValue;
} else {
gamestate = GET_DICE_ORDER;
myOutputValue = getDiceNumber(getAmountOfDice);
return myOutputValue;
}
}
if (gamestate == GET_DICE_ORDER) {
myOutputValue = getPlayerScore();
}
if (currentPlayer == 1) {
currentPlayer = 2;
gamestate = GET_DICE_ROLL;
return (
myOutputValue +
" It is now Player 2's turn!<br>Please input the same amount of dice that player 1 chose!"
);
// did not manage to make both player 1 and 2 roll the same amount of dice automaticallyz
}
if (currentPlayer == 2) {
gamestate = COMPARESCORE;
currentPlayer = 3; //to continue down to the next if statement
return myOutputValue + "Press submit to see who is the winner.";
}
if (gamestate == COMPARESCORE) {
console.log("gamestate = compare");
myOutputValue = `Player 1 score: ${totalScore[0]} <br>Player 2 score: ${totalScore[1]}`;
//player1 wins
if (totalScore[0] > totalScore[1]) {
myOutputValue = myOutputValue + "<br>Player 1 Wins!";
}
//player 2 wins
else if (totalScore[1] > totalScore[0]) {
myOutputValue = myOutputValue + "<br>Player 2 Wins!";
}
//draw
else if (totalScore[0] == totalScore[1]) {
myOutputValue = myOutputValue + "<br>Its a tie!";
}
currentPlayer = 1;
gamestate = GET_DICE_ROLL;
totalScore = [];
return (
myOutputValue +
"<br>Please input how many dice (ranging from 2 - 10 dice) you would like to roll and press submit to play another round or if you would like to change game modes, please refresh your browser!"
);
}
}
}