-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
87 lines (59 loc) · 2.71 KB
/
main.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
83
84
85
86
87
const choice = document.querySelectorAll('#choices img')
const instructions = document.querySelector('.instructions')
const score = document.querySelector('.score')
const roundResult = document.querySelector('.round_result')
var playerScore = 0;
var computerScore = 0;
var roundsPlayed = 1;
choice.forEach((img) => {
img.addEventListener('click', (e) => {
playerMove = img.id
game()
})
})
function computerPlay() {
const selection = ["rock", "paper", "scissors"]
return selection[Math.floor(Math.random() * 3)]
};
function game() {
computerSelection = computerPlay()
playerSelection = playerMove
if (roundsPlayed < 4) {
if (computerSelection === playerSelection) {
instructions.innerHTML = "This round is a tie!<br>";
roundResult.innerHTML = "ROUND " + roundsPlayed + ": " + computerSelection.replace(/^[a-z]/, u => u.toUpperCase()) + " equals " + playerSelection + ".";
roundsPlayed++;
playerScore++;
computerScore++;
} else {
if((computerSelection == "scissors" && playerSelection == "rock") || (computerSelection == "paper" && playerSelection == "scissors") ||
(computerSelection == "rock" && playerSelection == "paper")){
instructions.innerHTML = "You won this round!<br>";
roundResult.innerHTML = "ROUND " + roundsPlayed + ": " + playerSelection.replace(/^[a-z]/, u => u.toUpperCase()) + " beats " + computerSelection + ".";
roundsPlayed++;
playerScore = playerScore + 2;
} else {
instructions.innerHTML = "You lost this round!<br>"
roundResult.innerHTML = "ROUND " + roundsPlayed + ": " + computerSelection.replace(/^[a-z]/, u => u.toUpperCase()) + " beats " + playerSelection + ".";
roundsPlayed++;
computerScore = computerScore + 2;
}
}
score.innerHTML = "Player: " + playerScore + "<br>Computer: " + computerScore;
if (roundsPlayed < 4) {
}else {
if (playerScore !== computerScore) {
if (playerScore > computerScore){
instructions.innerHTML = "GAME OVER - You just won the game!!!";
score.innerHTML = "Player final score: " + playerScore + "<br>Computer final score: " + computerScore;
}else {
instructions.innerHTML = "GAME OVER - You just lost the game! ";
score.innerHTML = "Player final score: " + playerScore + "<br>Computer final score: " + computerScore;
}
} else {
instructions.innerHTML = "GAME OVER - It's a tie!";
score.innerHTML = "Player final score: " + playerScore + "<br>Computer final score: " + computerScore;
}
}
}
}