blib/lib/Game/Brandubh/Rules.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/Engine.pm ↑ Summary blib/lib/Game/Brandubh/Terminal.pm »
1 package Game::Brandubh::Rules;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶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 use Game::Brandubh::Engine ();
12  
13 our $VERSION = '0.03';
14  
15 use constant {
16 ONGOING => 0,
17 BY_CORNER => 1,
18 BY_CAPTURE => 2,
19 BY_NO_PIECES => 3,
20 DRAW_REPETITION => 4,
21 DRAW_NO_MOVE => 5,
22 DRAW_PLY_CAP => 6,
23 };
24  
25 use constant {
26 PLAY_OK => 0,
27 PLAY_OVER => 1,
28 PLAY_ILLEGAL => 2,
29 };
30  
31 our @EXPORT_OK = qw(
32 ONGOING BY_CORNER BY_CAPTURE BY_NO_PIECES
33 DRAW_REPETITION DRAW_NO_MOVE DRAW_PLY_CAP
34 PLAY_OK PLAY_OVER PLAY_ILLEGAL
35 outcome_name
36 );
37 our %EXPORT_TAGS = (all => \@EXPORT_OK);
38  
39 my @NAME;
40 BEGIN { @NAME = qw(ongoing corner capture no_pieces repetition no_move ply_cap) }
41  
42 sub outcome_name {
43 my ($outcome) = @_;
44 return undef unless defined $outcome && $outcome =~ /\A[0-9]+\z/ && $outcome < @NAME;
45 return $NAME[$outcome];
46 }
47  
48 has _ptr => (is => 'rw', private => 1);
49  
50 has _start => (is => 'ro', init_arg => 'position', private => 1);
51  
52 has _rules => (is => 'ro', init_arg => 'variant', private => 1);
53  
54 my %ADOPTING;
55  
56 sub BUILD {
57 my ($self) = @_;
58 my $given = $self->_ptr;
59 if ($given) {
60 return if delete $ADOPTING{$given};
61 $self->_ptr(0);
62 Carp::croak("Game::Brandubh::Rules: a game is made by new or clone");
63 }
64  
65 my ($ptr, $err) = _new_game($self->_start, $self->_rules);
66 Carp::croak("Game::Brandubh::Rules: the position was refused, code $err")
67 unless defined $ptr;
68 $self->_ptr($ptr);
69 return;
70 }
71  
72 my $adopt = sub {
73 my ($class, $ptr) = @_;
74 $ADOPTING{$ptr} = 1;
75 return $class->new(_ptr => $ptr);
76 };
77  
78 sub clone {
79 my ($self) = @_;
80 return $adopt->(ref($self), _copy_game($self->_ptr));
81 }
82  
83 sub DEMOLISH {
84 my ($self) = @_;
85 my $ptr = $self->_ptr;
86 return unless $ptr;
87 _drop_game($ptr);
88 $self->_ptr(0);
89 return;
90 }
91  
92 sub position { _position($_[0]->_ptr) }
93 sub key_hex { _key_hex($_[0]->_ptr) }
94 sub key_at { _key_at_hex($_[0]->_ptr, $_[1]) }
95 sub side { _side($_[0]->_ptr) }
96 sub at { _at($_[0]->_ptr, $_[1]) }
97 sub count { _count($_[0]->_ptr, $_[1]) }
98 sub ply { _ply($_[0]->_ptr) }
99 sub ply_cap { _cap($_[0]->_ptr) }
100 sub variant { _variant($_[0]->_ptr) }
101  
102 sub board {
103 my ($self) = @_;
104 return Game::Brandubh::Engine->new(position => _position($self->_ptr));
105 }
106  
107 sub outcome { _outcome($_[0]->_ptr) }
108 sub is_over { _outcome($_[0]->_ptr) != ONGOING }
109 sub is_draw { _outcome($_[0]->_ptr) >= DRAW_REPETITION }
110 sub repeats { _repeats($_[0]->_ptr) }
111  
112 sub winner {
113 my $side = _winner($_[0]->_ptr);
114 return $side < 0 ? undef : $side;
115 }
116  
117 sub winner_of {
118 my $side = _winner_of($_[1]);
119 return $side < 0 ? undef : $side;
120 }
121  
122 sub moves {
123 my @m = _moves($_[0]->_ptr);
124 return wantarray ? @m : scalar @m;
125 }
126  
127 sub play {
128 my ($answer, $flags) = _play($_[0]->_ptr, $_[1]);
129 return wantarray ? ($answer, $flags) : $answer;
130 }
131  
132 sub undo { _undo($_[0]->_ptr) }
133 sub preview { _preview($_[0]->_ptr, $_[1]) }
134 sub why_not { _why_not($_[0]->_ptr, $_[1], $_[2]) }
135  
136 sub live { _games_live() }
137  
138 sub search {
139 my ($self, %with) = @_;
140 my $budget = $with{budget};
141 Carp::croak('Game::Brandubh::Rules: search needs a budget, a whole number of nodes from 1 up')
142 unless defined $budget && !ref $budget && $budget =~ /\A[0-9]{1,9}\z/ && $budget >= 1;
143 my $seed = defined $with{seed} ? $with{seed} : 1;
144 Carp::croak('Game::Brandubh::Rules: a search seed is a whole number below 4294967296')
145 unless !ref $seed && $seed =~ /\A[0-9]{1,10}\z/ && $seed < 4294967296;
146 my $depth = defined $with{depth} ? $with{depth} : 0;
147 Carp::croak('Game::Brandubh::Rules: a search depth is a whole number')
148 unless !ref $depth && $depth =~ /\A[0-9]{1,3}\z/;
149  
150 my ($mv, $nodes, $reached, $score, $stopped) = _search($self->_ptr, $budget, $seed, $depth, $with{weights});
151 return undef unless $mv;
152 return { move => $mv, nodes => $nodes, depth => $reached, score => $score, stopped => $stopped };
153 }
154  
155 sub evaluate { _evaluate($_[0]->_ptr, $_[1]) }
156  
157 sub weights { _weights() }
158  
159 1;
160  
161 __END__
« blib/lib/Game/Brandubh/Engine.pm ↑ Summary blib/lib/Game/Brandubh/Terminal.pm »