Forum: Ruby Hash Lookup Problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Paul (Guest)
on 2008-10-26 01:59
(Received via mailing list)
I am writing a blackjack simulator.  I have classes like Dealer,
Player, Deck, and Play_Strategy.  Play_Strategy describes the strategy
a player will use.
The strategy basically says what a player should do (hit, stand, etc)
depending on the cards they are dealt and the dealer's upcard.

I created a new object for a basic strategy
(basic.Play_Strategy.new).  Then I have a series of hashes that I have
set as instance variables instantiated as new hashes (ex. @lookuptable
= Hash.new).

The idea is that player1 (player1 = Player.new) will look up their
hand as the key in the basic.action hash.  Then it will lookup the
correct action in another hash based on the dealer.upcard.

Here is the structure of the hashes.

basic.lookup5 = {2 => "Hit",3 => "Hit",4 => "Hit",5 => "Hit",6 =>
"Hit",7 => "Hit",8 => "Hit",9 => "Hit","T" => "Hit","A" => "Hit"}
...(removed the full list of arrays to shorten the post)...
basic.lookupAA = {2 => "Split",3 => "Split",4 => "Split",5 => "Split",
6 => "Split",7 => "Split",8 => "Split",9 => "Split","T" => "Split","A"
=> "Split"}

basic.action = {5 => basic.lookup5[dealer.upcard],6 =>
basic.lookup6[dealer.upcard],7 => basic.lookup7[dealer.upcard],8=>
basic.lookup8[dealer.upcard],
 9=> basic.lookup9[dealer.upcard],10=> basic.lookup10[dealer.upcard],
11 => basic.lookup11[dealer.upcard],12=>
basic.lookup12[dealer.upcard],
 13 => basic.lookup13[dealer.upcard],14=>
basic.lookup14[dealer.upcard],15 => basic.lookup15[dealer.upcard],16
=> basic.lookup16[dealer.upcard],
 17 => basic.lookup17[dealer.upcard], 18 =>
basic.lookup18[dealer.upcard], 19 => basic.lookup19[dealer.upcard], 20
=> basic.lookup20[dealer.upcard],
 21 => basic.lookup21[dealer.upcard],"A2" =>
basic.lookupA2[dealer.upcard],"A3" =>
basic.lookupA3[dealer.upcard],"A4" => basic.lookupA4[dealer.upcard],
 "A5"=> basic.lookupA5[dealer.upcard], "A6"=>
basic.lookupA6[dealer.upcard],"A7"=>
basic.lookupA7[dealer.upcard],"A8" => basic.lookupA8[dealer.upcard],
 "A9"=> basic.lookupA9[dealer.upcard], "22" =>
basic.lookup22[dealer.upcard],"33"=>
basic.lookup33[dealer.upcard],"44" => basic.lookup44[dealer.upcard],
 "55" => basic.lookup55[dealer.upcard], "66" =>
basic.lookup66[dealer.upcard],"77" =>
basic.lookup77[dealer.upcard],"88" => basic.lookup88[dealer.upcard],
 "99" => basic.lookup99[dealer.upcard], "TT"=>
basic.lookupTT[dealer.upcard], "AA"=> basic.lookupAA[dealer.upcard]}

Here is what I get...
puts "Player should #{basic.action[player1.hand.cards]}." #This is the
problem!  It is as if player.1.hand.cards is an empty array.  I get
"nil".

I ran some basic testing by seeing if other variables work, and they
do as follows:

puts player1.hand.inspect # returns an array of two cards.  As
expected.
puts dealer.hand.inspect # returns an array of two cards.  As
expected.
dealer.upcard = dealer.hand[-1] #returns a single integer.  As
expected.

puts "Player has #{player1.hand.value}." # this works too
puts "Dealer shows #{dealer.upcard}." #this works too
puts "Key is #{player1.hand.cards}." # this works and returns the key
as expected.


Any ideas why this doesn't work.  When I set this structure up without
objects it works fine.  In other words, instead of basic.action or
basic.lookup5 I just  made them variables not associated with any
class (ie. basic_action or basic_lookup5) and it is ok.  It must be
something with classes I don't understand.

Thanks for any guidance.

Paul
Thomas S. (Guest)
on 2008-10-26 02:48
(Received via mailing list)
On Oct 25, 5:58 pm, Paul <removed_email_address@domain.invalid> wrote:
>
> 6 => "Split",7 => "Split",8 => "Split",9 => "Split","T" => "Split","A"
> => basic.lookup16[dealer.upcard],
> basic.lookup22[dealer.upcard],"33"=>
> "nil".
>
> I ran some basic testing by seeing if other variables work, and they
> do as follows:
>
> puts player1.hand.inspect # returns an array of two cards.  As
> expected.

If player1.hand is an array, why do you think player1.hand.cards will
return anything at all?

t
This topic is locked and can not be replied to.