Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var texasHoldem = {
game: {
potSize: 0,
players: 0,
player: {},
bet: {}
},
makeGame: function (playersArray) {
this.game.player = playersArray;
this.game.players = playersArray.length;
this.game.deck = this.makeDeck();
return this.game;
},
makeDeck: function () {
var deck = [],
Sdeck = [],
suitArray = ["spades", "hearts", "diamonds", "clubs"];
for (var i in suitArray) {
for (var number = 2; number < 15; number++) {
deck.push(new Card(suitArray[i], number));
}
}
//shuffle
var deck_size = deck.length;
for (var i = 0; 0 < deck_size; i++) {
var random_num = Math.floor(Math.random() * deck_size);
if (random_num == 52) random_num = 0;
if (!deck[random_num]) continue;
Sdeck.push(deck[random_num]);
deck.splice(random_num, 1);
}
console.log(deck);
return Sdeck;
},
deal: function (playersArray) {
// build the game object
this.makeGame(playersArray);
// starting bet
for (var i = 0; i < this.game.players; i++) {
this.game.player[i].chips--;
this.game.potSize++;
}
// starting deal
for (var i in this.game.player) {
var player = this.game.player[i];
player.cards.push(this.game.deck.pop());
}
return this.game;
},
bet: function (player, bet) {
player.chips--;
this.game.potSize++;
return this.game;
}
};
function Player(name, chips, cards) {
this.name = name;
this.chips = chips;
this.cards = cards || [];
}
function Card(suit, number) {
this.suit = suit;
this.number = number;
}
var playersArray = [];
playersArray.push(new Player('billy', 1000));
playersArray.push(new Player('ro', 1000));
exports.index = function(req, res){
if(req.xhr){
res.json(texasHoldem.deal(playersArray));
}else{
res.render('index', {title: "game"})
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment