Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 > 10) {
- return 10;
- } else {
- return number;
- }
- }
- }
- function Hand() {
- var hand = [deal(), deal()];
- this.getHand = function() {
- return hand;
- }
- this.hitMe = function() {
- hand.push(deal());
- }
- this.printHand = function() {
- var string = "";
- for (var i = 0; i < hand.length; i++) {
- string += hand[i].getNumber() + " of suit " + hand[i].getSuit();
- if (i < (hand.length - 1)) {
- string += ", ";
- }
- }
- return string;
- }
- this.score = function() {
- var aces = 0;
- var sum = 0;
- for (var i = 0; i < hand.length; i++) {
- sum += hand[i].getValue();
- if (hand[i].getValue() == 11) {
- aces++;
- }
- }
- while (aces > 0 && sum > 21) {
- aces--;
- sum -= 10;
- }
- return sum;
- }
- }
- function deal() {
- var suit = Math.floor(Math.random( ) * 4 + 1);
- var number = Math.floor(Math.random( ) * 13 + 1);
- return new Card(suit, number);
- }
- function playAsDealer() {
- var hand = new Hand();
- while (hand.score() < 17) {
- hand.hitMe();
- }
- return hand;
- }
- function playAsUser() {
- var hand = new Hand();
- var decision = confirm("Your hand is "+ hand.printHand() + ": Hit OK to hit (take another card) or Cancel to stand");
- while(decision) {
- hand.hitMe();
- decision = confirm("Your hand is "+ hand.printHand() + ": Hit OK to hit (take another card) or Cancel to stand");
- }
- return hand;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement