-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
142 lines (133 loc) · 3.95 KB
/
script.js
File metadata and controls
142 lines (133 loc) · 3.95 KB
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
const MAXSCORE = 3;
//variables
const actions = ["Rock", "Paper", "Scissors"]
var playerScore = 0;
var computerScore = 0;
var currentRound = 0;
/**
* Tells the user that the previous game was over and starts a new game
*
* sets the score values to 0
*/
function newGame(){
const roundResult = document.querySelector(".current-round");
if(playerScore == MAXSCORE){
roundResult.innerText = "Player WON! Click any button to start a new GAME"
}
if(computerScore == MAXSCORE){
roundResult.innerText = "Computer WON! Click any button to start a new GAME"
}
playerScore = 0, computerScore = 0, currentRound=0;
}
/**
* updates player score when they win a round
*/
function playerWon(){
playerScore += 1;
}
/**
* updates computer score when they win a round
*/
function computerWon(){
computerScore += 1;
}
//Function To generate a random computer choice from [rock, paper, scissors]
function getComputerChoice(){
var rand = Math.floor(Math.random()*3)
return actions[rand];
}
/**
* Returns the result of 1 round of rps game
*
* @param {string} playerSelection, the player choice
* @param {string} computerSelection, the computer random selection
* @return {string} a string indicating whether the player or the computer won the the game
*/
function playRound(playerSelection, computerSelection){
var player = playerSelection.toLowerCase();
var computer = computerSelection.toLowerCase();
var flag = 0;
switch(player){
case "rock":
switch(computer){
case "scissors":
flag = 1;
break;
case "paper":
flag = 2;
break;
default:
flag = 0;
}
break;
case "paper":
switch(computer){
case "rock":
flag = 1;
break;
case "scissors":
flag = 2;
break;
default:
flag = 0;
}
break;
case "scissors":
switch(computer){
case "paper":
flag = 1;
break;
case "rock":
flag = 2;
break;
default:
flag = 0;
}
break;
default:
flag = -1;
}
switch (flag){
case 1:
playerWon();
return "you win! " + playerSelection + " beats " + computerSelection;
break;
case 2:
computerWon();
console.log(computerScore);
return "you Lose! " + computerSelection + " beats " + playerSelection;
break;
default:
return "TIE! Both Choices were "+ computerSelection ;
}
}
// const playerSelection = "Rock";
//adds event listener to each button and runs the game after the player presses a button
document.querySelectorAll(".button").forEach(button=>{
button.addEventListener('click', (button)=>{
updateRound(button.target.id);
});
})
/**
* updates the current round string after pressing one of the buttons
*
* @param {string} button, the ID (rock, paper, scissors) of the pressed button
*/
function updateRound(button){
const playerSelection = button;
var computerSelection = getComputerChoice();
const roundResult = document.querySelector(".current-round")
roundResult.innerText = playRound(playerSelection, computerSelection);
updateScores();
}
/**
* updates the Score for both the plyer and Computer
* Calls newGame() if the max score was reached by one of the players
*/
function updateScores(){
document.querySelector(".player-score").innerText = playerScore;
document.querySelector(".computer-score").innerText = computerScore;
if(playerScore == MAXSCORE || computerScore == MAXSCORE){
newGame();
}
}