line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# -*- Perl -*- |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package Game::Marad 0.07; |
4
|
2
|
|
|
2
|
|
235434
|
use 5.26.0; |
|
2
|
|
|
|
|
19
|
|
5
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
60
|
|
6
|
2
|
|
|
2
|
|
1303
|
use Object::Pad 0.802; |
|
2
|
|
|
|
|
22656
|
|
|
2
|
|
|
|
|
171
|
|
7
|
|
|
|
|
|
|
class Game::Marad :strict(params); |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use constant { |
10
|
2
|
|
|
|
|
6438
|
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
|
|
|
2
|
|
927
|
}; |
|
2
|
|
|
|
|
4
|
|
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
|
|
|
1
|
1
|
4
|
field $board :reader; |
|
1
|
|
|
|
|
9
|
|
36
|
2
|
|
|
2
|
1
|
7
|
field $move_count :reader; |
|
2
|
|
|
|
|
8
|
|
37
|
3
|
|
|
3
|
1
|
11
|
field $player :reader; |
|
3
|
|
|
|
|
13
|
|
38
|
5
|
|
|
5
|
1
|
416
|
field $score :reader; |
|
5
|
|
|
|
|
29
|
|
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
|
|
|
7
|
1
|
26
|
method is_owner( $x, $y ) { |
|
7
|
|
|
|
|
13
|
|
|
7
|
|
|
|
|
9
|
|
|
7
|
|
|
|
|
11
|
|
|
7
|
|
|
|
|
8
|
|
62
|
7
|
100
|
100
|
|
|
72
|
return 0 if $x < 0 or $x >= BOARD_SIZE or $y < 0 or $y >= BOARD_SIZE; |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
63
|
3
|
|
|
|
|
9
|
my $piece = $board->[$y][$x]; |
64
|
3
|
100
|
|
|
|
14
|
return 0 if $piece == PIECE_EMPTY; |
65
|
2
|
100
|
|
|
|
14
|
return 0 unless ( $piece >> PLAYER_BIT & 1 ) == $player; |
66
|
1
|
|
|
|
|
5
|
return 1; |
67
|
|
|
|
|
|
|
} |
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
|
|
|
13
|
1
|
152
|
method move( $srcx, $srcy, $dstx, $dsty ) { |
|
13
|
|
|
|
|
20
|
|
|
13
|
|
|
|
|
19
|
|
|
13
|
|
|
|
|
15
|
|
|
13
|
|
|
|
|
18
|
|
|
13
|
|
|
|
|
15
|
|
|
13
|
|
|
|
|
14
|
|
75
|
13
|
100
|
100
|
|
|
105
|
return 0, "out of bounds" |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
76
|
|
|
|
|
|
|
if $srcx < 0 |
77
|
|
|
|
|
|
|
or $srcx >= BOARD_SIZE |
78
|
|
|
|
|
|
|
or $srcy < 0 |
79
|
|
|
|
|
|
|
or $srcy >= BOARD_SIZE; |
80
|
|
|
|
|
|
|
|
81
|
9
|
|
|
|
|
15
|
my $piece = $board->[$srcy][$srcx]; |
82
|
9
|
100
|
|
|
|
27
|
return 0, "not a piece" if $piece == PIECE_EMPTY; |
83
|
|
|
|
|
|
|
|
84
|
8
|
100
|
|
|
|
31
|
return 0, "not owner" unless ( $piece >> PLAYER_BIT & 1 ) == $player; |
85
|
|
|
|
|
|
|
|
86
|
7
|
|
|
|
|
16
|
my ( $move_type, $stepx, $stepy ) = _move_type( $srcx, $srcy, $dstx, $dsty ); |
87
|
7
|
100
|
|
|
|
21
|
return 0, "invalid move" if $move_type == MOVE_NOPE; |
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
|
|
|
|
|
12
|
my $piece_type = $piece & TYPE_MASK; |
93
|
6
|
100
|
|
|
|
19
|
return 0, "invalid move type" unless ( $move_type & $piece_type ) > 0; |
94
|
|
|
|
|
|
|
|
95
|
5
|
|
|
|
|
14
|
_move_stack( $board, $move_count, $srcx, $srcy, $stepx, $stepy ); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# score points for motion in the middle |
98
|
5
|
|
|
|
|
9
|
my $center = $board->[MIDDLE][MIDDLE]; |
99
|
5
|
100
|
100
|
|
|
17
|
if ( $center > 0 and ( $center & MOVED_MASK ) == MOVED_MASK ) { |
100
|
2
|
|
|
|
|
4
|
$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
|
|
|
|
|
9
|
for my $row ( $board->@* ) { |
106
|
45
|
|
|
|
|
59
|
for my $col ( $row->@* ) { |
107
|
405
|
|
|
|
|
501
|
$col &= ~MOVED_MASK; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
5
|
|
|
|
|
11
|
$player ^= 1; |
112
|
5
|
100
|
|
|
|
13
|
$move_count = _move_count() if $player == 0; |
113
|
|
|
|
|
|
|
|
114
|
5
|
|
|
|
|
46
|
return 1, "ok"; |
115
|
|
|
|
|
|
|
} |
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
|
|
|
1
|
1
|
6
|
method size() { return BOARD_SIZE } |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
2
|
|
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
|
|
|
1
|
1
|
17
|
method zobrist() { |
|
1
|
|
|
|
|
19
|
|
|
1
|
|
|
|
|
3
|
|
126
|
1
|
|
|
|
|
2
|
my @meta; |
127
|
1
|
|
|
|
|
2
|
my $location = 0; |
128
|
1
|
|
|
|
|
3
|
for my $row ( $board->@* ) { |
129
|
9
|
|
|
|
|
12
|
for my $piece ( $row->@* ) { |
130
|
81
|
100
|
|
|
|
136
|
next if $piece == 0; |
131
|
14
|
|
|
|
|
24
|
my $type = ($piece & TYPE_MASK) - 1; |
132
|
14
|
|
|
|
|
17
|
my $owner = $piece >> PLAYER_BIT & 1; |
133
|
14
|
|
|
|
|
26
|
push @meta, [ $type, $owner, $location++ ]; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
} |
136
|
1
|
|
|
|
|
5
|
return @meta; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
######################################################################## |
140
|
|
|
|
|
|
|
# |
141
|
|
|
|
|
|
|
# SUBROUTINES |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
# this many moves happen in each turnpair |
144
|
0
|
|
|
0
|
|
0
|
sub _move_count () { 1 + int rand(MAX_MOVES) } |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
# moves stuff with less recursion than in prior versions (before 0.05) |
147
|
10
|
|
|
10
|
|
9437
|
sub _move_stack ( $grid, $moves, $srcx, $srcy, $stepx, $stepy ) { |
|
10
|
|
|
|
|
15
|
|
|
10
|
|
|
|
|
14
|
|
|
10
|
|
|
|
|
16
|
|
|
10
|
|
|
|
|
11
|
|
|
10
|
|
|
|
|
15
|
|
|
10
|
|
|
|
|
15
|
|
|
10
|
|
|
|
|
11
|
|
148
|
10
|
|
|
|
|
17
|
my $point = []; |
149
|
10
|
|
|
|
|
29
|
$point->@[XX,YY] = ($srcx, $srcy); |
150
|
10
|
|
|
|
|
19
|
my @stack = $point; |
151
|
10
|
|
|
|
|
37
|
while ( $moves > 0 ) { |
152
|
37
|
|
|
|
|
54
|
my $point = []; |
153
|
37
|
|
|
|
|
77
|
$point->@[ XX, YY ] = ( $stack[-1][XX] + $stepx, $stack[-1][YY] + $stepy ); |
154
|
|
|
|
|
|
|
# edge: ran out of space for pushing |
155
|
|
|
|
|
|
|
last |
156
|
37
|
100
|
100
|
|
|
187
|
if $point->[XX] < 0 |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
157
|
|
|
|
|
|
|
or $point->[XX] >= BOARD_SIZE |
158
|
|
|
|
|
|
|
or $point->[YY] < 0 |
159
|
|
|
|
|
|
|
or $point->[YY] >= BOARD_SIZE; |
160
|
30
|
|
|
|
|
48
|
push @stack, $point; |
161
|
|
|
|
|
|
|
# empty cell: swap along the stack to advance the pieces |
162
|
30
|
100
|
|
|
|
69
|
if ( $grid->[ $stack[-1][YY] ][ $stack[-1][XX] ] == PIECE_EMPTY ) { |
163
|
27
|
|
|
|
|
50
|
for my $i ( reverse 0 .. ( $#stack - 1 ) ) { |
164
|
|
|
|
|
|
|
# downside: this may happen more than it needs to |
165
|
37
|
|
|
|
|
60
|
$grid->[ $stack[$i][YY] ][ $stack[$i][XX] ] |= MOVED_MASK; |
166
|
37
|
|
|
|
|
46
|
my $j = $i + 1; |
167
|
37
|
|
|
|
|
103
|
( $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
|
|
|
|
|
39
|
shift @stack; |
179
|
27
|
|
|
|
|
89
|
$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
|
|
|
17
|
|
13231
|
sub _move_type ( $x1, $y1, $x2, $y2 ) { |
|
17
|
|
|
|
|
27
|
|
|
17
|
|
|
|
|
22
|
|
|
17
|
|
|
|
|
22
|
|
|
17
|
|
|
|
|
23
|
|
|
17
|
|
|
|
|
21
|
|
187
|
17
|
100
|
100
|
|
|
56
|
return MOVE_NOPE, undef, undef if $x1 == $x2 and $y1 == $y2; |
188
|
|
|
|
|
|
|
|
189
|
15
|
|
|
|
|
37
|
my ( $dy, $plus_x ) = ( $y2 - $y1, $x2 > $x1 ); |
190
|
15
|
100
|
|
|
|
44
|
return MOVE_SQUARE, ( $plus_x ? 1 : -1 ), 0 if $dy == 0; |
|
|
100
|
|
|
|
|
|
191
|
|
|
|
|
|
|
|
192
|
11
|
|
|
|
|
20
|
my ( $dx, $plus_y ) = ( $x2 - $x1, $y2 > $y1 ); |
193
|
11
|
100
|
|
|
|
32
|
return MOVE_SQUARE, 0, ( $plus_y ? 1 : -1 ) if $dx == 0; |
|
|
100
|
|
|
|
|
|
194
|
|
|
|
|
|
|
|
195
|
9
|
100
|
|
|
|
59
|
return MOVE_DIAGONAL, ( $plus_x ? 1 : -1 ), ( $plus_y ? 1 : -1 ) |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
196
|
|
|
|
|
|
|
if abs($dx) == abs($dy); |
197
|
|
|
|
|
|
|
|
198
|
1
|
|
|
|
|
5
|
return MOVE_NOPE, undef, undef; |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
1; |
202
|
|
|
|
|
|
|
__END__ |