File Coverage

blib/lib/Games/Board.pm
Criterion Covered Total %
statement 39 40 97.5
branch 3 6 50.0
condition 1 3 33.3
subroutine 11 11 100.0
pod 6 6 100.0
total 60 66 90.9


line stmt bran cond sub pod time code
1 4     4   68865 use strict;
  4         20  
  4         114  
2 4     4   21 use warnings;
  4         9  
  4         166  
3             package Games::Board 1.014;
4             # ABSTRACT: a parent class for board games
5              
6 4     4   20 use Carp;
  4         9  
  4         225  
7 4     4   1603 use Games::Board::Space;
  4         12  
  4         112  
8 4     4   1596 use Games::Board::Piece;
  4         11  
  4         1156  
9              
10             #pod =head1 SYNOPSIS
11             #pod
12             #pod use Games::Board;
13             #pod
14             #pod my $board = Games::Board->new;
15             #pod
16             #pod $board->add_space(
17             #pod id => 'go',
18             #pod dir => { next => 'mediterranean', prev => 'boardwalk' },
19             #pod cost => undef
20             #pod );
21             #pod
22             #pod my $tophat = Games::Board::Piece->new(id => 'tophat')->move(to => 'go');
23             #pod
24             #pod =head1 DESCRIPTION
25             #pod
26             #pod This module provides a base class for representing board games.
27             #pod
28             #pod =method new
29             #pod
30             #pod This method constructs a new game board and returns it. As constructed it has
31             #pod no spaces or pieces on it.
32             #pod
33             #pod =cut
34              
35             sub new {
36 2     2 1 666 my $class = shift;
37              
38 2         34 my $board = {
39             spaces => { }
40             };
41              
42 2         9 bless $board => $class;
43             }
44              
45             #pod =method space
46             #pod
47             #pod my $space = $board->space($id);
48             #pod
49             #pod This method returns the space with the given C<$id>. If no space with that id
50             #pod exists, undef is returned.
51             #pod
52             #pod =cut
53              
54             sub space {
55 228     228 1 32906 my $board = shift;
56 228         304 my $space = shift;
57              
58 228         702 return $board->{spaces}{$space};
59             }
60              
61             #pod =method add_space
62             #pod
63             #pod my $space = $board->add_space(%args);
64             #pod
65             #pod This method adds a space to the board. It is passed a hash of attributes to
66             #pod use in creating a Games::Board::Space object. The object is created by calling
67             #pod the constructor on the class whose name is returned by the C
68             #pod method. This class must inherit from Games::Board::Space.
69             #pod
70             #pod =cut
71              
72             sub add_space {
73 68     68 1 1129 my ($board, %args) = @_;
74 68         90 my $space;
75              
76 68         100 $space = $board->spaceclass->new(board => $board, %args);
77              
78 68 50       96 return unless eval { $space->isa('Games::Board::Space') };
  68         180  
79              
80 68 50       139 if ($board->space($space->id)) {
81 0         0 carp "space '" . $space->id . "' already exists on board";
82             } else {
83 68         117 $board->{spaces}{$space->id} = $space;
84 68         180 return $space;
85             }
86             }
87              
88             #pod =method piececlass
89             #pod
90             #pod This method returns the class used for pieces on this board.
91             #pod
92             #pod =cut
93              
94 2     2 1 22 sub piececlass { 'Games::Board::Piece' }
95              
96             #pod =method spaceclass
97             #pod
98             #pod This method returns the class used for spaces on this board.
99             #pod
100             #pod =cut
101              
102 68     68 1 197 sub spaceclass { 'Games::Board::Space' }
103              
104             #pod =method add_piece
105             #pod
106             #pod my $piece = $board->add_piece(%args)
107             #pod
108             #pod This method adds a piece to the board. It is passed a hash of attributes to
109             #pod use in creating a Games::Board::Piece object. The object is created by calling
110             #pod the constructor on the class whose name is returned by the C
111             #pod method. This class must inherit from Games::Board::Piece.
112             #pod
113             #pod =cut
114              
115             sub add_piece {
116 3     3 1 528 my $board = shift;
117 3         12 my %args = @_;
118 3         6 my $piece;
119              
120 3         16 $piece = $board->piececlass->new(board => $board, @_);
121 3   33     22 $piece ||= shift;
122              
123 3 50       5 return unless eval { $piece->isa('Games::Board::Piece') };
  3         23  
124              
125 3         12 return $piece;
126             }
127              
128             "Family fun night!";
129              
130             __END__