Skip to content
Newer
Older
100755 126 lines (100 sloc) 2.95 KB
58ea3f4 @ericherman start with an ordered deck
authored Oct 10, 2011
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
0821ef5 @ericherman extract a Card class
authored Oct 17, 2011
5 use Card;
f4ed187 @ericherman extract a Deck class
authored Oct 17, 2011
6 use Deck;
23764bb @ericherman extract a Hand class
authored Oct 17, 2011
7 use Hand;
58ea3f4 @ericherman start with an ordered deck
authored Oct 10, 2011
8
11753b2 @ericherman change print function to return a string
authored Oct 17, 2011
9 sub display_cards {
8ac72e6 @ericherman perltidy
authored Oct 23, 2011
10 return join( ' ', map { $_->two_char() } @_ );
ce7436c @ericherman add a shuffle function
authored Oct 10, 2011
11 }
12
613c9e0 @ericherman move game visualizaton into a function
authored Dec 21, 2011
13 sub visualize_game {
14 my ( $house_hand, @hands ) = @_;
15
16 my @table_cards = @{ $house_hand->cards() };
17 print "on the table:\n";
18 print display_cards(@table_cards), "\n";
19 my $best_hand = $house_hand->best_hand();
20 print " ", $best_hand->{name}, " [ ",
21 join( ', ', @{ $best_hand->{rank} } ),
22 " ]:\n";
23 my @hand_cards = @{ $best_hand->{cards} };
24 print " [ ", display_cards(@hand_cards), " ]\n";
25
26 my $tie = 0;
27 my @winner = ('the table');
28
29 my $i = 0;
30 foreach my $hand (@hands) {
31 $i++;
32 print "\n";
33 print "player $i:\n";
34 print display_cards( @{ $hand->cards() }[0], @{ $hand->cards() }[1] ),
35 "\n";
36 my $player_hand = $hand->best_hand();
37 print " ", $player_hand->{name}, " [ ",
38 join( ', ', @{ $player_hand->{rank} } ), " ]:\n";
39 my @player_cards = @{ $player_hand->{cards} };
40 print " [ ", display_cards(@player_cards), " ]\n";
41
42 my $best_rank = $best_hand->{rank};
43 my $player_rank = $player_hand->{rank};
44 my $compare = Hand->compare_ranks( $player_rank, $best_rank );
45
46 if ( $compare > 0 ) {
47 $best_hand = $player_hand;
48 $tie = 0;
49 @winner = ("player $i");
50 }
51 elsif ( $compare == 0 ) {
52 $tie = 1;
53 push @winner, "player $i";
54 }
55 }
56
57 print "\n";
58 my $msg = ($tie) ? "The winners are: " : "The winner is: ";
59 print $msg, join( ', ', @winner ), "\n";
60 return $best_hand;
61 }
a86e7fc @ericherman use deck to deal cards to hands
authored Oct 17, 2011
62 my $deck = Deck->new();
58ea3f4 @ericherman start with an ordered deck
authored Oct 10, 2011
63
cdecdaf @ericherman add two_char printing method to Card
authored Oct 17, 2011
64 #print "initializing deck:\n";
a86e7fc @ericherman use deck to deal cards to hands
authored Oct 17, 2011
65 #print $deck->display_cards(), "\n";
cdecdaf @ericherman add two_char printing method to Card
authored Oct 17, 2011
66 #print "\n";
a86e7fc @ericherman use deck to deal cards to hands
authored Oct 17, 2011
67 $deck->shuffle();
8ac72e6 @ericherman perltidy
authored Oct 23, 2011
68
cdecdaf @ericherman add two_char printing method to Card
authored Oct 17, 2011
69 #print "shuffled deck:\n";
a86e7fc @ericherman use deck to deal cards to hands
authored Oct 17, 2011
70 #print $deck->display_cards(), "\n";
f4ed187 @ericherman extract a Deck class
authored Oct 17, 2011
71 #print "\n";
72
a86e7fc @ericherman use deck to deal cards to hands
authored Oct 17, 2011
73 my $num_players = 5;
db7eb06 @ericherman start to detect hand types
authored Oct 17, 2011
74
9d03dbd @ericherman re-visualize after every round
authored Dec 21, 2011
75 my $house_hand = Hand->new();
a86e7fc @ericherman use deck to deal cards to hands
authored Oct 17, 2011
76 my @hands;
8ac72e6 @ericherman perltidy
authored Oct 23, 2011
77 for ( my $i = 0 ; $i < $num_players ; $i++ ) {
a86e7fc @ericherman use deck to deal cards to hands
authored Oct 17, 2011
78 push @hands, Hand->new();
79 }
80
8ac72e6 @ericherman perltidy
authored Oct 23, 2011
81 for my $i ( 1 .. 2 ) {
a86e7fc @ericherman use deck to deal cards to hands
authored Oct 17, 2011
82 foreach my $hand (@hands) {
8ac72e6 @ericherman perltidy
authored Oct 23, 2011
83 $hand->add_card( $deck->deal_card() );
a86e7fc @ericherman use deck to deal cards to hands
authored Oct 17, 2011
84 }
85 }
86
9d03dbd @ericherman re-visualize after every round
authored Dec 21, 2011
87 visualize_game( $house_hand, @hands );
88 my $userinput = <STDIN>;
89 chomp($userinput);
90
91 my $burn = $deck->deal_card();
a86e7fc @ericherman use deck to deal cards to hands
authored Oct 17, 2011
92
8ac72e6 @ericherman perltidy
authored Oct 23, 2011
93 for my $i ( 1 .. 3 ) { #flop
94 my $card = $deck->deal_card();
95 foreach my $hand ( $house_hand, @hands ) {
96 $hand->add_card($card);
97 }
a86e7fc @ericherman use deck to deal cards to hands
authored Oct 17, 2011
98 }
99
9d03dbd @ericherman re-visualize after every round
authored Dec 21, 2011
100 visualize_game( $house_hand, @hands );
101 $userinput = <STDIN>;
102 chomp($userinput);
103
a86e7fc @ericherman use deck to deal cards to hands
authored Oct 17, 2011
104 $burn = $deck->deal_card();
105
106 my $turn = $deck->deal_card();
8ac72e6 @ericherman perltidy
authored Oct 23, 2011
107 foreach my $hand ( $house_hand, @hands ) {
a86e7fc @ericherman use deck to deal cards to hands
authored Oct 17, 2011
108 $hand->add_card($turn);
109 }
110
9d03dbd @ericherman re-visualize after every round
authored Dec 21, 2011
111 visualize_game( $house_hand, @hands );
112 $userinput = <STDIN>;
113 chomp($userinput);
114
a86e7fc @ericherman use deck to deal cards to hands
authored Oct 17, 2011
115 $burn = $deck->deal_card();
116
117 my $river = $deck->deal_card();
8ac72e6 @ericherman perltidy
authored Oct 23, 2011
118 foreach my $hand ( $house_hand, @hands ) {
a86e7fc @ericherman use deck to deal cards to hands
authored Oct 17, 2011
119 $hand->add_card($river);
120 }
db7eb06 @ericherman start to detect hand types
authored Oct 16, 2011
121
613c9e0 @ericherman move game visualizaton into a function
authored Dec 21, 2011
122 my $best_hand = visualize_game( $house_hand, @hands );
9f672a8 @ericherman rank the hands
authored Oct 23, 2011
123
ab27e49 @ericherman better reporting
authored Oct 24, 2011
124 printf( "\twith a %s (odds over %s to 1 against)\n",
125 $best_hand->{name}, $best_hand->{rank}->[0] );
Something went wrong with that request. Please try again.