| 1 | package Game::Reversi::Terminal; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use Object::Proto::Sugar -types; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Game::Reversi; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Game::Reversi::Board; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Game::Reversi::Bot; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use Game::Reversi::Notation; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use Game::Reversi::Opening; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use Game::Terminal::Kit::History; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 5 | ▶ | use Game::Terminal::Kit::Keys; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 5 | ▶ | use Game::Terminal::Kit::Run; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 5 | ▶ | use Game::Terminal::Kit::Screen; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 5 | ▶ | use Game::Terminal::Kit::Steer; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 5 | ▶ | use Getopt::Long (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 5 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 5 | my %GLYPH = ( | |||||||||||||||||||||||||||||||||||||||||
| 23 | b => { wide => "\x{25CF}", ascii => 'X' }, | ||||||||||||||||||||||||||||||||||||||||||
| 24 | w => { wide => "\x{25CB}", ascii => 'O' }, | ||||||||||||||||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 5 | my %TURNED = ( | |||||||||||||||||||||||||||||||||||||||||
| 28 | b => { wide => "\x{25C9}", ascii => 'x' }, | ||||||||||||||||||||||||||||||||||||||||||
| 29 | w => { wide => "\x{25CE}", ascii => 'o' }, | ||||||||||||||||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 5 | my %NAME = (b => 'dark', w => 'light'); | |||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 5 | my %LINE = ( | |||||||||||||||||||||||||||||||||||||||||
| 34 | wide => { h => "\x{2500}", v => "\x{2502}", tl => "\x{250C}", tr => "\x{2510}", | ||||||||||||||||||||||||||||||||||||||||||
| 35 | bl => "\x{2514}", br => "\x{2518}", t => "\x{252C}", b => "\x{2534}", | ||||||||||||||||||||||||||||||||||||||||||
| 36 | l => "\x{251C}", r => "\x{2524}", x => "\x{253C}" }, | ||||||||||||||||||||||||||||||||||||||||||
| 37 | ascii => { h => '-', v => '|', tl => '+', tr => '+', bl => '+', br => '+', | ||||||||||||||||||||||||||||||||||||||||||
| 38 | t => '+', b => '+', l => '+', r => '+', x => '+' }, | ||||||||||||||||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 5 | my %INK = (b => '1', w => '2', '*' => '33', grid => '2', | |||||||||||||||||||||||||||||||||||||||||
| 42 | turned => '1;32', placed => '1;36', cursor => '1;7'); | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 5 | my %STEP = (left => -1, up => -1, back_tab => -1, | |||||||||||||||||||||||||||||||||||||||||
| 45 | right => 1, down => 1, tab => 1); | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | has in => ( | ||||||||||||||||||||||||||||||||||||||||||
| 48 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 49 | isa => Any, | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 16 | ▶ | default => sub { \*STDIN } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 5 | ); | |||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | has out => ( | ||||||||||||||||||||||||||||||||||||||||||
| 54 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 55 | isa => Any, | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 7 | ▶ | default => sub { \*STDOUT } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 5 | ); | |||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | has level => ( | ||||||||||||||||||||||||||||||||||||||||||
| 60 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 61 | isa => Int, | ||||||||||||||||||||||||||||||||||||||||||
| 62 | default => 3 | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 5 | ); | |||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | has variant => ( | ||||||||||||||||||||||||||||||||||||||||||
| 66 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 67 | isa => Str, | ||||||||||||||||||||||||||||||||||||||||||
| 68 | default => 'historic' | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 5 | ); | |||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | has colour => ( | ||||||||||||||||||||||||||||||||||||||||||
| 72 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 73 | isa => Str, | ||||||||||||||||||||||||||||||||||||||||||
| 74 | default => 'b' | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 5 | ); | |||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | has seed => ( | ||||||||||||||||||||||||||||||||||||||||||
| 78 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 79 | default => 'terminal' | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 5 | ); | |||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | has [qw/quiet ascii/] => ( | ||||||||||||||||||||||||||||||||||||||||||
| 83 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 84 | isa => Bool, | ||||||||||||||||||||||||||||||||||||||||||
| 85 | default => 0 | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 5 | ); | |||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | has game => ( | ||||||||||||||||||||||||||||||||||||||||||
| 89 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 90 | isa => Any | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 5 | ); | |||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 5 | has last_move => (is => 'rw'); | |||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 5 | has keysource => (is => 'rw'); | |||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 40 | ▶ | has pending => (is => 'rw', default => sub { [] }); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 5 | has [qw/keyboard screen/] => (is => 'rw'); | |||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 5 | has history => (is => 'rw'); | |||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 5 | has _picking => (is => 'rw', init_arg => 'picking', private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | has _wants_ansi => ( | ||||||||||||||||||||||||||||||||||||||||||
| 106 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 107 | init_arg => 'ansi', | ||||||||||||||||||||||||||||||||||||||||||
| 108 | private => 1 | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 5 | ); | |||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 40 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=9 SCAR=22.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 40 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 40 | ▶ | $self->keyboard(Game::Terminal::Kit::Keys->new( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | in => $self->in, | ||||||||||||||||||||||||||||||||||||||||||
| 116 | pending => $self->pending, | ||||||||||||||||||||||||||||||||||||||||||
| 117 | ($self->keysource ? (keysource => $self->keysource) : ()), | ||||||||||||||||||||||||||||||||||||||||||
| 118 | )) unless $self->keyboard; | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 40 | ▶ | $self->screen(Game::Terminal::Kit::Screen->new( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | out => $self->out, | ||||||||||||||||||||||||||||||||||||||||||
| 121 | interactive => $self->interactive, | ||||||||||||||||||||||||||||||||||||||||||
| 122 | colour => $self->ansi, | ||||||||||||||||||||||||||||||||||||||||||
| 123 | ascii => $self->ascii, | ||||||||||||||||||||||||||||||||||||||||||
| 124 | )) unless $self->screen; | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 40 | $self->screen->utf8->flush_on; | |||||||||||||||||||||||||||||||||||||||||
| 126 | $self->history(Game::Terminal::Kit::History->new( | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 192 | ▶ | save => sub { $_[0]->to_text }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 5 | ▶ | load => sub { Game::Reversi->from_text($_[0]) }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 40 | ▶ | )) unless $self->history; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 40 | ▶ | die "Game::Reversi::Terminal: colour must be b or w" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->colour eq "b" or $self->colour eq "w"
MC/DC100%2/2
$self->colour eq "b" or $self->colour eq "w"
$self->colour eq "b"
$self->colour eq "w"
Truth table100%3/3
A or B
A= $self->colour eq "b"B= $self->colour eq "w"
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | unless $self->colour eq 'b' || $self->colour eq 'w'; | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 39 | ▶ | die "Game::Reversi::Terminal: there is no variant '" . $self->variant . "'" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | unless Game::Reversi::Opening->describes($self->variant); | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | die 'Game::Reversi::Terminal: there is no level ' . $self->level | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 38 | ▶ | unless grep { $_ == $self->level } Game::Reversi::Bot->levels; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 37 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 12692 | ▶ | sub ansi { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ansi: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 12692 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 144 | 12692 | ▶ | return 0 if Game::Terminal::Kit::Screen::no_colour(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 12690 | ▶ | return $self->_wants_ansi ? 1 : 0 if defined $self->_wants_ansi; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 10526 | return $self->interactive; | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 435 | ▶ | sub _reader { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_reader: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 435 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 151 | 435 | my $keyboard = $self->keyboard; | |||||||||||||||||||||||||||||||||||||||||
| 152 | 435 | $keyboard->in($self->in); | |||||||||||||||||||||||||||||||||||||||||
| 153 | 435 | $keyboard->pending($self->pending); | |||||||||||||||||||||||||||||||||||||||||
| 154 | 435 | ▶ | $keyboard->keysource($self->keysource) if $self->keysource; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 435 | return $keyboard; | |||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | 12647 | ▶ | sub _writer { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_writer: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 12647 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 160 | 12647 | my $screen = $self->screen; | |||||||||||||||||||||||||||||||||||||||||
| 161 | 12647 | $screen->out($self->out); | |||||||||||||||||||||||||||||||||||||||||
| 162 | 12647 | $screen->interactive($self->interactive); | |||||||||||||||||||||||||||||||||||||||||
| 163 | 12647 | $screen->colour($self->ansi); | |||||||||||||||||||||||||||||||||||||||||
| 164 | 12647 | return $screen; | |||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | 11662 | ▶ | sub _paint { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_paint: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | 11662 | my ($self, $glyph, $key) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 169 | 11662 | return $self->_writer->paint($glyph, $INK{$key}); | |||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | 926 | ▶ | sub _say { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_say: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | 926 | my ($self, @lines) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 174 | 926 | $self->_writer->say(@lines); | |||||||||||||||||||||||||||||||||||||||||
| 175 | 926 | return; | |||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | 173 | ▶ | sub _ask { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_ask: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 173 | my ($self, $prompt) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 180 | 173 | my $line = $self->_reader->read_line($prompt, $self->out); | |||||||||||||||||||||||||||||||||||||||||
| 181 | 173 | ▶ | return undef unless defined $line; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | 172 | $self->_say(''); | |||||||||||||||||||||||||||||||||||||||||
| 183 | 172 | return lc $line; | |||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | 5956 | ▶ | sub glyph { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
glyph: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | 5956 | my ($self, $colour) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 188 | 5956 | ▶ | return $GLYPH{$colour}{ $self->ascii ? 'ascii' : 'wide' }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | 253 | ▶ | sub board_text { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
board_text: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | 253 | my ($self, $game, $colour, %o) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 193 | 1464 | my %legal = map { $_->square => 1 } | |||||||||||||||||||||||||||||||||||||||||
| 194 | 253 | ▶ | @{ defined $colour ? $game->legal($colour) : [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | 253 | ▶ | my %flipped = map { $_ => 1 } @{ $o{flips} || [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$o{'flips'} || []
MC/DC100%1/1
$o{'flips'} || []
$o{'flips'}
Truth table100%2/2
A
A= $o{'flips'}
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | 253 | ▶ | my $line = $LINE{ $self->ascii ? 'ascii' : 'wide' }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 253 | ▶ | my $kind = $self->ascii ? 'ascii' : 'wide'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | 253 | my $files = ' ' . join ' ', 'a' .. 'h'; | |||||||||||||||||||||||||||||||||||||||||
| 200 | 2277 | ▶ | my $rule = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 2277 | my ($left, $join, $right) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 202 | return ' ' . $self->_paint( | ||||||||||||||||||||||||||||||||||||||||||
| 203 | 2277 | $left . join($join, ($line->{h} x 3) x 8) . $right, 'grid'); | |||||||||||||||||||||||||||||||||||||||||
| 204 | 253 | }; | |||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | 253 | my @lines = ($files, $rule->(@{$line}{qw/tl t tr/})); | |||||||||||||||||||||||||||||||||||||||||
| 207 | 253 | for my $row (0 .. 7) { | |||||||||||||||||||||||||||||||||||||||||
| 208 | 2024 | my $rank = 8 - $row; | |||||||||||||||||||||||||||||||||||||||||
| 209 | 2024 | my @cells; | |||||||||||||||||||||||||||||||||||||||||
| 210 | 2024 | for my $col (0 .. 7) { | |||||||||||||||||||||||||||||||||||||||||
| 211 | 16192 | my $square = $row * 8 + $col; | |||||||||||||||||||||||||||||||||||||||||
| 212 | 16192 | my $cell = $game->board->[$square]; | |||||||||||||||||||||||||||||||||||||||||
| 213 | 16192 | push @cells, $self->_cell($square, $cell, $kind, \%legal, | |||||||||||||||||||||||||||||||||||||||||
| 214 | \%flipped, %o); | ||||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||
| 216 | 2024 | my $bar = $self->_paint($line->{v}, 'grid'); | |||||||||||||||||||||||||||||||||||||||||
| 217 | 2024 | push @lines, " $rank " . $bar . join($bar, @cells) . $bar . " $rank"; | |||||||||||||||||||||||||||||||||||||||||
| 218 | 2024 | ▶ | push @lines, $rule->(@{$line}{qw/l x r/}) unless $row == 7; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||
| 220 | 253 | push @lines, $rule->(@{$line}{qw/bl b br/}), $files; | |||||||||||||||||||||||||||||||||||||||||
| 221 | 253 | return @lines; | |||||||||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | 233 | ▶ | sub since { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
since: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | 233 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 226 | 233 | ▶ | my $move = $self->last_move or return (); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | 173 | return (last => $move->square, flips => $move->flips); | |||||||||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | 329 | ▶ | sub remember { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
remember: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | 329 | my ($self, $move) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 232 | 329 | ▶ | $self->last_move(ref $move && $move->can('square') ? $move : undef); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $move and $move->can("square")
MC/DC0%0/2
ref $move and $move->can("square")
ref $move
$move->can("square")
Truth table33%1/3
A and B
A= ref $moveB= $move->can("square")
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | 329 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | 16192 | ▶ | sub _cell { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cell: called CC=12 SCAR=24.9
| |||||||||||||||||||||||||||||||||||||||||||
| 237 | 16192 | my ($self, $square, $cell, $kind, $legal, $flipped, %o) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | 16192 | ▶ | my $cursor = defined $o{cursor} && $o{cursor} == $square; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
defined $o{'cursor'} && $o{'cursor'} == $square
MC/DC100%2/2
defined $o{'cursor'} && $o{'cursor'} == $square
defined $o{'cursor'}
$o{'cursor'} == $square
Truth table100%3/3
A && B
A= defined $o{'cursor'}B= $o{'cursor'} == $square
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | 16192 | ▶ | my $placed = defined $o{last} && $o{last} == $square; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
defined $o{'last'} && $o{'last'} == $square
MC/DC100%2/2
defined $o{'last'} && $o{'last'} == $square
defined $o{'last'}
$o{'last'} == $square
Truth table100%3/3
A && B
A= defined $o{'last'}B= $o{'last'} == $square
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | 16192 | ▶ | if ($cursor) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | my $face = defined $cell ? $self->glyph($cell) | ||||||||||||||||||||||||||||||||||||||||||
| 244 | 59 | ▶ | : $o{to_play} ? $GLYPH{ $o{to_play} }{$kind} : '*'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 245 | 59 | return $self->_paint('>' . $face . '<', 'cursor'); | |||||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | 16133 | ▶ | return $self->_paint('[' . $self->glyph($cell) . ']', 'placed') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$placed and defined $cell
MC/DC50%1/2
$placed and defined $cell
$placed
defined $cell
Truth table66%2/3
A and B
A= $placedB= defined $cell
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | if $placed && defined $cell; | ||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | return ' ' . $self->_paint($TURNED{$cell}{$kind}, 'turned') . ' ' | ||||||||||||||||||||||||||||||||||||||||||
| 252 | 15960 | ▶ | if defined $cell && $flipped->{$square}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $cell and $flipped->{$square}
MC/DC100%2/2
defined $cell and $flipped->{$square}
defined $cell
$flipped->{$square}
Truth table100%3/3
A and B
A= defined $cellB= $flipped->{$square}
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | return ' ' . (defined $cell ? $self->_paint($self->glyph($cell), $cell) | ||||||||||||||||||||||||||||||||||||||||||
| 255 | 15560 | ▶ | : $legal->{$square} ? $self->_paint('*', '*') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | : ' ') . ' '; | ||||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | 14 | ▶ | sub start { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
start: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 260 | 14 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | 14 | ▶ | $self->game(Game::Reversi->new( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | variant => $self->variant, seed => $self->seed)) unless $self->game; | ||||||||||||||||||||||||||||||||||||||||||
| 264 | 14 | my $bot = Game::Reversi::Bot->new( | |||||||||||||||||||||||||||||||||||||||||
| 265 | level => $self->level, seed => $self->seed . ':bot'); | ||||||||||||||||||||||||||||||||||||||||||
| 266 | 14 | my $me = $self->colour; | |||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | 14 | $self->_intro; | |||||||||||||||||||||||||||||||||||||||||
| 269 | 14 | $self->history->start($self->game); | |||||||||||||||||||||||||||||||||||||||||
| 270 | 14 | my $seen = scalar @{ $self->game->events }; | |||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | 14 | ▶ | while ($self->game->status eq 'active') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 273 | 339 | my $game = $self->game; | |||||||||||||||||||||||||||||||||||||||||
| 274 | 339 | my $turn = $game->turn; | |||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | 339 | ▶ | if ($turn eq $me) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 277 | 178 | $self->history->turn($game); | |||||||||||||||||||||||||||||||||||||||||
| 278 | 178 | my $outcome = $self->_human_turn($game, $me); | |||||||||||||||||||||||||||||||||||||||||
| 279 | 178 | ▶ | if ($outcome && $outcome eq 'swap') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$outcome and $outcome eq "swap"
MC/DC100%2/2
$outcome and $outcome eq "swap"
$outcome
$outcome eq "swap"
Truth table100%3/3
A and B
A= $outcomeB= $outcome eq "swap"
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | 5 | $seen = scalar @{ $self->game->events }; | |||||||||||||||||||||||||||||||||||||||||
| 281 | 5 | next; | |||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||
| 283 | 173 | ▶ | if ($outcome) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 284 | 9 | $game->resign($me); | |||||||||||||||||||||||||||||||||||||||||
| 285 | 9 | last; | |||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||||||||||
| 288 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 289 | 161 | my $move = $bot->choose($game, $turn); | |||||||||||||||||||||||||||||||||||||||||
| 290 | 161 | ▶ | last unless $move; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | 161 | my $done = $game->play($turn, $move->square); | |||||||||||||||||||||||||||||||||||||||||
| 292 | 161 | $self->remember($done); | |||||||||||||||||||||||||||||||||||||||||
| 293 | 161 | ▶ | $self->_say('', $NAME{$turn} . ' plays ' . $move->name | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | . ', turning ' . $move->turned | ||||||||||||||||||||||||||||||||||||||||||
| 295 | . ($move->turned == 1 ? ' disc' : ' discs')) | ||||||||||||||||||||||||||||||||||||||||||
| 296 | if $move->phase eq 'play'; | ||||||||||||||||||||||||||||||||||||||||||
| 297 | 161 | ▶ | $self->_say('', $NAME{$turn} . ' places on ' . $move->name) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 298 | if $move->phase eq 'place'; | ||||||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | 325 | $seen = $self->_announce_passes($game, $seen); | |||||||||||||||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | 14 | $self->_finish($self->game); | |||||||||||||||||||||||||||||||||||||||||
| 305 | 14 | return $self->game->result; | |||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | 10 | ▶ | sub _travel { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_travel: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 309 | 10 | my ($self, $way) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 310 | 10 | my $history = $self->history; | |||||||||||||||||||||||||||||||||||||||||
| 311 | 10 | ▶ | my $rebuilt = $way eq 'redo' ? $history->redo : $history->undo; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | 10 | ▶ | return $way eq 'redo' ? 'nothing to redo' : 'nothing to take back' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 313 | unless $rebuilt; | ||||||||||||||||||||||||||||||||||||||||||
| 314 | 5 | $self->game($rebuilt); | |||||||||||||||||||||||||||||||||||||||||
| 315 | 5 | $self->last_move(undef); | |||||||||||||||||||||||||||||||||||||||||
| 316 | 5 | return; | |||||||||||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | 14 | ▶ | sub _intro { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_intro: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 320 | 14 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 321 | 14 | my $me = $self->colour; | |||||||||||||||||||||||||||||||||||||||||
| 322 | $self->_say( | ||||||||||||||||||||||||||||||||||||||||||
| 323 | 14 | ▶ | 'Reversi. You are ' . $self->glyph($me) . ', ' . $NAME{$me} . '.', | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 324 | $self->variant eq 'historic' | ||||||||||||||||||||||||||||||||||||||||||
| 325 | ? 'The historic opening: the first four discs are placed by the ' | ||||||||||||||||||||||||||||||||||||||||||
| 326 | . 'players, on the centre four, and turn nothing.' | ||||||||||||||||||||||||||||||||||||||||||
| 327 | : 'The Othello opening: four discs are already on the board.', | ||||||||||||||||||||||||||||||||||||||||||
| 328 | 'Enter a square such as d3. "help" for help, "quit" to give up.', | ||||||||||||||||||||||||||||||||||||||||||
| 329 | ) unless $self->quiet; | ||||||||||||||||||||||||||||||||||||||||||
| 330 | 14 | return; | |||||||||||||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | 161 | ▶ | sub keys_available { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
keys_available: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 334 | 161 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 335 | 161 | return $self->_reader->keys_available; | |||||||||||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | 180 | ▶ | sub picking { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
picking: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 339 | 180 | my ($self, @set) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 340 | 180 | ▶ | $self->_picking($set[0] ? 1 : 0) if @set; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 341 | 180 | ▶ | return 0 if $self->quiet; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 342 | 178 | ▶ | return $self->_picking ? 1 : 0 if defined $self->_picking; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 343 | 160 | return $self->keys_available; | |||||||||||||||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||
| 346 | 0 | ▶ | sub raw { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
raw: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 347 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 348 | 0 | return $self->keyboard->raw; | |||||||||||||||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||
| 351 | 23 | ▶ | sub enter_raw { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
enter_raw: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 352 | 23 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 353 | 23 | ▶ | return $self->_reader->enter_raw ? $self : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||
| 356 | 0 | ▶ | sub leave_raw { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
leave_raw: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 357 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 358 | 0 | $self->_reader->leave_raw; | |||||||||||||||||||||||||||||||||||||||||
| 359 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||
| 362 | 78 | ▶ | sub read_key { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 363 | 78 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 364 | 78 | return $self->_reader->read_key; | |||||||||||||||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||
| 367 | 23213 | ▶ | sub interactive { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
interactive: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 368 | 23213 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 369 | 23213 | ▶ | my $out = $self->out or return 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 370 | 23213 | ▶ | return (eval { -t $out }) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||
| 373 | 0 | ▶ | sub clear { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clear: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 374 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 375 | 0 | $self->_writer->clear(scrollback => 1); | |||||||||||||||||||||||||||||||||||||||||
| 376 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||
| 379 | 60 | ▶ | sub square_line { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
square_line: called CC=6 SCAR=18.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 380 | 60 | my ($self, $game, $me, $square) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||
| 382 | 60 | my $name = Game::Reversi::Board->name_of($square); | |||||||||||||||||||||||||||||||||||||||||
| 383 | 60 | my $trial = $game->clone; | |||||||||||||||||||||||||||||||||||||||||
| 384 | 60 | my $move = $trial->play($me, $square); | |||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||
| 386 | 60 | ▶ | return "$name: " . $move->message | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $move and $move->isa("Game::Reversi::Error")
MC/DC0%0/2
ref $move and $move->isa("Game::Reversi::Error")
ref $move
$move->isa("Game::Reversi::Error")
Truth table33%1/3
A and B
A= ref $moveB= $move->isa("Game::Reversi::Error")
| |||||||||||||||||||||||||||||||||||||||||||
| 387 | if ref $move && $move->isa('Game::Reversi::Error'); | ||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||
| 389 | 60 | ▶ | return "$name: placed, turning nothing yet" if $move->phase eq 'place'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||
| 391 | 60 | my $count = $trial->counts; | |||||||||||||||||||||||||||||||||||||||||
| 392 | return sprintf '%s: turns %d %s, and you would be %d to %d', | ||||||||||||||||||||||||||||||||||||||||||
| 393 | $name, $move->turned, ($move->turned == 1 ? 'disc' : 'discs'), | ||||||||||||||||||||||||||||||||||||||||||
| 394 | 60 | ▶ | $count->{$me}, $count->{ $me eq 'b' ? 'w' : 'b' }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||
| 397 | 0 | ▶ | sub pick_help { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pick_help: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 398 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 399 | 0 | 'arrows walk the squares marked *, which are the only ones that play.', | |||||||||||||||||||||||||||||||||||||||||
| 400 | 'the line under the board says what the move would turn.', | ||||||||||||||||||||||||||||||||||||||||||
| 401 | 'enter plays it, t goes back to typing, q gives up.', | ||||||||||||||||||||||||||||||||||||||||||
| 402 | 'u takes your last turn back, r plays it again.', | ||||||||||||||||||||||||||||||||||||||||||
| 403 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||
| 406 | 22 | ▶ | sub pick_square { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pick_square: called CC=23 SCAR=35.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 407 | 22 | my ($self, $game, $me) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||
| 409 | 22 | my @legal = sort { $a <=> $b } map { $_->square } @{ $game->legal($me) }; | |||||||||||||||||||||||||||||||||||||||||
| 410 | 22 | ▶ | return $self->_typed_turn($game, $me) unless @legal; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | 22 | my $at = 0; | |||||||||||||||||||||||||||||||||||||||||
| 413 | 22 | ▶ | if (my $last = $self->last_move) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 414 | 5 | my ($gap) = sort { $a <=> $b } map { abs($_ - $last->square) } @legal; | |||||||||||||||||||||||||||||||||||||||||
| 415 | $at = Game::Terminal::Kit::Steer::rest(\@legal, | ||||||||||||||||||||||||||||||||||||||||||
| 416 | 5 | ▶ | sub { abs($_[0] - $last->square) == $gap }); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||
| 419 | 22 | my @notice; | |||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||
| 421 | 22 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 422 | 59 | my $count = $game->counts; | |||||||||||||||||||||||||||||||||||||||||
| 423 | $self->_writer->show([ | ||||||||||||||||||||||||||||||||||||||||||
| 424 | '', | ||||||||||||||||||||||||||||||||||||||||||
| 425 | $self->board_text($game, $me, $self->since, | ||||||||||||||||||||||||||||||||||||||||||
| 426 | cursor => $legal[$at], to_play => $me), | ||||||||||||||||||||||||||||||||||||||||||
| 427 | ' discs: ' . $self->glyph(q{b}) . ' ' . $count->{b} | ||||||||||||||||||||||||||||||||||||||||||
| 428 | . ' ' . $self->glyph(q{w}) . ' ' . $count->{w}, | ||||||||||||||||||||||||||||||||||||||||||
| 429 | ' ' . $self->square_line($game, $me, $legal[$at]), | ||||||||||||||||||||||||||||||||||||||||||
| 430 | 59 | ▶ | (map { ' ' . $_ } @notice), | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->history->legend || ()
MC/DC0%0/2
$self->history->legend || ()
$self->history->legend
()
Truth table66%2/3
A || B
A= $self->history->legendB= ()
| |||||||||||||||||||||||||||||||||||||||||||
| 431 | ' ' . $self->_paint(join(', ', 'arrows to choose, enter to play, ? for the keys', | ||||||||||||||||||||||||||||||||||||||||||
| 432 | $self->history->legend || ()), 'grid'), | ||||||||||||||||||||||||||||||||||||||||||
| 433 | ]); | ||||||||||||||||||||||||||||||||||||||||||
| 434 | 59 | @notice = (); | |||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||
| 436 | 59 | my $key = $self->read_key; | |||||||||||||||||||||||||||||||||||||||||
| 437 | 59 | ▶ | return 1 if !defined $key || $key eq 'eof' || $key eq 'interrupt'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
not defined $key or $key eq "eof" or $key eq "interrupt"
not defined $key or $key eq "eof"
MC/DC0%0/3
not defined $key or $key eq "eof" or $key eq "interrupt"
not defined $key
$key eq "eof"
$key eq "interrupt"
Truth table25%1/4
not(A) or B or C
A= not defined $keyB= $key eq "eof"C= $key eq "interrupt"
| |||||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||
| 439 | 59 | ▶ | if ($STEP{$key}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 440 | 32 | $at = Game::Terminal::Kit::Steer::ring(scalar @legal, $at, $STEP{$key}); | |||||||||||||||||||||||||||||||||||||||||
| 441 | 32 | next; | |||||||||||||||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||||||||||||||||
| 443 | 0 | ▶ | if ($key eq 'home') { $at = 0; next } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 444 | 0 | ▶ | if ($key eq 'end') { $at = $#legal; next } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||
| 446 | 27 | ▶ | if ($key eq 'enter' || $key eq ' ') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$key eq "enter" or $key eq " "
MC/DC50%1/2
$key eq "enter" or $key eq " "
$key eq "enter"
$key eq " "
Truth table66%2/3
A or B
A= $key eq "enter"B= $key eq " "
| |||||||||||||||||||||||||||||||||||||||||||
| 447 | 4 | my $played = $game->play($me, $legal[$at]); | |||||||||||||||||||||||||||||||||||||||||
| 448 | 4 | ▶ | if (ref $played && $played->isa('Game::Reversi::Error')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $played and $played->isa("Game::Reversi::Error")
MC/DC0%0/2
ref $played and $played->isa("Game::Reversi::Error")
ref $played
$played->isa("Game::Reversi::Error")
Truth table33%1/3
A and B
A= ref $playedB= $played->isa("Game::Reversi::Error")
| |||||||||||||||||||||||||||||||||||||||||||
| 449 | 0 | @notice = ($played->message . '.'); | |||||||||||||||||||||||||||||||||||||||||
| 450 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 451 | } | ||||||||||||||||||||||||||||||||||||||||||
| 452 | 4 | $self->remember($played); | |||||||||||||||||||||||||||||||||||||||||
| 453 | 4 | ▶ | $self->_say(' you play ' . $played->name | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 454 | . ($played->phase eq 'play' | ||||||||||||||||||||||||||||||||||||||||||
| 455 | ? ', turning ' . $played->turned | ||||||||||||||||||||||||||||||||||||||||||
| 456 | . ($played->turned == 1 ? ' disc' : ' discs') | ||||||||||||||||||||||||||||||||||||||||||
| 457 | : '')); | ||||||||||||||||||||||||||||||||||||||||||
| 458 | 4 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||
| 461 | 23 | ▶ | if ($key eq 'u' || $key eq 'r') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$key eq "u" or $key eq "r"
MC/DC100%2/2
$key eq "u" or $key eq "r"
$key eq "u"
$key eq "r"
Truth table100%3/3
A or B
A= $key eq "u"B= $key eq "r"
| |||||||||||||||||||||||||||||||||||||||||||
| 462 | 7 | ▶ | my $nothing = $self->_travel($key eq 'u' ? 'undo' : 'redo'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 463 | 7 | ▶ | return 'swap' unless $nothing; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 464 | 4 | @notice = ($nothing . '.'); | |||||||||||||||||||||||||||||||||||||||||
| 465 | 4 | next; | |||||||||||||||||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||||||||||||||||
| 467 | 16 | ▶ | if ($key eq 'q') { return 1 } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 468 | 0 | ▶ | if ($key eq 't') { $self->picking(0); $self->leave_raw; return $self->_typed_turn($game, $me) } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 469 | 0 | ▶ | if ($key eq '?') { @notice = $self->pick_help; next } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||
| 471 | 1 | @notice = ('that key does nothing here. ? for the ones that do'); | |||||||||||||||||||||||||||||||||||||||||
| 472 | } | ||||||||||||||||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||
| 475 | 178 | ▶ | sub _human_turn { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_human_turn: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 476 | 178 | my ($self, $game, $me) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||||||
| 478 | 178 | ▶ | if ($self->picking) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 479 | 12 | ▶ | return $self->pick_square($game, $me) if $self->enter_raw; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 480 | 1 | $self->picking(0); | |||||||||||||||||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||
| 483 | 167 | return $self->_typed_turn($game, $me); | |||||||||||||||||||||||||||||||||||||||||
| 484 | } | ||||||||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||||||||
| 486 | 167 | ▶ | sub _typed_turn { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_typed_turn: called CC=18 SCAR=29.1
| |||||||||||||||||||||||||||||||||||||||||||
| 487 | 167 | my ($self, $game, $me) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||
| 489 | 167 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 490 | 173 | my $count = $game->counts; | |||||||||||||||||||||||||||||||||||||||||
| 491 | 173 | $self->_say("", $self->board_text($game, $me, $self->since)); | |||||||||||||||||||||||||||||||||||||||||
| 492 | $self->_say( | ||||||||||||||||||||||||||||||||||||||||||
| 493 | ' discs: ' . $self->glyph(q{b}) . ' ' . $count->{b} . ' ' | ||||||||||||||||||||||||||||||||||||||||||
| 494 | . $self->glyph(q{w}) . ' ' . $count->{w}, | ||||||||||||||||||||||||||||||||||||||||||
| 495 | 173 | ▶ | ' ' . ($game->phase eq 'opening' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 496 | ? 'Place a disc on one of the centre four.' | ||||||||||||||||||||||||||||||||||||||||||
| 497 | : 'Your move. The squares marked * are the ones that outflank something.'), | ||||||||||||||||||||||||||||||||||||||||||
| 498 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||||||
| 500 | 173 | my $answer = $self->_ask(' > '); | |||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||
| 502 | 173 | ▶ | return 1 unless defined $answer; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 503 | 172 | ▶ | return 1 if $answer eq 'quit' || $answer eq 'resign'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$answer eq "quit" or $answer eq "resign"
MC/DC50%1/2
$answer eq "quit" or $answer eq "resign"
$answer eq "quit"
$answer eq "resign"
Truth table66%2/3
A or B
A= $answer eq "quit"B= $answer eq "resign"
| |||||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||||
| 505 | 169 | ▶ | if ($answer eq 'help') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 506 | 1 | $self->_help($game, $me); | |||||||||||||||||||||||||||||||||||||||||
| 507 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||
| 508 | } | ||||||||||||||||||||||||||||||||||||||||||
| 509 | 168 | ▶ | if ($answer eq 'board' || $answer eq '') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$answer eq "board" or $answer eq ""
MC/DC0%0/2
$answer eq "board" or $answer eq ""
$answer eq "board"
$answer eq ""
Truth table33%1/3
A or B
A= $answer eq "board"B= $answer eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 510 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 511 | } | ||||||||||||||||||||||||||||||||||||||||||
| 512 | 168 | ▶ | if ($answer eq 'undo' || $answer eq 'redo') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$answer eq "undo" or $answer eq "redo"
MC/DC100%2/2
$answer eq "undo" or $answer eq "redo"
$answer eq "undo"
$answer eq "redo"
Truth table100%3/3
A or B
A= $answer eq "undo"B= $answer eq "redo"
| |||||||||||||||||||||||||||||||||||||||||||
| 513 | 3 | my $nothing = $self->_travel($answer); | |||||||||||||||||||||||||||||||||||||||||
| 514 | 3 | ▶ | return 'swap' unless $nothing; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 515 | 1 | $self->_say(' ' . $nothing . '.'); | |||||||||||||||||||||||||||||||||||||||||
| 516 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||
| 517 | } | ||||||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||||||
| 519 | 165 | my $square = Game::Reversi::Notation->text_to_square($answer); | |||||||||||||||||||||||||||||||||||||||||
| 520 | 165 | ▶ | if (!defined $square) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 521 | 3 | $self->_say(' "' . $answer . '" is not a square. Try d3, or "help".'); | |||||||||||||||||||||||||||||||||||||||||
| 522 | 3 | next; | |||||||||||||||||||||||||||||||||||||||||
| 523 | } | ||||||||||||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||||||||||||
| 525 | 162 | my $played = $game->play($me, $square); | |||||||||||||||||||||||||||||||||||||||||
| 526 | 162 | ▶ | $self->remember($played) unless ref $played && $played->isa("Game::Reversi::Error"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $played and $played->isa("Game::Reversi::Error")
MC/DC50%1/2
ref $played and $played->isa("Game::Reversi::Error")
ref $played
$played->isa("Game::Reversi::Error")
Truth table66%2/3
A and B
A= ref $playedB= $played->isa("Game::Reversi::Error")
| |||||||||||||||||||||||||||||||||||||||||||
| 527 | 162 | ▶ | if (ref $played && $played->isa('Game::Reversi::Error')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $played and $played->isa("Game::Reversi::Error")
MC/DC50%1/2
ref $played and $played->isa("Game::Reversi::Error")
ref $played
$played->isa("Game::Reversi::Error")
Truth table66%2/3
A and B
A= ref $playedB= $played->isa("Game::Reversi::Error")
| |||||||||||||||||||||||||||||||||||||||||||
| 528 | 1 | $self->_say(' ' . $played->message . '.'); | |||||||||||||||||||||||||||||||||||||||||
| 529 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||
| 530 | } | ||||||||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||||||||
| 532 | 161 | ▶ | $self->_say(' you play ' . $played->name | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 533 | . ($played->phase eq 'play' | ||||||||||||||||||||||||||||||||||||||||||
| 534 | ? ', turning ' . $played->turned | ||||||||||||||||||||||||||||||||||||||||||
| 535 | . ($played->turned == 1 ? ' disc' : ' discs') | ||||||||||||||||||||||||||||||||||||||||||
| 536 | : '')); | ||||||||||||||||||||||||||||||||||||||||||
| 537 | 161 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 538 | } | ||||||||||||||||||||||||||||||||||||||||||
| 539 | } | ||||||||||||||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||||||||||||
| 541 | 1 | ▶ | sub _help { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_help: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 542 | 1 | my ($self, $game, $me) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 543 | 1 | $self->_say( | |||||||||||||||||||||||||||||||||||||||||
| 544 | '', | ||||||||||||||||||||||||||||||||||||||||||
| 545 | ' Enter a square as a file and a rank, such as d3 or f5.', | ||||||||||||||||||||||||||||||||||||||||||
| 546 | ' A move must outflank at least one of the other side\'s discs;', | ||||||||||||||||||||||||||||||||||||||||||
| 547 | ' every disc it outflanks is turned, and you do not get to choose.', | ||||||||||||||||||||||||||||||||||||||||||
| 548 | ' The squares marked * are the ones you can play.', | ||||||||||||||||||||||||||||||||||||||||||
| 549 | ' A turn with no legal move is forfeited for you automatically.', | ||||||||||||||||||||||||||||||||||||||||||
| 550 | ' The game ends when neither side can move, which can happen with', | ||||||||||||||||||||||||||||||||||||||||||
| 551 | ' the board not full. "undo" takes your last turn back, "redo"', | ||||||||||||||||||||||||||||||||||||||||||
| 552 | ' plays it again, and "quit" gives up.', | ||||||||||||||||||||||||||||||||||||||||||
| 553 | '', | ||||||||||||||||||||||||||||||||||||||||||
| 554 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 555 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 556 | } | ||||||||||||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||||||||||||
| 558 | 325 | ▶ | sub _announce_passes { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_announce_passes: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 559 | 325 | my ($self, $game, $seen) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 560 | 325 | my $events = $game->events; | |||||||||||||||||||||||||||||||||||||||||
| 561 | 325 | for my $i ($seen .. $#$events) { | |||||||||||||||||||||||||||||||||||||||||
| 562 | 334 | my $event = $events->[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 563 | 334 | ▶ | next unless $event->{kind} eq 'pass'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 564 | 4 | my $colour = $event->{payload}{colour}; | |||||||||||||||||||||||||||||||||||||||||
| 565 | 4 | $self->_say(' ' . $NAME{$colour} . ' has no legal move, so that turn is ' | |||||||||||||||||||||||||||||||||||||||||
| 566 | . 'forfeited and the other side plays again.'); | ||||||||||||||||||||||||||||||||||||||||||
| 567 | } | ||||||||||||||||||||||||||||||||||||||||||
| 568 | 325 | return scalar @$events; | |||||||||||||||||||||||||||||||||||||||||
| 569 | } | ||||||||||||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||||||||||||
| 571 | 15 | ▶ | sub _finish { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_finish: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||||||||||||||||
| 572 | 15 | my ($self, $game) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 573 | 15 | my $result = $game->result; | |||||||||||||||||||||||||||||||||||||||||
| 574 | 15 | ▶ | return unless $result; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||||||||||||||||
| 576 | 15 | $self->_say('', $self->board_text($game, undef), ''); | |||||||||||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||||||||||||
| 578 | 15 | my $count = $result->counts; | |||||||||||||||||||||||||||||||||||||||||
| 579 | $self->_say(' discs on the board: ' . $self->glyph(q{b}) . ' ' . $count->{b} | ||||||||||||||||||||||||||||||||||||||||||
| 580 | 15 | . ' ' . $self->glyph(q{w}) . ' ' . $count->{w}); | |||||||||||||||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||||||||||||||||
| 582 | 15 | my $score = $result->score; | |||||||||||||||||||||||||||||||||||||||||
| 583 | 15 | ▶ | if ($score) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 584 | 6 | my $empty = Game::Reversi::Board->empties($game->board); | |||||||||||||||||||||||||||||||||||||||||
| 585 | $self->_say(' final score: ' . $self->glyph(q{b}) . ' ' . $score->{b} | ||||||||||||||||||||||||||||||||||||||||||
| 586 | . ' ' . $self->glyph(q{w}) . ' ' . $score->{w} | ||||||||||||||||||||||||||||||||||||||||||
| 587 | 6 | ▶ | . ($empty ? " (the $empty empty squares go to the winner)" : '')); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 588 | } | ||||||||||||||||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||||||||||||||||
| 590 | 15 | my $me = $self->colour; | |||||||||||||||||||||||||||||||||||||||||
| 591 | 15 | ▶ | $self->_say('', !defined $result->winner ? ' A tie.' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 592 | : $result->winner eq $me ? ' You win.' | ||||||||||||||||||||||||||||||||||||||||||
| 593 | : ' You lose.'); | ||||||||||||||||||||||||||||||||||||||||||
| 594 | 15 | ▶ | $self->_say(' (' . $result->result . ')') unless $result->natural; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 595 | 15 | return; | |||||||||||||||||||||||||||||||||||||||||
| 596 | } | ||||||||||||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||||||||||||
| 598 | 9 | ▶ | sub _usage { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_usage: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 599 | 9 | return <<'USAGE'; | |||||||||||||||||||||||||||||||||||||||||
| 600 | reversi - play Reversi in a terminal | ||||||||||||||||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||||||||||||||||
| 602 | reversi [--level 1..5] [--variant historic|othello] | ||||||||||||||||||||||||||||||||||||||||||
| 603 | [--colour b|w] [--seed STRING] [--ascii] | ||||||||||||||||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||||||||||||||||
| 605 | --level how hard the opponent is, 1 to 5. Default 3. | ||||||||||||||||||||||||||||||||||||||||||
| 606 | --variant historic, the real Reversi opening where the players place the | ||||||||||||||||||||||||||||||||||||||||||
| 607 | first four discs themselves, or othello, where those four are | ||||||||||||||||||||||||||||||||||||||||||
| 608 | already down. Default historic. | ||||||||||||||||||||||||||||||||||||||||||
| 609 | --colour b to play dark and move first, w to play light. Default b. | ||||||||||||||||||||||||||||||||||||||||||
| 610 | --seed makes a game reproducible, and decides how the bot opens. | ||||||||||||||||||||||||||||||||||||||||||
| 611 | --ascii X and O on a plain grid, for a terminal without box characters. | ||||||||||||||||||||||||||||||||||||||||||
| 612 | --nopick type the squares out instead of pointing at them. | ||||||||||||||||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||||||||||||||||
| 614 | On a terminal the arrow keys walk the squares marked *, which are the only | ||||||||||||||||||||||||||||||||||||||||||
| 615 | ones that play, and the line under the board says what the move would turn | ||||||||||||||||||||||||||||||||||||||||||
| 616 | and what the score would become. Enter plays it, t goes back to typing, | ||||||||||||||||||||||||||||||||||||||||||
| 617 | u takes your last turn back, r plays it again and ? lists the keys. Typed, | ||||||||||||||||||||||||||||||||||||||||||
| 618 | the same are undo and redo. Needs Term::ReadKey; without it the squares are typed. | ||||||||||||||||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||||||||||||||||
| 620 | The square the last move went on is bracketed and the discs it turned are | ||||||||||||||||||||||||||||||||||||||||||
| 621 | drawn ringed, so both are visible with --ascii and with colour off. | ||||||||||||||||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||||||||||||||||
| 623 | Exit status is 0 if you won or tied, 1 if you lost, 2 for a bad option. | ||||||||||||||||||||||||||||||||||||||||||
| 624 | USAGE | ||||||||||||||||||||||||||||||||||||||||||
| 625 | } | ||||||||||||||||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||||||||||||||||
| 627 | 2 | ▶ | sub options { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
options: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 628 | return [ | ||||||||||||||||||||||||||||||||||||||||||
| 629 | 2 | { name => 'level', type => 'int', min => 1, max => 5, help => 'how hard the opponent is' }, | |||||||||||||||||||||||||||||||||||||||||
| 630 | { name => 'variant', type => 'enum', choices => [qw(historic othello)], help => 'place the first four discs yourselves, or find them down' }, | ||||||||||||||||||||||||||||||||||||||||||
| 631 | { name => 'colour', type => 'enum', choices => [qw(b w)], help => 'b plays dark and moves first, w plays light' }, | ||||||||||||||||||||||||||||||||||||||||||
| 632 | { name => 'seed', type => 'str', help => 'makes a game reproducible' }, | ||||||||||||||||||||||||||||||||||||||||||
| 633 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 634 | } | ||||||||||||||||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||||||||||||||||
| 636 | 9 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=15 SCAR=47.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 637 | 9 | my ($class, @argv) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 638 | 9 | my %opt = (level => 3, variant => 'historic', colour => 'b', seed => 'reversi'); | |||||||||||||||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||||||||||||||||
| 640 | 9 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||
| 641 | 9 | ▶ | local $SIG{__WARN__} = sub { print STDERR "reversi: $_[0]" }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 642 | 9 | Getopt::Long::GetOptionsFromArray(\@argv, \%opt, | |||||||||||||||||||||||||||||||||||||||||
| 643 | 'level=i', 'variant=s', 'colour=s', 'seed=s', 'ascii', 'pick!', 'help|h'); | ||||||||||||||||||||||||||||||||||||||||||
| 644 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 645 | 9 | ▶ | if (!$ok) { print STDERR _usage(); return 2 } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 646 | 8 | ▶ | if ($opt{help}) { print _usage(); return 0 } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 647 | 0 | ▶ | if (@argv) { print STDERR "reversi: not an option: $argv[0]\n", _usage(); return 2 } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||||||||||||||||
| 649 | 0 | ▶ | $opt{colour} = 'b' if lc $opt{colour} eq 'dark' || lc $opt{colour} eq 'black'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
lc $opt{"colour"} eq "dark" or lc $opt{"colour"} eq "black"
MC/DC0%0/2
lc $opt{"colour"} eq "dark" or lc $opt{"colour"} eq "black"
lc $opt{"colour"} eq "dark"
lc $opt{"colour"} eq "black"
Truth table0%0/3
A or B
A= lc $opt{"colour"} eq "dark"B= lc $opt{"colour"} eq "black"
| |||||||||||||||||||||||||||||||||||||||||||
| 650 | 0 | ▶ | $opt{colour} = 'w' if lc $opt{colour} eq 'light' || lc $opt{colour} eq 'white'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
lc $opt{"colour"} eq "light" or lc $opt{"colour"} eq "white"
MC/DC0%0/2
lc $opt{"colour"} eq "light" or lc $opt{"colour"} eq "white"
lc $opt{"colour"} eq "light"
lc $opt{"colour"} eq "white"
Truth table0%0/3
A or B
A= lc $opt{"colour"} eq "light"B= lc $opt{"colour"} eq "white"
| |||||||||||||||||||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||||||||||||||||
| 652 | 0 | my $terminal = eval { | |||||||||||||||||||||||||||||||||||||||||
| 653 | $class->new( | ||||||||||||||||||||||||||||||||||||||||||
| 654 | level => $opt{level}, variant => $opt{variant}, | ||||||||||||||||||||||||||||||||||||||||||
| 655 | colour => $opt{colour}, seed => $opt{seed}, | ||||||||||||||||||||||||||||||||||||||||||
| 656 | ascii => $opt{ascii}, | ||||||||||||||||||||||||||||||||||||||||||
| 657 | 0 | ▶ | (defined $opt{pick} ? (picking => $opt{pick} ? 1 : 0) : ())); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 658 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 659 | 0 | ▶ | if (!$terminal) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 660 | 0 | ▶ | my $why = $@ || "could not start\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$@ || "could not start\n"
MC/DC0%0/1
$@ || "could not start\n"
$@
Truth table0%0/2
A
A= $@
| |||||||||||||||||||||||||||||||||||||||||||
| 661 | 0 | $why =~ s/ at \S+ line \d+\.?\n?\z//; | |||||||||||||||||||||||||||||||||||||||||
| 662 | 0 | $why =~ s/\AGame::Reversi(?:::\w+)*: //; | |||||||||||||||||||||||||||||||||||||||||
| 663 | 0 | chomp $why; | |||||||||||||||||||||||||||||||||||||||||
| 664 | 0 | print STDERR "reversi: $why\n"; | |||||||||||||||||||||||||||||||||||||||||
| 665 | 0 | return 2; | |||||||||||||||||||||||||||||||||||||||||
| 666 | } | ||||||||||||||||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||||||||||||||||
| 668 | my $result = Game::Terminal::Kit::Run::guard( | ||||||||||||||||||||||||||||||||||||||||||
| 669 | 0 | ▶ | sub { $terminal->start }, $terminal->keyboard, $terminal->screen); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 670 | 0 | ▶ | return 0 unless $result; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 671 | 0 | ▶ | return 0 if !defined $result->winner; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 672 | 0 | ▶ | return 0 if $result->winner eq $opt{colour}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 673 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 674 | } | ||||||||||||||||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||||||||||||||||
| 676 | |||||||||||||||||||||||||||||||||||||||||||
| 677 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||||||||||||||||
| 679 | __END__ | ||||||||||||||||||||||||||||||||||||||||||