Skip to content

Commit

Permalink
Add initial game
Browse files Browse the repository at this point in the history
  • Loading branch information
Asmatzaile committed Aug 24, 2022
0 parents commit 9bbda04
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
11 changes: 11 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Rock Paper Scissors</title>
<script src="./script.js"></script>
</head>
<body>

</body>
</html>
79 changes: 79 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
function getPlayerChoice() {
return wordToNum(prompt("Rock, paper or scissors?: "));
}

function getComputerChoice() {
return Math.floor(Math.random()*3);
}

function numToWord(num) {
switch(num) {
case 0:
return "Rock";
case 1:
return "Paper";
case 2:
return "Scissors";
};
}

function wordToNum(word) {
word = word.toLowerCase();
switch(word) {
case "rock":
return 0;
case "paper":
return 1;
case "scissors":
return 2;
}
}

function playRound(playerSelection, computerSelection) {
return result = ((((playerSelection-computerSelection)+1)%3+3)%3-1)
}




function game() {
let playerScore = 0;
let computerScore = 0;

for (let i = 0; i < 5; i++) {
let playerSelection = getPlayerChoice();
let computerSelection = getComputerChoice();
let roundResult = playRound(playerSelection, computerSelection);

let playerText = numToWord(playerSelection);
let computerText = numToWord(computerSelection);

if (roundResult === 1) {
playerScore++;
console.log(`You win! ${playerText} beats ${computerText}!`);
} else if (roundResult === -1) {
computerScore++;
console.log(`You lose! ${playerText} is beaten by ${computerText}`);
} else {
console.log(`It's a tie! You both chose ${playerText}`);
}

console.log("Player Score: " + playerScore);
console.log("Computer Score: " + computerScore);
}

console.log("GAME ENDED!")
reportWinner();



function reportWinner() {
if (playerScore>computerScore) {
console.log("PLAYER WINS!")
} else if (playerScore<computerScore) {
console.log("COMPUTER WINS!")
} else {
console.log("IT'S A TIE!")
}
}
}

0 comments on commit 9bbda04

Please sign in to comment.