From 9a225d2fce1a93927b8e6def3637a64e2335924f Mon Sep 17 00:00:00 2001 From: Francisco Pessano Date: Tue, 23 Nov 2021 20:51:37 -0300 Subject: [PATCH] Play again option added --- index.js | 377 ++++++++++++++++++++++++++++++--------------------- package.json | 3 +- 2 files changed, 222 insertions(+), 158 deletions(-) diff --git a/index.js b/index.js index 8013a03..96db5a1 100644 --- a/index.js +++ b/index.js @@ -68,16 +68,234 @@ const messagesList = { english: ' - Defeat count: ', spanish: ' - Derrotas: ', + }, + }, + + playAgain: { + + title: { + + english: 'Do you wanna play again?', + spanish: '¿Quieres jugar otra vez?' + }, + + options: { + + english: ['Yes', 'No'], + spanish: ['Sí', 'No'] } }, + thanksForPlay: { + english: 'Thanks for playing! Francisco Pessano', + spanish: 'Gracias por jugar! Francisco Pessano' + } } victoryCount = 0 consecutiveVictoryCount = 0 defeatCount = 0 +function gameLogic(languageSelection) { + + function showStats() { + + console.log() + console.log(messagesList.stats.title[languageSelection]) + console.log(messagesList.stats.victory[languageSelection] + victoryCount) + console.log(messagesList.stats.consecutiveVictory[languageSelection] + consecutiveVictoryCount) + console.log(messagesList.stats.defeat[languageSelection] + defeatCount) + } + + function victoryMessage() { + + victoryCount++ + consecutiveVictoryCount++ + console.log(messagesList.victory[languageSelection]) + } + + function defeatMessage() { + + defeatCount++ + consecutiveVictoryCount = 0 + console.log(messagesList.defeat[languageSelection]) + } + + function translateOption(userSelection, actualLanguage, targetLanguage) { + + const index = messagesList.options[actualLanguage].findIndex((element) => element === userSelection) + + return messagesList.options[targetLanguage][index] + } + + function translatePlayAgainOption(userSelection, actualLanguage, targetLanguage) { + + const index = messagesList.playAgain.options[actualLanguage].findIndex((element) => element === userSelection) + + return messagesList.playAgain.options[targetLanguage][index] + } + + inquirer.prompt({ + + name: 'userSelecction', + message: messagesList.play[languageSelection], + type: 'list', + + choices: [...messagesList.options[languageSelection]] + }) + + .then((answer) => { + + const options = [...messagesList.options['english']] + const computerSelection = options[Math.floor(Math.random() * options.length)] + + let userSelection = answer.userSelecction + userSelection = translateOption(userSelection, languageSelection, 'english') + + console.log() + console.log(messagesList.computerSelection[languageSelection] + translateOption(computerSelection, 'english', languageSelection)) + + if (computerSelection === userSelection) { + + console.log(messagesList.tie[languageSelection]) + } + + // if (computerSelection === 'Rock') { + + // if (userSelection === 'Paper') { + + // victoryMessage() + // return + // } + + // if (userSelection === 'Scissors') { + + // defeatMessage() + // return + // } + + // return + // } + + // if (computerSelection === 'Paper') { + + // if (userSelection === 'Rock') { + + // defeatMessage() + // return + // } + + // if (userSelection === 'Scissors') { + + // victoryMessage() + // return + // } + + // return + // } + + // if (computerSelection === 'Scissors') { + + // if (userSelection === 'Rock') { + + // victoryMessage() + // return + // } + + // if (userSelection === 'Paper') { + + // defeatMessage() + // return + // } + + // return + // } + + switch (computerSelection) { + + case 'Rock': + + if (userSelection === 'Paper') { + + victoryMessage() + } + + if (userSelection === 'Scissors') { + + defeatMessage() + } + + break; + + case 'Paper': + + if (userSelection === 'Rock') { + + defeatMessage() + } + + if (userSelection === 'Scissors') { + + victoryMessage() + } + + break; + + case 'Scissors': + + if (userSelection === 'Rock') { + + victoryMessage() + } + + if (userSelection === 'Paper') { + + defeatMessage() + } + + break; + + default: + break; + } + + showStats() + + console.log('') + + inquirer.prompt({ + + name: 'playAgain', + message: messagesList.playAgain.title[languageSelection], + type: 'list', + + choices: [...messagesList.playAgain.options[languageSelection]] + }) + .then((answer) => { + + const response = translatePlayAgainOption(answer.playAgain, languageSelection, 'english') + + if (response === 'Yes') { + + console.log('----------------') + console.log() + gameLogic(languageSelection) + return + } + + if (response === 'No') { + + console.log() + console.log(messagesList.thanksForPlay[languageSelection]) + console.log() + return + } + + }) + }) +} + inquirer.prompt({ name: 'languageSelect', @@ -92,165 +310,10 @@ inquirer.prompt({ const languageSelection = answer.languageSelect.toLowerCase() - function showStats() { - - console.log() - console.log(messagesList.stats.title[languageSelection]) - console.log(messagesList.stats.victory[languageSelection] + victoryCount) - console.log(messagesList.stats.consecutiveVictory[languageSelection] + consecutiveVictoryCount) - console.log(messagesList.stats.defeat[languageSelection] + defeatCount) - } - - function victoryMessage() { - - victoryCount++ - consecutiveVictoryCount++ - console.log(messagesList.victory[languageSelection]) - } - - function defeatMessage() { - - defeatCount++ - consecutiveVictoryCount = 0 - console.log(messagesList.defeat[languageSelection]) - } - - function translateOption(userSelection, actualLanguage, targetLanguage) { - - const index = messagesList.options[actualLanguage].findIndex((element) => element === userSelection) - - return messagesList.options[targetLanguage][index] - } - console.log('') console.log(messagesList.welcome[languageSelection]) console.log('') - inquirer.prompt({ - - name: 'userSelecction', - message: messagesList.play[languageSelection], - type: 'list', - - choices: [...messagesList.options[languageSelection]] - }) - - .then((answer) => { - - const options = [...messagesList.options['english']] - const computerSelection = options[Math.floor(Math.random() * options.length)] - - let userSelection = answer.userSelecction - userSelection = translateOption(userSelection, languageSelection, 'english') - - console.log() - console.log(messagesList.computerSelection[languageSelection] + translateOption(computerSelection, 'english', languageSelection)) - - if (computerSelection === userSelection) { - - console.log(messagesList.tie[languageSelection]) - } - - // if (computerSelection === 'Rock') { - - // if (userSelection === 'Paper') { - - // victoryMessage() - // return - // } - - // if (userSelection === 'Scissors') { - - // defeatMessage() - // return - // } - - // return - // } - - // if (computerSelection === 'Paper') { - - // if (userSelection === 'Rock') { - - // defeatMessage() - // return - // } - - // if (userSelection === 'Scissors') { - - // victoryMessage() - // return - // } - - // return - // } - - // if (computerSelection === 'Scissors') { - - // if (userSelection === 'Rock') { - - // victoryMessage() - // return - // } - - // if (userSelection === 'Paper') { - - // defeatMessage() - // return - // } - - // return - // } - - switch (computerSelection) { - - case 'Rock': - - if (userSelection === 'Paper') { - - victoryMessage() - } - - if (userSelection === 'Scissors') { - - defeatMessage() - } - - break; - - case 'Paper': - - if (userSelection === 'Rock') { - - defeatMessage() - } - - if (userSelection === 'Scissors') { - - victoryMessage() - } - - break; - - case 'Scissors': - - if (userSelection === 'Rock') { - - victoryMessage() - } - - if (userSelection === 'Paper') { - - defeatMessage() - } - - break; - - default: - break; - } - - showStats() - - }) + gameLogic(languageSelection) + }) \ No newline at end of file diff --git a/package.json b/package.json index 680c02b..8f90ada 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "game": "node index.js" }, "keywords": [], "author": "",