Skip to content

Commit 9fe76d8

Browse files
This is Black Jack game where if a person gets a sum of 21 by randomly picking cards, he/she wins the bet
1 parent af5a0b3 commit 9fe76d8

File tree

4 files changed

+150
-0
lines changed

4 files changed

+150
-0
lines changed
47.8 KB
Loading

blackJack/index.css

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
body{
2+
position: relative;
3+
font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif;
4+
display: flex;
5+
flex-direction: column;
6+
background-size: cover;
7+
width: 500px;
8+
height: 400px;
9+
align-items: center;
10+
justify-content: center;
11+
}
12+
13+
img{
14+
position: absolute;
15+
z-index: -10;
16+
height: 400px;
17+
width: 500px;
18+
}
19+
20+
h1{
21+
margin-top: 0px;
22+
padding-top: 20px;
23+
color: goldenrod;
24+
font-weight: bold;
25+
}
26+
27+
p{
28+
color: white;
29+
font-weight: bold;
30+
}
31+
32+
.italic{
33+
font-style: italic;
34+
}
35+
36+
button{
37+
color: #045127;
38+
background-color: goldenrod;
39+
width: 150px;
40+
height: 40px;
41+
padding: 5px 10px;
42+
margin-bottom: 10px;
43+
border: 1px solid #045127;
44+
font-weight: bold;
45+
border-radius: 5px;
46+
}

blackJack/index.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<link rel="stylesheet" href="index.css">
8+
<title>Black Jack</title>
9+
</head>
10+
<body>
11+
<img src="backgroundImage/casino table.jpg" alt="">
12+
<h1>Blackjack</h1>
13+
<p id="message-el" class="italic">Want to play a round?</p>
14+
<p id="cards-el">Cards:</p>
15+
<p id="sum-el">Sum:</p>
16+
<button onclick="startGame()">Start Game</button>
17+
<button onclick="newCard()">New Card</button>
18+
<p id="player-el"></p>
19+
<script src="index.js"></script>
20+
</body>
21+
</html>

blackJack/index.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
let cards = []
2+
3+
let sum = 0
4+
let hasBlackJack = false;
5+
let isAlive = false;
6+
let message = '';
7+
8+
let messageEl = document.getElementById('message-el')
9+
let sumEl = document.getElementById('sum-el')
10+
let cardsEl = document.getElementById('cards-el');
11+
let playerEl = document.getElementById('player-el')
12+
13+
let player = {
14+
name: 'Sarfraz',
15+
chips: '150'
16+
}
17+
18+
playerEl.textContent = player.name + ': $' + player.chips
19+
20+
function getRandonCard(){
21+
22+
let randomNumber = Math.floor( Math.random()*13 ) + 1;
23+
24+
if(randomNumber === 1){
25+
randomNumber = 11;
26+
}
27+
else if(randomNumber > 10){
28+
randomNumber = 10;
29+
}
30+
else{
31+
randomNumber;
32+
}
33+
34+
return randomNumber;
35+
36+
}
37+
38+
function startGame(){
39+
let firstCard = getRandonCard();
40+
let secondCard = getRandonCard();
41+
isAlive= true;
42+
43+
sum= firstCard + secondCard;
44+
cards = [firstCard, secondCard];
45+
46+
renderGame();
47+
}
48+
49+
function renderGame(){
50+
51+
cardsEl.textContent = 'Cards: ';
52+
for(let i=0; i<cards.length; i++){
53+
cardsEl.textContent += cards[i]+ ' ';
54+
}
55+
56+
sumEl.textContent = 'Sum: ' + sum;
57+
58+
if(sum <21){
59+
message ='Do you want to draw a card ?'
60+
}
61+
62+
else if(sum ===21){
63+
message = 'You have won the game'
64+
hasBlackJack = true;
65+
}
66+
67+
else{
68+
message = 'You have lost the game'
69+
isAlive = false;
70+
}
71+
72+
messageEl.textContent = message;
73+
74+
}
75+
76+
function newCard(){
77+
if(isAlive === true && hasBlackJack=== false){
78+
let card = getRandonCard();
79+
sum += card;
80+
cards.push(card);
81+
renderGame();
82+
}
83+
}

0 commit comments

Comments
 (0)