-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
335 lines (291 loc) · 10.1 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Frist Challenge:Your age in days
function ageindays()
{
var birthyear=prompt("What is your age...?Batana SUAR!");
var ages=(2020-birthyear)*365;
var h1=document.createElement('h1');
var answer=document.createTextNode("You are " + ages + " Days Old!");
h1.setAttribute('id','ageindays');
h1.appendChild(answer);
document.getElementById("flex-box-result").appendChild(h1);
}
function reset()
{
document.getElementById("ageindays").remove();
}
//Challenge 2:Generate Cat
function generateCat(){
var image=document.createElement("img");
var div=document.getElementById("flex-cat-gen");
image.src="https://cdn2.thecatapi.com/images/34i.gif";
div.appendChild(image);
}
//Challenge 3:Rock,Paper,Scissors
function rpsgame(yourChoice)
{
console.log(yourChoice);
var humanchoice,botchoice;
humanchoice=yourChoice.id;
botchoice=NumberToChoice(randtoRps());
console.log("Computer choice:",botchoice);
results=decideWinner(humanchoice,botchoice);//[0,1] human lost bot wins
console.log(results);
message=finalMessage(results);//{"message":YouWin}
rpsFrontEnd(yourChoice.id,botchoice,message);
}
function randtoRps()
{
return Math.floor(Math.random() * 3);
}
function NumberToChoice(number)
{
return["rock","paper","scissors"][number];
}
function decideWinner(yourChoice,computerChoice)
{
var rpsDatabase={
"rock":{"scissors": 1,"rock":0.5,"paper":0},
"paper":{"rock":1,"paper":0.5,"scissors":0},
"scissors":{"paper":1,"scissors":0.5,"rock":0}
};
var yourScore=rpsDatabase[yourChoice][computerChoice];
var compuerScore=rpsDatabase[computerChoice][yourChoice];
return [yourScore,compuerScore];
}
function finalMessage([yourScore, compuerScore]){
if(yourScore===0)
{
return{"message":"You Lost!","color":"red"};
}
else if(yourScore==="0.5"){
return{"message":"You Tied!","color":"yellow"};
}
else{
return{"message":"You Won!","color":"green"};
}
}
function rpsFrontEnd (humanImageChoice , botImageChoice , finalMessage) {
var imagesDatabase={
"rock":document.getElementById("rock").src,
"paper":document.getElementById("paper").src,
"scissors":document.getElementById("scissors").src
}
//lets remove all the image
document.getElementById("rock").remove();
document.getElementById("paper").remove();
document.getElementById("scissors").remove();
var humanDiv=document.createElement("div");
var botDiv=document.createElement("div");
var messageDiv=document.createElement("div");
humanDiv.innerHTML="<img src='" + imagesDatabase[humanImageChoice] + "' height=150 width=150 style='box-shadow: 0px 10px 50px rgba(37,50,233,1);'>"
messageDiv.innerHTML="<h1 style='color: " + finalMessage['color'] +"; font-size:60px; padding:30px; '>" + finalMessage["message"] + "</h1>"
botDiv.innerHTML="<img src='" + imagesDatabase[botImageChoice] + "' height=150 width=150 style='box-shadow: 0px 10px 50px rgba(243,38,24,1);'>"
document.getElementById("flex-box-rps-div").appendChild(humanDiv);
document.getElementById("flex-box-rps-div").appendChild(messageDiv);
document.getElementById("flex-box-rps-div").appendChild(botDiv);
}
//Challenge 4:Change the color of all button
var all_buttons=document.getElementsByTagName("button");
var copyAllButtons=[];
for(let i=0;i<all_buttons.length; i++)
{
copyAllButtons.push(all_buttons[i].classList[1]);
}
function buttonColorChange(buttonlove)
{
if(buttonlove.value==="red"){
buttonsRed();
}
else if(buttonlove.value==="green"){
buttonsGreen();
}
else if(buttonlove.value==="reset")
{
buttonColorReset();
}
else if(buttonlove.value==="random")
{
randomColors();
}
}
function buttonsRed()
{
for(let i=0;i<all_buttons.length;i++){
all_buttons[i].classList.remove(all_buttons[i].classList[1]);
all_buttons[i].classList.add("btn-danger");
}
}
function buttonsGreen()
{
for(let i=0;i<all_buttons.length;i++){
all_buttons[i].classList.remove(all_buttons[i].classList[1]);
all_buttons[i].classList.add("btn-success");
}
}
function buttonColorReset()
{
for(let i=0;i<all_buttons.length;i++)
{
all_buttons[i].classList.remove(all_buttons[i].classList[1]);
all_buttons[i].classList.add(copyAllButtons[i]);
}
}
function randomColors()
{
let choices=["btn-primary","btn-danger","btn-success","btn-warning"]
for(let i=0;i<all_buttons.length;i++)
{
let randomNumber=Math.floor(Math.random*4);
all_buttons[i].classList.remove(all_buttons[i].classList[1]);
all_buttons[i].classList.add(choices[randomNumber]);
}
}
//Challenge 5:Blackjack
let blackjackgame={
"you":{"scoreSpan":"#your-blackjack-result","div":"#your-box","score":0},
"dealer":{"scoreSpan":"#dealer-blackjack-result","div":"#dealer-box","score":0},
"cards":["2","3","4","5","6","7","8","9","10","K","Q","A","J"],
"cardsMap":{"2": 2,"3": 3,"4":4 ,"5":5,"6":6,"7":7,"8":8,"9":9,"10":10,"K":10,"Q":10,"A":[1,11],"J":10},
"wins":0,
"losses":0,
"draws":0,
"isStand":false,
"turnsOver":false,
};
const YOU=blackjackgame["you"]
const DEALER=blackjackgame["dealer"]
const hitSound=new Audio("sounds/swish.m4a");
const winSound=new Audio("sounds/cash.mp3");
const lossSound=new Audio("sounds/aww.mp3");
document.querySelector("#blackjack-hit").addEventListener("click",blackjackHit);
document.querySelector("#blackjack-stand").addEventListener("click",dealerLogic);
document.querySelector("#blackjack-deal").addEventListener("click",blackjackDeal);
function blackjackHit(){
if(blackjackgame["isStand"]===false){
let card=randomCard();
showCard(card,YOU);
updateScore(card,YOU);
showScore(YOU);
}
}
function randomCard(){
let randomIndex=Math.floor(Math.random()*13);
return blackjackgame['cards'][randomIndex];
}
function showCard(card,activePlayer){
if(activePlayer["score"]<=21){
let cardImage=document.createElement("img");
cardImage.src=`images/${card}.png`;
document.querySelector(activePlayer["div"]).appendChild(cardImage);
hitSound.play();
}
}
function blackjackDeal(){
if(blackjackgame["turnsOver"]===true){
blackjackgame["isStand"]=false;
let yourImages=document.querySelector("#your-box").querySelectorAll("img");
let dealerImages=document.querySelector("#dealer-box").querySelectorAll("img");
for(i=0;i<yourImages.length;i++){
yourImages[i].remove();
}
for(i=0;i<dealerImages.length;i++){
dealerImages[i].remove();
}
YOU["score"]=0;
DEALER["score"]=0;
document.querySelector("#your-blackjack-result").textContent=0;
document.querySelector("#dealer-blackjack-result").textContent=0;
document.querySelector("#your-blackjack-result").style.color="#ffffff";
document.querySelector("#dealer-blackjack-result").style.color="#ffffff";
document.querySelector("#blackjack-result").textContent="Let's Play!";
document.querySelector("#blackjack-result").style.color="black";
blackjackgame["turnsOver"]=true;
}
}
function updateScore(card,activePlayer)
{
if(card==="A"){
//if adding 11 keeps score less than 21 add it otherwise add 1
if(activePlayer["score"] +blackjackgame["cardsMap"][card][1]<=21){
activePlayer["score"]+=blackjackgame["cardsMap"][card][1];
} else{
activePlayer["score"] +=blackjackgame["cardsMap"][card][0];
}
}else{
activePlayer["score"]+=blackjackgame["cardsMap"][card];
}
}
function showScore(activePlayer)
{
if(activePlayer["score"]>21){
document.querySelector(activePlayer["scoreSpan"]).textContent="BUST!";
document.querySelector(activePlayer["scoreSpan"]).style.color="red";
}else{
document.querySelector(activePlayer["scoreSpan"]).textContent=activePlayer["score"];
}
}
function sleep(ms){
return new Promise(resolve=>setTimeout(resolve,ms));
}
async function dealerLogic(){
blackjackgame["isStand"]=true;
while(DEALER["score"]<16 && blackjackgame["isStand"]===true){
let card=randomCard();
showCard(card,DEALER);
updateScore(card,DEALER);
showScore(DEALER);
await sleep(1000);
}
blackjackgame["turnsOver"]=true;
let winner=computeWinner();
showResult(winner);
}
//compute winner and return who just won
function computeWinner(){
let winner;
if(YOU["score"]<=21){
//higher score than dealer or when dealer busts but you don't
if(YOU["score"]>DEALER["score"] || (DEALER["score"]>21)){
blackjackgame["wins"]++;
winner=YOU;
}else if(YOU["score"]<DEALER["score"]){
blackjackgame["losses"]++;
winner=DEALER;
}else if(YOU["score"]===DEALER["score"]){
blackjackgame["draws"]++;
}
//when user busts but dealer doesn't
}else if(YOU["score"]>21 && DEALER["score"]<=21){
blackjackgame["losses"]++;
winner=DEALER;
//when you and the dealer busts
}else if(YOU["score"]>21 && DEALER["score"]>21){
blackjackgame["draws"]++;
}
console.log(blackjackgame);
return winner;
}
function showResult(winner){
let message,messageColor;
if(blackjackgame["turnsOver"]===true){
if(winner===YOU){
document.querySelector("#wins").textContent=blackjackgame["wins"];
message="You Won!";
messageColor="green";
winSound.play();
}
else if(winner===DEALER){
document.querySelector("#losses").textContent=blackjackgame["losses"];
message="You Lost!";
messageColor="red";
lossSound.play();
}
else{
document.querySelector("#draws").textContent=blackjackgame["draws"];
message="You Drew!";
messageColor="black";
}
document.querySelector("#blackjack-result").textContent=message;
document.querySelector("#blackjack-result").style.color=messageColor;
}
}