#!/usr/bin/perl

# Games::Checkers, Copyright (C) 1996-2012 Mikhael Goikhman, migo@cpan.org
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Try --help for usage.

use strict;
use warnings;

use FindBin;
use lib "$FindBin::Bin/../lib";
use Getopt::Long qw(:config no_ignore_case bundling);

use Games::Checkers::Game;

my $script_name = ($0 =~ m:([^/]+)$:, $1);
my $no_menu = 0;
my $edit_board = 0;
my $size = undef;
my $board = undef;  # here the string, but could be Board too
my $black = 0;
my $level = 5;
my $delay = 1;
my $random = 'none';  # '' means both, 'w' or 'b' - white or black only
my $use_term = 0;  # 0 means detect automatically
my $dumb_term = 0;
my $dumb_chars = 0;
my $fullscreen = 0;
my $max_move_num = 400;
my $variant = undef;  # means 'default'

sub show_help (;$) {
	my $is_error = shift || 0;
	my $out = $is_error ? \*STDERR : \*STDOUT;
	my $usage = qq{
		Usage: $script_name [OPTIONS]
		The automatic Checkers gameplay.

		Options:
			-h --help          show this help and exit
			-s --size SIZE     specify the board size, like "12" or "10x8"
			-e --edit          allow editing board pieces before the start
			-n --no-menu       skip interactive menu and start to play immediately
			-b --board STR     init board with non-default setup, like "b4,d2/a3/h6"
			-B --black         cause pieces of the opposite color to start the game
			-l --level N       strength level (default: $level)
			-d --delay N       pause in seconds between the moves (default: $delay)
			-r --random [w|b]  perform random (not best) moves for both/white/black
			-t --use-term      use terminal even if graphical frontend is available
			-T --dumb-term     do not position terminal cursor
			-C --dumb-chars    do not use fancy drawing characters
			-f --fullscreen    start in fullscreen mode (incompatible with -t)
			-m --move-num N    limit the game moves to the number
			-g --give-away     change rules to "give away"
			-v --variant NAME  'russian', 'english', 'spanish', 'international' etc.
	};
	$usage =~ s/^\n//; $usage =~ s/^\t\t?//mg;
	print $out $usage;
	exit $is_error;
}

GetOptions(
	"h|help"        => sub { show_help(0) },
	"s|size=s"      => \$size,
	"n|no-menu"     => \$no_menu,
	"e|edit"        => \$edit_board,
	"b|board=s"     => \$board,
	"B|black"       => \$black,
	"l|level=i"     => \$level,
	"d|delay=s"     => \$delay,
	"r|random:s"    => \$random,
	"t|use-term!"   => \$use_term,
	"T|dumb-term!"  => \$dumb_term,
	"C|dumb-chars!" => \$dumb_chars,
	"f|fullscreen!" => \$fullscreen,
	"m|move-num=s"  => \$max_move_num,
	"g|give-away!"  => \$ENV{GIVE_AWAY},
	"v|variant=s"   => \$variant,
) || show_help(1);

my $game = Games::Checkers::Game->new(
	variant => $variant,
	size => $size,
	board => $board,
	black => $black,
	title => join(' - ', ("Computer (lv $level)") x 2),
	level => $level,
	random => $random eq 'none' ? 0 : $random || 1,
	max_move_num => $max_move_num,
	show_menu => !$no_menu,
	edit_board => $edit_board,
	use_term => $use_term,
	dumb_term => $dumb_term,
	dumb_chars => $dumb_chars,
	fullscreen => $fullscreen,
);

$game->show_board;

my $num_moves = 0;
while ($game->can_move) {
	$game->sleep($delay);
	last if $game->is_max_move_num_reached;
	$game->show_move($game->choose_move);
	$game->show_board;
}

$game->show_result;
