blib/lib/Games/Sequential/Position.pm

filter: Statementstmt 94.2% 33 / 35 Branchbran 50.0% 5 / 10 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 81.8% 9 / 11 Podpod 100% 5 / 5 totaltotal 76.0% 54 / 71 CC 8 SCAR 11.4
CC
8
Cov
78%
init
6.7
new
3.2
apply
2.0
CRAP 3.1
« blib/lib/Games/Sequential.pm ↑ Summary blib/lib/Games/AlphaBeta/Position.pm »
1 package Games::Sequential::Position;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: not called CC=1 SCAR=0.0
8  
9 6 our $VERSION = '0.3.1';
10  
11 =head1 NAME
12  
13 Games::Sequential::Position - base Position class for use with Games::Sequential
14  
15 =head1 SYNOPSIS
16  
17 package My::GamePos;
18 use base Games::Sequential::Position;
19  
20 sub init { ... } # setup initial state
21 sub apply { ... }
22  
23 package main;
24 my $pos = My::GamePos->new;
25 my $game = Games::Sequential->new($pos);
26  
27  
28 =head1 DESCRIPTION
29  
30 Games::Sequential::Position is a base class for position-classes
31 that can be used with L<Games::Sequential>. This class is
32 provided for convenience; you don't need this class to use
33 C<Games::Sequential>. It is also possible to use this class on
34 its own.
35  
36 =head1 PURE VIRTUAL METHODS
37  
38 Modules inheriting this class must implement at least the
39 C<apply()> method. If you chose to not use this class, you must
40 also implement a C<copy()> method which makes a deep copy of the
41 object.
42  
43 =over 4
44  
45 =item apply($move)
46  
47 Accept a move and apply it to the current state producing the
48 next state. Return a reference to itself. Note that this method
49 is responsible for also advancing the state's perception of which
50 player's turn it is.
51  
52 Something like this (sans error checking):
53  
54 sub apply {
55 my ($self, $move) = @_;
56  
57 ... apply $move, creating next position ...
58  
59 return $self;
60 }
61  
62 =cut
63  
64 0 ▶sub apply {
Subroutine apply: not called CC=1 SCAR=6.9
Pod documented
65 0 croak "apply(): Call to pure virtual method\n";
66 }
67  
68 =back
69  
70 =head1 METHODS
71  
72 The following methods are provided by this class.
73  
74 =over 4
75  
76 =item new [@list]
77  
78 Create and return an object. Any arguments is passed on to the
79 C<init()> method. Return a blessed hash reference.
80  
81 =cut
82  
83 6 ▶sub new {
Subroutine new: called CC=3 SCAR=11.8
Pod documented
84 6 my $invocant = shift;
85 6 ▶ my $class = ref($invocant) || $invocant;
Condition33%1/3
ref $invocant || $invocant
l!l&&r!l&&!r
060
MC/DC0%0/2
ref $invocant || $invocant
ref $invocant $invocant
Truth table33%1/3
A || B
A= ref $invocantB= $invocant
ABresult
1X1
011
000
86 6 my $self = bless {}, $class;
87  
88 6 ▶ $self->init(@_) or carp "Failed to initialise object!";
Branch: 1 branch
BranchTrueFalseTotal
$self->init(@_) or carp("Failed to initialise object!") 6 0 6
89  
90 6 return $self;
91 }
92  
93  
94 =item init [@list]
95  
96 Initialize an object. By default, this only means setting player
97 1 to be the current player.
98  
99 This method is called by C<new()>. You You probably want to
100 override this method and initialise your position there.
101  
102 =cut
103  
104 6 ▶sub init {
Subroutine init: called CC=5 SCAR=19.1
Pod documented
105 6 my $self = shift;
106 6 ▶ my $args = @_ && ref($_[0]) ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] ? : 0 6 6
Condition33%1/3
@_ and ref $_[0]
!ll&&!rl&&r
600
MC/DC0%0/2
@_ and ref $_[0]
@_ ref $_[0]
Truth table33%1/3
A and B
A= @_B= ref $_[0]
ABresult
0X0
100
111
107 6 my %config = (
108 player => 1,
109 );
110  
111 6 @$self{keys %config} = values %config;
112  
113 # Override defaults
114 6 ▶ while (my ($key, $val) = each %{ $args }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$args;}) 0 6 6
115 0 ▶ $self->{$key} = $val if exists $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$key} 0 0 0
116 }
117  
118 6 return $self;
119 }
120  
121  
122 =item copy
123  
124 Clone a position.
125  
126 =cut
127  
128 5738 ▶sub copy {
Subroutine copy: called CC=1 SCAR=0.0
Pod documented
129 5738 my $self = shift;
130 5738 return dclone($self);
131 }
132  
133  
134 =item player [$player]
135  
136 Read and/or set the current player. If argument is given, that
137 will be set to the current player.
138  
139 =cut
140  
141 202 ▶sub player {
Subroutine player: called CC=2 SCAR=6.9
Pod documented
142 202 my $self = shift;
143 202 ▶ $self->{player} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 101 101 202
144 202 return $self->{player};
145 }
146  
147  
148  
149 6 1; # ensure using this module works
150 __END__
« blib/lib/Games/Sequential.pm ↑ Summary blib/lib/Games/AlphaBeta/Position.pm »