Marad.pm

filter: Statementstmt 99.0% 103 / 104 Branchbran 100% 44 / 44 Conditioncond 100% 33 / 33 MC/DCmcdc 100% 16 / 16 Subroutinesub 93.3% 14 / 15 Podpod 100% 8 / 8 totaltotal 99.0% 218 / 220 CC 39 SCAR 21.1
CC
39
Cov
99%
move
14.0
_move_type
10.0
_move_stack
8.0
CRAP 8.2
« Marad/ConfigData.pm ↑ Summary
1 # -*- Perl -*-
2  
3 package Game::Marad 0.07;
4 2 ▶use 5.26.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Object::Pad 0.802;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 class Game::Marad :strict(params);
8  
9 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 BOARD_SIZE => 9,
11 MIDDLE => 4, # the middle square is used for scoring
12  
13 MAX_MOVES => 4,
14  
15 TYPE_MASK => 0b0000_1111, # piece type
16 PLAYER_BIT => 6, # owned by player 1 or 2?
17 MOVED_MASK => 0b1000_0000, # has the piece moved?
18  
19 MOVE_NOPE => 0,
20 MOVE_SQUARE => 1, # NOTE also Rook
21 MOVE_DIAGONAL => 2, # NOTE also Bishop
22  
23 PIECE_EMPTY => 0,
24 PIECE_ROOK => 1,
25 PIECE_BISHOP => 2,
26 PIECE_KING => 3, # NOTE Rook and Bishop bits are set
27  
28 XX => 0,
29 YY => 1,
30 2 };
31  
32 # NOTE the client is expected to be well behaved and to not modify the
33 # contents of board nor the score (hiding these references would only
34 # make such impolite behavior slightly more difficult)
35 1 ▶field $board :reader;
Subroutine board: called CC=1 SCAR=0.0
Pod documented
36 2 ▶field $move_count :reader;
Subroutine move_count: called CC=1 SCAR=0.0
Pod documented
37 3 ▶field $player :reader;
Subroutine player: called CC=1 SCAR=0.0
Pod documented
38 5 ▶field $score :reader;
Subroutine score: called CC=1 SCAR=0.0
Pod documented
39  
40 ADJUST {
41 $board = [
42 [qw/0 0 0 0 0 0 0 0 0/], # starting game state
43 [qw/0 2 0 0 0 0 0 66 0/],
44 [qw/0 1 0 0 0 0 0 65 0/],
45 [qw/0 1 0 0 0 0 0 65 0/],
46 [qw/0 3 0 0 0 0 0 67 0/],
47 [qw/0 1 0 0 0 0 0 65 0/],
48 [qw/0 1 0 0 0 0 0 65 0/],
49 [qw/0 2 0 0 0 0 0 66 0/],
50 [qw/0 0 0 0 0 0 0 0 0/],
51 ];
52 $move_count = _move_count();
53 $player = 0;
54 $score = [ 0, 0 ];
55 }
56  
57 ########################################################################
58 #
59 # METHODS
60  
61 7 ▶method is_owner( $x, $y ) {
Subroutine is_owner: called CC=7 SCAR=19.5
Pod documented
62 7 ▶ return 0 if $x < 0 or $x >= BOARD_SIZE or $y < 0 or $y >= BOARD_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if $x < 0 or $x >= 9 or $y < 0 or $y >= 9 4 3 7
Condition100%9/9
$x < 0 or $x >= 9 or $y < 0 or $y >= 9
l!l&&r!l&&!r
313
$x < 0 or $x >= 9 or $y < 0
l!l&&r!l&&!r
214
$x < 0 or $x >= 9
l!l&&r!l&&!r
115
MC/DC100%4/4
$x < 0 or $x >= 9 or $y < 0 or $y >= 9
$x < 0 $x >= 9 $y < 0 $y >= 9
Truth table100%5/5
A or B or C or D
A= $x < 0B= $x >= 9C= $y < 0D= $y >= 9
ABCDresult
1XXX1
01XX1
001X1
00011
00000
63 3 my $piece = $board->[$y][$x];
64 3 ▶ return 0 if $piece == PIECE_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if $piece == 0 1 2 3
65 2 ▶ return 0 unless ( $piece >> PLAYER_BIT & 1 ) == $player;
Branch: 1 branch
BranchTrueFalseTotal
unless ($piece >> 6 & 1) == $player 1 1 2
66 1 return 1;
67 2 }
68  
69 # try to carry out a game move involving two points, generally from a
70 # player selecting a piece to move and a direction (via the destination
71 # point) to move in. the move may not be possible for various reasons.
72 # if possible a move may cause a bunch of changes to the board and other
73 # game state
74 13 ▶method move( $srcx, $srcy, $dstx, $dsty ) {
Subroutine move: called CC=14 SCAR=26.4
Pod documented
75 13 ▶ return 0, "out of bounds"
Branch: 1 branch
BranchTrueFalseTotal
if $srcx < 0 or $srcx >= 9 or $srcy < 0 or $srcy >= 9 4 9 13
Condition100%9/9
$srcx < 0 or $srcx >= 9 or $srcy < 0 or $srcy >= 9
l!l&&r!l&&!r
319
$srcx < 0 or $srcx >= 9 or $srcy < 0
l!l&&r!l&&!r
2110
$srcx < 0 or $srcx >= 9
l!l&&r!l&&!r
1111
MC/DC100%4/4
$srcx < 0 or $srcx >= 9 or $srcy < 0 or $srcy >= 9
$srcx < 0 $srcx >= 9 $srcy < 0 $srcy >= 9
Truth table100%5/5
A or B or C or D
A= $srcx < 0B= $srcx >= 9C= $srcy < 0D= $srcy >= 9
ABCDresult
1XXX1
01XX1
001X1
00011
00000
76 if $srcx < 0
77 or $srcx >= BOARD_SIZE
78 or $srcy < 0
79 or $srcy >= BOARD_SIZE;
80  
81 9 my $piece = $board->[$srcy][$srcx];
82 9 ▶ return 0, "not a piece" if $piece == PIECE_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if $piece == 0 1 8 9
83  
84 8 ▶ return 0, "not owner" unless ( $piece >> PLAYER_BIT & 1 ) == $player;
Branch: 1 branch
BranchTrueFalseTotal
unless ($piece >> 6 & 1) == $player 1 7 8
85  
86 7 my ( $move_type, $stepx, $stepy ) = _move_type( $srcx, $srcy, $dstx, $dsty );
87 7 ▶ return 0, "invalid move" if $move_type == MOVE_NOPE;
Branch: 1 branch
BranchTrueFalseTotal
if $move_type == 0 1 6 7
88  
89 # this is probably too clever: the king by virtue of being number 3
90 # has both the square and diagonal move bits set, while rooks and
91 # bishops have only one of them set
92 6 my $piece_type = $piece & TYPE_MASK;
93 6 ▶ return 0, "invalid move type" unless ( $move_type & $piece_type ) > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless ($move_type & $piece_type) > 0 1 5 6
94  
95 5 _move_stack( $board, $move_count, $srcx, $srcy, $stepx, $stepy );
96  
97 # score points for motion in the middle
98 5 my $center = $board->[MIDDLE][MIDDLE];
99 5 ▶ if ( $center > 0 and ( $center & MOVED_MASK ) == MOVED_MASK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($center > 0 and ($center & 128) == 128) 2 3 5
Condition100%3/3
$center > 0 and ($center & 128) == 128
!ll&&!rl&&r
212
MC/DC100%2/2
$center > 0 and ($center & 128) == 128
$center > 0 ($center & 128) == 128
Truth table100%3/3
A and B
A= $center > 0B= ($center & 128) == 128
ABresult
0X0
100
111
100 2 $score->[ $center >> PLAYER_BIT & 1 ]++;
101 }
102 # clear any moved bits (keeps the map clean, only the middle moved
103 # bit really needs to be cleared, provided things that iterate over
104 # the $board extract the type and owner)
105 5 for my $row ( $board->@* ) {
106 45 for my $col ( $row->@* ) {
107 405 $col &= ~MOVED_MASK;
108 }
109 }
110  
111 5 $player ^= 1;
112 5 ▶ $move_count = _move_count() if $player == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $player == 0 2 3 5
113  
114 5 return 1, "ok";
115 2 }
116  
117 # boards of different sizes might be supported in which case clients may
118 # need something like the following to obtain that information
119 1 ▶method size() { return BOARD_SIZE }
Subroutine size: called CC=1 SCAR=0.0
Pod documented
120  
121 # Zobrist Hashing metadata. the caller will need to make suitable use of
122 # this, such as by indexing the [ type, owner, location ] values into an
123 # 3x2x81 array populated with 64-bit random values as generated perhaps
124 # by the ->irand64 method of the Math::Random::PCG32 module and then ^=
125 1 ▶method zobrist() {
Subroutine zobrist: called CC=4 SCAR=13.9
Pod documented
126 1 my @meta;
127 1 my $location = 0;
128 1 for my $row ( $board->@* ) {
129 9 for my $piece ( $row->@* ) {
130 81 ▶ next if $piece == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $piece == 0 67 14 81
131 14 my $type = ($piece & TYPE_MASK) - 1;
132 14 my $owner = $piece >> PLAYER_BIT & 1;
133 14 push @meta, [ $type, $owner, $location++ ];
134 }
135 }
136 1 return @meta;
137 2 }
138  
139 ########################################################################
140 #
141 # SUBROUTINES
142  
143 # this many moves happen in each turnpair
144 0 ▶sub _move_count () { 1 + int rand(MAX_MOVES) }
Subroutine _move_count: not called CC=1 SCAR=6.9
145  
146 # moves stuff with less recursion than in prior versions (before 0.05)
147 10 ▶sub _move_stack ( $grid, $moves, $srcx, $srcy, $stepx, $stepy ) {
Subroutine _move_stack: called CC=8 SCAR=20.8
148 10 my $point = [];
149 10 $point->@[XX,YY] = ($srcx, $srcy);
150 10 my @stack = $point;
151 10 ▶ while ( $moves > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($moves > 0) 37 3 40
152 37 my $point = [];
153 37 $point->@[ XX, YY ] = ( $stack[-1][XX] + $stepx, $stack[-1][YY] + $stepy );
154 # edge: ran out of space for pushing
155 last
156 37 ▶ if $point->[XX] < 0
Branch: 1 branch
BranchTrueFalseTotal
if $point->[0] < 0 or $point->[0] >= 9 or $point->[1] < 0 or $point->[1] >= 9 7 30 37
Condition100%9/9
$point->[0] < 0 or $point->[0] >= 9 or $point->[1] < 0 or $point->[1] >= 9
l!l&&r!l&&!r
6130
$point->[0] < 0 or $point->[0] >= 9 or $point->[1] < 0
l!l&&r!l&&!r
5131
$point->[0] < 0 or $point->[0] >= 9
l!l&&r!l&&!r
3232
MC/DC100%4/4
$point->[0] < 0 or $point->[0] >= 9 or $point->[1] < 0 or $point->[1] >= 9
$point->[0] < 0 $point->[0] >= 9 $point->[1] < 0 $point->[1] >= 9
Truth table100%5/5
A or B or C or D
A= $point->[0] < 0B= $point->[0] >= 9C= $point->[1] < 0D= $point->[1] >= 9
ABCDresult
1XXX1
01XX1
001X1
00011
00000
157 or $point->[XX] >= BOARD_SIZE
158 or $point->[YY] < 0
159 or $point->[YY] >= BOARD_SIZE;
160 30 push @stack, $point;
161 # empty cell: swap along the stack to advance the pieces
162 30 ▶ if ( $grid->[ $stack[-1][YY] ][ $stack[-1][XX] ] == PIECE_EMPTY ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$grid[$stack[-1][1]][$stack[-1][0]] == 0) 27 3 30
163 27 for my $i ( reverse 0 .. ( $#stack - 1 ) ) {
164 # downside: this may happen more than it needs to
165 37 $grid->[ $stack[$i][YY] ][ $stack[$i][XX] ] |= MOVED_MASK;
166 37 my $j = $i + 1;
167 37 ( $grid->[ $stack[$i][YY] ][ $stack[$i][XX] ],
168 $grid->[ $stack[$j][YY] ][ $stack[$j][XX] ]
169 )
170 = (
171 $grid->[ $stack[$j][YY] ][ $stack[$j][XX] ],
172 $grid->[ $stack[$i][YY] ][ $stack[$i][XX] ]
173 );
174 # in theory one could collect a list of moves for use by
175 # an animation routine, or have a callback for that. but
176 # that would use more CPU and memory
177 }
178 27 shift @stack;
179 27 $moves--;
180 }
181 # non-empty cell: put it onto the stack next time around
182 }
183 }
184  
185 # given two points, what sort of movement is it? (may not be valid)
186 17 ▶sub _move_type ( $x1, $y1, $x2, $y2 ) {
Subroutine _move_type: called CC=10 SCAR=23.0
187 17 ▶ return MOVE_NOPE, undef, undef if $x1 == $x2 and $y1 == $y2;
Branch: 1 branch
BranchTrueFalseTotal
if $x1 == $x2 and $y1 == $y2 2 15 17
Condition100%3/3
$x1 == $x2 and $y1 == $y2
!ll&&!rl&&r
1322
MC/DC100%2/2
$x1 == $x2 and $y1 == $y2
$x1 == $x2 $y1 == $y2
Truth table100%3/3
A and B
A= $x1 == $x2B= $y1 == $y2
ABresult
0X0
100
111
188  
189 15 my ( $dy, $plus_x ) = ( $y2 - $y1, $x2 > $x1 );
190 15 ▶ return MOVE_SQUARE, ( $plus_x ? 1 : -1 ), 0 if $dy == 0;
Branch: 2 branches
BranchTrueFalseTotal
if $dy == 0 4 11 15
$plus_x ? : 2 2 4
191  
192 11 my ( $dx, $plus_y ) = ( $x2 - $x1, $y2 > $y1 );
193 11 ▶ return MOVE_SQUARE, 0, ( $plus_y ? 1 : -1 ) if $dx == 0;
Branch: 2 branches
BranchTrueFalseTotal
if $dx == 0 2 9 11
$plus_y ? : 1 1 2
194  
195 9 ▶ return MOVE_DIAGONAL, ( $plus_x ? 1 : -1 ), ( $plus_y ? 1 : -1 )
Branch: 3 branches
BranchTrueFalseTotal
if abs $dx == abs $dy 8 1 9
$plus_x ? : 4 4 8
$plus_y ? : 4 4 8
196 if abs($dx) == abs($dy);
197  
198 1 return MOVE_NOPE, undef, undef;
199 }
200  
201 2 1;
202 __END__
« Marad/ConfigData.pm ↑ Summary