Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 87 lines (69 sloc) 2.143 kb
0cfbfcd Carl Mäsak generalized the move syntax regexes to arbritary board sizes
authored
1 use v6;
88eff0c initial version, rescued from email
Carl Masak authored
2
b2d3243 Carl Mäsak separated out game logic into Druid::Game
authored
3 use Druid::Game;
d04fa42 Carl Mäsak renamed s/Board/View/ as suggested by jonalv++
authored
4 use Druid::View;
5 use Druid::View::Text;
b0b5ddc Carl Mäsak separated out most player logic into Druid::Player
authored
6 use Druid::Player;
7 use Druid::Player::Human;
8 use Druid::Player::Computer;
88eff0c initial version, rescued from email
Carl Masak authored
9
b0b5ddc Carl Mäsak separated out most player logic into Druid::Player
authored
10 if any(@*ARGS) eq '--help' {
11 # RAKUDO: Heredocs.
12 say .substr(4) for split "\n", '
13 Druid. Implementation © Carl Mäsak 2008-2009. Rules © Cameron Browne.
88eff0c initial version, rescued from email
Carl Masak authored
14
b0b5ddc Carl Mäsak separated out most player logic into Druid::Player
authored
15 Usage:
16 % perl6 druid [switches]
09e6088 Carl Mäsak added the ability to pass
authored
17
b0b5ddc Carl Mäsak separated out most player logic into Druid::Player
authored
18 --size=<size> Give the board a size between 3 and 26.
19 --computer=[1|2] The computer controls player one or two.
20 --help Display this help text.
21 ';
88eff0c initial version, rescued from email
Carl Masak authored
22
b0b5ddc Carl Mäsak separated out most player logic into Druid::Player
authored
23 exit(0);
88eff0c initial version, rescued from email
Carl Masak authored
24 }
25
67ecf2f Carl Mäsak added --size parameter
authored
26 my $board_size = 8;
27 for @*ARGS -> $argument {
28 if $argument ~~ / '--size=' (\d+) / {
29 if $0 < (my $MIN_SIZE = 3) {
30 say "The board size may not be smaller than $MIN_SIZE.";
31 exit(1);
32 }
c3307c3 Carl Mäsak move coordinates are now collected via match objects
authored
33 elsif $0 > (my $MAX_SIZE = 26) {
67ecf2f Carl Mäsak added --size parameter
authored
34 say "The board size may not be larger than $MAX_SIZE.";
35 exit(1);
36 }
37 $board_size = +$0;
38 }
39 }
40
372511c Carl Mäsak s/Game_/Game/, jonathan++
authored
41 my Druid::Game $game .= new(:size($board_size));
d04fa42 Carl Mäsak renamed s/Board/View/ as suggested by jonalv++
authored
42 my Druid::View $view = Druid::View::Text.new(:$game);
88eff0c initial version, rescued from email
Carl Masak authored
43
ee044f9 Carl Mäsak turned the player menu into cmdline flags
authored
44 die 'Druid currently does not support computer-vs-computer play.'
b0b5ddc Carl Mäsak separated out most player logic into Druid::Player
authored
45 if any(@*ARGS) eq '--computer=1'&'--computer=2';
46 my Druid::Player ($player1, $player2);
47 $player1 = any(@*ARGS) eq '--computer=1'
48 ?? Druid::Player::Computer.new(:$game, :color(1))
49 !! Druid::Player::Human.new( :$game, :color(1));
50 $player2 = any(@*ARGS) eq '--computer=2'
51 ?? Druid::Player::Computer.new(:$game, :color(2))
52 !! Druid::Player::Human.new( :$game, :color(2));
5982277 introduced a computer player (which only makes random moves for now)
masak authored
53
b2d3243 Carl Mäsak separated out game logic into Druid::Game
authored
54 my $pass = /^ 'pass' | 'p' $/;
55
09e6088 Carl Mäsak added the ability to pass
authored
56 my $passes_in_a_row = 0;
88eff0c initial version, rescued from email
Carl Masak authored
57 loop {
b0b5ddc Carl Mäsak separated out most player logic into Druid::Player
authored
58 for $player1, $player2 -> $player {
88eff0c initial version, rescued from email
Carl Masak authored
59
b0b5ddc Carl Mäsak separated out most player logic into Druid::Player
authored
60 $view.show();
61
62 my $move = $player.choose_move();
88eff0c initial version, rescued from email
Carl Masak authored
63
09e6088 Carl Mäsak added the ability to pass
authored
64 if $move ~~ $pass {
65 if ++$passes_in_a_row >= 2 {
66 say "It's a draw.";
67 exit(0);
68 }
69 }
70 elsif $move !~~ $pass {
71 $passes_in_a_row = 0;
72 }
73
e2cbb4d Carl Mäsak [Druid::Game] s/make_move/make-move/
authored
74 $player.make-move($move);
7785135 now checks whether the last player made a winning move
masak authored
75
f63897f Carl Mäsak made twenty-ninth test pass
authored
76 if $game.finished() {
d04fa42 Carl Mäsak renamed s/Board/View/ as suggested by jonalv++
authored
77 $view.show();
883bd2a now prints a newline between the final board and the win message
masak authored
78 print "\n";
6cb87f3 now prints the board one last time before declaring a winner
masak authored
79
7785135 now checks whether the last player made a winning move
masak authored
80 say "$player won.";
81 exit(0);
82 }
88eff0c initial version, rescued from email
Carl Masak authored
83 }
84 }
3093d84 added modeline at the end for perl6.vim
masak authored
85
86 # vim: filetype=perl6
Something went wrong with that request. Please try again.