mirror of
https://github.com/FranP-code/Rock-Paper-and-Scissors-CLI-Game.git
synced 2025-10-13 00:32:38 +00:00
Play again option added
This commit is contained in:
377
index.js
377
index.js
@@ -68,16 +68,234 @@ const messagesList = {
|
|||||||
|
|
||||||
english: ' - Defeat count: ',
|
english: ' - Defeat count: ',
|
||||||
spanish: ' - Derrotas: ',
|
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
|
victoryCount = 0
|
||||||
consecutiveVictoryCount = 0
|
consecutiveVictoryCount = 0
|
||||||
defeatCount = 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({
|
inquirer.prompt({
|
||||||
|
|
||||||
name: 'languageSelect',
|
name: 'languageSelect',
|
||||||
@@ -92,165 +310,10 @@ inquirer.prompt({
|
|||||||
|
|
||||||
const languageSelection = answer.languageSelect.toLowerCase()
|
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('')
|
||||||
console.log(messagesList.welcome[languageSelection])
|
console.log(messagesList.welcome[languageSelection])
|
||||||
console.log('')
|
console.log('')
|
||||||
|
|
||||||
inquirer.prompt({
|
gameLogic(languageSelection)
|
||||||
|
|
||||||
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()
|
|
||||||
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
@@ -4,7 +4,8 @@
|
|||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
|
"game": "node index.js"
|
||||||
},
|
},
|
||||||
"keywords": [],
|
"keywords": [],
|
||||||
"author": "",
|
"author": "",
|
||||||
|
|||||||
Reference in New Issue
Block a user