-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
147 lines (137 loc) · 4.43 KB
/
code.js
File metadata and controls
147 lines (137 loc) · 4.43 KB
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
var DECK_id = null;
var current_score = 0;
var bot_score = 0;
// get_deck() returns a promise that resolves to the deck id
async function get_deck() {
if (DECK_id) {
return DECK_id;
}
const response = await fetch(
"https://deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1"
);
const data = await response.json();
DECK_id = data.deck_id;
return DECK_id;
}
// get_card(number) returns a promise that resolves to the card
async function get_card(number) {
const deck_id = await get_deck();
const response = await fetch(
`https://deckofcardsapi.com/api/deck/${deck_id}/draw/?count=${number}`
);
const data = await response.json();
for (let i = 0; i < data.cards.length; i++) {
console.log(data.cards[i].value);
}
}
// get card image with number parameter and add it to the page
async function get_card_image(number) {
const deck_id = await get_deck();
const response = await fetch(
`https://deckofcardsapi.com/api/deck/${deck_id}/draw/?count=${number}`
);
const data = await response.json();
for (let i = 0; i < data.cards.length; i++) {
if (data.cards[i].value === "JACK") data.cards[i].value = 10;
if (data.cards[i].value === "QUEEN") data.cards[i].value = 10;
if (data.cards[i].value === "KING") data.cards[i].value = 10;
if (data.cards[i].value === "ACE") data.cards[i].value = 11;
current_score = current_score + parseInt(data.cards[i].value);
const card_image = data.cards[i].image;
const img = document.createElement("img");
img.src = card_image;
document.body.appendChild(img);
}
change_h1();
await check_result();
}
// change h1 text to "blackjack, your score is : current_score"
async function change_h1() {
const h1 = document.querySelector("h1");
h1.innerText = `Blackjack, your score is : ${current_score}`;
}
// check_result() checks if the player has won or lost change title to "you won" or "you lost"
async function check_result() {
if (current_score > 21) {
const h1 = document.querySelector("h1");
h1.innerText = `You lost, you got ${current_score}`;
remove_buttons();
reload();
}
if (current_score === 21) {
const h1 = document.querySelector("h1");
h1.innerText = `You won, you got blackjack`;
remove_buttons();
reload();
}
}
// bot() is the bot that plays against the player
async function bot() {
while (bot_score < 17) {
const deck_id = await get_deck();
const response = await fetch(
`https://deckofcardsapi.com/api/deck/${deck_id}/draw/?count=1`
);
const data = await response.json();
if (data.cards[0].value === "JACK") data.cards[0].value = 10;
if (data.cards[0].value === "QUEEN") data.cards[0].value = 10;
if (data.cards[0].value === "KING") data.cards[0].value = 10;
if (data.cards[0].value === "ACE") data.cards[0].value = 11;
bot_score = bot_score + parseInt(data.cards[0].value);
// show the bot card on the page before breaking one line only
const card_image = data.cards[0].image;
const img = document.createElement("img");
img.src = card_image;
document.body.appendChild(img);
}
if (bot_score > 21) {
const h1 = document.querySelector("h1");
h1.innerText = `You won, the bot got ${bot_score}`;
remove_buttons();
reload();
}
if (bot_score > current_score && bot_score < 21) {
const h1 = document.querySelector("h1");
h1.innerText = `You lost, the bot got ${bot_score}`;
remove_buttons();
reload();
}
if (bot_score < current_score) {
const h1 = document.querySelector("h1");
h1.innerText = `You won, the bot got ${bot_score}`;
remove_buttons();
reload();
}
if (bot_score === current_score) {
const h1 = document.querySelector("h1");
h1.innerText = `You tied, the bot got ${bot_score}`;
remove_buttons();
reload();
}
}
// stop function that add a horizontal divider an h2 "the bot :" and start bot()
async function stop() {
const hr = document.createElement("hr");
const h2 = document.createElement("h2");
h2.innerText = "The bot :";
document.body.appendChild(hr);
document.body.appendChild(h2);
await bot();
}
async function reload() {
setTimeout(function () {
location.reload();
}, 2000);
}
// remove every input type button in html
async function remove_buttons() {
const buttons = document.querySelectorAll("button");
for (let i = 0; i < buttons.length; i++) {
buttons[i].style.display = "none";
}
}
async function main() {
await get_deck();
get_card_image(2);
}
main();