@@ -4,91 +4,63 @@ function computerPlay() {
4
4
return gameList [ Math . floor ( Math . random ( ) * gameList . length ) ] ;
5
5
}
6
6
7
- // 設定猜拳一回合並印出輸贏的函數
7
+ // 設定猜拳遊戲(達到5分獲勝)的函數
8
8
function playRound ( playerSelection , computerSelection ) {
9
9
if ( ( playerSelection == "Rock" && computerSelection == "Rock" ) ||
10
10
( playerSelection == "Paper" && computerSelection == "Paper" ) ||
11
11
( playerSelection == "Scissors" && computerSelection == "Scissors" ) ) {
12
- return "It's a tie" ;
12
+ container1 . textContent = "It's a tie" ;
13
13
}
14
- else if ( playerSelection == "Rock" && computerSelection == "Paper" ) {
15
- return "You LOSE! Paper beats Rock!" ;
14
+ else if ( ( playerSelection == "Rock" && computerSelection == "Paper" ) ||
15
+ ( playerSelection == "Paper" && computerSelection == "Scissors" ) ||
16
+ ( playerSelection == "Scissors" && computerSelection == "Rock" ) ) {
17
+ container1 . textContent = "You LOSE! " + computerSelection + " beats " + playerSelection + "!" ;
18
+ computerScore ++ ;
16
19
}
17
- else if ( playerSelection == "Rock" && computerSelection == "Scissors" ) {
18
- return "You WIN! Rock beats Scissors!" ;
19
- }
20
- else if ( playerSelection == "Paper" && computerSelection == "Rock" ) {
21
- return "You WIN! Paper beats Rock!" ;
22
- }
23
- else if ( playerSelection == "Paper" && computerSelection == "Scissors" ) {
24
- return "You LOSE! Paper beats Scissors!" ;
25
- }
26
- else if ( playerSelection == "Scissors" && computerSelection == "Rock" ) {
27
- return "You LOSE! Rock beats Scissors!" ;
28
- }
29
- else if ( playerSelection == "Scissors" && computerSelection == "Paper" ) {
30
- return "You WIN! Scissors beats Paper!" ;
31
- }
32
- }
33
-
34
- // 設定計算猜拳分數(誰先到5分就贏)的函數
35
- function game ( ) {
36
- let playerScore = 0 ;
37
- let computerScore = 0 ;
38
- while ( playerScore <= 5 || computerScore <= 5 ) {
39
- const playerSelection = prompt ( "Enter 'Rock', ' Paper', or 'Scissors'" , ) ;
40
- const computerSelection = computerPlay ( ) ;
41
- if ( playerSelection == "Rock" && computerSelection == "Paper" ) {
42
- ++ computerScore ;
43
- }
44
- else if ( playerSelection == "Rock" && computerSelection == "Scissors" ) {
45
- ++ playerScore ;
46
- }
47
- else if ( playerSelection == "Paper" && computerSelection == "Rock" ) {
48
- ++ playerScore ;
49
- }
50
- else if ( playerSelection == "Paper" && computerSelection == "Scissors" ) {
51
- ++ computerScore ;
52
- }
53
- else if ( playerSelection == "Scissors" && computerSelection == "Rock" ) {
54
- ++ computerScore ;
55
- }
56
- else if ( playerSelection == "Scissors" && computerSelection == "Paper" ) {
57
- ++ playerScore ;
58
- }
59
- alert ( playRound ( playerSelection , computerSelection ) ) ;
60
- alert ( "Your score: " + playerScore + ", Computer Score: " + computerScore ) ;
61
- if ( playerScore == 5 ) {
62
- break ;
63
- }
64
- else if ( computerScore == 5 ) {
65
- break ;
66
- }
20
+ else if ( ( playerSelection == "Rock" && computerSelection == "Scissors" ) ||
21
+ ( playerSelection == "Paper" && computerSelection == "Rock" ) ||
22
+ ( playerSelection == "Scissors" && computerSelection == "Paper" ) ) {
23
+ container1 . textContent = "You WIN! " + playerSelection + " beats " + computerSelection + "!" ;
24
+ playerScore ++ ;
67
25
}
68
- if ( playerScore == 5 ) {
69
- alert ( "You WIN! You got 5 score" )
70
- } else { alert ( "You LOSE! Computer got 5 score" ) }
71
- }
26
+ container2 . textContent = "Player Score: " + playerScore ;
27
+ container3 . textContent = "Computer Score: " + computerScore ;
28
+ if ( computerScore == 5 ) {
29
+ alert ( "You LOSE the game! Please try AGAIN!" ) ;
30
+ computerScore = 0 ;
31
+ playerScore = 0 ;
32
+ }
33
+ else if ( playerScore == 5 ) {
34
+ alert ( "You WIN the game! You can play AGAIN!" ) ;
35
+ computerScore = 0 ;
36
+ playerScore = 0 ;
37
+ }
38
+ }
39
+
40
+ let playerScore = 0 ;
41
+ let computerScore = 0 ;
72
42
73
43
const btn1 = document . querySelector ( "#rock" ) ;
74
44
const btn2 = document . querySelector ( "#paper" ) ;
75
45
const btn3 = document . querySelector ( "#scissors" ) ;
76
- const container = document . querySelector ( "#container" ) ;
46
+ const container1 = document . getElementById ( "container1" ) ;
47
+ const container2 = document . getElementById ( "container2" ) ;
48
+ const container3 = document . getElementById ( "container3" ) ;
77
49
78
50
btn1 . addEventListener ( "click" , ( ) => {
79
51
playerSelection = "Rock" ;
80
52
computerSelection = computerPlay ( ) ;
81
- container . textContent = playRound ( playerSelection , computerSelection ) ;
53
+ playRound ( playerSelection , computerSelection ) ;
82
54
} ) ;
83
55
84
56
btn2 . addEventListener ( "click" , ( ) => {
85
57
playerSelection = "Paper" ;
86
58
computerSelection = computerPlay ( ) ;
87
- container . textContent = playRound ( playerSelection , computerSelection ) ;
59
+ playRound ( playerSelection , computerSelection ) ;
88
60
} ) ;
89
61
90
62
btn3 . addEventListener ( "click" , ( ) => {
91
63
playerSelection = "Scissors" ;
92
64
computerSelection = computerPlay ( ) ;
93
- container . textContent = playRound ( playerSelection , computerSelection ) ;
65
+ playRound ( playerSelection , computerSelection ) ;
94
66
} ) ;
0 commit comments