Created
January 2, 2025 11:12
-
-
Save AhmetEnesKCC/e8582a613150b731233e145095c1f00d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const readline = require("readline"); | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
}); | |
console.log("Game Started"); | |
const options = ["tas", "kagit", "makas"]; | |
const whoWinner = (pcOption, userOption) => { | |
if (pcOption === userOption) { | |
return "tie"; | |
} | |
if ( | |
(pcOption === "tas" && userOption === "makas") || | |
(pcOption === "makas" && userOption === "kagit") || | |
(pcOption === "kagit" && userOption === "tas") | |
) { | |
return "pc"; | |
} | |
return "user"; | |
}; | |
const optionsLength = options.length; | |
createOption = () => { | |
const optionIndex = Math.floor(Math.random() * (optionsLength - 0)) + 0; | |
const result = options[optionIndex]; | |
return result; | |
}; | |
// Listen always | |
let score = 0; | |
const askUser = () => { | |
rl.question("Enter your choose: ", (input) => { | |
const pcOption = createOption(); | |
const winner = whoWinner(pcOption, input); | |
if (winner === "user") { | |
score++; | |
} | |
console.log(`PC: ${pcOption}, You: ${input}, WINNER: ${winner}`); | |
const askForClose = () => { | |
rl.question("Do you want to play again: Y/n", (answer) => { | |
if (answer === "Y") { | |
askUser(); | |
} else if (answer === "n") { | |
console.log("Game ends..."); | |
console.log("Your score: ", score); | |
rl.close(); | |
} else { | |
console.log("Choose either Y or n"); | |
askForClose(); | |
} | |
}); | |
}; | |
askForClose(); | |
}); | |
}; | |
askUser(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment