-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
150 lines (129 loc) · 3.75 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
148
149
150
// TODOs:
//Fix not showing when player has 5 points
//Game state
let playerScore = 0;
let computerScore = 0;
const scoreLimit = 5;
//Showing game state
const gameArea = document.querySelector('.results-area');
const buttons = document.querySelector('.button-container');
buttons.addEventListener('click', (event) => {
//which button is clicked
let choice = event.target.name;
playRound(choice, getComputerChoice());
});
function announceWinner (winner) {
alert(`${winner} wins!`);
return;
}
function resetGame() {
playerScore = 0;
computerScore = 0;
gameArea.textContent = '';
}
function updateScore (roundResult) {
gameArea.textContent =`Round result: ${roundResult}, current score is:
Player: ${playerScore}
Computer: ${computerScore}`;
}
// Randomly select a move for computer
function getComputerChoice() {
const randomChoice = Math.floor(Math.random() * 3);
if (randomChoice === 0) {
return 'rock';
}
else if (randomChoice === 1) {
return 'paper';
}
else if (randomChoice === 2) {
return 'scissors';
}
};
function getPlayerChoice() {
//prompt for input
let playerChoice;
while (!playerChoice) {
let playerInput = prompt('Enter r for ROCK, p for PAPER, s for SCISSORS');
// allow user to escape out of input loop
//TODO: Stop returning undefined if player escapes out
if (playerInput === null || playerInput === '') {
break;
}
switch (playerInput) {
case 'r':
playerChoice = 'rock';
break;
case 'p':
playerChoice = 'paper';
break;
case 's':
playerChoice = 'scissors';
break;
}
}
return playerChoice;
}
function playRound (playerSelection, computerSelection) {
// take player inputs and return string that declares round winner and player choices
//For any given player selection define win, loss and tie
//If Tie - replay round
let roundResult;
playerSelection = playerSelection.toLowerCase();
// check player selection against computer selection and determine winner
if (playerSelection === 'rock') {
switch (computerSelection) {
case 'rock':
roundResult = 'tie';
break;
case 'paper':
roundResult = 'loss';
break;
case 'scissors':
roundResult = 'win';
break;
}
}
else if (playerSelection ==='paper') {
switch (computerSelection) {
case 'rock':
roundResult = 'win';
break;
case 'paper':
roundResult = 'tie';
break;
case 'scissors':
roundResult = 'loss';
break;
}
}
else if (playerSelection ==='scissors') {
switch (computerSelection) {
case 'rock':
roundResult = 'loss';
break;
case 'paper':
roundResult = 'win';
break;
case 'scissors':
roundResult = 'tie';
break;
}
}
console.log (`player chose ${playerSelection}, computer chose ${computerSelection}`)
if (roundResult === 'win') {
playerScore++;
}
else if (roundResult === 'loss') {
computerScore ++;
}
updateScore(roundResult);
if (playerScore >= scoreLimit) {
announceWinner('Player');
return resetGame();
}
else if (computerScore >= scoreLimit) {
announceWinner('Computer');
return resetGame();
}
return;
}