-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript-ui.js
177 lines (148 loc) · 4.26 KB
/
script-ui.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
175
176
177
/*=============
UI
=============*/
// - BUTTONS -
const rockBtn = document.querySelector(".rock.emoji");
const paperBtn = document.querySelector(".paper.emoji");
const scissorsBtn = document.querySelector(".scissors.emoji");
// - TEXT AND ICON -
const scoreInfo = document.querySelector(".main h2");
const scoreResult = document.querySelector(".main h3");
const playerEmoji = document.querySelector(".emoji.player");
const computerEmoji = document.querySelector(".emoji.computer");
const scorePlayer = document.querySelector("#score-player");
const scoreComputer = document.querySelector("#score-computer");
// - MODAL -
const modal = document.querySelector("#modal");
const modalResult = document.querySelector("#result");
const btnAgain = document.querySelector("#again");
// Click e succedono cose
rockBtn.addEventListener("click", () => fiveGame("rock"));
paperBtn.addEventListener("click", () => fiveGame("paper"));
scissorsBtn.addEventListener("click", () => fiveGame("scissors"));
btnAgain.addEventListener("click", newGame);
window.addEventListener("keydown", noEscape);
/*============
FUNCTIONS
============*/
// Global Score
let playerScore = 0;
let computerScore = 0;
// Non si può uscire dal game over
function noEscape(e) {
if (e.keyCode === 27) {
e.preventDefault();
}
}
// 5 Partite
function fiveGame(player) {
if (player === "rock") {
game("rock");
} else if (player === "paper") {
game("paper");
} else {
game("scissors");
}
if (playerScore === 5 || computerScore === 5) {
stop();
}
}
// New Game
function newGame() {
scoreInfo.innerText = "Rock, Paper or Scissors?";
scoreResult.innerText = "First to score 5 points wins the game";
playerEmoji.innerText = "❔";
computerEmoji.innerText = "❔";
scorePlayer.innerText = "0";
scoreComputer.innerText = "0";
modal.close();
playerScore = 0;
computerScore = 0;
}
// Stop all
function stop() {
if (playerScore === 5) {
modalResult.innerText = "You Won!";
} else if (computerScore === 5) {
modalResult.innerText = "You lost...";
}
modal.showModal();
}
// One game
function game(player) {
let computer = computerPlay();
switch (player) {
case "rock":
playerEmoji.innerText = "👊🏻";
if (computer === "rock") {
tie("Rock", "Rock");
computerEmoji.innerText = "👊🏻";
} else if (computer === "paper") {
lose("Rock", "Paper");
computerEmoji.innerText = "✋🏻";
} else if (computer === "scissors") {
win("Rock", "Scossors");
computerEmoji.innerText = "✌🏻";
}
break;
case "paper":
playerEmoji.innerText = "✋🏻";
if (computer === "rock") {
win("Paper", "Rock");
computerEmoji.innerText = "👊🏻";
} else if (computer === "paper") {
tie("Paper", "Paper");
computerEmoji.innerText = "✋🏻";
} else if (computer === "scissors") {
lose("Paper", "Scissors");
computerEmoji.innerText = "✌🏻";
}
break;
case "scissors":
playerEmoji.innerText = "✌🏻";
if (computer === "rock") {
lose("Scissors", "Rock");
computerEmoji.innerText = "👊🏻";
} else if (computer === "paper") {
win("Scissors", "Paper");
computerEmoji.innerText = "✋🏻";
} else if (computer === "scissors") {
tie("Scissors", "Scissors");
computerEmoji.innerText = "✌🏻";
}
break;
}
}
// What appen when you choose?
function win(player, computer) {
scoreInfo.innerText = "You won!";
scoreResult.innerText = `${player} beats ${computer}`;
playerScore++;
scorePlayer.innerText = playerScore;
}
function tie(player, computer) {
scoreInfo.innerText = "It's a tie!";
scoreResult.innerText = `${player} ties with ${computer}`;
}
function lose(player, computer) {
scoreInfo.innerText = "You lost!";
scoreResult.innerText = `${player} is beaten by ${computer}`;
computerScore++;
scoreComputer.innerText = computerScore;
}
// Scelta Random del computer
function computerPlay() {
let random = Math.floor(Math.random() * 3);
result = "";
switch (random) {
case 0:
result = "rock";
break;
case 1:
result = "paper";
break;
default:
result = "scissors";
}
return result;
}