Card Game in JavaScript

Messing around with prototypical inheritance

by Augustus Yuan

JavaScript

var Utils = {
	shape: ["pill", "diamond", "squiggly"],
  color: ["red", "blue", "green"],
  pattern: ["striped", "solid", "blank"],
  number: [1, 2, 3]
};

function Card(shape, color, pattern, number) {
	this.shape = Utils.shape[shape];
  this.color = Utils.color[color];
  this.pattern = Utils.pattern[pattern];
  this.number = number;
}
Card.prototype.view = function() {
  return "<div class=\"card-container\">" + this.shape + this.color + "</div>";
}

function Deck(cards) {
  this._populateDeck = function() {
  	var _deck = [];
    for (var i=0; i<3; i++) {
      for (var j=0; j<3; j++) {
        for (var k=0; k<3; k++) {
          for (var l=0; l<3; l++) {
            _deck.push(new Card(i,j,k,l));
          }
        }
      }
    }
    return _deck;
  }
  this._shuffle = function(cards) {
    var m = cards.length, t, i;

    // While there remain elements to shuffle…
    while (m) {

      // Pick a remaining element…
      i = Math.floor(Math.random() * m--);

      // And swap it with the current element.
      t = cards[m];
      cards[m] = cards[i];
      cards[i] = t;
    }

    return cards;
  }

	this.maxSize = 3*3*3*3;
  this.cards = cards || this._shuffle(this._populateDeck());
}

console.log(new Deck());