Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
85 lines (67 sloc) 2.06 KB
use v6;
use Druid::Game;
use Druid::View::Text;
use Druid::Player::Human;
use Druid::Player::Computer;
if any(@*ARGS) eq '--help' {
# RAKUDO: Heredocs.
say .substr(4) for split "\n", '
Druid. Implementation © Carl Mäsak 2008-2009. Rules © Cameron Browne.
Usage:
% perl6 druid [switches]
--size=<size> Give the board a size between 3 and 26.
--computer=[1|2] The computer controls player one or two.
--help Display this help text.
';
exit(0);
}
my $board-size = 8;
for @*ARGS -> $argument {
if $argument ~~ / '--size=' (\d+) / {
if $0 < (my $MIN_SIZE = 3) {
say "The board size may not be smaller than $MIN_SIZE.";
exit(1);
}
elsif $0 > (my $MAX_SIZE = 26) {
say "The board size may not be larger than $MAX_SIZE.";
exit(1);
}
$board-size = +$0;
}
}
my Druid::Game $game .= new(:size($board-size));
my Druid::View $view = Druid::View::Text.new(:$game);
die 'Druid currently does not support computer-vs-computer play.'
if any(@*ARGS) eq '--computer=1'&'--computer=2';
my Druid::Player ($player1, $player2);
$player1 = any(@*ARGS) eq '--computer=1'
?? Druid::Player::Computer.new(:$game, :color(1))
!! Druid::Player::Human.new( :$game, :color(1));
$player2 = any(@*ARGS) eq '--computer=2'
?? Druid::Player::Computer.new(:$game, :color(2))
!! Druid::Player::Human.new( :$game, :color(2));
my $pass = /^ 'pass' | 'p' $/;
my $passes-in-a-row = 0;
loop {
for $player1, $player2 -> $player {
$view.show();
my $move = $player.choose-move();
if $move ~~ $pass {
if ++$passes-in-a-row >= 2 {
say "It's a draw.";
exit(0);
}
}
elsif $move !~~ $pass {
$passes-in-a-row = 0;
}
$player.make-move($move);
if $game.finished() {
$view.show();
print "\n";
say "$player won.";
exit(0);
}
}
}
# vim: filetype=perl6
Jump to Line
Something went wrong with that request. Please try again.