Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: Poker problem

by olus (Curate)
on Dec 03, 2008 at 13:34 UTC ( #727669=note: print w/replies, xml ) Need Help??

in reply to Poker problem


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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://727669]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2018-06-18 04:30 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (107 votes). Check out past polls.