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.
HI ,
You can follow a procedure where user gves an input ('Y' ) and then the game starts by shuffling the cards follwed by math.random function .
Your code will shiffle the card every time the game starts.
If the user inputs ('N'), the user exits .
#SAMPLECODESTRUCTURE
#userinput
function startGame () {
let input;
input = prompt("Welcome to Blackjack. Do you want to play? (Y)es or
(N)o").toUpperCase();
if(input=='Y')
{
//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]]
}
}
else
exit ;