#!perl

# Domino game script supplied by Games::Domino v0.25
# Author: Mohammad S Anwar (mohammad.anwar at yahoo.com)

use strict; use warnings;
use Games::Domino;

select(STDOUT);
$|=1;

$SIG{'INT'} = sub {
    print {*STDOUT} "\n\nCaught Interrupt (^C), Aborting the game.\n"; exit(1);
};

my $game = Games::Domino->new;
print {*STDOUT} $game->instructions, "\n";

my ($response);
do {
    my $move = 1;
    do {
        my ($index);
        if ($move % 2 == 1) {
            print {*STDOUT} $game->show, "\n";
            do {
                print {*STDOUT} "Pick your tile [" . $game->get_available_tiles . "] or [B]? ";
                $index = <STDIN>;
                chomp $index;
            } until ($game->is_valid_tile($index));
        }

        $game->play($index);
        $move++;

    } until ($game->is_over);

    print {*STDOUT} $game->show, "\n";
    print {*STDOUT} $game->result, "\n";
    $game->reset;

    do {
        print {*STDOUT} "Do you wish to continue (Y/N)? ";
        $response = <STDIN>;
        chomp($response);
    } until (defined $response && ($response =~ /^[Y|N]$/i));

} until ($response =~ /^N$/i);

print {*STDOUT} "\nThank you.\n";
