Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Poker problem

by gman1983 (Novice)
on Dec 02, 2008 at 14:08 UTC ( [id://727417]=perlquestion: print w/replies, xml ) Need Help??

gman1983 has asked for the wisdom of the Perl Monks concerning the following question:

Some guys helped me on here yesterday. Thanks. The code takes poker hands and analyzes the hand I have "AsAc" and removes either of my cards from all hands in the array of arrays. It's almost done, but now I need to be able to reference the array of array with updated hands. example: I need to be able to reference @theirHands@hand3 after I've removed all hands that have my cards, because later I will randomly pick hands from those arrays and assign them to other players,and do the proccess all over again. It's a poker iterator that finds prwin values and i want to remove "known" cards so they can't be reassigned. The process with happen 10,0000 times to find prob of situation happening. Please help me Perl Monks.
use strict; use warnings; my @hand3 = qw/KsQs JsAs/; my @hand4 = qw/AsKc 2s3s/; my @hand899 = qw/As3c/; my @theirHands = (\@hand3, \@hand4, \@hand899); my $myHand = "AcAs"; my %myCards = map {$_ => 0} $myHand =~ /(.{2})/g; print "My hand: ", join '', keys %myCards, "\n"; for my $hands (@theirHands) { # If any of my cards match an opposing hands' card, reject the opp +osing hand my @retained; for my $hand (@$hands) { next if grep {exists $myCards{$_}} $hand =~ /(.{2})/g; push @retained, $hand; } print "@retained\n"; }
Prints: My hand: AsAc KsQs 2s3s

Replies are listed 'Best First'.
Re: Poker problem
by zentara (Archbishop) on Dec 02, 2008 at 14:33 UTC
    because later I will randomly pick hands from those arrays and assign them to other players,and do the proccess all over again. It's a poker iterator that finds prwin values and i want to remove "known" cards so they can't be reassigned.

    Something sounds fishy, probability wise if you are saving hands, and reassigning them, with a "known cards" factor. Maybe you could amplify on what you are trying to test?

    From my limited understanding of your description, wouldn't it be easier to approach it from another angle? Instead of trying to "remove known cards so they can't be reassigned", start with a hash of cards as keys, with the values being the number of that card in the deck, then decrement 1 each time the card is dealt?


    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Poker problem
by JavaFan (Canon) on Dec 02, 2008 at 14:29 UTC
    This doesn't solve your direct problem (but mostly because I don't know what you want), but, considering that there are only 52 possible cards, why not represent hands as bitstrings? In that case, it's easy to subtract a known hand from the set of possible cards (just add (&) with the negation (~)).
Re: Poker problem
by YuckFoo (Abbot) on Dec 02, 2008 at 21:31 UTC
    Frame anything as a poker problem and I'll probably bite.

    YuckCall

    #!/usr/bin/perl use strict; use Storable qw(dclone); use Data::Dumper; my $players = { big_slicks => { list => [qw(AsKs AhKh AdKd AcKc)], }, big_hearts => { list => [qw(AhKh KhQh QhJh JhTh)], }, ace_ragman => { list => [qw(As7c Ah6d Ac8s Ad9h)], }, }; my $my_hand = 'AcAs'; for (1..10) { my $hands = iterate($players, $my_hand); $hands->{my_hand} = $my_hand; print Dumper $hands; } #----------------------------------------------------------- sub iterate { my $players = shift; my $my_hand = shift; $players = dclone($players); for my $card ($my_hand =~ /(.{2})/g) { update($players, $card); } my $player_hands = {}; my @player_keys = keys(%$players); while (@player_keys) { my $player = splice(@player_keys, rand(@player_keys), 1); my $hand_list = $players->{$player}{list}; my $pick = splice(@$hand_list, rand(@$hand_list), 1); for my $card ($pick =~ /(.{2})/g) { update($players, $card); } $player_hands->{$player} = $pick; } return $player_hands; } #----------------------------------------------------------- sub update { my $players = shift; my $card = shift; for my $player (keys(%$players)) { my @new_list = grep { $_ !~ m/$card/ } @{$players->{$player}{list} +}; $players->{$player}{list} = \@new_list; } }
Re: Poker problem
by gman1983 (Novice) on Dec 02, 2008 at 14:36 UTC
    No thanks, I know what you're talking about, but it's easier this way. It's just finding Prwin values from handranges I KNOW the opponents play, give them a hand at random and remove it from the other player's hand range. It has to work this way, because I'm using a modedule Games:Poker, that works with hand names.
      If you absolutely need to do it that way, you will need to save a record of all hands dealt. Setup a hash, with keys like game_number, hand_number, etc, and store it all. Then you can reclaim it at will. Setting up your deeply nested hash structure, so it works well with the module you are using may take some ingenuity on your part. You probably want to feed a sub, the game number, player, hand number, and get back an array ref. You may even need to use a db, to prevent your hash from consuming too much memory.

      I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Poker problem
by olus (Curate) on Dec 03, 2008 at 13:34 UTC

    gman1983

    I don't fully understand your problem, but it seems that you are having problems dealing with having duplicate cards in players hands. To avoid that, I suggest that you deal with it right from the start, that is, right when you are dealing the cards to the players. So my suggestion is:

    # build the original deck of cards my @card_values = (2, 3, 4, 5, 6, 7, 8, 9, 10, 'V', 'D', 'R', 'A'); my @hearts = map {$_." h"} @card_values; my @clubs = map {$_." c"} @card_values; my @diamonds = map {$_." d"} @card_values; my @spades = map {$_." s"} @card_values; # scramble the deck of cards sub scramble { my @original_deck = (@hearts, @clubs, @diamons, @spades); @deck = (); while (@original_deck) { push @deck, splice(@original_deck, rand(@original_deck), 1); } }

    Now you have a pile (array) of 52 scrambled, and not repeated, cards that you can start shifting to deal them to the players. Whenever you want to give a card, you just shift one from the @deck. For instance, if you want to give me a hand of 5 cards:

    my @hand = (); push @hand, shift @deck for 0..4;

    That was my approach when I wrote the single player Place your bets

Re: Poker problem
by gman1983 (Novice) on Dec 03, 2008 at 01:13 UTC
    Is there a way to access the elements of the $hands. I now need to access the cards to remove them from the real deck. Thanks,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://727417]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-16 17:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found