Permalink
Browse files

basic texas holdem simulation

  • Loading branch information...
1 parent b2989f1 commit 6b7aaae38c8691ec6d53235e1b2e142e43e0568d @datapimp committed Apr 1, 2013
View
@@ -1,5 +1,7 @@
require 'poker-calculator/card'
require 'poker-calculator/hand'
+require 'poker-calculator/deck'
+require 'poker-calculator/texas_holdem'
module PokerCalculator
Version = '0.0.1'
@@ -0,0 +1,32 @@
+module PokerCalculator
+ class Deck
+ attr_reader :cards
+
+ def initialize
+ @cards = []
+
+ Card::Suits.keys.each do |suit|
+ Card::Values.each do |value|
+ @cards << PokerCalculator::Card.new("#{ value }#{ suit }")
+ end
+ end
+
+ shuffle!
+ end
+
+ def shuffle!
+ @cards.shuffle!
+ @cards
+ end
+
+ def deal
+ @cards.pop
+ end
+
+ def remove specific
+ specific = PokerCalculator::Card.new(specific) unless specific.is_a?(PokerCalculator::Card)
+ @cards.reject! {|card| card == specific}
+ specific
+ end
+ end
+end
@@ -81,6 +81,20 @@ def beaten_by?(other)
false
end
+ def <=>(other)
+ return -1 if beaten_by?(other)
+ return 1 if other.beaten_by?(self)
+ return 0
+ end
+
+ def <(other)
+ beaten_by?(other)
+ end
+
+ def >(other)
+ other.beaten_by?(self)
+ end
+
def to_s
cards.map(&:to_s)
end
@@ -0,0 +1,92 @@
+module PokerCalculator
+ class TexasHoldem
+
+ def initialize(options={})
+ @number_of_players = options[:number_of_players] || 9
+ @players = {}
+ end
+
+ def simulate!
+ deal
+ flop
+ turn
+ river
+ @hands = hands
+ end
+
+ def players
+ @players
+ end
+
+ def deck
+ @deck ||= PokerCalculator::Deck.new()
+ end
+
+ def completed_hands
+ @hands
+ end
+
+ def winners
+ @rankings = {}
+
+ player_hands.map do |player|
+ seat, hand = player
+ @rankings[seat] = hands.index(hand)
+ end
+
+ @rankings
+ end
+
+ def player_hands
+ return @player_hands if @player_hands
+
+ @player_hands = {}
+
+ players.map do |player|
+ seat, hole_cards = player
+ @player_hands[seat] = PokerCalculator::Hand.new((community + hole_cards).map(&:to_s).join(" "))
+ end
+
+ @player_hands
+ end
+
+ def hands
+ return @hands if @hands
+
+ @hands = player_hands.values
+ @hands.sort! {|a,b| a <=> b}
+ @hands.reverse!
+
+ @hands
+ end
+
+ def deal
+ @number_of_players.times do |seat|
+ @players[seat] = []
+ @players[seat] << deck.deal << deck.deal
+ end
+ end
+
+ def burn
+ deck.deal
+ end
+
+ def flop
+ deal if @players.empty?
+ @flop ||= 3.times.map { deck.deal }
+ end
+
+ def turn
+ @turn ||= deck.deal
+ end
+
+ def river
+ @river ||= deck.deal
+ end
+
+ def community
+ [@flop,@turn,@river].compact.flatten
+ end
+
+ end
+end
@@ -0,0 +1,32 @@
+require "spec_helper"
+
+describe PokerCalculator::TexasHoldem do
+ let(:game) { PokerCalculator::TexasHoldem.new(:players=>9) }
+
+ it "should have a deck with 52 cards" do
+ game.deck.cards.length.should == 52
+ end
+
+ it "should deal a flop by burning a card and dealing 3 to the community" do
+ game.flop
+ game.community.length.should == 3
+ end
+
+ it "should deal a turn card" do
+ game.flop
+ game.turn
+ game.community.length.should == 4
+ end
+
+ it "should deal a river card" do
+ game.flop
+ game.turn
+ game.river
+ game.community.length.should == 5
+ end
+
+ it "should deal hole cards to players" do
+ game.deal
+ binding.pry
+ end
+end

0 comments on commit 6b7aaae

Please sign in to comment.