Thursday, May 5, 2011

Perl for the day: while loop construct


use strict;
use warnings;

# to play cricket in my perl program

print "\nPress any of 0 to 9\n\n";

use Term::ReadKey;
my $bat = 1;
my $player;
my $num;
my $team;
my $ran = 0;
my $spot = 0;
my @res;

ReadMode 'cbreak';

while (++$spot < 3) {
    print "Team: ", $spot, "\n";
    $num = 0;
    $team = 0;
    while (++$num < 11) {
        print "    Player ", $num, ": ";
        $player = 0;
        while ($bat != 0) {
            $bat = int(ReadKey(0));
            $ran = int(rand(12345)) + $bat;
            $bat = $ran % 6;
            if ($bat != 0) {
                print $bat, "+";
            }
            $player += $bat;
        }
        print "=", $player, "\n";
        $team += $player;
        $bat = 1;
    }
    print "  Total score = ", $team,"\n";
    $res[$spot] = $team;
}
if ($res[1] > $res[2]) {
    print "Team 1 Won\n";
} elsif ($res[2] > $res[1]) {
    print "Team 2 Won\n";
} else {
    print "Match Tie\n";
}

No comments:

Post a Comment