#!perl

use strict; use warnings;
use Games::TicTacToe;

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

my $response = 'Y';
while (defined($response)) {
    if ($response =~ /^Y$/i) {
        my $tictactoe = Games::TicTacToe->new;
        print {*STDOUT} $tictactoe->getGameBoard();
        $tictactoe->addPlayer();
        my $index = 1;
        while (!$tictactoe->isGameOver()) {
            $tictactoe->play();
            print {*STDOUT} $tictactoe->getGameBoard() if ($index % 2 == 0);
            $index++;
        }

        print {*STDOUT} "Do you wish to continue (Y/N)? ";
        $response = <STDIN>;
        chomp($response);
    }
    elsif ($response =~ /^N$/i) {
        print {*STDOUT} "Thank you.\n";
        last;
    }
    elsif ($response !~ /^[Y|N]$/i) {
        print {*STDOUT} "Invalid response, please enter (Y/N): ";
        $response = <STDIN>;
        chomp($response);
    }
}
