File Coverage

blib/lib/Game/Life/NDim.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Game::Life::NDim;
2              
3             # Created on: 2010-01-04 18:52:01
4             # Create by: Ivan Wills
5             # $Id$
6             # $Revision$, $HeadURL$, $Date$
7             # $Revision$, $Source$, $Date$
8              
9 2     2   39081 use Moose;
  0            
  0            
10             use warnings;
11             use version;
12             use Carp;
13             use Scalar::Util;
14             use List::Util qw/sum/;
15             #use List::MoreUtils;
16             use Data::Dumper qw/Dumper/;
17             use English qw/ -no_match_vars /;
18             use Game::Life::NDim::Board;
19              
20             use overload '""' => \&to_string;
21              
22             our $VERSION = version->new('0.0.2');
23             our @EXPORT_OK = qw/game_of_life/;
24             our %EXPORT_TAGS = ();
25              
26             has board => (
27             is => 'rw',
28             isa => 'Game::Life::NDim::Board',
29             required => 1,
30             );
31              
32             has rules => (
33             is => 'rw',
34             isa => 'ArrayRef',
35             default => sub {[]},
36             );
37              
38             sub game_of_life {
39             my %params = @_;
40              
41             my $board = Game::Life::NDim::Board->new(%params);
42             die "Where's my wrap? " . Dumper \%params, $board if $params{wrap} && !$board->wrap;
43             my %new = (board => $board);
44             $new{types} = $params{types} if $params{types};
45              
46             return __PACKAGE__->new(%new);
47             }
48              
49             sub add_rule {
50             my ($self, @rules) = @_;
51              
52             while (@rules) {
53             my $rule = shift @rules;
54             if (ref $rule eq 'CODE') {
55             push @{ $self->rules }, $rule;
56             }
57             else {
58             my $value = shift @rules;
59             push @{ $self->rules },
60             $rule eq 'live' ? sub { $_[0] ? undef : ( sum $_[0]->surround ) > $value ? 1 : undef }
61             : $rule eq 'die' ? sub { !$_[0] ? undef : ( sum $_[0]->surround ) < $value ? 0 : undef }
62             : die "The rule \"$rule\" is unknown\n";
63             }
64             }
65              
66             return $self;
67             }
68              
69             sub process {
70             my ($self) = @_;
71              
72             while ( defined ( my $life = $self->board->next_life() ) ) {
73             $life->process($self->rules);
74             }
75              
76             return $self;
77             }
78              
79             sub set {
80             my ($self) = @_;
81              
82             while ( defined ( my $life = $self->board->next_life() ) ) {
83             $life->set();
84             }
85              
86             return $self;
87             }
88              
89             sub to_string {
90             my ($self) = @_;
91              
92             return $self->board->to_string();
93             }
94              
95             1;
96              
97             __END__
98              
99             =head1 NAME
100              
101             Game::Life::NDim - Infrastructure for playing Conway's game of life with support for multiple cell types and 2D or 3D boards.
102              
103             =head1 VERSION
104              
105             This documentation refers to Game::Life::NDim version 0.0.2.
106              
107             =head1 SYNOPSIS
108              
109             use Game::Life::NDim;
110              
111             # Brief but working code example(s) here showing the most common usage(s)
112             # This section will be as far as many users bother reading, so make it as
113             # educational and exemplary as possible.
114              
115              
116             =head1 DESCRIPTION
117              
118             TODO
119              
120             =head1 SUBROUTINES/METHODS
121              
122             =head2 Exportable Functions
123              
124             =head2 C<game_of_life ( %params )>
125              
126             =head2 Class Methods
127              
128             =head3 C<new ( %params )>
129              
130             Param: C<dims> - array of ints - The dimensions of the game (in zero based form ie [1,1] for a 2x2 board
131              
132             Param: C<rand> - bool - If true sets the board with random life types
133              
134             Param: C<types> - hash ref - List of types (keys) and their relative likely hood to be found default {0=> ,1=> }
135              
136             =head2 Object Methods
137              
138             =head3 C<add_rule ( )>
139              
140             =head3 C<process ()>
141              
142             =head3 C<set ()>
143              
144             =head3 C<to_string ()>
145              
146             =head1 DIAGNOSTICS
147              
148             =head1 CONFIGURATION AND ENVIRONMENT
149              
150             =head1 DEPENDENCIES
151              
152             =head1 INCOMPATIBILITIES
153              
154             =head1 BUGS AND LIMITATIONS
155              
156             There are no known bugs in this module.
157              
158             Please report problems to Ivan Wills (ivan.wills@gmail.com).
159              
160             Patches are welcome.
161              
162             =head1 AUTHOR
163              
164             Ivan Wills - (ivan.wills@gmail.com)
165              
166             =head1 LICENSE AND COPYRIGHT
167              
168             Copyright (c) 2010 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077).
169             All rights reserved.
170              
171             This module is free software; you can redistribute it and/or modify it under
172             the same terms as Perl itself. See L<perlartistic>. This program is
173             distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
174             without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
175             PARTICULAR PURPOSE.
176              
177             =cut