#!perl

use strict; use warnings;
use Games::Cards::Pair;

$|=1;

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

my $game = Games::Cards::Pair->new({debug => 1});

my ($count);
do {
    print {*STDOUT} "How many cards do you want to play with? (min:12, max:54): ";
    $count = <STDIN>;
    chomp($count);
} until ($game->is_valid_card_count($count));

$game->cards($count);

my ($response);
do {
    $game->init;
    print {*STDOUT} $game->get_board, "\n";
    print {*STDOUT} "You have 30 seconds to memorise the card positions.\n";
    sleep 30;
    $game->screen->clear;

    do {
        my ($cards);
        print {*STDOUT} $game->as_string(1);
        do {
            print {*STDOUT} "Pick pair of cards: ";
            $cards = <STDIN>;
            chomp($cards);
        } until ($cards =~ /^\d+\,\d+$/);

        $game->play($cards);

    } until ($game->is_over);

    print "\nTotal moves: " . $game->count . "\n";

    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} "Thank you.\n";
