From 8396f49485cfb63a7896869541527a3c8f3c68c8 Mon Sep 17 00:00:00 2001 From: R4Y815 Date: Thu, 14 Oct 2021 22:48:18 +0800 Subject: [PATCH 1/5] completed in class code for Secret Word Game. --- script.js | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/script.js b/script.js index bbe8a29..9509fe9 100644 --- a/script.js +++ b/script.js @@ -1,4 +1,79 @@ +var scoreBanana = 0; +var scoreChisel = 0; +var scoreFaucet = 0; +var gameNumber = 0; var main = function (input) { - var myOutputValue = 'hello world'; + gameNumber = gameNumber + 1; + console.log("### GAME NUMBER ###"); + console.log(gameNumber); + var randomSecretWord = readSecretWordNmbr(randomNumber()); + console.log("randomSecretWord = "); + console.log(randomSecretWord); + + if ( + input.toLowerCase() == randomSecretWord && //player is only correct guess when the input is the same as computer's secret choice. + input.toLowerCase() == "banana" + ) { + scoreBanana = scoreBanana + 1; // always fixing the score at one for each correct word means the score don't increase if player uses back the same word. + } + console.log("** ScoreBanana **"); + console.log(scoreBanana); + if ( + input.toLowerCase() == randomSecretWord && + input.toLowerCase() == "chisel" + ) { + scoreChisel = scoreChisel + 1; + } + console.log("** scoreChisel **"); + console.log(scoreChisel); + if ( + input.toLowerCase() == randomSecretWord && + input.toLowerCase() == "faucet" + ) { + scoreFaucet = scoreFaucet + 1; + } + console.log("** scoreFaucet **"); + console.log(scoreFaucet); + + var sumScore = scoreBanana + scoreChisel + scoreFaucet; + var scoreDiff = 3 - sumScore; + + var myOutputValue = `Wrong guess
Your guess was ${input}.
Secret Word: ${randomSecretWord}.
You need ${scoreDiff} more correct guesses to win. `; + var correctGuess = `Correct guess
Your guess was ${input}.
Secret Word: ${randomSecretWord}.
You need ${scoreDiff} more correct guesses to win. `; + var win = `You WIN!
Your guess was ${input}.
Secret Word: ${randomSecretWord}.
`; + + if (input.toLowerCase() == randomSecretWord) { + myOutputValue = correctGuess; + } + + if (sumScore >= 2) { + myOutputValue = win; + } + return myOutputValue; }; + +var win = "You win"; + +// TO KEEP SCORE OF THE CORRECT WORD + +var randomNumber = function () { + var randomDecimal = Math.random() * 3; // Math.random() --> 0 --- <1 0.1 , 0.2121241, 0.999999999999 float 2.09999 + var randomInteger = Math.floor(randomDecimal); //0,1,2 + return randomInteger + 1; // 1,2,3 +}; + +// read random Number to output random GameHand +var readSecretWordNmbr = function (secretWordIndex) { + var secretWordString = ""; + if (secretWordIndex == 1) { + secretWordString = "banana"; + } + if (secretWordIndex == 2) { + secretWordString = "chisel"; + } + if (secretWordIndex == 3) { + secretWordString = "faucet"; + } + return secretWordString; +}; From 4c88d1df5e2a551301aa4ea165f0ed7ca4a2a2bc Mon Sep 17 00:00:00 2001 From: R4Y815 Date: Thu, 14 Oct 2021 22:52:39 +0800 Subject: [PATCH 2/5] fixed each word correct guess score at 1 every submit --- script.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/script.js b/script.js index 9509fe9..26ae04e 100644 --- a/script.js +++ b/script.js @@ -14,7 +14,7 @@ var main = function (input) { input.toLowerCase() == randomSecretWord && //player is only correct guess when the input is the same as computer's secret choice. input.toLowerCase() == "banana" ) { - scoreBanana = scoreBanana + 1; // always fixing the score at one for each correct word means the score don't increase if player uses back the same word. + scoreBanana = 1; // always fixing the score at one for each correct word means the score don't increase if player uses back the same word. } console.log("** ScoreBanana **"); console.log(scoreBanana); @@ -22,7 +22,7 @@ var main = function (input) { input.toLowerCase() == randomSecretWord && input.toLowerCase() == "chisel" ) { - scoreChisel = scoreChisel + 1; + scoreChisel = 1; } console.log("** scoreChisel **"); console.log(scoreChisel); @@ -30,7 +30,7 @@ var main = function (input) { input.toLowerCase() == randomSecretWord && input.toLowerCase() == "faucet" ) { - scoreFaucet = scoreFaucet + 1; + scoreFaucet = 1; } console.log("** scoreFaucet **"); console.log(scoreFaucet); From 1c1b321a4023fe3b2b69ad8e8482f0f47f001977 Mon Sep 17 00:00:00 2001 From: R4Y815 Date: Fri, 15 Oct 2021 10:41:39 +0800 Subject: [PATCH 3/5] secret word guessing game, win only by guessing correctly 2wice in a roll --- script.js | 59 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 16 deletions(-) diff --git a/script.js b/script.js index 26ae04e..33f1d4a 100644 --- a/script.js +++ b/script.js @@ -4,17 +4,34 @@ var scoreFaucet = 0; var gameNumber = 0; var main = function (input) { gameNumber = gameNumber + 1; - console.log("### GAME NUMBER ###"); + console.log("##### GAME NUMBER #####"); console.log(gameNumber); + var randomSecretWord = readSecretWordNmbr(randomNumber()); console.log("randomSecretWord = "); console.log(randomSecretWord); + /* //====================================== + // INPUT CONTROL FLOW TEST BLOC; + if (gameNumber == 2) { + randomSecretWord = "banana"; + } + if (gameNumber == 3) { + randomSecretWord = "chisel"; + } + if (gameNumber == 4) { + randomSecretWord = "faucet"; + } + if (gameNumber == 6) { + randomSecretWord = "faucet"; + } + //====================================== */ + if ( - input.toLowerCase() == randomSecretWord && //player is only correct guess when the input is the same as computer's secret choice. + input.toLowerCase() == randomSecretWord && input.toLowerCase() == "banana" ) { - scoreBanana = 1; // always fixing the score at one for each correct word means the score don't increase if player uses back the same word. + scoreBanana = scoreBanana + 1; } console.log("** ScoreBanana **"); console.log(scoreBanana); @@ -22,7 +39,7 @@ var main = function (input) { input.toLowerCase() == randomSecretWord && input.toLowerCase() == "chisel" ) { - scoreChisel = 1; + scoreChisel = scoreChisel + 1; } console.log("** scoreChisel **"); console.log(scoreChisel); @@ -30,33 +47,43 @@ var main = function (input) { input.toLowerCase() == randomSecretWord && input.toLowerCase() == "faucet" ) { - scoreFaucet = 1; + scoreFaucet = scoreFaucet + 1; } console.log("** scoreFaucet **"); console.log(scoreFaucet); var sumScore = scoreBanana + scoreChisel + scoreFaucet; - var scoreDiff = 3 - sumScore; + var scoreDiff = 2 - sumScore; + + //need message for when the person doesn't win 2wice in a row. + var TwoWrongGuesses = `Wrong guess
Your guess was ${input}.
Secret Word: ${randomSecretWord}.
You need ${scoreDiff} correct guesses in a row to win. Try again. `; - var myOutputValue = `Wrong guess
Your guess was ${input}.
Secret Word: ${randomSecretWord}.
You need ${scoreDiff} more correct guesses to win. `; - var correctGuess = `Correct guess
Your guess was ${input}.
Secret Word: ${randomSecretWord}.
You need ${scoreDiff} more correct guesses to win. `; - var win = `You WIN!
Your guess was ${input}.
Secret Word: ${randomSecretWord}.
`; + var myOutputValue = TwoWrongGuesses; - if (input.toLowerCase() == randomSecretWord) { - myOutputValue = correctGuess; + var correct1stGuess = `Correct guess...

Your guess was ${input}.
Secret Word: ${randomSecretWord}.

You need ${scoreDiff} more correct guess to win. `; + + var correct2wiceInArow = ` YOU WIN!

You have guessed the secret word twice in a roll!

Your guess for the 2nd word was ${input}.
Secret Word: ${randomSecretWord}.
`; + + var gameOver = `You have already beaten the game in this round.

Please refresh the webpage to restart the game. `; + + if (input.toLowerCase() == randomSecretWord && sumScore == 1) { + myOutputValue = correct1stGuess; } - if (sumScore >= 2) { - myOutputValue = win; + if (sumScore == 2 && input.toLowerCase() == randomSecretWord) { + myOutputValue = correct2wiceInArow; } + if ( + (sumScore > 2 && input.toLowerCase() == randomSecretWord) || + (sumScore >= 2 && input.toLowerCase() != randomSecretWord) + ) { + myOutputValue = gameOver; + } return myOutputValue; }; -var win = "You win"; - // TO KEEP SCORE OF THE CORRECT WORD - var randomNumber = function () { var randomDecimal = Math.random() * 3; // Math.random() --> 0 --- <1 0.1 , 0.2121241, 0.999999999999 float 2.09999 var randomInteger = Math.floor(randomDecimal); //0,1,2 From de4224f6c24686798776f74e0c8218b6bd598e7a Mon Sep 17 00:00:00 2001 From: R4Y815 Date: Fri, 15 Oct 2021 18:48:12 +0800 Subject: [PATCH 4/5] Randomised score required to win. Output required score for next round after winning. --- script.js | 123 +++++++++++++++++++++++++++++------------------------- 1 file changed, 65 insertions(+), 58 deletions(-) diff --git a/script.js b/script.js index 33f1d4a..aa94663 100644 --- a/script.js +++ b/script.js @@ -1,85 +1,79 @@ -var scoreBanana = 0; -var scoreChisel = 0; -var scoreFaucet = 0; +var random2to4 = function () { + var randomDecimal = Math.random() * 3; // Math.random() --> 0 --- <1 0.1 , 0.2121241, 0.999999999999 float 2.09999 + var randomInteger = Math.floor(randomDecimal); //0,1,2 + return randomInteger + 2; // returns 2,3, or 4. +}; + var gameNumber = 0; +var nTimesNeeded = random2to4(); //N times needed for winning--> this starts first. +var randomNtimesNeeded = "not yet activated"; //2nd: this is for count when player wins all N times. can't start this definition anywhere locally in the main function. +var sumScore = 0; +console.log("===GLOBAL nTimesNeeded to 1st ROUND==="); +console.log(nTimesNeeded); + var main = function (input) { gameNumber = gameNumber + 1; console.log("##### GAME NUMBER #####"); console.log(gameNumber); + console.log("===LOCAL nTimesNeeded to Win CURRENT ROUND ==="); + console.log(nTimesNeeded); var randomSecretWord = readSecretWordNmbr(randomNumber()); console.log("randomSecretWord = "); console.log(randomSecretWord); - /* //====================================== - // INPUT CONTROL FLOW TEST BLOC; - if (gameNumber == 2) { - randomSecretWord = "banana"; - } - if (gameNumber == 3) { - randomSecretWord = "chisel"; - } - if (gameNumber == 4) { - randomSecretWord = "faucet"; - } - if (gameNumber == 6) { - randomSecretWord = "faucet"; - } - //====================================== */ - - if ( - input.toLowerCase() == randomSecretWord && - input.toLowerCase() == "banana" - ) { - scoreBanana = scoreBanana + 1; - } - console.log("** ScoreBanana **"); - console.log(scoreBanana); - if ( - input.toLowerCase() == randomSecretWord && - input.toLowerCase() == "chisel" - ) { - scoreChisel = scoreChisel + 1; - } - console.log("** scoreChisel **"); - console.log(scoreChisel); if ( - input.toLowerCase() == randomSecretWord && - input.toLowerCase() == "faucet" + (input.toLowerCase() == randomSecretWord && + input.toLowerCase() == "banana") || + (input.toLowerCase() == randomSecretWord && + input.toLowerCase() == "chisel") || + (input.toLowerCase() == randomSecretWord && input.toLowerCase() == "faucet") ) { - scoreFaucet = scoreFaucet + 1; + sumScore = sumScore + 1; } - console.log("** scoreFaucet **"); - console.log(scoreFaucet); - var sumScore = scoreBanana + scoreChisel + scoreFaucet; - var scoreDiff = 2 - sumScore; + var scoreDiff = nTimesNeeded - sumScore; // This tells the player how many more correct guesses needed to win current round. + var myOutputValue = ""; //need message for when the person doesn't win 2wice in a row. - var TwoWrongGuesses = `Wrong guess
Your guess was ${input}.
Secret Word: ${randomSecretWord}.
You need ${scoreDiff} correct guesses in a row to win. Try again. `; + var wrongGuess = `Your guess was ${input}.

Secret Word: ${randomSecretWord}.

You need ${nTimesNeeded} correct guess/guesses in a row to win. Try again. `; - var myOutputValue = TwoWrongGuesses; + var correctGuess = `Correct guess...

Your guess was ${input}.
Secret Word: ${randomSecretWord}.

You need ${scoreDiff} more correct guess/guesses to win. `; - var correct1stGuess = `Correct guess...

Your guess was ${input}.
Secret Word: ${randomSecretWord}.

You need ${scoreDiff} more correct guess to win. `; - - var correct2wiceInArow = ` YOU WIN!

You have guessed the secret word twice in a roll!

Your guess for the 2nd word was ${input}.
Secret Word: ${randomSecretWord}.
`; + if (input.toLowerCase() == randomSecretWord && sumScore == nTimesNeeded) { + myOutputValue = outputForWin; + randomNtimesNeeded = random2to4(); + } + console.log("-----> randomNtimesNeeded ------>"); + console.log(randomNtimesNeeded); - var gameOver = `You have already beaten the game in this round.

Please refresh the webpage to restart the game. `; + var outputForWin = ` YOU WIN!

You have guessed the secret word correctly ${nTimesNeeded} times in a roll!

Your ${nmbrSuffix( + nTimesNeeded + )} guess was ${input}.
Secret Word: ${randomSecretWord}.

In the next round, you will need to make
${randomNtimesNeeded} correct guesses in a row`; - if (input.toLowerCase() == randomSecretWord && sumScore == 1) { - myOutputValue = correct1stGuess; + if (input.toLowerCase() != randomSecretWord) { + myOutputValue = wrongGuess; + sumScore = 0; } - if (sumScore == 2 && input.toLowerCase() == randomSecretWord) { - myOutputValue = correct2wiceInArow; + if (input.toLowerCase() == randomSecretWord && sumScore < nTimesNeeded) { + myOutputValue = correctGuess; //because sumScore and nTimesNeeded are tied to scoreDiff, I added an extra condition for better control, such that the output is correct to the exact game situation before winning after 1st correct guess. } - if ( - (sumScore > 2 && input.toLowerCase() == randomSecretWord) || - (sumScore >= 2 && input.toLowerCase() != randomSecretWord) - ) { - myOutputValue = gameOver; + if (input.toLowerCase() == randomSecretWord && sumScore == nTimesNeeded) { + nTimesNeeded = randomNtimesNeeded; //NEXT ROUND'S N TIMES TRANSFERED TO 'nTimesNeeded' + myOutputValue = outputForWin; + sumScore = 0; } + console.log(">>>>>> scoreDiff = nTimesNeeded - sumScore"); + console.log(scoreDiff); + + console.log(">>>>>> sumScore END of this guess= "); + console.log(sumScore); + + console.log("nTimesNeeded to WIN CURRENT / NEXT ROUND ==="); + console.log(nTimesNeeded); + return myOutputValue; }; @@ -102,5 +96,18 @@ var readSecretWordNmbr = function (secretWordIndex) { if (secretWordIndex == 3) { secretWordString = "faucet"; } - return secretWordString; + return "chisel"; +}; + +var nmbrSuffix = function (nTimesNeeded) { + if (nTimesNeeded == 2) { + stNdThSuffix = "2nd"; + } + if (nTimesNeeded == 3) { + stNdThSuffix = "3rd"; + } + if (nTimesNeeded == 4) { + stNdThSuffix = "4th"; + } + return stNdThSuffix; }; From 6f97b3b43f5639e9205f2c18582c47d50971a4fa Mon Sep 17 00:00:00 2001 From: R4Y815 Date: Fri, 15 Oct 2021 18:50:36 +0800 Subject: [PATCH 5/5] corrected logical error for random secretWord return. --- script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script.js b/script.js index aa94663..0af08aa 100644 --- a/script.js +++ b/script.js @@ -96,7 +96,7 @@ var readSecretWordNmbr = function (secretWordIndex) { if (secretWordIndex == 3) { secretWordString = "faucet"; } - return "chisel"; + return secretWordString; // amend to 1 string input to test. }; var nmbrSuffix = function (nTimesNeeded) {