-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
150 lines (133 loc) · 4.94 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
let computerSelection , playerSelection, result;
let playerScoreCounter = 0;
let computerScoreCounter = 0;
const rockButton = document.querySelector("#rock");
const paperButton = document.querySelector("#paper");
const scissorsButton = document.querySelector("#scissors");
const resetButton = document.querySelector(".reset");
const computerScore = document.querySelector("#computer-score");
const playerScore = document.querySelector("#player-score");
computerScore.textContent = computerScoreCounter;
playerScore.textContent = playerScoreCounter;
rockButton.addEventListener("click",()=> runGame("rock"));
paperButton.addEventListener("click",()=> runGame("paper"));
scissorsButton.addEventListener("click",()=> runGame("scissor"));
resetButton.addEventListener("click", () => resetGame());
function getComputerSelection()
{
let selectionId;
const computerChoice = document.querySelector("#computer-choice");
selectionId = Math.floor((Math.random()*3)+1);
switch (selectionId){
case 1:
computerChoice.textContent = "Rock";
return "rock";
break;
case 2:
computerChoice.textContent = "Paper";
return "paper";
break;
case 3:
computerChoice.textContent = "Scissor";
return "scissor";
break;
}
}
function findWinner(computerChoice, playerChoice)
{
switch (playerChoice)
{
case "rock":
return computerChoice === "scissor"? "win" : computerChoice === "paper" ? "lose" : "draw";
break;
case "paper":
return computerChoice === "rock"? "win" : computerChoice === "scissor" ? "lose" : "draw";
break;
case "scissor":
return computerChoice === "paper"? "win" : computerChoice === "rock" ? "lose" : "draw";
break;
}
}
function setImage (imageBox, selection){
switch (selection) {
case "rock":
imageBox.style.backgroundImage = "url(./images/rock.jpg)";
break;
case "paper":
imageBox.style.backgroundImage = "url(./images/paper.jpg)";
break;
case "scissor":
imageBox.style.backgroundImage = "url(./images/scissor.jpg)";
break;
}
}
function resetGame ()
{
computerScoreCounter = 0;
playerScoreCounter = 0;
computerScore.textContent = computerScoreCounter;
playerScore.textContent = playerScoreCounter;
document.querySelector("#computer-choice").textContent = "";
const playerImage = document.querySelector(".player-image");
const computerImage = document.querySelector(".computer-image");
playerImage.style.backgroundImage = "url(./images/question-mark.jpg)";
computerImage.style.backgroundImage = "url(./images/question-mark.jpg)";
playerImage.style.border = "3px solid white";
computerImage.style.border = "3px solid white";
document.querySelector(".result-text").textContent = "";
rockButton.disabled = false;
paperButton.disabled = false;
scissorsButton.disabled = false;
resetButton.disabled = true;
}
function SetGameOverState()
{
rockButton.disabled = true;
paperButton.disabled = true;
scissorsButton.disabled = true;
resetButton.disabled = false;
}
function runGame(playerSelection)
{
computerSelection = getComputerSelection();
let winner = 3; //
const playerImage = document.querySelector(".player-image");
const computerImage = document.querySelector(".computer-image");
setImage(playerImage,playerSelection);
setImage(computerImage,computerSelection);
result = findWinner(computerSelection,playerSelection);
if(result === "win")
{
console.log ("You win. " + playerSelection + " beats "+ computerSelection);
playerImage.style.border = "3px solid green";
computerImage.style.border = "3px solid red";
playerScoreCounter++;
playerScore.textContent = playerScoreCounter;
}
else if(result === "lose")
{
console.log ("You lose. " + computerSelection + " beats "+ playerSelection);
computerImage.style.border = "3px solid green";
playerImage.style.border = "3px solid red";
computerScoreCounter++;
computerScore.textContent = computerScoreCounter;
}
else
{
console.log ("It's a draw.");
computerImage.style.border = "3px solid white";
playerImage.style.border = "3px solid white";
}
winner = computerScoreCounter === 5 ? 2 : playerScoreCounter === 5 ? 1 : 3;
if(winner === 1)
{
document.querySelector(".result-text").textContent = "You win. Press the Reset button to play again!";
SetGameOverState();
}
else if(winner === 2)
{
document.querySelector(".result-text").textContent = "Computer win. Press the Reset button to play again!";
SetGameOverState();
}
}
//prompt("You chose " + playerSelection + " and computer chose " + computerSelection + ". You "+ result);