| 1 | package Games::Sequential; | ||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use 5.006001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | our $VERSION = '0.4.2'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | Games::Sequential - sequential games framework with OO interface | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | package My::GamePos; | ||||||||||||||||||||||||||||
| 16 | use base qw(Games::Sequential::Position); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | sub apply { ... } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | package main; | ||||||||||||||||||||||||||||
| 21 | use My::GamePos; | ||||||||||||||||||||||||||||
| 22 | use Games::Sequential; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | my $pos = My::GamePos->new; | ||||||||||||||||||||||||||||
| 25 | my $game = Games::Sequential->new($pos); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | $game->debug(1); | ||||||||||||||||||||||||||||
| 28 | $game->move($mv); | ||||||||||||||||||||||||||||
| 29 | $game->undo; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | Games::Sequential is a framework for producing sequential games. | ||||||||||||||||||||||||||||
| 35 | Among other things it keeps track of the sequence of moves, and | ||||||||||||||||||||||||||||
| 36 | provides an unlimited C<undo()> mechanism. It also has methods to | ||||||||||||||||||||||||||||
| 37 | C<clone()> or take a C<snapshot()> of a game. | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | Users must pass an object representing the initial state of the | ||||||||||||||||||||||||||||
| 40 | game as the first argument to C<new()>. This object must provide | ||||||||||||||||||||||||||||
| 41 | the two methods C<copy()> and C<apply()>. You can use | ||||||||||||||||||||||||||||
| 42 | L<Games::Sequential::Position> as a base class, in which case the | ||||||||||||||||||||||||||||
| 43 | C<copy()> method will be provided for you. The C<apply()> method | ||||||||||||||||||||||||||||
| 44 | must take a move and apply it to the current position, producing | ||||||||||||||||||||||||||||
| 45 | the next position in the game. | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =head1 METHODS | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | Users must not modify the referred-to values of references | ||||||||||||||||||||||||||||
| 50 | returned by any of the below methods. | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =over 4 | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =item new $initialpos [@list] | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | Create and return a new L<Games::Sequential> object. The first | ||||||||||||||||||||||||||||
| 57 | argument must be an object representing the initial position of | ||||||||||||||||||||||||||||
| 58 | the game. The C<debug> option can also be set here. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =cut | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 4 | my $invocant = shift; | |||||||||||||||||||||||||||
| 64 | 4 | ▶ | my $class = ref($invocant) || $invocant; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $invocant || $invocant
MC/DC0%0/2
ref $invocant || $invocant
ref $invocant
$invocant
Truth table33%1/3
A || B
A= ref $invocantB= $invocant
| |||||||||||||||||||||||||||||
| 65 | 4 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 4 | ▶ | $self->_init(@_) or carp "Failed to init object!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 4 | return $self; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =item _init [@list] | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | I<Internal method> | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | Initialize a L<Games::Sequential> object. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =cut | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 4 | ▶ | sub _init { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=7 SCAR=23.0
| |||||||||||||||||||||||||||||
| 81 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 82 | 4 | ▶ | my $pos = shift or croak "No initial position given!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 4 | ▶ | my $args = @_ && ref($_[0]) ? shift : { @_ }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ and ref $_[0]
MC/DC0%0/2
@_ and ref $_[0]
@_
ref $_[0]
Truth table33%1/3
A and B
A= @_B= ref $_[0]
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 4 | my %config = ( | |||||||||||||||||||||||||||
| 86 | # Stacks for backtracking | ||||||||||||||||||||||||||||
| 87 | pos_hist => [ $pos ], | ||||||||||||||||||||||||||||
| 88 | move_hist => [], | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # Debug and statistics | ||||||||||||||||||||||||||||
| 91 | debug => 0, | ||||||||||||||||||||||||||||
| 92 | ); | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # Set defaults | ||||||||||||||||||||||||||||
| 95 | 4 | @$self{keys %config} = values %config; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # Override defaults | ||||||||||||||||||||||||||||
| 98 | 4 | ▶ | while (my ($key, $val) = each %{ $args }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | $self->{$key} = $val if exists $self->{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 4 | ▶ | croak "no apply() method defined for position object" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | unless $pos->can("apply"); | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 4 | return $self; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =item debug [$value] | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | Return current debug level and, if invoked with an argument, set | ||||||||||||||||||||||||||||
| 112 | to new value. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =cut | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 3 | ▶ | sub debug { | ||||||||||||||||||||||||||
|
Subroutine
debug: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 118 | 3 | my $prev = $self->{debug}; | |||||||||||||||||||||||||||
| 119 | 3 | ▶ | $self->{debug} = shift if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 3 | return $prev; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =item peek_pos | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | Return reference to current position. | ||||||||||||||||||||||||||||
| 127 | Use this for drawing the board etc. | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =cut | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 389 | ▶ | sub peek_pos { | ||||||||||||||||||||||||||
|
Subroutine
peek_pos: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 132 | 389 | my $self = shift; | |||||||||||||||||||||||||||
| 133 | 389 | return $self->{pos_hist}[-1]; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =item peek_move | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | Return reference to last applied move. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =cut | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 4 | ▶ | sub peek_move { | ||||||||||||||||||||||||||
|
Subroutine
peek_move: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 144 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 145 | 4 | return $self->{move_hist}[-1]; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =item move $move | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | Apply $move to the current position, keeping track of history. | ||||||||||||||||||||||||||||
| 152 | A reference to the new position is returned, or undef on failure. | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =cut | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 128 | ▶ | sub move { | ||||||||||||||||||||||||||
|
Subroutine
move: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 157 | 128 | my ($self, $move) = @_; | |||||||||||||||||||||||||||
| 158 | 128 | my ($pos, $npos) = $self->peek_pos; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 128 | ▶ | $npos = $pos->copy or croak "$pos->copy() failed"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 128 | ▶ | $npos->apply($move) or croak "$pos->apply() failed"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 128 | push @{ $self->{pos_hist} }, $npos; | |||||||||||||||||||||||||||
| 164 | 128 | push @{ $self->{move_hist} }, $move; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 128 | return $self->peek_pos; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =item undo | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | Undo last move. A reference to the previous position is returned, | ||||||||||||||||||||||||||||
| 173 | or undef if there was no more moves to undo. | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =cut | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 1 | ▶ | sub undo { | ||||||||||||||||||||||||||
|
Subroutine
undo: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 178 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 179 | 1 | ▶ | return unless pop @{ $self->{move_hist} }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 1 | ▶ | pop @{ $self->{pos_hist} } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | or croak "move and pos stack out of sync!"; | ||||||||||||||||||||||||||||
| 182 | 1 | return $self->peek_pos; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 4 | 1; # ensure using this module works | |||||||||||||||||||||||||||
| 187 | __END__ | ||||||||||||||||||||||||||||