| 1 | package Game::Reversi::Board; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 19 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 19 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 19 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 19 | ▶ | use constant SIZE => 8; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 19 | ▶ | use constant CELLS => 64; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 19 | my @RAY = ([-1,-1], [-1,0], [-1,1], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | [ 0,-1], [ 0,1], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | [ 1,-1], [ 1,0], [ 1,1]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | sub rays { return map { [ @$_ ] } @RAY } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rays: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 615 | ▶ | sub empty { return [ (undef) x CELLS ] } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
empty: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | ▶ | sub clone { my ($class, $board) = @_; return [ @$board ] } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clone: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 670244 | ▶ | sub other { my ($class, $colour) = @_; return $colour eq 'b' ? 'w' : 'b' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
other: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 2410 | ▶ | sub square_of { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
square_of: called CC=5 SCAR=19.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 2410 | my ($class, $file, $rank) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 2410 | ▶ | return undef unless defined $file && defined $rank; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $file and defined $rank
MC/DC0%0/2
defined $file and defined $rank
defined $file
defined $rank
Truth table33%1/3
A and B
A= defined $fileB= defined $rank
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 2410 | ▶ | return undef unless $file =~ /\A[a-h]\z/ && $rank =~ /\A[1-8]\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$file =~ /\A[a-h]\z/ and $rank =~ /\A[1-8]\z/
MC/DC0%0/2
$file =~ /\A[a-h]\z/ and $rank =~ /\A[1-8]\z/
$file =~ /\A[a-h]\z/
$rank =~ /\A[1-8]\z/
Truth table33%1/3
A and B
A= $file =~ /\A[a-h]\z/B= $rank =~ /\A[1-8]\z/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 2410 | return (8 - $rank) * SIZE + (ord($file) - ord('a')); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 6816 | ▶ | sub name_of { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
name_of: called CC=5 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 6816 | my ($class, $square) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 6816 | ▶ | return undef unless defined $square && $square =~ /\A\d+\z/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%3/9
defined $square and $square =~ /\A\d+\z/ and $square >= 0 and $square < 64
defined $square and $square =~ /\A\d+\z/ and $square >= 0
defined $square and $square =~ /\A\d+\z/
MC/DC0%0/4
defined $square and $square =~ /\A\d+\z/ and $square >= 0 and $square < 64
defined $square
$square =~ /\A\d+\z/
$square >= 0
$square < 64
Truth table20%1/5
A and B and C and D
A= defined $squareB= $square =~ /\A\d+\z/C= $square >= 0D= $square < 64
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | && $square >= 0 && $square < CELLS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 6816 | my $row = int($square / SIZE); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 6816 | my $col = $square % SIZE; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 6816 | return chr(ord('a') + $col) . (8 - $row); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 11844589 | ▶ | sub flips_for { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
flips_for: called CC=20 SCAR=30.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 11844589 | my ($class, $board, $square, $colour) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 11844589 | ▶ | return () unless defined $square && $square >= 0 && $square < CELLS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $square and $square >= 0 and $square < 64
defined $square and $square >= 0
MC/DC0%0/3
defined $square and $square >= 0 and $square < 64
defined $square
$square >= 0
$square < 64
Truth table25%1/4
A and B and C
A= defined $squareB= $square >= 0C= $square < 64
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 11844589 | ▶ | return () if defined $board->[$square]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 11844587 | ▶ | my $them = $colour eq 'b' ? 'w' : 'b'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 11844587 | my $row = int($square / SIZE); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 11844587 | my $col = $square % SIZE; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 11844587 | my @flips; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 11844587 | for my $ray (@RAY) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 94756696 | my ($dr, $dc) = @$ray; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 94756696 | my ($r, $c) = ($row + $dr, $col + $dc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 94756696 | my @run; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 94756696 | ▶ | while ($r >= 0 && $r < SIZE && $c >= 0 && $c < SIZE) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
$r >= 0 and $r < 8 and $c >= 0 and $c < 8
$r >= 0 and $r < 8 and $c >= 0
$r >= 0 and $r < 8
MC/DC100%4/4
$r >= 0 and $r < 8 and $c >= 0 and $c < 8
$r >= 0
$r < 8
$c >= 0
$c < 8
Truth table100%5/5
A and B and C and D
A= $r >= 0B= $r < 8C= $c >= 0D= $c < 8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 84832215 | my $cell = $board->[ $r * SIZE + $c ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 84832215 | ▶ | last unless defined $cell && $cell eq $them; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $cell and $cell eq $them
MC/DC100%2/2
defined $cell and $cell eq $them
defined $cell
$cell eq $them
Truth table100%3/3
A and B
A= defined $cellB= $cell eq $them
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 12252904 | push @run, $r * SIZE + $c; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 12252904 | ($r, $c) = ($r + $dr, $c + $dc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 94756696 | ▶ | next unless @run; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 8063234 | ▶ | next unless $r >= 0 && $r < SIZE && $c >= 0 && $c < SIZE; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
$r >= 0 and $r < 8 and $c >= 0 and $c < 8
$r >= 0 and $r < 8 and $c >= 0
$r >= 0 and $r < 8
MC/DC100%4/4
$r >= 0 and $r < 8 and $c >= 0 and $c < 8
$r >= 0
$r < 8
$c >= 0
$c < 8
Truth table100%5/5
A and B and C and D
A= $r >= 0B= $r < 8C= $c >= 0D= $c < 8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 7273852 | my $end = $board->[ $r * SIZE + $c ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 7273852 | ▶ | next unless defined $end && $end eq $colour; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $end and $end eq $colour
MC/DC50%1/2
defined $end and $end eq $colour
defined $end
$end eq $colour
Truth table66%2/3
A and B
A= defined $endB= $end eq $colour
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 3288624 | push @flips, @run; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 11844587 | return @flips; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 261373 | ▶ | sub legal_moves { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
legal_moves: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 261373 | my ($class, $board, $colour) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 11093119 | return grep { scalar $class->flips_for($board, $_, $colour) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 261373 | grep { !defined $board->[$_] } 0 .. CELLS - 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 23244 | ▶ | sub has_move { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
has_move: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 23244 | my ($class, $board, $colour) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 23244 | for my $square (0 .. CELLS - 1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 346835 | ▶ | next if defined $board->[$square]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 154395 | ▶ | return 1 if $class->flips_for($board, $square, $colour); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 999 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 394897 | ▶ | sub apply { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
apply: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 394897 | my ($class, $board, $square, $colour) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 394897 | my @flips = $class->flips_for($board, $square, $colour); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 394897 | ▶ | die "Game::Reversi::Board: $colour cannot play " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$class->name_of($square) // "that square"
MC/DC0%0/1
$class->name_of($square) // "that square"
$class->name_of($square)
Truth table50%1/2
A
A= $class->name_of($square)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | . ($class->name_of($square) // 'that square') . ", it outflanks nothing" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | unless @flips; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 394896 | my $after = [ @$board ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 394896 | $after->[$square] = $colour; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 394896 | $after->[$_] = $colour for @flips; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 394896 | return $after; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 275970 | ▶ | sub count { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
count: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 275970 | my ($class, $board) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 275970 | my %n = (b => 0, w => 0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 275970 | for my $cell (@$board) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 17662080 | ▶ | $n{$cell}++ if defined $cell; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 275970 | return \%n; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 275209 | ▶ | sub empties { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
empties: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 275209 | my ($class, $board) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 275209 | return scalar grep { !defined } @$board; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 19 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||