File Coverage

blib/lib/Games/Board/Piece.pm
Criterion Covered Total %
statement 36 37 97.3
branch 11 18 61.1
condition 2 3 66.6
subroutine 9 9 100.0
pod 6 6 100.0
total 64 73 87.6


line stmt bran cond sub pod time code
1 4     4   26 use strict;
  4         9  
  4         115  
2 4     4   20 use warnings;
  4         9  
  4         167  
3             package Games::Board::Piece 1.014;
4             # ABSTRACT: a parent class for board game pieces
5              
6 4     4   20 use Carp;
  4         7  
  4         1516  
7              
8             #pod =head1 SYNOPSIS
9             #pod
10             #pod use Games::Board;
11             #pod
12             #pod my $board = Games::Board->new;
13             #pod
14             #pod $board->add_space(
15             #pod id => 'go',
16             #pod dir => { next => 'mediterranean', prev => 'boardwalk' },
17             #pod cost => undef
18             #pod );
19             #pod
20             #pod my $tophat = Games::Board::Piece->new(id => 'tophat')->move(to => 'go');
21             #pod
22             #pod =head1 DESCRIPTION
23             #pod
24             #pod This module provides a base class for representing the pieces in a board game.
25             #pod
26             #pod =cut
27              
28             #pod =method new
29             #pod
30             #pod This method constructs a new game piece and returns it.
31             #pod
32             #pod =cut
33              
34             sub new {
35 3     3 1 23 my ($class, %args) = @_;
36              
37 3 50       30 return unless $args{id};
38 3 50       9 return unless eval { $args{board}->isa('Games::Board') };
  3         21  
39              
40 3         15 my $piece = { %args };
41              
42 3         12 bless $piece => $class;
43             }
44              
45             #pod =method id
46             #pod
47             #pod This returns the piece's id.
48             #pod
49             #pod =cut
50              
51             sub id {
52 8     8 1 11 my $self = shift;
53 8         39 $self->{id};
54             }
55              
56             #pod =method board
57             #pod
58             #pod This returns the board object to which the piece is related.
59             #pod
60             #pod =cut
61              
62             sub board {
63 2     2 1 5 my $self = shift;
64 2         8 $self->{board};
65             }
66              
67             #pod =method current_space_id
68             #pod
69             #pod This returns the id of the space on which the piece currently rests, if any.
70             #pod It it's not on any space, it returns undef.
71             #pod
72             #pod =cut
73              
74             sub current_space_id {
75 6     6 1 1016 my $piece = shift;
76 6         23 $piece->{current_space};
77             }
78              
79             #pod =method current_space
80             #pod
81             #pod This returns the Space on which the piece currently rests, if any. It it's not
82             #pod on any space, it returns undef.
83             #pod
84             #pod =cut
85              
86             sub current_space {
87 4     4 1 9 my $piece = shift;
88 4 100       16 return unless $piece->{current_space};
89 2         10 $piece->board->space($piece->{current_space});
90             }
91              
92             #pod =method move
93             #pod
94             #pod $piece->move(dir => 'up')
95             #pod
96             #pod $piece->move(to => $space)
97             #pod
98             #pod This method moves the piece to a new space on the board. If the method call is
99             #pod in the first form, the piece is moved to the space in the given direction from
100             #pod the piece's current space. If the method call is in the second form, and
101             #pod C<$space> is a Games::Board::Space object, the piece is moved to that space.
102             #pod
103             #pod =cut
104              
105             sub move {
106 4     4 1 10 my $piece = shift;
107 4         21 my ($how, $which) = @_;
108 4         8 my $new_space;
109 4         24 my $old_space = $piece->current_space;
110              
111 4 100       17 if ($how eq 'dir') {
    50          
112 2 50       13 return unless $old_space;
113 2 50       14 return unless $new_space = $old_space->dir($which);
114             } elsif ($how eq 'to') {
115 2 50       10 return unless eval { $which->isa('Games::Board::Space') };
  2         22  
116 2         12 $new_space = $which;
117             } else {
118 0         0 return;
119             }
120              
121 4 50 66     33 return unless !$old_space || $old_space->take($piece);
122 4         20 $new_space->receive($piece);
123             }
124              
125             1;
126              
127             __END__