rholdem 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.txt ADDED
@@ -0,0 +1,3 @@
1
+ == 0.0.1 / 2007-09-30
2
+
3
+ * First release of library
data/Manifest.txt ADDED
@@ -0,0 +1,16 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/rholdem.rb
6
+ lib/card.rb
7
+ lib/deck.rb
8
+ lib/game.rb
9
+ lib/hand.rb
10
+ lib/player.rb
11
+ spec/rholdem_spec.rb
12
+ spec/card_spec.rb
13
+ spec/deck_spec.rb
14
+ spec/game_spec.rb
15
+ spec/hand_spec.rb
16
+ spec/player_spec.rb
data/README.txt ADDED
@@ -0,0 +1,117 @@
1
+ rholdem
2
+ by Russell Authur
3
+ http://rholdem.rubyforge.org
4
+
5
+ == DESCRIPTION:
6
+
7
+ Ruby implementation of Texas Hold'em poker games (limit games only to start). The intended use of the library is for simulations, not as much for human games though interactive play is possible. There are no plans to support non Texas Hold'em poker games or variations of Texas Hold'em other than no-limit.
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ - Simulate limit games of 2 to 11 players
12
+ - Provide custom AI players to test out ideas
13
+ - Track totals for each player to use with AI fitness evaluation
14
+
15
+ == SYNOPSIS:
16
+
17
+ # run a sample game, 5/10 limit, 10 players
18
+ require 'rubygems'
19
+ require 'rholdem'
20
+
21
+ include Holdem
22
+
23
+ g = Game.new(5.0, 10)
24
+ g.run_game
25
+ puts "board: #{g.board.join(' ')}"
26
+ puts "pot: #{g.pot}"
27
+ g.winners.each { |p| puts "#{p.hand.ranking} from hole cards: #{p.hole_cards.join(' ')}" }
28
+
29
+ # once you have a custom AI setup
30
+ require 'rubygems'
31
+ require 'rholdem'
32
+
33
+ include Holdem
34
+
35
+ g = Game.new(5.0) do |position|
36
+ MyCustomPlayerClass.new(position)
37
+ end
38
+
39
+ g.run_game ...
40
+
41
+ == REQUIREMENTS:
42
+
43
+ - The Ruby programming language
44
+ - RubyGems
45
+ - rSpec if you want to run the tests
46
+
47
+ == INSTALL:
48
+
49
+ sudo gem install rholdem
50
+
51
+ == LICENSE:
52
+
53
+ GNU LESSER GENERAL PUBLIC LICENSE
54
+
55
+ Version 3, 29 June 2007
56
+
57
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
58
+
59
+ Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
60
+
61
+ This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below.
62
+ 0. Additional Definitions.
63
+
64
+ As used herein, “this License” refers to version 3 of the GNU Lesser General Public License, and the “GNU GPL” refers to version 3 of the GNU General Public License.
65
+
66
+ “The Library” refers to a covered work governed by this License, other than an Application or a Combined Work as defined below.
67
+
68
+ An “Application” is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library.
69
+
70
+ A “Combined Work” is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the “Linked Version”.
71
+
72
+ The “Minimal Corresponding Source” for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version.
73
+
74
+ The “Corresponding Application Code” for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work.
75
+ 1. Exception to Section 3 of the GNU GPL.
76
+
77
+ You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL.
78
+ 2. Conveying Modified Versions.
79
+
80
+ If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version:
81
+
82
+ * a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or
83
+ * b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy.
84
+
85
+ 3. Object Code Incorporating Material from Library Header Files.
86
+
87
+ The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following:
88
+
89
+ * a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License.
90
+ * b) Accompany the object code with a copy of the GNU GPL and this license document.
91
+
92
+ 4. Combined Works.
93
+
94
+ You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following:
95
+
96
+ * a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License.
97
+ * b) Accompany the Combined Work with a copy of the GNU GPL and this license document.
98
+ * c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document.
99
+ * d) Do one of the following:
100
+ o 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.
101
+ o 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version.
102
+ * e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.)
103
+
104
+ 5. Combined Libraries.
105
+
106
+ You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following:
107
+
108
+ * a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License.
109
+ * b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work.
110
+
111
+ 6. Revised Versions of the GNU Lesser General Public License.
112
+
113
+ The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
114
+
115
+ Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation.
116
+
117
+ If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.
data/Rakefile ADDED
@@ -0,0 +1,35 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rake'
4
+ require 'rake/testtask'
5
+ require 'rake/rdoctask'
6
+
7
+ require 'rake'
8
+ require 'spec/rake/spectask'
9
+
10
+ require 'rubygems'
11
+ require 'hoe'
12
+ require './lib/rholdem.rb'
13
+
14
+ Hoe.new('rholdem', Holdem::VERSION) do |p|
15
+ p.rubyforge_name = 'rholdem'
16
+ p.author = 'Russell Authur'
17
+ p.email = 'russell.authur@gmail.com'
18
+ p.summary = "Ruby Texas Hold'em Simulations Library"
19
+ p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n")
20
+ p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
21
+ p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
22
+ p.remote_rdoc_dir = '' # Release to root
23
+ end
24
+
25
+ desc "Run all examples with RCov"
26
+ Spec::Rake::SpecTask.new('rcov') do |t|
27
+ t.spec_files = FileList['spec/**/*.rb']
28
+ t.rcov = true
29
+ t.rcov_opts = ['--exclude', 'spec']
30
+ end
31
+
32
+ desc "Generate ruby-prof HTML graphs"
33
+ task :profile do |t|
34
+ system 'ruby profile/profile.rb'
35
+ end
data/lib/card.rb ADDED
@@ -0,0 +1,77 @@
1
+ module Holdem
2
+ class Card
3
+ SUITS = "cdhs"
4
+ RANKS = "23456789TJQKA"
5
+
6
+ SUITS_LOOKUP = {
7
+ 'c' => 0,
8
+ 'd' => 1,
9
+ 'h' => 2,
10
+ 's' => 3,
11
+ }
12
+
13
+ RANKS_LOOKUP = {
14
+ '2' => 0,
15
+ '3' => 1,
16
+ '4' => 2,
17
+ '5' => 3,
18
+ '6' => 4,
19
+ '7' => 5,
20
+ '8' => 6,
21
+ '9' => 7,
22
+ 'T' => 8,
23
+ 'J' => 9,
24
+ 'Q' => 10,
25
+ 'K' => 11,
26
+ 'A' => 12,
27
+ }
28
+
29
+ attr_reader :suit, :rank
30
+
31
+ def initialize(card)
32
+ if card.is_a?(String) and card.length == 2
33
+ @rank = RANKS_LOOKUP[card[0, 1]]
34
+ @suit = SUITS_LOOKUP[card[1, 1].downcase]
35
+ else
36
+ raise "Received bad constructor: #{card.to_s} which is a #{card.class}"
37
+ end
38
+
39
+ if @rank.nil? or @suit.nil?
40
+ raise "Invalid rank '#{card[0, 1]}' or invalid suit '#{card[1, 1]}'"
41
+ end
42
+ end
43
+
44
+ def to_s
45
+ RANKS[@rank].chr + SUITS[@suit].chr
46
+ end
47
+
48
+ def ==(compare_to)
49
+ if(compare_to.is_a?(Card))
50
+ return self.rank == compare_to.rank
51
+ end
52
+ end
53
+
54
+ def ===(compare_to)
55
+ return false unless self == compare_to
56
+ return true if self.suit == compare_to.suit
57
+ false
58
+ end
59
+
60
+ def <=>(compare_to)
61
+ if(compare_to.is_a?(Card))
62
+ return 0 if self == compare_to
63
+ return -1 if self.rank < compare_to.rank
64
+ return 1
65
+ end
66
+ end
67
+
68
+ def >(compare_to)
69
+ return true if (self <=> compare_to) == 1
70
+ false
71
+ end
72
+
73
+ def <(compare_to)
74
+ return compare_to > self
75
+ end
76
+ end
77
+ end
data/lib/deck.rb ADDED
@@ -0,0 +1,26 @@
1
+ require File.dirname(__FILE__) + "/card"
2
+
3
+ module Holdem
4
+ class Deck
5
+ attr_reader :cards
6
+
7
+ def shuffle
8
+ @cards = @cards.sort_by { rand }
9
+ end
10
+
11
+ def initialize
12
+ @cards = []
13
+ Card::SUITS.each_byte do |suit|
14
+ Card::RANKS.each_byte do |rank|
15
+ @cards << Card.new(rank.chr + suit.chr)
16
+ end
17
+ end
18
+
19
+ shuffle
20
+ end
21
+
22
+ def deal
23
+ @cards.pop
24
+ end
25
+ end
26
+ end
data/lib/game.rb ADDED
@@ -0,0 +1,239 @@
1
+ require File.dirname(__FILE__) + "/player"
2
+
3
+ module Holdem
4
+ class Game
5
+ ROUNDS = [
6
+ :preflop,
7
+ :flop,
8
+ :turn,
9
+ :river,
10
+ :showdown
11
+ ]
12
+ BET_CAP = 4
13
+
14
+ attr_reader :sb, :bb, :round, :players, :deck, :pot, :bets_in_round, :board, :winners
15
+
16
+ def initialize(small_bet, num_players=10)
17
+ raise "Cannot create a game with #{num_player.to_s} players" unless (2..11).include?(num_players)
18
+
19
+ @sb = small_bet/2.0
20
+ @bb = small_bet
21
+ @round = ROUNDS[0]
22
+ @deck = Deck.new
23
+ @pot = 0.0
24
+ @bets_in_round = 1
25
+ @winners = []
26
+ @board = []
27
+
28
+ # create players and deal hole cards
29
+ @players = []
30
+ (0...num_players).each do |position|
31
+ if(block_given?)
32
+ @players << yield(position)
33
+ else
34
+ @players << Player.new
35
+ end
36
+ end
37
+ 2.times do
38
+ @players.each { |p| p.deal(@deck.deal) }
39
+ end
40
+
41
+ # small and big blind
42
+ if(@players.length == 2)
43
+ @players[1].pay(@sb)
44
+ @players[0].pay(@bb)
45
+ else
46
+ @players[0].pay(@sb)
47
+ @players[1].pay(@bb)
48
+ end
49
+
50
+ @pot += (@sb + @bb)
51
+ end
52
+
53
+ def bet_size
54
+ if(ROUNDS.index(@round) <= 1)
55
+ @bb
56
+ else
57
+ 2.0*@bb
58
+ end
59
+ end
60
+
61
+ def owed(player)
62
+ return 0.0 if player.folded
63
+ (bets_in_round.to_f*bet_size)-player.in_round
64
+ end
65
+
66
+ def settled?(position=-1)
67
+ settled = true
68
+ @players.each do |p|
69
+ settled = false if owed(p) > 0.0 # owed takes folding into account
70
+ end
71
+ # option for big blind
72
+ settled = false if(position == 1 && @bets_in_round == 1 && @round == :preflop)
73
+
74
+ settled
75
+ end
76
+
77
+ def run
78
+ method('do_'+round.to_s).call()
79
+
80
+ if(num_active_players == 1)
81
+ @round = :showdown
82
+ else
83
+ @round = ROUNDS[ROUNDS.index(@round)+1]
84
+ end
85
+
86
+ if(@round == :showdown)
87
+ do_showdown
88
+ end
89
+ end
90
+
91
+ def run_game
92
+ while(@round != :showdown)
93
+ run
94
+ end
95
+ end
96
+
97
+ private
98
+
99
+ def do_preflop
100
+ if(@players.length == 2)
101
+ do_betting(1)
102
+ else
103
+ do_betting(2)
104
+ end
105
+ end
106
+
107
+ def do_flop
108
+ next_round
109
+ burn
110
+ 3.times do
111
+ deal
112
+ end
113
+ @players.each { |p| p.find_hand(@board) }
114
+ do_betting(0)
115
+ end
116
+
117
+ def do_turn
118
+ next_round
119
+ burn; deal
120
+ @players.each { |p| p.find_hand(@board) }
121
+ do_betting(0)
122
+ end
123
+
124
+ def do_river
125
+ next_round
126
+ burn; deal
127
+ @players.each { |p| p.find_hand(@board) }
128
+ do_betting(0)
129
+ end
130
+
131
+ def do_showdown
132
+ active_players.each do |a|
133
+ if(@winners.length == 0)
134
+ @winners << a
135
+ else
136
+ if(@winners[0].hand < a.hand)
137
+ @winners = [a]
138
+ elsif(@winners[0].hand == a.hand)
139
+ @winners << a
140
+ end
141
+ end
142
+ end
143
+
144
+ @winners.each { |w| w.award(@pot/@winners.length)}
145
+ end
146
+
147
+ # TODO: fix up this function
148
+ def do_betting(starting_position)
149
+ position = next_position(starting_position)
150
+
151
+ # go around 1 time first when not the preflop...
152
+ if([:flop, :turn, :river].include?(@round))
153
+ action = @players[position].act(self)
154
+ process_action(action, @players[position])
155
+ position = next_position(position+1)
156
+ while(position > 0)
157
+ action = @players[position].act(self)
158
+ process_action(action, @players[position])
159
+ position = next_position(position+1)
160
+ end
161
+ end
162
+
163
+ while(! settled?(position)) do
164
+ action = @players[position].act(self)
165
+ process_action(action, @players[position])
166
+ position = next_position(position+1)
167
+ end
168
+ end
169
+
170
+ def next_position(start)
171
+ position = start
172
+ position = 0 if position >= @players.length
173
+ while(@players[position].folded)
174
+ position += 1
175
+ position = 0 if position >= @players.length
176
+ end
177
+ position
178
+ end
179
+
180
+ def process_action(action, player)
181
+ case action
182
+ when :weak #fold or check
183
+ do_weak(player)
184
+ when :neutral #check or call
185
+ do_neutral(player)
186
+ when :aggressive #bet or raise (or call if capped)
187
+ if(@bets_in_round == BET_CAP)
188
+ do_neutral(player)
189
+ else
190
+ do_aggressive(player)
191
+ end
192
+ else
193
+ raise "Received unknown action #{action.to_s}"
194
+ end
195
+ end
196
+
197
+ def do_weak(player)
198
+ player.fold if owed(player) > 0.0
199
+ end
200
+
201
+ def do_neutral(player)
202
+ amt_owed = owed(player) # want to stay in but not raise, could be 0
203
+ player.pay(amt_owed)
204
+ @pot += amt_owed
205
+ end
206
+
207
+ def do_aggressive(player)
208
+ amt_owed = owed(player)
209
+ player.pay(amt_owed+bet_size)
210
+ @pot += amt_owed+bet_size
211
+ @bets_in_round += 1
212
+ end
213
+
214
+ def next_round
215
+ @bets_in_round = 0
216
+ @players.each { |p| p.next_round }
217
+ end
218
+
219
+ def num_active_players
220
+ active = 0
221
+ @players.each do |p|
222
+ active += 1 unless p.folded
223
+ end
224
+ active
225
+ end
226
+
227
+ def active_players
228
+ @players.reject { |p| p.folded }
229
+ end
230
+
231
+ def burn
232
+ @deck.deal
233
+ end
234
+
235
+ def deal
236
+ @board << @deck.deal
237
+ end
238
+ end
239
+ end