开发者

How to access variables within another function

开发者 https://www.devze.com 2023-03-22 21:02 出处:网络
I\'m writing code for a blackjack game and have run into some problems. I have written two functions: one for the initial deal and one for each consecutive hit. this is the deal function:

I'm writing code for a blackjack game and have run into some problems. I have written two functions: one for the initial deal and one for each consecutive hit. this is the deal function:

var deck = [1,2,3,4,5,6,7,8,9,10,"Jack","Queen","King","Ace"];

function deal() {
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 开发者_Python百科= deck[card2_val];

var hand = card1 + ", " + card2;

{//card1 Conditionals
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;
    }
}

{//card2 Conditionals
if (card2 == "Jack") {
    card2_val = 10;
    }
else if (card2 == "Queen") {
    card2_val = 10;
    }
else if (card2 == "King") {
    card2_val = 10;
    }
else if (card2 == "Ace") {
    card2_val = 11;
    }
}

var res = card1_val + card2_val;
document.getElementById("result").innerHTML = hand;
//document.getElementById("test").innerHTML = card1_val + ", " + card2_val;

if (res > 21) {
    alert("Blackjack!");
    }
}

This is the hit function:

function hit() {
var card_val = Math.floor(Math.random() * deck.length);
var nhand = deck[card_val];
bucket = hand + nhand
}

If you look at hit() I am using the var hand from deal(). I can't make it global because I need the value to be a fresh random each time. How do I access this same variable without rewriting lines of code? Any help would be appreciated.


You can either

  • Declare hand outside of the function scope with just var hand; and use hand in the deal function without redeclaring it as a var;
  • Or use window.hand when declaring hand in the deal function


global variables are evil. i would take more object oriented approach, like this

var Hand = function(bjcallback) {

    this.cards = [];

    this.onblackjack = bjcallback;

    this.deck = [1,2,3,4,5,6,7,8,9,10,"Jack","Queen","King","Ace"];

    this.values = {
        "Jack": 10,
        "Queen": 10,
        "King": 10,
        "Ace": 11
    };

    this.sum = function() {
        var i, x, res = 0;
        for (i in this.cards) {
            x = this.cards[i];
            if (typeof(x) != 'number') { x = this.values[x] };
            res += x;
        };
        return res
    };

    this.pick = function() {
        var pos = Math.floor(Math.random() * this.deck.length);
        var card = this.deck[pos];
        console.log(card);
        return card
    };

    this.deal = function(n) {
        n = n || 2;
        for (var i=0; i<n; i++) this.cards.push(this.pick())
    };

    this.hit = function() {
        this.cards.push(this.pick());
        if (this.sum() > 21) this.onblackjack();
    }
}

var hurray = function() { alert('Blackjack!') };
var hand = new Hand(hurray);

hand.deal();
hand.hit();

note that i'm not much into cards so i might have confused terminology or counting

0

精彩评论

暂无评论...
验证码 换一张
取 消