#! Perl -w # shutthebox.pl -- classic game use strict; my($roll1, $roll2, $c1, @c2, @opts, $choice, $score, %pegs); my $cnum="0"; my $endgame="0"; srand; sub assign { foreach my $num ("1".."12") { $pegs{$num} = 1; } } sub show_board { print "\n\n"; foreach my $num ("1".."12") { if ($pegs{$num}) { print "$num "; } else { print "# "; } } print "\n"; } sub roll { print "Roll was: "; $roll1 = 1 + int rand(6); $roll2 = 1 + int rand(6); print "$roll1 and $roll2"; } sub find_combos { $c1 = $roll1 + $roll2; @c2 = ($roll1, $roll2); } sub process_combos { $cnum = "0"; @opts = undef; print "\nOptions: \n"; if ($pegs{$c1}) { print "\t".++$cnum.") Put down $c1\n"; $opts[$cnum] = $c1; } unless ($c2[0] == $c2[1]) { if ($pegs{$c2[0]} && $pegs{$c2[1]}) { print "\t".++$cnum.") Put down $c2[0] and $c2[1]\n"; $opts[$cnum] = "13"; } } foreach("1".."12"){ unless (defined($pegs{$_})) { $endgame++; } } if ($endgame == 12){ print "YOU WON!"; exit; } unless($opts[1]) { print "\tnone\nYOU LOSE!\n"; foreach my $left ("1".."12") { if ($pegs{$left}) { $score += $left; } } print "\tScore: $score (lower is best)"; exit; } print "Which option? "; chomp($choice=<STDIN>); if ($opts[$choice] == $c1) { $pegs{$c1} = 0; } elsif ($opts[$choice] == 13 && $choice <= 2) { $pegs{$c2[0]} = 0; $pegs{$c2[1]} = 0; } else { print "Invalid option choice!\n"; process_combos(); } } assign(); while(1) { show_board(); roll(); find_combos(); process_combos(); }

In reply to Shut the Box : 12 Number Edition by munchie

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.