Here is the code I have written:
function p_deal(id) {
var card1_val = Math.floor(Math.random() * deck.length);
var card2_val = Math.floor(Math.random() * deck.length);
var card1 = deck[card1_val];
var card2 = deck[card2_val];
var hand = card1 + ", " + card2;
var res = card1_val + card2_val;
document.getElementById(id).innerHTML = hand;
and
function hit(id) {
if (bucket == 0) {
bucket = " ";
}
var card3_val = Math.floor(Math.random() * deck.length);
var nhand = deck[card3_val];
bucket = bucket + " " + nhand + ", ";
bucket_val = bucket_val + card1_val + card2_val + card3_val;
if (bucket_val >= 22) {
var r = confirm("Bust! By " + nhand);
if (r == true) {
refresh();
}
else {
refresh();
}
}
document.getElementById(id).innerHTML = bucket;
light = light + 1;
if (light == 5) {
alert("Five Card Blackjack! You Win!");
refresh();
}
}
The card_val variables within bucket val are from the p_deal(id) function. In order for the program to work, the card_val values must be the same each time both functions are called but they need to be regenerated each time the function is called (so multiple players can have different hands). However, as local variables I find it difficult to use them in another function. What can i do here?
Math.floor(Math.random() * deck.length). Might I suggest shuffling the deck?.shift()off the top card of the shuffled array.if (card1 == "Jack") { card1_val = 10; } else if (card1 == "Queen") { card1_val = 10; } else if (card1 == "King") { card1_val = 10; } else if (card1 == "Ace") { card1_val = 11; }I didn't know how else to make sure the faces were treated as values. How can I combine both approaches?