The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/perl

package App::Games::Domino;

use Games::Domino;
use Moo;
use namespace::clean;
use MooX::Options;

option 'verbose' => (is => 'ro', order => 1, default => sub { 0 }, doc => 'Play the game in verbose mode.');
option 'cheat'   => (is => 'ro', order => 2, default => sub { 0 }, doc => 'Play the game in cheat mode.'  );

sub run {
    my ($self) = @_;

    select(STDOUT);
    $|=1;

    my $game = Games::Domino->new(debug => $self->{verbose}, cheat => $self->{cheat});

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

    print {*STDOUT} $game->about_game,  "\n";
    $game->pause;
    print {*STDOUT} $game->how_to_play, "\n";
    $game->pause('Press any key to start the game...');

    my ($response);
    do {
        $game->screen->clear;
        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";
}

package main;

use strict; use warnings;

App::Games::Domino->new_with_options->run;