blib/lib/Game/Brandubh/Engine.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Game/Brandubh/Variant.pm ↑ Summary blib/lib/Game/Brandubh/Rules.pm »
1 package Game::Brandubh::Engine;
2  
3 14 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 14 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶use Object::Proto::Sugar;
Subroutine BEGIN: called CC=1 SCAR=2.6
10  
11 our $VERSION = '0.03';
12  
13 require XSLoader;
14 XSLoader::load('Game::Brandubh', $VERSION);
15  
16 use constant {
17 EMPTY => 0,
18 ATTACKER => 1,
19 DEFENDER => 2,
20 KING => 3,
21 BORDER => 4,
22  
23 ATTACKERS => 0,
24 DEFENDERS => 1,
25  
26 SIZE => 7,
27 SQUARES => 49,
28 };
29  
30 use constant {
31 POS_OK => 0,
32 POS_NULL => 1,
33 POS_ROWS => 2,
34 POS_WIDTH => 3,
35 POS_LETTER => 4,
36 POS_SIDE => 5,
37 POS_LONG => 6,
38 };
39  
40 use constant {
41 WHY_OK => 0,
42 WHY_OFF_BOARD => 1,
43 WHY_NO_PIECE => 2,
44 WHY_NOT_YOURS => 3,
45 WHY_NO_MOVE => 4,
46 WHY_NOT_A_LINE => 5,
47 WHY_THRONE => 6,
48 WHY_CORNER => 7,
49 WHY_BLOCKED => 8,
50 };
51  
52 use constant {
53 DID_CAPTURE => 1,
54 KING_TAKEN => 2,
55 KING_HOME => 4,
56 };
57  
58 our @EXPORT_OK = qw(
59 EMPTY ATTACKER DEFENDER KING BORDER
60 ATTACKERS DEFENDERS SIZE SQUARES
61 POS_OK POS_NULL POS_ROWS POS_WIDTH POS_LETTER POS_SIDE POS_LONG
62 WHY_OK WHY_OFF_BOARD WHY_NO_PIECE WHY_NOT_YOURS WHY_NO_MOVE
63 WHY_NOT_A_LINE WHY_THRONE WHY_CORNER WHY_BLOCKED
64 DID_CAPTURE KING_TAKEN KING_HOME
65 other
66 );
67 our %EXPORT_TAGS = (all => \@EXPORT_OK);
68  
69 sub other { $_[0] == ATTACKERS ? DEFENDERS : ATTACKERS }
70  
71 has _ptr => (is => 'rw', private => 1);
72  
73 has _empty => (is => 'ro', init_arg => 'empty', private => 1, default => 0);
74  
75 has _position => (is => 'ro', init_arg => 'position', private => 1);
76  
77 my %ADOPTING;
78  
79 sub BUILD {
80 my ($self) = @_;
81 my $given = $self->_ptr;
82 if ($given) {
83 return if delete $ADOPTING{$given};
84 $self->_ptr(0);
85 Carp::croak("Game::Brandubh::Engine: a board is made by new, of_string or clone");
86 }
87  
88 my $position = $self->_position;
89 if (defined $position) {
90 my ($ptr, $err) = _of_string($position);
91 Carp::croak("Game::Brandubh::Engine: the position was refused, code $err")
92 unless defined $ptr;
93 $self->_ptr($ptr);
94 return;
95 }
96  
97 $self->_ptr($self->_empty ? _new_empty() : _new_board());
98 return;
99 }
100  
101 my $adopt = sub {
102 my ($class, $ptr) = @_;
103 $ADOPTING{$ptr} = 1;
104 return $class->new(_ptr => $ptr);
105 };
106  
107 sub of_string {
108 my ($class, $position) = @_;
109 my ($ptr, $err) = _of_string(defined $position ? $position : '');
110 return (undef, $err) unless defined $ptr;
111 return ($adopt->($class, $ptr), POS_OK);
112 }
113  
114 sub clone {
115 my ($self) = @_;
116 return $adopt->(ref($self), _copy_board($self->_ptr));
117 }
118  
119 sub DEMOLISH {
120 my ($self) = @_;
121 my $ptr = $self->_ptr;
122 return unless $ptr;
123 _drop_board($ptr);
124 $self->_ptr(0);
125 return;
126 }
127  
128 sub at { _at($_[0]->_ptr, $_[1]) }
129 sub put { _put($_[0]->_ptr, $_[1], $_[2]); return $_[0] }
130 sub lift { _lift($_[0]->_ptr, $_[1]); return $_[0] }
131 sub side { _side($_[0]->_ptr) }
132 sub set_side { _set_side($_[0]->_ptr, $_[1]); return $_[0] }
133 sub count { _count($_[0]->_ptr, $_[1]) }
134 sub king_square { _king_square($_[0]->_ptr) }
135 sub to_string { _to_string($_[0]->_ptr) }
136 sub key_hex { _key_hex($_[0]->_ptr) }
137 sub key_full_hex { _key_full_hex($_[0]->_ptr) }
138  
139 sub square_of { my $c = shift; _square_of($_[0], $_[1]) }
140 sub file_of { my $c = shift; _file_of($_[0]) }
141 sub rank_of { my $c = shift; _rank_of($_[0]) }
142 sub on_board { my $c = shift; _on_board($_[0]) }
143 sub is_throne { my $c = shift; _is_throne($_[0]) }
144 sub is_corner { my $c = shift; _is_corner($_[0]) }
145 sub beside_throne { my $c = shift; _beside_throne($_[0]) }
146 sub side_of { my $c = shift; _side_of($_[0]) }
147 sub stride { _stride() }
148  
149 sub all_squares {
150 my @squares;
151 for my $rank (0 .. SIZE - 1) {
152 push @squares, map { _square_of($_, $rank) } 0 .. SIZE - 1;
153 }
154 return @squares;
155 }
156  
157 sub zobrist_hex { my $c = shift; _zobrist_hex($_[0], $_[1]) }
158 sub zobrist_side_hex { _zobrist_side_hex() }
159 sub abi_version { _abi_version() }
160 sub live { _live() }
161  
162 sub move { _move_make($_[1], $_[2]) }
163 sub move_from { _move_from($_[1]) }
164 sub move_to { _move_to($_[1]) }
165 sub moves_max { _moves_max() }
166  
167 sub moves {
168 my @m = _gen_moves($_[0]->_ptr, $_[1]);
169 return wantarray ? @m : scalar @m;
170 }
171  
172 sub is_legal { _is_legal($_[0]->_ptr, $_[1], $_[2]) }
173 sub why_not { _why_not($_[0]->_ptr, $_[1], $_[2], $_[3]) }
174 sub relocate { _relocate($_[0]->_ptr, $_[1]); return $_[0] }
175  
176 sub perft_slides { _perft_slides($_[0]->_ptr, $_[1], $_[2]) }
177  
178 sub hostile_to { _hostile_to($_[0]->_ptr, $_[1], $_[2]) }
179 sub captures_at { _captures_at($_[0]->_ptr, $_[1], $_[2]) }
180 sub do_move { _do_move($_[0]->_ptr, $_[1], $_[2]) }
181 sub undo_move { _undo_move($_[0]->_ptr, $_[1]); return $_[0] }
182 sub preview { _preview($_[0]->_ptr, $_[1], $_[2]) }
183 sub perft { _perft($_[0]->_ptr, $_[1], $_[2]) }
184  
185 1;
186  
187 __END__
« blib/lib/Game/Brandubh/Variant.pm ↑ Summary blib/lib/Game/Brandubh/Rules.pm »