1

I have been doing the following just fine:

 var game = function() {
    var self = this;
    self.deck = [
        { id: 1, name: "Ace", suit: "Spade" },
        { id: 2, name: "King", suit: "Spade" },
        { id: 3, name: "Queen", suit: "Spade" }
    ];
 });

However, I decided to add another object called "card" ...

 var card = function() {
    var self = this;

    self.id = ko.observable();
    self.name = ko.observable();
    self.suit = ko.observable();

    self.someUsefulMethod = function() {
       // does something useful ...
    });
 });

How can I change the game.deck so that it instantiates with the same cards but uses the card object?

In c# I would do the following ...

var deck = new List() {
    new Card { id = 1, name = "Ace", suit = "Spade" },
    new Card { id = 2, name = "King", suit = "Spade" },
    new Card { id = 3, name = "Queen", suit = "Spade" },
};

... but I don't know how to do this in javascript.

2 Answers 2

3

When creating the deck, you just put card objects in it rather than plain objects:

So rather than this:

self.deck = [
    { id: 1, name: "Ace", suit: "Spade" },
    { id: 2, name: "King", suit: "Spade" },
    { id: 3, name: "Queen", suit: "Spade" }
];

You do something like this:

self.deck = [
    new Card(1, "Ace", "Spade"),
    new Card(2, "King", "Spade"),
    new Card(3, "Queen", "Spade")
];

And, you create a card object that takes the appropriate arguments in its constructor:

function Card(id, name, suit) {
    this.id = id;
    this.name = name;
    this.suit = suit;
}

In reality, you'd probably want to automate this like this:

function createDeck() {
    var deck = [];
    var cardSuits = ["Spade", "Hearts", "Diamonds", "Clubs"];
    var cardNames = ["Ace", "King", "Queen", "Jack", "Ten", "Nine", ...];

    for (var s = 0; s < cardSuits.length; s++) {
        for (var n = 0; n < cardNames.length; n++) {
            deck.push(new Card(n + 1, cardSuits[s], cardNames[n]));
        }
    }
    return deck;
}

Or, if you want deck to be an object that you create with new deck():

function Deck() {
    this.cards = [];
    var cardSuits = ["Spade", "Hearts", "Diamonds", "Clubs"];
    var cardNames = ["Ace", "King", "Queen", "Jack", "Ten", "Nine", ...];

    for (var s = 0; s < cardSuits.length; s++) {
        for (var n = 0; n < cardNames.length; n++) {
            this.cards.push(new Card(n + 1, cardSuits[s], cardNames[n]));
        }
    }
}

Deck.prototype = {
    dealCards: function(numberOfCards) {
        numberOfCards = Math.min(numberOfCards, this.cards.length);
        var cards = [], rand;
        for (var i = 0, i < numberOfCards; i++) {
            rand = Math.floor(Math.random() * this.cards.length);
            cards.push(this.cards[rand]);
            this.cards.splice(rand, 1);
        }
        return cards;
    },
    dealHands: function(numHands, numCards) {
        numCards = Math.min(Math.floor(this.cards.length / numHands), numCards);
        var hands = [];
        for (var i = 0; i < numHands; i++) {
            hands.push(this.dealCards(numCards));
        }
        return hands;
    }
};
Sign up to request clarification or add additional context in comments.

1 Comment

Added some additional methods to a Deck object.
2

Your card function is not yet a constructor, and should by convention be named Card not card. There are more complicated approaches that could be taken, but this should get you started...

var Card = function(id, name, suit) { 
    this.id = ko.observable(id);
    this.name = ko.observable(name);
    this.suit = ko.observable(suit);

    this.somethingRequiringThis = function () {
        alert(self.suit + ' ' + self.name);
    };
}

var deck = [ new Card(1, "ace", "spades"), new Card(2, "deuce", "spades").... ];

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.