| 1 | package Games::Go::Play; |
|---|
| 2 | |
|---|
| 3 | use warnings; |
|---|
| 4 | use strict; |
|---|
| 5 | use Carp; |
|---|
| 6 | use YAML::Syck; |
|---|
| 7 | use Data::Dumper; |
|---|
| 8 | |
|---|
| 9 | # use Games::Go::Coordinate; |
|---|
| 10 | use version; our $VERSION = qv('0.0.1'); |
|---|
| 11 | |
|---|
| 12 | use base qw/Class::Accessor::Fast/; |
|---|
| 13 | __PACKAGE__->mk_accessors(qw(space black white out pass)); |
|---|
| 14 | |
|---|
| 15 | our $B_SIZE = 19; |
|---|
| 16 | our $SPACE = '0'; |
|---|
| 17 | our $BLACK = '1'; |
|---|
| 18 | our $WHITE = '2'; |
|---|
| 19 | our $OUT = '9'; |
|---|
| 20 | |
|---|
| 21 | sub new { |
|---|
| 22 | my ( $class, $size ) = @_; |
|---|
| 23 | $size ||= $B_SIZE; |
|---|
| 24 | my $self = { |
|---|
| 25 | 'size' => $size, |
|---|
| 26 | 'board' => [], |
|---|
| 27 | 'teban' => $BLACK, |
|---|
| 28 | 'black' => { |
|---|
| 29 | 'agehama' => 0, |
|---|
| 30 | 'ji' => 0, |
|---|
| 31 | }, |
|---|
| 32 | 'white' => { |
|---|
| 33 | 'agehama' => 0, |
|---|
| 34 | 'ji' => 0, |
|---|
| 35 | }, |
|---|
| 36 | 'pass' => 0, |
|---|
| 37 | }; |
|---|
| 38 | |
|---|
| 39 | # my $c1 = Games::Go::Coordinate->new( x => 4, y => 3 ); |
|---|
| 40 | # my $c2 = Games::Go::Coordinate->new( x => 4, y => 10 ); |
|---|
| 41 | # print $c1; |
|---|
| 42 | # print $c2; |
|---|
| 43 | bless $self, $class; |
|---|
| 44 | $self->{'board'} = $self->init(); |
|---|
| 45 | return $self; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | sub init { |
|---|
| 49 | my ($self) = @_; |
|---|
| 50 | my @board; |
|---|
| 51 | for ( my $i = 0 ; $i <= $self->{'size'} ; $i++ ) { |
|---|
| 52 | $board[0][$i] = $OUT; |
|---|
| 53 | $board[$i][0] = $OUT; |
|---|
| 54 | } |
|---|
| 55 | for ( my $i = 1 ; $i <= $self->{'size'} ; $i++ ) { |
|---|
| 56 | for ( my $j = 1 ; $j <= $self->{'size'} ; $j++ ) { |
|---|
| 57 | $board[$i][$j] = $SPACE; |
|---|
| 58 | } |
|---|
| 59 | } |
|---|
| 60 | $self->{'board'} = \@board; |
|---|
| 61 | return \@board; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | sub show { |
|---|
| 65 | my ($self) = @_; |
|---|
| 66 | my $board = " "; |
|---|
| 67 | my $symbol = { |
|---|
| 68 | $BLACK => ' *', |
|---|
| 69 | $WHITE => ' o', |
|---|
| 70 | $SPACE => ' +', |
|---|
| 71 | $OUT => ' ', |
|---|
| 72 | }; |
|---|
| 73 | for ( my $x = 1 ; $x <= $self->{'size'} ; $x++ ) { |
|---|
| 74 | $board .= sprintf( "% 2s", $x ); |
|---|
| 75 | } |
|---|
| 76 | $board .= "\n"; |
|---|
| 77 | my $i = 0; |
|---|
| 78 | foreach my $x ( @{ $self->{'board'} } ) { |
|---|
| 79 | $board .= sprintf( "% 2s", $i ) if ($i); |
|---|
| 80 | foreach ( @{$x} ) { |
|---|
| 81 | $board .= $symbol->{$_}; |
|---|
| 82 | } |
|---|
| 83 | $board .= "\n"; |
|---|
| 84 | $i++; |
|---|
| 85 | } |
|---|
| 86 | return $board; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | sub hit { |
|---|
| 90 | my ( $self, $points, $symbol ) = @_; |
|---|
| 91 | $symbol ||= $self->{'teban'}; |
|---|
| 92 | unless ( $self->can_hit( $points, $symbol ) ) { |
|---|
| 93 | return; |
|---|
| 94 | } |
|---|
| 95 | $self->{'pass'} = 0; |
|---|
| 96 | $self->{'board'}[ $points->[1] ][ $points->[0] ] = $symbol; |
|---|
| 97 | $self->{'teban'} = $self->other_side( $self->{'teban'} ); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | sub can_hit { |
|---|
| 101 | my ( $self, $points, $symbol ) = @_; |
|---|
| 102 | return 0 unless ( $self->_is_in_board($points) ); |
|---|
| 103 | return 0 unless ( $self->_is_space($points) ); |
|---|
| 104 | return 1; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | sub _is_space { |
|---|
| 108 | my ( $self, $points ) = @_; |
|---|
| 109 | if ( $self->{'board'}[ $points->[1] ][ $points->[0] ] eq $SPACE ) { |
|---|
| 110 | return 1; |
|---|
| 111 | } |
|---|
| 112 | return 0; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | sub _is_in_board { |
|---|
| 116 | my ( $self, $points ) = @_; |
|---|
| 117 | if ( $points->[0] <= $self->{'size'} && $points->[1] <= $self->{'size'} ) { |
|---|
| 118 | return 1; |
|---|
| 119 | } |
|---|
| 120 | return 0; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | sub pass { |
|---|
| 124 | my ($self) = @_; |
|---|
| 125 | $self->{'pass'}++; |
|---|
| 126 | if ( $self->{'pass'} >= '2' ) { |
|---|
| 127 | $self->end; |
|---|
| 128 | return; |
|---|
| 129 | } |
|---|
| 130 | $self->{'teban'} = $self->other_side( $self->{'teban'} ); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | sub other_side { |
|---|
| 134 | my ( $self, $symbol ) = @_; |
|---|
| 135 | return ( $symbol eq $BLACK ) ? $WHITE : $BLACK; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | sub end { |
|---|
| 139 | my ($self) = @_; |
|---|
| 140 | print "[TODO] end.\n"; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | 1; |
|---|
| 144 | __END__ |
|---|
| 145 | |
|---|
| 146 | =head1 NAME |
|---|
| 147 | |
|---|
| 148 | Games::Go::Play - Play 'go' on Perl. |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | =head1 VERSION |
|---|
| 152 | |
|---|
| 153 | This document describes Games::Go::Play version 0.0.1 |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | =head1 SYNOPSIS |
|---|
| 157 | |
|---|
| 158 | use Games::Go::Play; |
|---|
| 159 | my $go = Games::Go::Play->new('19'); |
|---|
| 160 | $go->hit([2,3]); |
|---|
| 161 | $go->hit([3,3]); |
|---|
| 162 | $go->hit([3,4]); |
|---|
| 163 | $go->show; |
|---|
| 164 | |
|---|
| 165 | =head1 DESCRIPTION |
|---|
| 166 | |
|---|
| 167 | This module make you to be able to play 'go' on perl. |
|---|
| 168 | |
|---|
| 169 | # |
|---|
| 170 | |
|---|
| 171 | =head2 Methods |
|---|
| 172 | =over |
|---|
| 173 | =item new |
|---|
| 174 | new Constructor of play field(board). |
|---|
| 175 | =item init |
|---|
| 176 | Initialize play field(board). |
|---|
| 177 | =item hit |
|---|
| 178 | Do one action(itte utu). |
|---|
| 179 | =item pass |
|---|
| 180 | Pass. |
|---|
| 181 | =item resign |
|---|
| 182 | Resign the game. |
|---|
| 183 | =item can_hit |
|---|
| 184 | Check to be able to hit the point. |
|---|
| 185 | =item other_side |
|---|
| 186 | return next player. |
|---|
| 187 | =item show |
|---|
| 188 | Show play field. |
|---|
| 189 | =item end |
|---|
| 190 | End the game. |
|---|
| 191 | =back |
|---|
| 192 | |
|---|
| 193 | =head1 AUTHOR |
|---|
| 194 | |
|---|
| 195 | Junichiro Tobe C<< <junichiro.tobe@gmail.com> >> |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | =head1 LICENCE AND COPYRIGHT |
|---|
| 199 | |
|---|
| 200 | Copyright (c) 2007, Junichiro Tobe C<< <junichiro.tobe@gmail.com> >>. All rights reserved. |
|---|
| 201 | |
|---|
| 202 | This module is free software; you can redistribute it and/or |
|---|
| 203 | modify it under the same terms as Perl itself. See L<perlartistic>. |
|---|