File Coverage

blib/lib/Games/Sequential/Position.pm
Criterion Covered Total %
statement 32 34 94.1
branch 4 8 50.0
condition 2 6 33.3
subroutine 9 10 90.0
pod 5 5 100.0
total 52 63 82.5


line stmt bran cond sub pod time code
1             package Games::Sequential::Position;
2 5     5   24188 use strict;
  5         14  
  5         262  
3 5     5   21 use warnings;
  5         8  
  5         121  
4              
5 5     5   22 use Carp;
  5         11  
  5         379  
6 5     5   5020 use Storable qw(dclone);
  5         17471  
  5         345  
7 5     5   133 use 5.006001;
  5         16  
  5         1575  
8              
9             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. This class is
32             provided for convenience; you don't need this class to use
33             C. 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 method. If you chose to not use this class, you must
40             also implement a C 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             sub apply {
65 0     0 1 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 method. Return a blessed hash reference.
80              
81             =cut
82              
83             sub new {
84 5     5 1 1698391 my $invocant = shift;
85 5   33     64 my $class = ref($invocant) || $invocant;
86 5         29 my $self = bless {}, $class;
87              
88 5 50       54 $self->init(@_) or carp "Failed to initialise object!";
89              
90 5         37 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. You You probably want to
100             override this method and initialise your position there.
101              
102             =cut
103              
104             sub init {
105 5     5 1 65 my $self = shift;
106 5 50 33     52 my $args = @_ && ref($_[0]) ? shift : { @_ };
107 5         17 my %config = (
108             player => 1,
109             );
110              
111 5         26 @$self{keys %config} = values %config;
112              
113             # Override defaults
114 5         18 while (my ($key, $val) = each %{ $args }) {
  5         32  
115 0 0       0 $self->{$key} = $val if exists $self->{$key};
116             }
117              
118 5         52 return $self;
119             }
120              
121              
122             =item copy
123              
124             Clone a position.
125              
126             =cut
127              
128             sub copy {
129 2946     2946 1 4282 my $self = shift;
130 2946         9423857 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             sub player {
142 102     102 1 108 my $self = shift;
143 102 100       201 $self->{player} = shift if @_;
144 102         246 return $self->{player};
145             }
146              
147              
148              
149             1; # ensure using this module works
150             __END__