From 3c49cf49a2db213f6838b95f91770f6b53ef4a4c Mon Sep 17 00:00:00 2001 From: array-in-a-matrix Date: Wed, 9 Jun 2021 17:53:37 -0400 Subject: [PATCH] rock paper scissors game --- index.html | 5 +++- index/RPS.html | 46 ++++++++++++++++++++++++++++++++++ index/RPS.js | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 index/RPS.html create mode 100644 index/RPS.js diff --git a/index.html b/index.html index 71656b2..4d59205 100644 --- a/index.html +++ b/index.html @@ -18,7 +18,6 @@

Array in a Matrix

Welcome!

+

Welcome to my page

diff --git a/index/RPS.html b/index/RPS.html new file mode 100644 index 0000000..c51203c --- /dev/null +++ b/index/RPS.html @@ -0,0 +1,46 @@ + + + + + + + Array in a Matrix + + + + + + + +
+

Simple rock paper scissors game

+

made with Javascript

+ + +
+ + diff --git a/index/RPS.js b/index/RPS.js new file mode 100644 index 0000000..0bf8ba8 --- /dev/null +++ b/index/RPS.js @@ -0,0 +1,68 @@ +let userChoice = "" + +const getUserChoice = (userInput) => { + userChoice = userInput +}; + +function getComputerChoice() { + let cpuChoice = Math.floor(Math.random() * 3); + if (cpuChoice === 0) { + return "rock"; + } else if (cpuChoice === 1) { + return "paper"; + } else if (cpuChoice === 2) { + return "scissors"; + } +} + +function determineWinner(userChoice, computerChoice) { + if (userChoice === computerChoice) { + return "Tie game!"; + } + if (userChoice === "rock") { + if (computerChoice === "paper") { + return "Computer won!"; + } else { + return "You won!"; + } + } + if (userChoice === "paper") { + if (computerChoice === "scissors") { + return "Computer won!"; + } else { + return "You won!"; + } + } + if (userChoice === "scissors") { + if (computerChoice === "rock") { + return "Computer won!"; + } else { + return "You won!"; + } + } + if (userChoice === "bomb") { + return "You won with cheats!"; + } +} + +function playGame() { + var computerChoice = getComputerChoice(); + console.log("Player: " + userChoice); + console.log("CPU: " + computerChoice); + console.log(determineWinner(userChoice, computerChoice)); + + var div = document.getElementById('score') + var p = document.getElementById("text"); + var strong = document.getElementById("winner"); + + p.textContent = `You: ${userChoice}` + p.textContent += ` CPU: ${computerChoice}` + strong.textContent = determineWinner(userChoice, computerChoice); + document.getElementById("game").appendChild(div).appendChild(p) + document.getElementById("game").appendChild(div).appendChild(strong) +} + +function play(input){ + getUserChoice(input) + playGame() +}