-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
82 lines (54 loc) · 1.97 KB
/
game.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
81
82
// options the computer can choose from
const options = ['rock', 'paper', 'scissors']
// winning move matrix
const winningMoves = {
"rock": "scissors",
"paper": "rock",
"scissors": "paper"
}
// scores on the doors
let humanScore = 0
let computerScore = 0
function getComputerChoice() {
return options[Math.round(Math.random() * 2)]
}
function playRound(humanChoice, computerChoice) {
document.querySelector("#message").textContent = `Human chose ${humanChoice}, Computer chose ${computerChoice}`
if( humanChoice === computerChoice ) {
document.querySelector("#message").textContent = document.querySelector("#message").textContent + ', no score draw'
}
else if (winningMoves[humanChoice] === computerChoice) {
humanScore = humanScore + 1
document.querySelector("#message").textContent = document.querySelector("#message").textContent + ', human scores'
}
else {
computerScore = computerScore + 1
document.querySelector("#message").textContent = document.querySelector("#message").textContent + ', computer scores'
}
document.querySelector("#human").textContent = humanScore
document.querySelector("#computer").textContent = computerScore
if (computerScore == 5) {
document.querySelector("#message").textContent = 'Computer wins.'
}
if (humanScore == 5) {
document.querySelector("#message").textContent = 'Human wins.'
}
}
function playGame(rounds) {
for (game = 0; game < 5; game++) {
console.log(`Playing game ${game + 1}`)
playRound(getHumanChoice(), getComputerChoice())
console.log(`Human : ${humanScore}, Computer : ${computerScore}`)
}
}
document.addEventListener('click', (event) => {
switch (event.target.id) {
case 'rock':
case 'paper':
case 'scissors':
playRound(event.target.id, getComputerChoice())
break;
default:
// ignore the event
}
})