-
Notifications
You must be signed in to change notification settings - Fork 348
Expand file tree
/
Copy pathscript.js
More file actions
260 lines (214 loc) · 7.43 KB
/
script.js
File metadata and controls
260 lines (214 loc) · 7.43 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
////////////////////////////
//// States and Variables //
////////////////////////////
// Win-Loss Record
var playerWinRecord = 0;
var systemWinRecord = 0;
var gameTieRecord = 0;
var playerWinPerc = 0;
var systemWinPerc = 0;
// Username
var currentGameMode = "waiting for user name";
var userName = "";
//Reverse Game Mode
var isReversedGameMode = false;
///////////////
//// Main ////
///////////////
var main = function (input) {
// inputs: "stone", "paper", "scissors" -- "add reversed in front for reversed game mode"
var myOutputValue = "";
if (currentGameMode == "waiting for user name") {
userName = input;
currentGameMode = "SPS Game";
myOutputValue = `Welcome [ ${userName} ] ! to the Scissors, Paper, Stone Game!`;
return myOutputValue;
}
// check if user wants to play the game in reversed (input is "reversed")
if (input == "reverse") {
reverseGameMode();
myOutputValue = ""; // empty string so as to prank the friend :)))
return myOutputValue;
}
// clean user input
var userInputCleaned = cleanStr(input);
// check if user wants to play the reversed game
var isReversedGame = userInputCleaned.includes("reversed");
if (isReversedGame == true) {
reverseGameMode(); // to reverse the reversed game mode
userInputCleaned = extractSpsStr(userInputCleaned);
}
// validate user input
if (validateInput(userInputCleaned) == false) {
return "bruh.<br><br>Invalid input. <br><br> Please enter one of the following: <br>- Scissors<br>- Paper<br>- Stone";
}
myOutputValue = spsLogic(userInputCleaned, isReversedGameMode);
// if user input contains "reversed" revert game mode back to previous game reversed state
if (isReversedGame == true) {
reverseGameMode();
}
return myOutputValue;
};
////////////////////////////
//// Helper Functions ////
///////////////////////////
var cleanStr = function (uncleanedStr) {
// clean user input (lowercase + remove leading and trailing spaces)
return uncleanedStr.trim().toLowerCase();
};
var extractSpsStr = function (userInputPara) {
// extract SPS text from reversed user input
return userInputPara.split(" ")[1];
};
var formatStr = function (stringToFormat) {
// format strings for output aesthetics
return stringToFormat.charAt(0).toUpperCase() + stringToFormat.slice(1);
};
var addSpacesToStr = function (numOfBlankSpace) {
var blankText = "";
for (let i = 0; i < numOfBlankSpace; i++) {
blankText += " ";
}
return blankText;
};
var validateInput = function (userInputPara) {
// check whether user input is one of the game options: Scissors, Paper or Stone
// if it is not, highlight to user and end the program
if (
!(
userInputPara == "scissors" ||
userInputPara == "paper" ||
userInputPara == "stone"
)
) {
return false;
}
return true;
};
var generateSysInput = function () {
// generate system's input of Scissors, Paper or Stone
// initalise variable
var sPs = "";
// initialise variable and assign random number of 0 to less than 3
var randNum = Math.random() * 3;
// initalise variable and get integer from random number
var intNum = Math.floor(randNum);
// assign Scissors, Paper or Stone values based on generated random integer
if (intNum == 0) {
sPs = "scissors";
} else if (intNum == 1) {
sPs = "paper";
} else sPs = "stone";
return sPs;
};
var generateGameStatsMsg = function () {
// generate game stat msg to be added to final output
playerWinPerc =
0 + (playerWinRecord / (playerWinRecord + systemWinRecord)) * 100;
systemWinPerc =
0 + (systemWinRecord / (playerWinRecord + systemWinRecord)) * 100;
// to prevent variable from having naan value
if (isNaN(playerWinPerc)) {
playerWinPerc = 0;
}
if (isNaN(systemWinRecord)) {
systemWinRecord = 0;
}
return `<br><br><br> ## ${userName}'s skills ##
<br>Win/Loss: Player < ${playerWinRecord} > VS System: < ${systemWinRecord} >
<br>Ties:${addSpacesToStr(9)}Count${addSpacesToStr(2)}< ${gameTieRecord} >
<br>Win %:${addSpacesToStr(5)}Player < ${playerWinPerc.toFixed(
2
)}% > VS System: < ${systemWinPerc.toFixed(2)}% >`;
};
var generateSystemMsg = function () {
// generate system message based on player vs system win record
if (playerWinRecord == systemWinRecord) {
return "<br><br><br><br>System: A worthy adversary indead";
}
if (playerWinRecord > systemWinRecord) {
return "<br><br><br><br>System: Don't let this get into your head, human.";
}
return "<br><br><br><br>System: Hah. Weak.";
};
var reversedGameModeStatus = function () {
// displays reversed game mode status
if (isReversedGameMode == false) {
return "Game Mode: Normal<br><br>";
}
return "Game Mode: Reversed<br><br>";
};
var spsLogic = function (cleanedUserInput, isReversed) {
// SPS logic - generate system's choice, evaluate with user's choice and output results
// generate system's choice of 'Scissors', 'Paper' or 'Stone'
var systemSps = generateSysInput();
// format SPS output
var formattedUserInput = formatStr(cleanedUserInput);
var formattedSysInput = formatStr(systemSps);
// return tie message for tie condition
if (cleanedUserInput == systemSps) {
gameTieRecord += 1;
return (
reversedGameModeStatus() +
`Player: [ ${formattedUserInput} ] VS System: [ ${formattedSysInput} ]
<br><br>You tied ._.<br><br><i>"We live to fight another day - Ragnar Lothbrok (Vikings)"</i>
<br><br>Enter input to play again!` +
generateGameStatsMsg() +
generateSystemMsg()
);
}
// return win message depending on game reversal condition
if (isReversed == true) {
// return win message for reversed win conditions
if (
(cleanedUserInput == "stone" && systemSps == "paper") ||
(cleanedUserInput == "scissors" && systemSps == "stone") ||
(cleanedUserInput == "paper" && systemSps == "scissors")
) {
playerWinRecord += 1;
return (
reversedGameModeStatus() +
`Player: [ ${formattedUserInput} ] VS System: [ ${formattedSysInput} ]
<br><br>You won!!! :D
<br><br><i>"To the victor belong the spoils of the enemy - William L. Marcy"</i>
<br><br>Enter input to play again!` +
generateGameStatsMsg() +
generateSystemMsg()
);
}
} else if (
// return win message for normal win conditions
(cleanedUserInput == "scissors" && systemSps == "paper") ||
(cleanedUserInput == "paper" && systemSps == "stone") ||
(cleanedUserInput == "stone" && systemSps == "scissors")
) {
playerWinRecord += 1;
return (
reversedGameModeStatus() +
`Player: [ ${formattedUserInput} ] VS System: [ ${formattedSysInput} ]
<br><br>You won!!! :D
<br><br><i>"To the victor belong the spoils of the enemy - William L. Marcy"</i>
<br><br>Enter input to play again!` +
generateGameStatsMsg() +
generateSystemMsg()
);
}
// return loss message otherwise
systemWinRecord += 1;
return (
reversedGameModeStatus() +
`Player: [ ${formattedUserInput} ] VS System: [ ${formattedSysInput} ]
<br><br>You lost :(<br><br><i>"Fall down seven times, stand up eight - Japanese proverb"</i>
<br><br>Enter input to play again!` +
generateGameStatsMsg() +
generateSystemMsg()
);
};
var reverseGameMode = function () {
// toggles reversed game mode
if (isReversedGameMode == false) {
isReversedGameMode = true;
} else if (isReversedGameMode == true) {
isReversedGameMode = false;
}
};