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
200 lines (164 loc) · 5.55 KB
/
script.js
File metadata and controls
200 lines (164 loc) · 5.55 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
// 2 players taking turns
//player clicks submit, the game rolls 2 dice.
//output is the 2 numbers of the dice rolls (random), example 3 and 6
// the player picks the order of the dice they want (or can we automatically choose from the player?)
// player 2 turns
// winner determined (the higher combined number)
// FIRST STEP -- input: submit. output: 2 numbers from random dice.
// SECOND STEP -- include player 2.
// 1. Global variables for currentPlayer; allPlayersScore
// 2. Refactor myOutputMessage to interact with each player,1 and 2
// 3. write logic for player 1 to go first then player 2
// 4. comparing score and final winner
// THIRD STEP -- Game is plating continuesly without needing to refresh the page
// Global Variables
var diceRollStage = "ROLL THE DICE";
var chooseOrderStage = "CHOOSE THE DICE ORDER";
var compareScoresStage = "COMPARE THE SCORES STAGE";
var gameState = diceRollStage;
var currentPlayerRolls = [];
var currentPlayer = 1;
var player1Score = 0;
var player2Score = 0;
// Helper function
var rollDice = function () {
console.log("start the rollDice()");
var randomDecimal = Math.random() * 6;
var randomInteger = Math.floor(randomDecimal) + 1;
console.log("rollDice output: ", randomInteger);
return randomInteger;
};
var rollDiceForPlayer = function () {
console.log("start of rollDiceForPlayer()");
var counter = 0;
currentPlayerRolls = [];
while (counter < 2) {
currentPlayerRolls.push(rollDice());
counter = counter + 1;
}
console.log(
"rollDiceForPlayer changes, currentPlayerRolls: ",
currentPlayerRolls
);
return (
"Hi Ho Player #" +
currentPlayer +
"...<br><br>Dice #1: " +
currentPlayerRolls[0] +
"<br><br>Dice #2: " +
currentPlayerRolls[1] +
"<br><br>Next, please type in '1' to choose the first dice as the first digit of your final value or '2' to choose the second dice."
);
};
var getPlayerScore = function (playerInput) {
var playerScore;
// input validation
if (playerInput != 1 && playerInput != 2) {
console.log("invalid input, NOT 1 or 2");
return (
"OOPSIE! <br><br>Please input ONLY '1' or '2'. <br><br> A gentle reminder, this will determine which dice to use as the first digit of your value. <br><br>Dice #1: " +
currentPlayerRolls[0] +
"<br><br>Dice #2: " +
currentPlayerRolls[1] +
"."
);
}
// input == 1
if (playerInput == 1) {
console.log("input == 1");
playerScore = Number(
String(currentPlayerRolls[0]) + String(currentPlayerRolls[1])
);
if (currentPlayer === 1) {
player1Score = playerScore;
}
if (currentPlayer === 2) {
player2Score = playerScore;
}
return "Your chose value is: " + playerScore;
}
// input == 2
if (playerInput == 2) {
console.log("input == 2");
playerScore = Number(
String(currentPlayerRolls[1]) + String(currentPlayerRolls[0])
);
if (currentPlayer === 1) {
player1Score = playerScore;
}
if (currentPlayer === 2) {
player2Score = playerScore;
}
return "Player " + currentPlayer + ", your chosen value is: " + playerScore;
}
};
var comparePlayersScores = function () {
console.log("player scores 1 and 2", player1Score, player2Score);
var compareMessage =
"Player 1 score: " + player1Score + "<br>Player 2 score: " + player2Score;
//player 1 wins
if (player1Score > player2Score) {
compareMessage =
compareMessage + "<br><br> Who is the winner?<br><br>PLAYER 1!";
}
//player 2 wins
if (player1Score < player2Score) {
compareMessage =
compareMessage + "<br><br> Who is the winner?<br><br>PLAYER 2!";
}
//tie
if (player1Score === player2Score) {
compareMessage =
compareMessage + "<br><br> Who is the winner?<br><br>IT'S A TIE!";
}
return compareMessage;
};
var resetGame = function () {
currentPlayer = 1;
gameState = diceRollStage;
};
var main = function (input) {
console.log("Checking game state on submit click", gameState);
console.log("checking currentPlayer on submit click: ", currentPlayerRolls);
var myOutputMessage = "";
if (gameState == diceRollStage) {
console.log("gameState == diceRollStage");
// Display dice rolled as output message
myOutputMessage = rollDiceForPlayer();
//change the game state
gameState = chooseOrderStage;
return myOutputMessage;
}
if (gameState == chooseOrderStage) {
console.log("gameState == chooseOrderStage");
// call playerScore function
myOutputMessage = getPlayerScore(input);
if (currentPlayer == 1) {
console.log("end of player 1's turn, now player 2's turn");
currentPlayer = 2;
gameState = diceRollStage;
return myOutputMessage + "<br><br>Let's take turn... Go, player 2!";
}
if (currentPlayer == 2) {
console.log(
"end of player 2's turn, next submit click will calculate score"
);
gameState = compareScoresStage;
return (
myOutputMessage + "<br><br> Click 'submit' to calculate the scores!"
);
}
}
if (gameState == compareScoresStage) {
console.log("gameState == compareScoreStage");
myOutputMessage = comparePlayersScores();
resetGame();
console.log("current player after reset: ", currentPlayer);
console.log("game state after reset: ", gameState);
return myOutputMessage;
}
};
//WHAT ARE THE PROBLEMS?
//1. player 1 wins, all good. player 1 looses, score for player 2 undefined.
//2. for tie, scores also undefined.
//3. last function to refresh automatically, displaying the same number over and over again, as if the random does not work.