+ Input:
+ - Enter name on first input - Enter scissors, paper or stone
+ subsequently - Add "reversed" to game input to play the game with
+ reversed rules (i.e. reversed stone)
+
diff --git a/script.js b/script.js
index bbe8a29..46f6ff7 100644
--- a/script.js
+++ b/script.js
@@ -1,4 +1,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) {
- var myOutputValue = 'hello world';
+ // 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.
Invalid input.
Please enter one of the following: - Scissors - Paper - 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 `
## ${userName}'s skills ##
+ Win/Loss: Player < ${playerWinRecord} > VS System: < ${systemWinRecord} >
+ Ties:${addSpacesToStr(9)}Count${addSpacesToStr(2)}< ${gameTieRecord} >
+ 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 "
System: A worthy adversary indead";
+ }
+ if (playerWinRecord > systemWinRecord) {
+ return "
System: Don't let this get into your head, human.";
+ }
+ return "
System: Hah. Weak.";
+};
+
+var reversedGameModeStatus = function () {
+ // displays reversed game mode status
+ if (isReversedGameMode == false) {
+ return "Game Mode: Normal
";
+ }
+ return "Game Mode: Reversed
";
+};
+
+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} ]
+
You tied ._.
"We live to fight another day - Ragnar Lothbrok (Vikings)"
+