Skip to content

HTTPS clone URL

Subversion checkout URL

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