arozwalak
1/6/2014 - 5:24 PM

Javascript: Black Jack Game

Javascript: Black Jack Game

// Card Constructor
function Card(suit, number) {
    var suit = suit;
    var number = number;
    
    this.getNumber = function() {
        return number;
    }
    this.getSuit = function() {
        return suit;
    }
    this.getValue = function() {
        if (number === 1) {
            return 11;
        }
        else if (number > 1 && number < 11) {
            return number;
        }
        else {
            return 10;
        }
    }
}

// Hand Constructor
function Hand() {
    var hand = [deal(), deal()];
    this.getHand = function() {
        return hand;
    };
    this.score = function() {
        var aces = 0;
        var sum = 0;
        for (var i = 0; i < hand.length; i++) {
            if (hand[i].getValue() === 11) {
                aces++;
            }
            sum += hand[i].getValue();
        }
        for (var j = 0; j < aces; j++) {
            if (sum > 21) {
                sum -= 10;
            }
        }
        return sum;
    };
    this.printHand = function() {
        var result = "";
        for (var i = 0; i < hand.length; i++) {
            var suit = hand[i].getSuit();
            var number = hand[i].getNumber();
            switch (suit) {
                case 1:
                    suit = "hearts";
                    break;
                case 2:
                    suit = "diamonds";
                    break;
                case 3:
                    suit = "spades";
                    break;
                case 4:
                    suit = "clubs";
                    break;
            }
            switch (number) {
                case 1:
                    number = "As";
                    break;
                case 11:
                    number = "Jack";
                    break;
                case 12:
                    number = "Queen";
                    break;
                case 13:
                    number = "King";
                    break;
            }
            if (i === hand.length - 1) {
                result += (number + " of " + suit);
            }
            else {
                result += (number + " of " + suit + ", ");
            }
        }
        
        return result;
    };
    this.hitMe = function() {
        hand[hand.length] = deal();
    };
}

function deal() {
    var number = Math.floor(Math.random() * 13 + 1);
    var suit = Math.floor(Math.random() * 4 + 1);
    // var number = 1;
    // var suit = 1;
    return new Card(suit, number);
}

function playAsDealer() {
    var hand = new Hand();
    var score = hand.score();
    while (score < 17) {
        hand.hitMe();
        score = hand.score();
    }
    return hand;
};

function playAsUser() {
    var hand = new Hand();
    var hit = confirm(hand.printHand());
    var score = hand.score();
    if (score >= 21) { return hand; }
    while(hit) {
        hand.hitMe();
        score = hand.score();
        if (score >= 21) { return hand; }
        hit = confirm(hand.printHand());
    }
    return hand;
};

function declareWinner(userHand, dealerHand) {
    if ((userHand.score() > dealerHand.score() && userHand.score() <= 21)
        || dealerHand.score() > 21 && userHand.score() <= 21) {
        return "You won!";
    }
    else if (userHand.score() === dealerHand.score() && userHand.score() <= 21){
        return "You tied!";
    }
    else {
        return "You lose!";
    }
}

function playGame() {
    var userHand = playAsUser();
    var dealerHand = playAsDealer();
    var winner = declareWinner(userHand, dealerHand);
    console.log(userHand.printHand(), userHand.score());
    console.log(dealerHand.printHand(), dealerHand.score());
    console.log(winner);
}

playGame();