-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
147 lines (125 loc) · 4.06 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
function computerPlay(){
var result = Math.floor(Math.random() * 10);
if( result <= 3) {
return 'rock';
}
else if(result <= 6) {
return 'paper';
}
else {
return 'scissors';
}
}
function playRound(playerSelection, computerSelection) {
switch(playerSelection) {
case 'rock':
console.log("player chose rock");
if(computerSelection === 'rock') {
return 0; //tie
}
else if(computerSelection === 'paper') {
return -1; //CPU wins
}
else {
return 1; //You win
}
break;
case 'paper':
if(computerSelection === 'rock') {
return 1; //You win
}
else if(computerSelection === 'paper') {
return 0; //tie
}
else {
return -1; //CPU wins
}
break;
case 'scissors':
if(computerSelection === 'rock') {
return -1; //CPU wins
}
else if(computerSelection === 'paper') {
return 1; //You win
}
else {
return 0; //tie
}
break;
default:
return "something broke!";
}
}
// function playerPlay(){
// let message = "Rock, Paper, or Scissors?";
// while(true) {
// let result = prompt(message);
// console.log("result is " + result)
// if ( (result.toLowerCase() != "rock") && (result.toLowerCase() != "paper") && (result.toLowerCase() != "scissors")) {
// message = "Invalid response. Response must be 'rock', 'paper', or 'scissors'.";
// }
// else {
// return result;
// }
// }
// }
// function game() {
// let playerScore = 0;
// let computerScore = 0;
// for (let i = 0; i < 5; i++) {
// let playerSelection = playerPlay();
// let computerSelection = computerPlay();
// result = playRound(playerSelection, computerSelection);
// switch(result){
// case -1:
// computerScore++;
// console.log("You lose! " + computerSelection + " beats " + playerSelection + ".");
// break;
// case 0:
// console.log("It's a tie!");
// break;
// case 1:
// playerScore++;
// console.log("You win! " + playerSelection + " beats " + computerSelection + ".");
// }
// }
// console.log("Final Score: Player - " + playerScore + ", Computer - " + computerScore);
// }
let playerScore = 0;
let computerScore = 0;
const buttons = document.querySelectorAll('button');
const score = document.querySelector('.score');
const resultsText = document.querySelector('.results');
console.log(buttons);
function updateScore(result, playerSelection, computerSelection) {
switch(result) {
case -1:
computerScore++;
resultsText.textContent = ("You lose! " + computerSelection + " beats " + playerSelection + ".");
break;
case 0:
resultsText.textContent = ("It's a tie!");
break;
case 1:
playerScore++;
resultsText.textContent = ("You win! " + playerSelection + " beats " + computerSelection + ".");
}
score.textContent = "Player : " + playerScore + " Computer: " + computerScore;
if (playerScore >= 5) {
resultsText.textContent = ("It's over! Player is first to get to 5!");
}
else if (computerScore >= 5) {
resultsText.textContent = ("OOF! CPU is first to get to 5!");
}
}
buttons.forEach((button) => {
button.addEventListener('click', () => {
let computerSelection = computerPlay();
let result = playRound(button.id, computerSelection);
updateScore(result, button.id, computerSelection);
});
});
//const playerSelection = playerPlay();
//const computerSelection = computerPlay();
//console.log(playRound(playerSelection, computerSelection));
//game();