-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
82 lines (64 loc) · 2.85 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
let playerScore = 0;
let computerScore = 0;
let totalGames= 0;
const rockButton = document.getElementsByClassName("rock-optn")[0];
const paperButton = document.getElementsByClassName("paper-optn")[0];
const scissorsButton = document.getElementsByClassName("scissors-optn")[0];
const roundResults = document.getElementsByClassName("round-results")[0];
const playerRoundInfo = document.getElementsByClassName("player-round-info")[0];
const computerRoundInfo = document.getElementsByClassName("computer-round-info")[0];
const playerSelectionDisplay = document.getElementsByClassName("player-selection")[0];
const computerSelectionDisplay = document.getElementsByClassName("computer-selection")[0];
rockButton.onclick = function() {gameLog(0)};
paperButton.onclick = function() {gameLog(1)};
scissorsButton.onclick = function() {gameLog(2)};
// Computer randomly picks an number 0, 1, or 2 which will eventually be mapped
// to rock, paper or scissors, respectively.
function computerSelection() {
return Math.floor(Math.random()*3)
}
// Get string input from from user and convert it to a number for the game's
// model.
// two players and returns the result of the round.
function playRound(playerOne, playerTwo) {
let choices = ["rock", "paper", "scissors"];
if (playerOne == playerTwo) {
return {tie: true, winner: null, playerChoice: choices[playerOne], computerChoice: choices[playerTwo]}
} else if ((playerOne + 1) % 3 == playerTwo ) {
return {tie: false, winner: "computer", playerChoice: choices[playerOne], computerChoice: choices[playerTwo]}
} else {
return {tie: false, winner: "player", playerChoice: choices[playerOne], computerChoice: choices[playerTwo]}
}
}
function updateDisplay(displayNode, newDisplayItem) {
let itemSource = "images/" + newDisplayItem + ".png"
if (displayNode.childElementCount !== 0 ) {
displayNode.removeChild(displayNode.children[0]);
}
let img = document.createElement("img")
img.setAttribute("src", itemSource);
displayNode.appendChild(img);
}
function gameLog(playerChoice) {
let round = playRound(playerChoice, computerSelection());
updateDisplay(playerSelectionDisplay, round.playerChoice);
updateDisplay(computerSelectionDisplay, round.computerChoice);
totalGames += 1;
if (!round.tie) {
if (round.winner == "player") {
playerScore += 1;
} else {
computerScore += 1;
}
}
roundResults.innerHTML = `Round - ${totalGames}`;
playerRoundInfo.innerText = `You - ${playerScore}`;
computerRoundInfo.innerText = `Computer - ${computerScore}`;
if (playerScore >= 5) {
roundResults.innerHTML = "You won!";
alert("Press F5 to play again");
} else if (computerScore >= 5) {
roundResults.innerHTML = "You lost";
alert("Press F5 to play again");
}
}