# Copyright 2006 Michael Lewis # Distributed under the terms of the GNU General Public License v2 require "PlayerHand" require "Table" class Player attr_reader :moveTable def initialize( money = 0.0 ) @hands = Array.new @money = money @bet_total = 0.0 @win_total = 0.0 @verbose = false @handIndex = 0 @moveTable = Array.new 10.times { @moveTable << Array.new } @moveTable.each { |i| 27.times { |j| i[j] = "h" } } m = @moveTable (1..4).each { |i| m[i][1] = "d" } (0..7).each { |i| m[i][2] = "d" } (0..8).each { |i| m[i][3] = "d" } (2..4).each { |i| m[i][4] = "s" } (0..4).each { |i| (5..8).each { |j| m[i][j] = "s" } } (0..9).each { |i| m[i][9] = "s" } (3..4).each { |i| m[i][10] = "d" } (3..4).each { |i| m[i][11] = "d" } (2..4).each { |i| m[i][12] = "d" } (2..4).each { |i| m[i][13] = "d" } (1..4).each { |i| m[i][14] = "d" } (0..6).each { |i| m[i][15] = "s" } (1..4).each { |i| m[i][15] = "d" } (0..9).each { |i| m[i][16] = "s" } (17..18).each do |j| (0..1).each { |i| m[i][j] = "l" } (2..5).each { |i| m[i][j] = "l" } end (3..4).each { |i| m[i][19] = "l" } (0..7).each { |i| m[i][20] = "d" } (0..4).each { |i| m[i][21] = "l" } (0..5).each { |i| m[i][22] = "l" } (23..26).each { |j| (0..9).each { |i| m[i][j] = "l" } } m[5][24] = "s" m[8][24] = "s" m[9][24] = "s" (0..9).each { |i| m[i][25] = "s" } end def mt @moveTable end def verbose( v ) @verbose = true end def place_bet @hands.clear @hands << PlayerHand.new( 1.0 ) @bet_total += 1.0 @money -= 1.0 end def win if @verbose then puts hand.to_s + "Win"end @money += hand.bet * 2 @win_total += hand.bet * 2 hand.over end def push if @verbose then puts hand.to_s + "Push" end @money += hand.bet @win_total += hand.bet hand.over end def lose if @verbose then puts hand.to_s + "Lose" end @bet = nil hand.over end def double @money -= hand.bet @bet_total += hand.bet hand.double end def blackjack if @verbose then puts hand.to_s + "Black Jack" end @money += hand.bet * 2.5 @win_total += hand.bet * 2.5 hand.over end def high_edge( e = 50 ) @win_total = e @bet_total = e end def low_edge( e = 50 ) @win_total = 0 @bet_total = e end def reset_edge @win_total = 0.0 @bet_total = 0.0 end def insurance #we don't want insurance by default return false end def player_edge @win_total / @bet_total end def house_edge 1.0 / player_edge end # "h" = hit # "s" = stand # "d" = double # "l" = split # We're getting our own hand passed back to us def move( table ) show = table.showing_card - 2 total = hand.hand_value # 0 = 3-8 total # 1 = 9 # 2..8 = 10..16 total # 9 = 17-21 total # 10 = S13 # 11..16 = S14-S19 # 17..26 = Pair of 2's to Pair of 11's # Precedence is higher to lower my_hand = 0; # Let's map the hand to the table #If two cards are the same if hand.length == 2 and hand[0] == hand[1] c = hand[0] my_hand = 15 + c elsif hand.soft? if total > 19 my_hand = 16 else my_hand = total - 3 end else if total <= 8 my_hand = 0 elsif total >= 17 my_hand = 9 else my_hand = total - 8 end end move = @moveTable[show][my_hand] move = "h" if move == "d" and hand.length > 2 # puts move + ",[" + show.to_s + "][" + my_hand.to_s + "]" return move end def turn @hands.each_index do |i| @handIndex = i yield @hands[ i ] end @handIndex = 0 end def split newHand = PlayerHand.new( hand.bet ) newHand.add_card hand.slice!( 1 ) @hands.insert( ( @handIndex + 1 ), newHand ) newHand.split hand.split @money -= hand.bet @bet_total += hand.bet #lock the hands if you split aces if hand[0] == 11 hand.lock newHand.lock end end protected #this returns the currently selected hand def hand @hands[ @handIndex ] end def reset_cash @money = 0 end end