Created
May 26, 2016 22:39
-
-
Save jobelenus/8d41ff197d82a3f4ba9f70a2087ebf88 to your computer and use it in GitHub Desktop.
Working JS under Node
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
#!/usr/bin/env node | |
var util = require('util'); | |
console.log('start'); | |
var A = 14; | |
var K = 13; | |
var Q = 12; | |
var J = 11; | |
var TEN = 10; | |
var NINE = 9; | |
var EIGHT = 8; | |
var SEVEN = 7; | |
var SIX = 6; | |
var FIVE = 5; | |
var FOUR = 4; | |
var THREE = 3; | |
var TWO = 2; | |
var CLUB = 'C'; | |
var SPADE = 'S'; | |
var HEART = 'H'; | |
var DIAMOND = 'D'; | |
function Card(rank, suit) { | |
this.rank = rank; | |
this.suit = suit; | |
this.game_value = 0; | |
this.dealt_to_player = -1; | |
} | |
Card.prototype.display = function() { | |
return this.rank + ' ' + this.suit; | |
}; | |
var Deck = { | |
ranks: [TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, J, Q, K, A], | |
suits: [SPADE, CLUB, HEART, DIAMOND], | |
create: function() { | |
var deck = []; | |
for (var s in this.suits) { | |
for (var r in this.ranks) { | |
deck.push(new Card(this.ranks[r], this.suits[s])); | |
} | |
} | |
return deck; | |
}, | |
shuffle: function(deck) { | |
var currentIndex = deck.length, temporaryValue, randomIndex; | |
// While there remain elements to shuffle... | |
while (0 !== currentIndex) { | |
// Pick a remaining element... | |
randomIndex = Math.floor(Math.random() * currentIndex); | |
currentIndex -= 1; | |
// And swap it with the current element. | |
temporaryValue = deck[currentIndex]; | |
deck[currentIndex] = deck[randomIndex]; | |
deck[randomIndex] = temporaryValue; | |
} | |
return deck; | |
} | |
}; | |
function Player(name) { | |
this.name = name; | |
this.hand = []; | |
this.points = 0; | |
} | |
function Gameplay(deck) { | |
this.deck = deck; | |
this.num_players = 0; | |
this.player_names = []; | |
this.players = []; | |
this.current_player = 0; | |
this.pot = []; | |
this.previous_pots = []; | |
this.create_players = function(player_names) { | |
this.num_players = player_names.length; | |
this.player_names = player_names; | |
var i = 0; | |
while (i < this.num_players) { | |
this.players.push(new Player(this.player_names[i])); | |
i++; | |
} | |
}; | |
this.cards_left = function() { | |
return this.deck.length > 0 ? true : false; | |
}; | |
this.deal = function(cards_per_player) { | |
var players_done = 0; | |
while (players_done != this.num_players) { | |
if (this.players[this.current_player].hand.length < cards_per_player) { | |
var card = this.deck.pop(); | |
card.dealt_to_player = this.current_player; | |
this.players[this.current_player].hand.push(card); | |
if (this.players[this.current_player].hand.length == cards_per_player) { | |
players_done++; | |
} | |
this.next_player(); | |
} | |
} | |
this.current_player = 0; | |
}; | |
this.draw = function(number_of_cards, discard) { | |
var cards = []; | |
if (this.cards_left) { | |
while (cards.length < number_of_cards) { | |
var card = this.deck.pop(); | |
if (!discard) { | |
card.dealt_to_player = this.current_player; | |
} else { | |
card.dealt_to_player = -1; | |
} | |
cards.push(card); | |
} | |
} | |
return cards; | |
}; | |
this.next_player = function() { | |
this.current_player++; | |
if (this.current_player >= this.num_players) { | |
this.current_player = 0; | |
} | |
}; | |
this.add_card_to_pot = function(card) { | |
var card_location = -1; | |
for (var c in this.players[this.current_player].hand) { | |
if (card.rank == this.players[this.current_player].hand[c].rank && card.suit == this.players[this.current_player].hand[c].suit) { | |
card_location = c; | |
break; | |
} | |
} | |
if (card_location != -1 ) { | |
card = (this.players[this.current_player].hand.splice(card_location, 1)).pop(); | |
} | |
this.pot.push(card); | |
}; | |
} | |
var Hearts = { | |
create: function() { | |
var deck = Deck.create(); | |
for (var c in deck) { | |
if (deck[c].suit == HEART) { | |
deck[c].game_value = 1; | |
} else if (deck[c].suit == SPADE && deck[c].rank == Q) { | |
deck[c].game_value = 13; | |
} | |
} | |
deck = Deck.shuffle(deck); | |
return deck; | |
}, | |
validate_card_to_pot: function(game, card) { | |
if (card.suit != game.pot[0].suit) { | |
for (var c in game.players[card.dealt_to_player].hand) { | |
if (game.players[card.dealt_to_player].hand[c].suit == card.suit) { | |
return false; | |
} | |
} | |
} | |
return true; | |
}, | |
find_start_player: function(game) { | |
for (var p in game.players) { | |
for (var c in game.players[p].hand) { | |
var card = game.players[p].hand[c]; | |
if (card.suit == CLUB && card.rank == 2) { | |
return { | |
player_number: p, | |
card: card | |
}; | |
} | |
} | |
} | |
return -1; | |
}, | |
find_pot_results: function(game) { | |
var starting_card = game.pot.shift(); | |
console.log('first pot card', util.inspect(starting_card)); | |
var current_winning_rank = starting_card.rank; | |
var current_winning_player = starting_card.dealt_to_player; | |
console.log('STARTING', 'player', current_winning_player, 'rank', current_winning_rank); | |
var pot_value = 0; | |
for (var i in game.pot) { | |
pot_value += game.pot[i].game_value; | |
if (game.pot[i].suit == starting_card.suit) { | |
if (game.pot[i].rank > current_winning_rank) { | |
current_winning_rank = game.pot[i].rank; | |
current_winning_player = game.pot[i].dealt_to_player; | |
console.log('CHANGED', 'player', current_winning_player, 'rank', current_winning_rank); | |
} | |
} | |
} | |
console.log('END', 'player', current_winning_player, 'rank', current_winning_rank); | |
game.players[current_winning_player].points += pot_value; | |
game.current_player = current_winning_player; | |
game.pot.unshift(starting_card); | |
game.previous_pots.push(game.pot); | |
game.pot = []; | |
return current_winning_player; | |
} | |
}; | |
var Spades = { | |
}; | |
// a working version of one hand of Hearts with 4 players | |
var deck = Hearts.create(); | |
var game = new Gameplay(deck); | |
game.create_players(['first', 'second', 'third', 'fourth']); | |
game.deal(13); | |
var starts_with = Hearts.find_start_player(game); | |
console.log('starts with', util.inspect(starts_with)); | |
game.current_player = starts_with.player_number; | |
game.add_card_to_pot(starts_with.card); | |
game.next_player(); | |
for (var c2 in game.players[game.current_player].hand) { | |
var card2 = game.players[game.current_player].hand[c2]; | |
if (Hearts.validate_card_to_pot(game, card2)) { | |
game.add_card_to_pot(card2); | |
break; | |
} | |
} | |
game.next_player(); | |
for (var c3 in game.players[game.current_player].hand) { | |
var card3 = game.players[game.current_player].hand[c3]; | |
if (Hearts.validate_card_to_pot(game, card3)) { | |
game.add_card_to_pot(card3); | |
break; | |
} | |
} | |
game.next_player(); | |
for (var c4 in game.players[game.current_player].hand) { | |
var card4 = game.players[game.current_player].hand[c4]; | |
if (Hearts.validate_card_to_pot(game, card4)) { | |
game.add_card_to_pot(card4); | |
break; | |
} | |
} | |
console.log('pot', util.inspect(game.pot)); | |
var winning_player = Hearts.find_pot_results(game); | |
console.log("POT WINNER", winning_player); | |
console.log('previous pots', util.inspect(game.previous_pots)); | |
console.log('new pot', util.inspect(game.pot)); | |
console.log('end'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment