-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblackJack.js
67 lines (62 loc) · 2.07 KB
/
blackJack.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
const card = [];
let sumCard =0;
let hasblackjack=false;
let isAlive = true;
let message="";
let cardEl = document.querySelector("#Card-el")
let player = {
playerName :"jeetu",
playerChips :122
}
let playerEl = document.getElementById("player-el")
playerEl.textContent = player.playerName + ": Rs" + player.playerChips
function getRendomCard() {
let floorNumber1 = Math.floor(Math.random() * 13) + 1;
if(floorNumber1 == 1){
return 11
}
else if(floorNumber1 == 11 || floorNumber1 == 12 || floorNumber1 == 13){
return 10
}
return floorNumber1;
}
function startGame() {
let firstCard = getRendomCard();
let secondCard= getRendomCard();
card = [firstCard , secondCard];
sumCard =firstCard+secondCard;
renderGame();
}
function renderGame(){
// document.getElementById("sum-el").textContent = "sum : " + sumCard;
//document.querySelector("#Card-el").textContent = "Card : " + card[0] + " " + card[1]
cardEl.textContent = "Card : "
for(let i = 0; i < card.length; i++){
cardEl.textContent += card[i] + " "
}
document.querySelector("#sum-el").textContent = "sum : " + sumCard
if(sumCard<21){
message = "Do you want to draw a new card ?";
}else if(sumCard == 21){
message = "Wohoo! you've got Backjack !";
hasblackjack= true;
}else if(sumCard>21){
message = "You're out of the game";
isAlive=false;
}else{
console.log("sorry ");
}
document.getElementById("message-el").textContent = message;
// console.log(message);
}
function newCard() {
//console.log("Drawing a new card from the deck");
if(hasblackjack === false && isAlive === true){
firstCard =getRendomCard();
secondCard = getRendomCard();
card.push(firstCard);
card.push(secondCard);
sumCard = firstCard + secondCard;
renderGame();
}
}