From f6b4349b1bdea1f0924b2c22f50cfa7443bada4f Mon Sep 17 00:00:00 2001 From: KenHo95 Date: Wed, 22 Feb 2023 22:12:04 +0800 Subject: [PATCH 1/3] Committing changes made to scipt.js --- script.js | 262 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 261 insertions(+), 1 deletion(-) diff --git a/script.js b/script.js index bbe8a29..bc8ba29 100644 --- a/script.js +++ b/script.js @@ -1,4 +1,264 @@ +//////////////////////////// +//// 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; + } + + //console.log("start of program"); + // 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 + + // console.log("validate input start"); + if ( + !( + userInputPara == "scissors" || + userInputPara == "paper" || + userInputPara == "stone" + ) + ) { + //console.log("validate false input end"); + return false; + } + + //console.log("validate true input end"); + 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)" +

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} ] +

You won!!! :D +

"To the victor belong the spoils of the enemy - William L. Marcy" +

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} ] +

You won!!! :D +

"To the victor belong the spoils of the enemy - William L. Marcy" +

Enter input to play again!` + + generateGameStatsMsg() + + generateSystemMsg() + ); + } + // return loss message otherwise + + systemWinRecord += 1; + return ( + reversedGameModeStatus() + + `Player: [ ${formattedUserInput} ] VS System: [ ${formattedSysInput} ] +

You lost :(

"Fall down seven times, stand up eight - Japanese proverb" +

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; + } +}; From 955a7c4864e18e52520984acaf20eb9c0f7ede0f Mon Sep 17 00:00:00 2001 From: KenHo95 Date: Wed, 22 Feb 2023 22:14:26 +0800 Subject: [PATCH 2/3] Added game instructions to webpage --- index.html | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/index.html b/index.html index d3d3e0e..2301fc5 100644 --- a/index.html +++ b/index.html @@ -51,7 +51,12 @@

Basics: Scissors Paper Stone 🚀

-

Input:

+

+ 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) +


From af86ac05f83935d2004e920b58a9c16cfba9f1e0 Mon Sep 17 00:00:00 2001 From: KenHo95 Date: Wed, 22 Feb 2023 22:18:03 +0800 Subject: [PATCH 3/3] Cleaned up console logs in script.js --- script.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/script.js b/script.js index bc8ba29..46f6ff7 100644 --- a/script.js +++ b/script.js @@ -42,7 +42,6 @@ var main = function (input) { return myOutputValue; } - //console.log("start of program"); // clean user input var userInputCleaned = cleanStr(input); @@ -99,7 +98,6 @@ 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 - // console.log("validate input start"); if ( !( userInputPara == "scissors" || @@ -107,11 +105,9 @@ var validateInput = function (userInputPara) { userInputPara == "stone" ) ) { - //console.log("validate false input end"); return false; } - //console.log("validate true input end"); return true; };