forked from rocketacademy/basics-scissors-paper-stone
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
285 lines (243 loc) · 7.69 KB
/
script.js
File metadata and controls
285 lines (243 loc) · 7.69 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
// prompt user for player name
var playerName = window.prompt("What is your name?");
while (playerName == "") {
playerName = window.prompt("Please enter your name?");
}
// define string constants for game mode variables
var NORMAL_MODE = "normal";
var REVERSE_MODE = "reverse";
// default game status
var currentGameMode = NORMAL_MODE;
// track the number of games played
var numGamesPlayed = 0;
// track the number of games player won
var numGamesWon = 0;
// track the number of games computer won
var numGamesLost = 0;
// track the number of games that were draws
var numGamesDraw = 0;
// track the last game result
var lastOutcome = "";
// define string constants for possible hands
var SCISSORS = "scissors";
var PAPER = "paper";
var STONE = "stone";
/**
* Set a function that returns "scissors" "paper" or "stone"
*/
var generateHand = function () {
var randomDecimal = Math.random() * 3;
var randomInteger = Math.floor(randomDecimal);
var hand = "";
// console.log("generateHand random integer: ");
// console.log(randomInteger);
if (randomInteger == 0) {
hand = SCISSORS;
}
if (randomInteger == 1) {
hand = PAPER;
}
if (randomInteger == 2) {
hand = STONE;
}
console.log("generated hand: ");
console.log(hand);
return hand;
};
/**
* Set a function for standard game mode that returns the outcome of the hand as "draw" "win" or "lose"
*/
var outcome = function (playerHand, computerHand) {
// if both parties chose the same object, it's a draw
if (playerHand == computerHand) {
return "draw";
}
// scissors beats paper, paper beats stone, and stone beats scissors.
// reversed scissors beats stone, reversed paper beats scissors, and reverse stone beats paper.
if (
(playerHand == SCISSORS && computerHand == PAPER) ||
(playerHand == PAPER && computerHand == STONE) ||
(playerHand == STONE && computerHand == SCISSORS)
) {
return "win";
} else {
return "lose";
}
};
/**
* Set a function for reverse game mode that returns the outcome of the hand as "draw" "win" or "lose"
*/
var reverseOutcome = function (playerHand, computerHand) {
// if both parties chose the same object, it's a draw
if (playerHand == computerHand) {
return "draw";
}
// scissors beats paper, paper beats stone, and stone beats scissors.
// reversed scissors beats stone, reversed paper beats scissors, and reverse stone beats paper.
if (
(playerHand == SCISSORS && computerHand == STONE) ||
(playerHand == PAPER && computerHand == SCISSORS) ||
(playerHand == STONE && computerHand == PAPER)
) {
return "win";
} else {
return "lose";
}
};
/**
* Set a function that returns the hand icon
*/
var getHandIcon = function (input) {
var handIcon = "";
if (input == SCISSORS) {
handIcon = "✂️";
}
if (input == STONE) {
handIcon = "🪨";
}
if (input == PAPER) {
handIcon = "🗒";
}
return handIcon;
};
/**
* Set a function that returns the Output message
*/
var getOutputMsg = function (
playerHand,
computerHand,
currentGameOutcome,
numGamesPlayed,
numGamesWon,
numGamesLost,
numGamesDraw,
playerName
) {
var playerHandIcon = getHandIcon(playerHand);
var computerHandIcon = getHandIcon(computerHand);
console.log("player HandIcon is: ");
console.log(playerHandIcon);
console.log("computerHandIcon is: ");
console.log(computerHandIcon);
return `The computer chose ${computerHand} ${computerHandIcon}. <br> You chose ${playerHand} ${playerHandIcon}. <br><br> You ${currentGameOutcome}!
<br><br>
So far ${playerName}, you have played ${numGamesPlayed} games. You have won ${numGamesWon} times, lost ${numGamesLost} times and drawed ${numGamesDraw} times. Keep going!
<br><br>
Now you can type "scissors" "paper" "stone" to play another round!
`;
};
/**
* Set a function that checks if input is valid, i.e. scissors, paper, stone, or reverse
*/
var validInput = function (input) {
return (
input == SCISSORS ||
input == PAPER ||
input == STONE ||
input == REVERSE_MODE
);
};
/**
* Play SPS with user input, return current game result and overall game stats.
* @param {string} input - Player's hand can be scissors, paper, stone, reversed scissors, reversed paper, reversed stone
*/
var main = function (input) {
var computerHand = "";
// var playerHand =
var myOutputValue = "";
var currentGameOutcome = "";
var playerHand = "";
console.log(validInput(input));
// first check if user input is one of scissors, paper, stone or reverse
if (validInput(input)) {
// return (myOutputValue = "valid input");
playerHand = input;
// check if player wants to reverse from this turn
if (input == REVERSE_MODE) {
currentGameMode = REVERSE_MODE;
return (myOutputValue =
'Game mode changed to reverse. Now you can type "scissors" "paper" "stone" to play another round!');
}
// see current game mode value
console.log("Current Game Mode value: ");
console.log(currentGameMode);
console.log(currentGameMode == NORMAL_MODE);
// play sps in normal mode
if (currentGameMode == NORMAL_MODE) {
console.log("Playing in normal mode");
computerHand = generateHand();
myOutputValue = "default";
currentGameOutcome = outcome(playerHand, computerHand);
console.log("player hand: ");
console.log(playerHand);
console.log("computer hand: ");
console.log(computerHand);
// add 1 to number of games played counter
numGamesPlayed = numGamesPlayed + 1;
// add 1 to win, loss or draw counter based on current game outcome
if (currentGameOutcome == "win") {
numGamesWon = numGamesWon + 1;
}
if (currentGameOutcome == "lose") {
numGamesLost = numGamesLost + 1;
}
if (currentGameOutcome == "draw") {
numGamesDraw = numGamesDraw + 1;
}
// set output msg based on current stats
myOutputValue = getOutputMsg(
playerHand,
computerHand,
currentGameOutcome,
numGamesPlayed,
numGamesWon,
numGamesLost,
numGamesDraw,
playerName
);
// set last game outcome to current game outcome
lastOutcome = currentGameOutcome;
return `You are playing in normal mode. <br><br> ${myOutputValue} <br><br> Type "reverse" to play in Reverse mode.`;
}
// play sps in reverse mode
if (currentGameMode == REVERSE_MODE) {
console.log("Playing in reverse mode");
computerHand = generateHand();
myOutputValue = "default";
currentGameOutcome = reverseOutcome(playerHand, computerHand);
console.log("player hand: ");
console.log(playerHand);
console.log("computer hand: ");
console.log(computerHand);
// add 1 to number of games played counter
numGamesPlayed = numGamesPlayed + 1;
// add 1 to win, loss or draw counter based on current game outcome
if (currentGameOutcome == "win") {
numGamesWon = numGamesWon + 1;
}
if (currentGameOutcome == "lose") {
numGamesLost = numGamesLost + 1;
}
if (currentGameOutcome == "draw") {
numGamesDraw = numGamesDraw + 1;
}
// set output msg based on current stats
myOutputValue = getOutputMsg(
playerHand,
computerHand,
currentGameOutcome,
numGamesPlayed,
numGamesWon,
numGamesLost,
numGamesDraw,
playerName
);
// set last game outcome to current game outcome
lastOutcome = currentGameOutcome;
return `You are playing in Reverse mode. <br><br> ${myOutputValue} <br><br> Type "normal" to play in Normal mode.`;
}
} else {
return (myOutputValue =
'Please input "scissors" "paper" "stone" or "reverse".');
}
};