-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
183 lines (171 loc) · 5.06 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
175
176
177
178
179
180
181
182
183
$(document).ready(function() {
let me = 0;
let ai = 0;
$("#rock").on("click", function(e) {
letMeClear();
e.preventDefault();
$(".fresult").html(``);
let choice = "Rock";
if (judge(choice, aiChoice()) === "ME") {
$("#rockk").css({ "border": "8px solid green", "border-radius": "95px" });
$("#scissorss").css({ "border": "8px solid red", "border-radius": "95px" });
me++;
$(".result").html(`
<h1 class="display-3"> You ${me} : ${ai} AI </h1>
`);
} else if (judge(choice, aiChoice()) === "Tie") {
$("#rockk").css({ "border": "8px solid blue", "border-radius": "95px" });
$(".result").html(`
<h1 class="display-3"> You ${me} : ${ai} AI </h1>
<h1 class="h5">Tie</h1>
`);
} else {
$("#rockk").css({ "border": "8px solid red", "border-radius": "95px" });
$("#paperr").css({ "border": "8px solid green", "border-radius": "95px" });
ai++;
$(".result").html(`
<h1 class="display-3"> You ${me} : ${ai} AI </h1>
`);
}
checkWinner(me, ai);
});
$("#paper").on("click", function(e) {
letMeClear();
e.preventDefault();
$(".fresult").html(``);
let choice = "Paper";
if (judge(choice, aiChoice()) === "ME") {
$("#paperr").css({ "border": "8px solid green", "border-radius": "95px" });
$("#rockk").css({ "border": "8px solid red", "border-radius": "95px" });
me++;
$(".result").html(`
<h1 class="display-3"> You ${me} : ${ai} AI</h1>
`);
} else if (judge(choice, aiChoice()) === "Tie") {
$("#paperr").css({ "border": "8px solid blue", "border-radius": "95px" });
$(".result").html(`
<h1 class="display-3"> You ${me} : ${ai} AI </h1>
<h1 class="h5">Tie</h1>
`);
} else {
$("#paperr").css({ "border": "8px solid red", "border-radius": "95px" });
$("#scissorss").css({
"border": "8px solid green",
"border-radius": "95px"
});
ai++;
$(".result").html(`
<h1 class="display-3"> You ${me} : ${ai} AI </h1>
`);
}
checkWinner(me, ai);
});
$("#scissors").on("click", function(e) {
letMeClear();
$(".fresult").html(``);
e.preventDefault();
let choice = "Scissors";
if (judge(choice, aiChoice()) === "ME") {
$("#scissorss").css({
"border": "8px solid green",
"border-radius": "95px"
});
$("#paperr").css({ "border": "8px solid red", "border-radius": "95px" });
me++;
$(".result").html(`
<h1 class="display-3"> You ${me} : ${ai} AI</h1>
`);
} else if (judge(choice, aiChoice()) === "Tie") {
$("#scissorss").css({
"border": "8px solid blue",
"border-radius": "95px"
});
$(".result").html(`
<h1 class="display-3"> You ${me} : ${ai} AI </h1>
<p class="h3">Tie</p>
`);
} else {
$("#scissorss").css({ "border": "8px solid red", "border-radius": "95px" });
$("#rockk").css({ "border": "8px solid green", "border-radius": "95px" });
ai++;
$(".result").html(`
<h1 class="display-3"> You ${me} : ${ai} AI</h1>
`);
}
checkWinner(me, ai);
});
let winsound = new Audio('/src/sound/win.mp3');
let losesound = new Audio('/src/sound/lose.mp3');
let checkWinner = function(first, second) {
if (first == 3) {
$(".fresult").html(`
<h1 class="display-1 won">You WON </h1>
<p class="lead"> Click your next choice to start game again </p>
`);
me = 0;
ai = 0;
winsound.play();
} else if (second == 3) {
$(".fresult").html(`
<h1 class="display-1 lost">You Lost </h1>
<p class="lead"> Click your next choice to start game again </p>
`);
me = 0;
ai = 0;
losesound.play();
}
};
let letMeClear = function() {
$("#rockk").css({ border: "none" });
$("#scissorss").css({ border: "none" });
$("#paperr").css({ border: "none" });
winsound.pause();
losesound.pause()
};
});
let judge = function(me, ai) {
switch ((me, ai)) {
case ("Rock", "Rock"):
return "Tie";
break;
case ("Rock", "Scissors"):
return "ME";
break;
case ("Rock", "Paper"):
return "Ai";
break;
case ("Peper", "Paper"):
return "Tie";
break;
case ("Peper", "Scissors"):
return "AI";
break;
case ("Peper", "Rock"):
return "ME";
break;
case ("Scissors", "Paper"):
return "ME";
break;
case ("Scissors", "Scissors"):
return "TIE";
break;
case ("Scissors", "Rock"):
return "AI";
break;
default:
return "You Break IT";
}
};
let aiChoice = function() {
let random = Math.random();
switch (true) {
case random < 0.333:
return "Rock";
break;
case random < 0.677:
return "Paper";
break;
default:
return "Scissors";
}
};