In: Computer Science
javascript BlackJack
i made a blackjack game, the code is below
//this method will return a shuffled deck
function shuffleDeck() {
//this will store array fo 52 objects
const decks = []
const suits = ['Hearts', 'Clubs', 'Diamonds', 'Spades']
const values = ['Ace', 'King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'One']
//run a loop till 52 times
for (let i = 0; i < 52; i++) {
//push a random item
decks.push({ suit: suits[Math.floor(Math.random() * suits.length)], value: values[Math.floor(Math.random() * values.length)] })
}
return decks
}
//function that will draw four cards form the deck
function draw(decks) {
return {
player: [decks[0], decks[1]],
computer: [decks[2], decks[3]]
}
}
//first shuffle the deck
const shuffled = shuffleDeck()
console.log((shuffled))
//now draw the card
const drawn = draw(shuffled)
console.log("\n\nDrawn Cards :" ,(drawn))
i want to make a node application that will listen for user input. this is what i have so far for it.
function startGame () {
let input;
input = prompt("Welcome to Blackjack. Do you want to play? (Y)es or
(N)o").toUpperCase();
if (input == 'Y') {}
}
i am stuck on how to finish the startGame Function.
Simple HTML Code for the front page and th Buttons:-
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>WELCOME TO BLACKJACK</h1>
<h4>DO YOU WANT TO PLAY?</h4>
<p id="text-area">Welcome to Blackjack!</p>
<button id="new-game-button">Start Game!</button>
<button id="hit-button">Hit!</button>
<button id="stay-button">Stay</button>
<script src="script.js"></script>
</body>
</html>
Javascript Code to connect and run the Game:-
let suits = ['Hearts', 'Clubs', 'Diamonds', 'Spades'];
let values = ['Ace', 'King', 'Queen', 'Jack',
'Ten', 'Nine', 'Eight', 'Seven', 'Six',
'Five', 'Four', 'Three', 'Two', 'One'
];
let textArea = document.getElementById('text-area');
let newGameButton = document.getElementById('new-game-button');
let hitButton = document.getElementById('hit-button');
let stayButton = document.getElementById('stay-button');
hitButton.style.display = 'none';
stayButton.style.display = 'none';
let gameStart = false,
gameOver = false,
playWon = false,
dealerCards = [],
playerCards = [],
dealerScore = 0,
playerScore = 0,
deck = [];
newGameButton.addEventListener('click', function() {
gameStarted = true;
gameOver = false;
playerWon = false;
deck = createDeck();
shuffleDeck(deck);
dealerCards = [getNextCard(), getNextCard()];
playerCards = [getNextCard(), getNextCard()];
newGameButton.style.display = 'none';
hitButton.style.display = 'inline';
stayButton.style.display = 'inline';
showStatus();
})
function createDeck() {
let deck = []
for (let suitIdx = 0; suitIdx < suits.length; suitIdx++) {
for (let valueIdx = 0; valueIdx < values.length; valueIdx++) {
let card = {
suit: suits[suitIdx],
value: values[valueIdx]
}
deck.push(card);
}
}
return deck;
}
function shuffleDeck(deck){
for(let i=0; i<deck.length; i++)
{
let swapIdx = Math.trunc(Math.random() *deck.length);
let tmp = deck[swapIdx];
deck[swapIdx] = deck[i];
deck[i] = tmp;
}
}
hitButton.addEventListener('click', function(){
playerCards.push(getNextCard());
checkForEndOfGame();
showStatus();
});
stayButton.addEventListener('click', function(){
gameOver = true;
checkForEndOfGame();
showStatus();
});
function checkForEndOfGame(){
updateScores();
if(gameOver){
while(dealerScore<playerScore &&
playerScore <=21 &&
dealerScore <=21){
dealerCards.push(getNextCard());
updateScores();
}
}
if(playerScore>21){
playerWon=false;
gameOver = true;
}
else if(dealerScore>21){
playerWon = true;
gameOver = true;
}
else if(gameOver){
if(playerScore>dealerScore){
playerWon = true;
}
else{
playerWon = false;
}
}
}
function getCardString(card) {
return card.value + " of " + card.suit;
}
function getCardNumericValue(card){
switch(card.value){
case 'Ace':
return 1;
case 'Two':
return 2;
case 'Three':
return 3;
case 'Four':
return 4;
case 'Five':
return 5;
case 'Six':
return 6;
case 'Seven':
return 7;
case 'Eight':
return 8;
case 'Nine':
return 9;
default:
return 10;
}
}
function showStatus()
{
if(!gameStarted)
{
textArea.innerText = 'Welcome to Blackjack!';
return;
}
let dealerCardString = '';
for(let i=0; i<dealerCards.length; i++)
{
dealerCardString += getCardString(dealerCards[i]) + '\n';
}
let playerCardString='';
for(let i=0; i<playerCards.length; i++)
{
playerCardString += getCardString(playerCards[i]) + '\n';
}
updateScores();
textArea.innerText = 'Dealer has:\n' +
dealerCardString +
'(score: ' + dealerScore + ')\n\n' +
'Player has:\n' +
playerCardString +
'(score: ' + playerScore + ')\n\n';
if(gameOver){
if(playerWon)
{
textArea.innerText += "YOU WIN!";
}
else{
textArea.innerText += "DEALER WINS";
}
newGameButton.style.display = 'inline';
hitButton.style.display = 'none';
stayButton.style.display = 'none';
}
}
function getScore(cardArray){
let score = 0;
let hasAce = false;
for(let i=0; i<cardArray.length; i++){
let card = cardArray[i];
score += getCardNumericValue(card);
if(card.value == 'Ace'){
hasAce = true;
}
if(hasAce && score+10<=21){
return score+10;
}
}
return score;
}
function updateScores(){
dealerScore = getScore(dealerCards);
playerScore = getScore(playerCards);
}
function getNextCard() {
return deck.shift();
}
Summary:-Run the code mentioned above and your Blackjack Game will be ready.You can style the Game according to your need so didnot provide the CSS part.