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

I was wondering if anyone had done anything related with the dice game craps in perl. Perhaps a perl ncurses craps or something like that. If not, anyone interested?

Buddha bless you.

Edit by tye

Replies are listed 'Best First'.
Re: Perl ncurses dice games?
by jryan (Vicar) on Sep 25, 2001 at 02:32 UTC

    When I was first learning perl quite some time ago, I wrote a casino-related program that was cheesily named "perlette". Its not really dice related, but it might give you a good start. To input bets: Name: (number, color, even/odd). Commands: Show bets, show winnings, spin, exit.

    #!/usr/bin/perl -w use strict; srand(time ^ ($$ + ($$<<15))); # ************* set comparment # to color my %compartments = (0 => 'green', 00 => 'green'); for (my $i=1; $i<=36; $i++) { $compartments{$i} = ($i<19) ? "red" : "black"; } my %money_per_player = (); my %bets = (); sub sbyvalue { $money_per_player{$b} == $money_per_player{$a}; } # ************* start main processing loop print "Place your bets!\n\n> "; while () { $_ = <>; chomp $_; if (m/(.+)(:\s)(\d+)/g) { if ($3 < 37 && $3 > 0) { my $money = 100; print "Hello $1! I haven't seen you before. You start wi +th $money.\n" unless exists $money_per_player{$1}; while ( my($key, $value) = each %money_per_player) { $money = $value if ($key eq $1); } $money_per_player{$1} = $money-1; $bets{$1} = $3; print "$1 bets on number $3.\n"; print "$1 now has \$$money_per_player{$1}.\n"; } elsif ($3 eq "00") { my $money = 100; print "Hello $1! I haven't seen you before. You start wi +th $money.\n" unless exists $money_per_player{$1}; while ( my($key, $value) = each %money_per_player) { $money = $value if ($key eq $1); } $money_per_player{$1} = $money-1; $bets{$1} = 37; print "$1 bets on number $3.\n"; print "$1 now has \$$money_per_player{$1}.\n"; } elsif ($3 == 0) { my $money = 100; print "Hello $1! I haven't seen you before. You start wi +th $money.\n" unless exists $money_per_player{$1}; while ( my($key, $value) = each %money_per_player) { $money = $value if ($key eq $1); } $money_per_player{$1} = $money-1; $bets{$1} = 0; print "$1 bets on number $3.\n"; print "$1 now has \$$money_per_player{$1}.\n"; } else { print "You have not entered a valid bet, please choose aga +in.\n"; } } elsif (m/(.+)(:\s)(.+)/g) { if ($3 eq "odd" || $3 eq "even" || $3 eq "black" || $3 eq "red +") { my $money = 100; print "Hello $1! I haven't seen you before. You start wi +th $money.\n" unless exists $money_per_player{$1}; while ( my($key, $value) = each %money_per_player) { $money = $value if ($key eq $1); } $money_per_player{$1} = $money-1; $bets{$1} = $3; print "$1 bets on $3.\n"; print "$1 now has \$$money_per_player{$1}.\n"; } else { print "You have not entered a valid bet, please choose aga +in.\n"; } } elsif (m/^exit$/) { exit; } elsif (m/^show bets$/) { while ( my($key, $value) = each %bets) { print "$key bets on $value.\n"; } } elsif (m/^show winnings$/) { foreach my $key (sort sbyvalue keys %money_per_player) { print "Now $key has \$$money_per_player{$key}.\n"; } } elsif (m/^spin$/) { print "The Perlette wheel is spinning......."; my $spin = int(rand(38)); $spin = "00" if ($spin == 37); print "$spin $compartments{$spin}\n"; while ( my($key, $value) = each %bets) { if ($value eq "odd") { $money_per_player{$key} += ($spin%2 == 1) ? 2 : 0; } elsif ($value eq "even") { $money_per_player{$key} += ($spin%2 == 0) ? 2 : 0; } elsif ($value eq "black") { $money_per_player{$key} += ($spin > 18) ? 2 : 0; } elsif ($value eq "red") { $money_per_player{$key} += ($spin < 19 && $spin !=0) ? + 2 : 0; } else { $money_per_player{$key} += ($value == $spin) ? 36 : 0; } } print "The players ranked by winnings:\n"; foreach my $key (sort sbyvalue keys %money_per_player) { print "Now $key has \$$money_per_player{$key}.\n"; } reset 'b'; print "\nPlace your bets!\n"; } else { print "You have not entered a valid command.\n"; } print "\n> "; }

    P.S. Don't make fun of the coding! I was a super-newbie then :)

Re: Perl ncurses dice games?
by jplindstrom (Monsignor) on Sep 25, 2001 at 02:04 UTC
    Well... I work for an online casino operator, so... yeah, I've done a few things :)

    But it's nothing I can open source or anything, although I have been thinking about a few basic classes for representing cards, various kinds of hands (Blackjack, Video Poker, Baccarat etc) and that kind of stuff, things that aren't at all specific to our business or our systems.

    I'm not too sure by boss has an immediate grasp of the advantages of a move like that though, so I think I'll let it rest for a while.

    /J

      Howdy!

      Games::Cards would not be a bad place to start...

      yours,
      Michael