-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
174 lines (150 loc) · 4.81 KB
/
main.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"use strict";
const score = [0, 0];
let playerMove = 0;
const playerScore = document.querySelector(".score-0");
const computerScore = document.querySelector(".score-1");
const btnRock = document.querySelector(".btnRock");
const btnPaper = document.querySelector(".btnPaper");
const btnScissor = document.querySelector(".btnScissor");
const optionsBox = document.querySelector(".options-box");
const displayMove = document.querySelector(".display-move");
const playerMoveImage = document.querySelector(".playerMoveImage");
const computerMoveImage = document.querySelector(".computerMoveImage");
const resultSection = document.querySelector(".result");
const btnContinue = document.querySelector(".btn-continue");
const msg = document.querySelector(".msg");
const newGame = document.querySelector(".btn-new-game");
const modal = document.querySelector(".modal");
const modalClose = document.querySelector(".modal-close");
const overlay = document.querySelector(".overlay");
const helpBtn = document.querySelector(".btn-help");
function init() {
playerScore.textContent = score[0];
computerScore.textContent = score[1];
}
function generateComputerMove() {
const computerMove = Math.floor(Math.random() * 3);
return computerMove;
}
function checkWinner(userMove, opponentMove) {
// 0 => Draw, 1 => Player, 2 => Opponent
// Main Logic of the Game
// Rock = 0, Paper = 1, Scissor = 2
// Rock vs Scissor, Paper vs Rock, Scissor vs Paper
if (
(userMove == 0 && opponentMove == 2) ||
(userMove == 1 && opponentMove == 0) ||
(userMove == 2 && opponentMove == 1)
) {
// Player wins
score[0] = score[0] + 1;
return 1;
} else if (
(userMove == 2 && opponentMove == 0) ||
(userMove == 0 && opponentMove == 1) ||
(userMove == 1 && opponentMove == 2)
) {
// Opponent wins
score[1] = score[1] + 1;
return 2;
} else {
// Draw
return 0;
}
}
function showMessage(message, textColor) {
msg.textContent = message;
msg.style.color = textColor;
}
function displayResult(winner) {
// Showing Result and Continue button
resultSection.classList.remove("hidden");
btnContinue.classList.remove("hidden");
if (winner == 1) {
showMessage("You won the move!", "#159b35");
} else if (winner == 2) {
showMessage("Opponent won the move.", "#9b111e");
} else {
showMessage("The move has drawn.", "#ffbb31");
}
}
function displayPlayersMove(user, move) {
if (move == 0) {
user == 0
? (playerMoveImage.src = "img/rock.png")
: (computerMoveImage.src = "img/rock.png");
} else if (move == 1) {
user == 0
? (playerMoveImage.src = "img/paper.png")
: (computerMoveImage.src = "img/paper.png");
} else {
user == 0
? (playerMoveImage.src = "img/scissor.png")
: (computerMoveImage.src = "img/scissor.png");
}
}
function checkIfGameOver() {
if (score[0] == 10) {
showMessage("Congratulations! You have won the game!", "#159b35");
btnContinue.classList.add("hidden");
} else if (score[1] == 10) {
showMessage("Unfortunately! You have lost the game.", "#9b111e");
btnContinue.classList.add("hidden");
}
}
function showMove(playerMove) {
const userMove = playerMove;
const opponentMove = generateComputerMove();
optionsBox.classList.add("hidden");
displayMove.classList.remove("hidden");
const winner = checkWinner(userMove, opponentMove);
init();
displayResult(winner);
// Displaying user move image
displayPlayersMove(0, userMove);
// Displaying opponent move image
displayPlayersMove(1, opponentMove);
checkIfGameOver();
}
function showMoveOptions() {
resultSection.classList.add("hidden");
btnContinue.classList.add("hidden");
optionsBox.classList.remove("hidden");
displayMove.classList.add("hidden");
}
function showModal() {
modal.classList.remove("hidden");
overlay.classList.remove("hidden");
}
function hideModal() {
modal.classList.add("hidden");
overlay.classList.add("hidden");
}
init();
btnRock.addEventListener("click", function () {
playerMove = 0;
showMove(playerMove);
});
btnPaper.addEventListener("click", function () {
playerMove = 1;
showMove(playerMove);
});
btnScissor.addEventListener("click", function () {
playerMove = 2;
showMove(playerMove);
});
btnContinue.addEventListener("click", showMoveOptions);
newGame.addEventListener("click", function () {
score[0] = 0;
score[1] = 0;
init();
showMoveOptions();
});
helpBtn.addEventListener("click", showModal);
modalClose.addEventListener("click", hideModal);
overlay.addEventListener("click", hideModal);
document.addEventListener("keydown", function (e) {
if (e.key === "Escape" && !modal.classList.contains("hidden")) {
hideModal();
}
});