-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
66 lines (60 loc) · 2.54 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
// 設定讓電腦隨機出拳的函數
const gameList = ["Rock", "Paper", "Scissors"];
function computerPlay() {
return gameList[Math.floor(Math.random() * gameList.length)];
}
// 設定猜拳遊戲(達到5分獲勝)的函數
function playRound(playerSelection, computerSelection) {
if ((playerSelection == "Rock" && computerSelection == "Rock") ||
(playerSelection == "Paper" && computerSelection == "Paper") ||
(playerSelection == "Scissors" && computerSelection == "Scissors")) {
container1.textContent = "It's a tie";
}
else if ((playerSelection == "Rock" && computerSelection == "Paper")||
(playerSelection == "Paper" && computerSelection == "Scissors")||
(playerSelection == "Scissors" && computerSelection == "Rock")) {
container1.textContent = "You LOSE! " + computerSelection + " beats " + playerSelection + "!";
computerScore++;
}
else if ((playerSelection == "Rock" && computerSelection == "Scissors")||
(playerSelection == "Paper" && computerSelection == "Rock") ||
(playerSelection == "Scissors" && computerSelection == "Paper")) {
container1.textContent = "You WIN! " + playerSelection + " beats " + computerSelection+ "!";
playerScore++;
}
container2.textContent = "Player Score: " + playerScore;
container3.textContent = "Computer Score: " + computerScore;
if (computerScore == 5) {
alert("You LOSE the game! Please try AGAIN!");
computerScore = 0;
playerScore = 0;
}
else if (playerScore == 5) {
alert("You WIN the game! You can play AGAIN!");
computerScore = 0;
playerScore = 0;
}
}
let playerScore = 0;
let computerScore = 0;
const btn1 = document.querySelector("#rock");
const btn2 = document.querySelector("#paper");
const btn3 = document.querySelector("#scissors");
const container1 = document.getElementById("container1");
const container2 = document.getElementById("container2");
const container3 = document.getElementById("container3");
btn1.addEventListener("click", () => {
playerSelection = "Rock";
computerSelection = computerPlay();
playRound(playerSelection, computerSelection);
});
btn2.addEventListener("click", () => {
playerSelection = "Paper";
computerSelection = computerPlay();
playRound(playerSelection, computerSelection);
});
btn3.addEventListener("click", () => {
playerSelection = "Scissors";
computerSelection = computerPlay();
playRound(playerSelection, computerSelection);
});