0

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?

6
  • You might want to find a better way of "shuffling" the deck than Math.floor(Math.random() * deck.length). Might I suggest shuffling the deck? Commented Jul 27, 2011 at 20:03
  • @Eric--could you elaborate some more? Do you have any ideas on how to better shuffle the deck? Commented Jul 27, 2011 at 22:15
  • You method of "shuffling" can pick the same card twice in a row. Instead, you should sort the deck in a random order, as in a real shuffle. The best way of doing this is to use a Fisher–Yates shuffle. To draw a card, simply .shift() off the top card of the shuffled array. Commented Jul 28, 2011 at 8:18
  • @Eric--I thought about doing that, but the problem is that I have attached conditional statements that define the face cards as values: 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? Commented Jul 29, 2011 at 19:43
  • 1
    Create a class for each card as well! Commented Jul 29, 2011 at 20:41

3 Answers 3

1

Looks like you want an object:

function Hand(id) {
    this.elem = document.getElementById(id);
    this.bucket = "";
    this.bucketVal = 0;
    this.deal = function() {
        this.card1 = Math.floor(Math.random() * deck.length);
        this.card2 = Math.floor(Math.random() * deck.length);
        this.elem.innerHTML = deck[this.card1] + ', ' + this.card2;
    }
    this.hit = function() {

        var newCard = Math.floor(Math.random() * deck.length);
        this.bucket += " " + deck[nhand] + ", ";
        this.bucketVal += this.card1 + this.card2 + newCard;

        if (this.bucketVal >= 22) {
            var r = confirm("Bust! By " + nhand);

            refresh();
        }

        this.elem.innerHTML = this.bucket;

        light = light + 1;

        if (light == 5) {
            alert("Five Card Blackjack! You Win!");
            refresh();
        }
    }
}

var player1Hand = new Hand('player1Id');
player1Hand.deal();
player1Hand.hit();
Sign up to request clarification or add additional context in comments.

5 Comments

@Eric--I don't know much about OOP. Can you break down this approach?
@codeninja: Are you having difficulty with the syntax, or just the concepts?
@Eric--its been a while since i commented on this thread, i hope you get this comment. what i don't understand about this approach is that hit() is embedded within Hand(id). doesn't that mean i cannot call hit independently? Please explain.
hit() becomes part of the Hand class. Any instance of a Hand class contains the hit method. My code shows an example of how to hit player 1.
@Eric--I got it, i didnt see that part of the code. thanks again.
1

easy way: make them global or pass them between the functions

harder but more advisable way: use objects instead and make those variable attributes of the object.

Comments

0

Make them global variables rather than local variables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.