[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Hash Lookup Problem

Paul

10/25/2008 9:58:00 PM

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

1 Answer

Trans

10/25/2008 11:48:00 PM

0



On Oct 25, 5:58=A0pm, Paul <pedra...@gmail.com> wrote:
> I am writing a blackjack simulator. =A0I have classes like Dealer,
> Player, Deck, and Play_Strategy. =A0Play_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). =A0Then I have a series of hashes that I have
> set as instance variables instantiated as new hashes (ex. @lookuptable
> =3D Hash.new).
>
> The idea is that player1 (player1 =3D Player.new) will look up their
> hand as the key in the basic.action hash. =A0Then it will lookup the
> correct action in another hash based on the dealer.upcard.
>
> Here is the structure of the hashes.
>
> basic.lookup5 =3D {2 =3D> "Hit",3 =3D> "Hit",4 =3D> "Hit",5 =3D> "Hit",6 =
=3D>
> "Hit",7 =3D> "Hit",8 =3D> "Hit",9 =3D> "Hit","T" =3D> "Hit","A" =3D> "Hit=
"}
> ...(removed the full list of arrays to shorten the post)...
> basic.lookupAA =3D {2 =3D> "Split",3 =3D> "Split",4 =3D> "Split",5 =3D> "=
Split",
> 6 =3D> "Split",7 =3D> "Split",8 =3D> "Split",9 =3D> "Split","T" =3D> "Spl=
it","A"
> =3D> "Split"}
>
> basic.action =3D {5 =3D> basic.lookup5[dealer.upcard],6 =3D>
> basic.lookup6[dealer.upcard],7 =3D> basic.lookup7[dealer.upcard],8=3D>
> basic.lookup8[dealer.upcard],
> =A09=3D> basic.lookup9[dealer.upcard],10=3D> basic.lookup10[dealer.upcard=
],
> 11 =3D> basic.lookup11[dealer.upcard],12=3D>
> basic.lookup12[dealer.upcard],
> =A013 =3D> basic.lookup13[dealer.upcard],14=3D>
> basic.lookup14[dealer.upcard],15 =3D> basic.lookup15[dealer.upcard],16
> =3D> basic.lookup16[dealer.upcard],
> =A017 =3D> basic.lookup17[dealer.upcard], 18 =3D>
> basic.lookup18[dealer.upcard], 19 =3D> basic.lookup19[dealer.upcard], 20
> =3D> basic.lookup20[dealer.upcard],
> =A021 =3D> basic.lookup21[dealer.upcard],"A2" =3D>
> basic.lookupA2[dealer.upcard],"A3" =3D>
> basic.lookupA3[dealer.upcard],"A4" =3D> basic.lookupA4[dealer.upcard],
> =A0"A5"=3D> basic.lookupA5[dealer.upcard], "A6"=3D>
> basic.lookupA6[dealer.upcard],"A7"=3D>
> basic.lookupA7[dealer.upcard],"A8" =3D> basic.lookupA8[dealer.upcard],
> =A0"A9"=3D> basic.lookupA9[dealer.upcard], "22" =3D>
> basic.lookup22[dealer.upcard],"33"=3D>
> basic.lookup33[dealer.upcard],"44" =3D> basic.lookup44[dealer.upcard],
> =A0"55" =3D> basic.lookup55[dealer.upcard], "66" =3D>
> basic.lookup66[dealer.upcard],"77" =3D>
> basic.lookup77[dealer.upcard],"88" =3D> basic.lookup88[dealer.upcard],
> =A0"99" =3D> basic.lookup99[dealer.upcard], "TT"=3D>
> basic.lookupTT[dealer.upcard], "AA"=3D> basic.lookupAA[dealer.upcard]}
>
> Here is what I get...
> puts "Player should #{basic.action[player1.hand.cards]}." #This is the
> problem! =A0It is as if player.1.hand.cards is an empty array. =A0I 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. =A0As
> expected.

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

t