Created
March 24, 2013 06:18
-
-
Save xadhix-zz/5230785 to your computer and use it in GitHub Desktop.
Sample Gist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created with JetBrains WebStorm. | |
* User: [email protected] | |
* Date: 2/24/13 | |
* Time: 7:57 PM | |
* To change this template use File | Settings | File Templates. | |
*/ | |
var Game = {}; | |
var Deck; | |
Deck = function Deck(withJoker) { | |
"use strict"; | |
this.cards = ["H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "HX", "HJ", "HQ", "HK", "HA", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CX", "CJ", "CQ", "CK", "CA", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "SX", "SJ", "SQ", "SK", "SA", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "DX", "DJ", "DQ", "DK", "DA"]; | |
//noinspection ConstantOnLefSideOfComparisonJS | |
if (true === withJoker) { | |
this.cards.push("J1"); | |
this.cards.push("J2"); | |
} | |
this.shuffle = function shuffle() { | |
var i, rand, a; | |
for (i = 0; i < this.cards.length; i = i + 1) { | |
rand = Math.random(); | |
rand = Math.round(rand * (this.cards.length - i - 1) + i); | |
a = this.cards[i]; | |
this.cards[i] = this.cards[rand]; | |
this.cards[rand] = a; | |
} | |
return this; | |
}; | |
return this; | |
}; | |
var Player; | |
Player = function Player(idx, id) { | |
"use strict"; | |
if (id) { | |
this.id = id; | |
} else { | |
this.id = "Player"; | |
} | |
this.idx = idx; | |
this.cardsState = [Game.numberOfPlayers]; | |
this.setState = function setState(owncards) { | |
var i, j, deckOfCards, self, others; | |
for (i = 0; i < this.cardsState.length; i = i + 1) { | |
this.cardsState[i] = {}; | |
} | |
deckOfCards = Game.deck.cards; | |
for (i = 0; i < deckOfCards.length; i = i + 1) { | |
//noinspection ConstantOnLefSideOfComparisonJS | |
if (0 <= owncards.indexOf(deckOfCards[i])) { | |
self = 1; | |
others = -1; | |
} else { | |
self = -1; | |
others = 0; | |
} | |
for (j = 0; j < this.cardsState.length; j = j + 1) { | |
if (this.idx === j) { | |
this.cardsState[j][deckOfCards[i]] = self; | |
} else { | |
this.cardsState[j][deckOfCards[i]] = others; | |
} | |
} | |
} | |
return this; | |
}; | |
this.cards = function cards() { | |
var hasThese = [], card; | |
for (card in this.cardsState[this.idx]) { | |
//noinspection ConstantOnLeftSideOfComparisonJS | |
if (this.cardsState[this.idx].hasOwnProperty(card) && 1 === this.cardsState[this.idx][card]) { | |
hasThese.push(card); | |
} | |
} | |
return hasThese; | |
}; | |
return this; | |
}; | |
Game.deck = new Deck(); | |
Game.numberOfPlayers = 4; | |
Game.setNumberOfPlayers = function setNumberOfPlayers(n) { | |
"use strict"; | |
Game.numberOfPlayers = n; | |
return this; | |
} | |
Game.deal = function deal(config) { | |
var n = Game.numberOfPlayers; | |
var startingFrom = 0; | |
//TODO : Accommodate config. | |
if (!config) { | |
config = {}; | |
} | |
if (config.shuffle === true) { | |
Game.deck.shuffle(); | |
} | |
if (config.n && typeof(config.n) === "number" && config.n > 0) { | |
n = config.n; | |
} | |
if (config["startingFrom"] !== "undefined" && typeof(config["startingFrom"]) == "number" && config["startingFrom"] >= 0 && config["startingFrom"] < n) { | |
startingFrom = config["startingFrom"]; | |
} | |
var cards = new Array(); | |
for (var i = 0; i < n; i++) { | |
cards.push(new Array()); | |
} | |
for (var i = 0; i < Game.deck.cards.length; i++) { | |
cards[startingFrom].push(Game.deck.cards[i]); | |
startingFrom = (startingFrom + 1) % n; | |
} | |
return cards; | |
} | |
Game.init = function init() { | |
var dealtCards = Game.deal({"shuffle":true}); | |
var players = []; | |
for (var i = 0; i < this.numberOfPlayers; i++) { | |
players.push((new Player(i, "Player" + (i + 1))).setState(dealtCards[i])); | |
} | |
Game.players = players; | |
return this; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment