From c698883b44ff67f5d4184c45aa7099a86a0874c5 Mon Sep 17 00:00:00 2001 From: yuzu-eva Date: Tue, 21 Mar 2023 23:19:41 +0100 Subject: added a rock-paper-scissors game --- scripts/rps.js | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 scripts/rps.js (limited to 'scripts') diff --git a/scripts/rps.js b/scripts/rps.js new file mode 100644 index 0000000..c1c1b9b --- /dev/null +++ b/scripts/rps.js @@ -0,0 +1,81 @@ +let compChoice = {Value: ""}; +let playerChoice; +let compChoiceInt; +let playerChoiceInt; +const buttons = document.querySelectorAll('.btn'); + +let playerScore = 0; +let compScore = 0; + +const player = document.querySelector("#player-score"); +player.textContent = `player score: ${playerScore}`; + +const computer = document.querySelector("#comp-score"); +computer.textContent = `computer score: ${compScore}`; + +const output = document.querySelector("#output"); +output.textContent = "hopefully you're better then a machine ://"; + +buttons.forEach((button) => { button.addEventListener('click', () => { + + playerChoice = button.id; + if (playerChoice == "rock") playerChoiceInt = 0; + else if (playerChoice == "paper") playerChoiceInt = 1; + else if (playerChoice == "scissors") playerChoiceInt = 2; + compChoiceInt = getComputerChoice(compChoice); + playGame(); + }) + +}) + +function getRandomInt() { + return Math.floor(Math.random() *3); +} + +function getComputerChoice(compChoice) { + let choiceNum = getRandomInt(); + if (choiceNum == 0) compChoice.Value = "rock"; + else if (choiceNum == 1) compChoice.Value = "paper"; + else if (choiceNum == 2) compChoice.Value = "scissors"; + return choiceNum; +} + +function playRound(){ + let winArray = [[0, 2, 1], + [1, 0, 2], + [2, 1, 0]]; + let result = winArray[playerChoiceInt][compChoiceInt]; + if (result == 0){ + output.textContent = `its a tie! you chose ${playerChoice} and the computer chose ${compChoice.Value}`; + } + else if (result == 1){ + output.textContent = `you won! you chose ${playerChoice} and the computer chose ${compChoice.Value}`; + playerScore++; + + } + else if (result == 2){ + output.textContent = `you lost! you chose ${playerChoice} and the computer chose ${compChoice.Value}`; + compScore++; + } +} + +function playGame(){ + output.textContent = "choose rock, paper, or scissors"; + playRound(); + player.textContent = `player score: ${playerScore}`; + computer.textContent = `computer score: ${compScore}`; + if (playerScore == 5){ + output.textContent = "you won the game :3"; + playerScore = 0; + compScore = 0; + player.textContent = `player score: ${playerScore}`; + computer.textContent = `computer score: ${compScore}`; + } + else if (compScore == 5){ + output.textContent = "you lost the game :/ little looser bitch faggot" + playerScore = 0; + compScore = 0; + player.textContent = `player score: ${playerScore}`; + computer.textContent = `computer score: ${compScore}`; + } +} -- cgit v1.2.3