#!perl

# Domino game script supplied by Games::Domino v0.18
# 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;

my ($response);
do {
    $game->show;
    do {
        my ($index);
        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);
        $game->show;
    } until ($game->is_over);

    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";
