| 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)); |
|---|
| 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 | }; |
|---|
| 37 | |
|---|
| 38 | # my $c1 = Games::Go::Coordinate->new( x => 4, y => 3 ); |
|---|
| 39 | # my $c2 = Games::Go::Coordinate->new( x => 4, y => 10 ); |
|---|
| 40 | # print $c1; |
|---|
| 41 | # print $c2; |
|---|
| 42 | bless $self, $class; |
|---|
| 43 | $self->{'board'} = $self->init(); |
|---|
| 44 | return $self; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | sub init { |
|---|
| 48 | my ($self) = @_; |
|---|
| 49 | my @board; |
|---|
| 50 | for ( my $i = 0 ; $i <= $B_SIZE ; $i++ ) { |
|---|
| 51 | $board[0][$i] = $OUT; |
|---|
| 52 | $board[$i][0] = $OUT; |
|---|
| 53 | } |
|---|
| 54 | for ( my $i = 1 ; $i <= $B_SIZE ; $i++ ) { |
|---|
| 55 | for ( my $j = 1 ; $j <= $B_SIZE ; $j++ ) { |
|---|
| 56 | $board[$i][$j] = $SPACE; |
|---|
| 57 | } |
|---|
| 58 | } |
|---|
| 59 | $self->{'board'} = \@board; |
|---|
| 60 | return \@board; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | sub show { |
|---|
| 64 | my ($self) = @_; |
|---|
| 65 | my $symbol = { |
|---|
| 66 | $BLACK => ' *', |
|---|
| 67 | $WHITE => ' o', |
|---|
| 68 | $SPACE => ' +', |
|---|
| 69 | $OUT => ' ', |
|---|
| 70 | }; |
|---|
| 71 | print " "; |
|---|
| 72 | for ( my $x = 1 ; $x <= $B_SIZE ; $x++ ) { |
|---|
| 73 | printf( "% 2s", $x ); |
|---|
| 74 | } |
|---|
| 75 | print "\n"; |
|---|
| 76 | my $i = 0; |
|---|
| 77 | foreach my $x ( @{ $self->{'board'} } ) { |
|---|
| 78 | printf( "% 2s", $i ) if ($i); |
|---|
| 79 | foreach ( @{$x} ) { |
|---|
| 80 | print $symbol->{$_}; |
|---|
| 81 | } |
|---|
| 82 | print "\n"; |
|---|
| 83 | $i++; |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | sub hit { |
|---|
| 88 | my ( $self, $points, $symbol ) = @_; |
|---|
| 89 | $symbol ||= $self->{'teban'}; |
|---|
| 90 | unless ($self->can_hit($points, $symbol)) { |
|---|
| 91 | return; |
|---|
| 92 | } |
|---|
| 93 | $self->{'board'}[ $points->[1] ][ $points->[0] ] = $symbol; |
|---|
| 94 | $self->{'teban'} = $self->other_side( $self->{'teban'} ); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | sub can_hit { |
|---|
| 98 | my ( $self, $points, $symbol ) = @_; |
|---|
| 99 | return 0 unless ($self->_is_space($points)); |
|---|
| 100 | return 1; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | sub _is_space{ |
|---|
| 104 | my ( $self, $points ) = @_; |
|---|
| 105 | if ( $self->{'board'}[ $points->[1] ][ $points->[0] ] eq $SPACE ){ |
|---|
| 106 | return 1; |
|---|
| 107 | } |
|---|
| 108 | return 0; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | sub pass { |
|---|
| 112 | my ($self) = @_; |
|---|
| 113 | $self->{'teban'} = $self->other_side( $self->{'teban'} ); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | sub other_side { |
|---|
| 117 | my ( $self, $symbol ) = @_; |
|---|
| 118 | return ( $symbol eq $BLACK ) ? $WHITE : $BLACK; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | 1; |
|---|
| 122 | __END__ |
|---|
| 123 | |
|---|
| 124 | =head1 NAME |
|---|
| 125 | |
|---|
| 126 | Games::Go::Play - Play 'go' on Perl. |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | =head1 VERSION |
|---|
| 130 | |
|---|
| 131 | This document describes Games::Go::Play version 0.0.1 |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | =head1 SYNOPSIS |
|---|
| 135 | |
|---|
| 136 | use Games::Go::Play; |
|---|
| 137 | my $go = Games::Go::Play->new('19'); |
|---|
| 138 | $go->hit([2,3]); |
|---|
| 139 | $go->hit([3,3]); |
|---|
| 140 | $go->hit([3,4]); |
|---|
| 141 | $go->show; |
|---|
| 142 | |
|---|
| 143 | =head1 DESCRIPTION |
|---|
| 144 | |
|---|
| 145 | This module make you to be able to play 'go' on perl. |
|---|
| 146 | |
|---|
| 147 | # |
|---|
| 148 | |
|---|
| 149 | =head2 Methods |
|---|
| 150 | =over |
|---|
| 151 | =item new |
|---|
| 152 | new Constructor of play field(board). |
|---|
| 153 | =item init |
|---|
| 154 | Initialize play field(board). |
|---|
| 155 | =item hit |
|---|
| 156 | Do one action(itte utu). |
|---|
| 157 | =item pass |
|---|
| 158 | Pass. |
|---|
| 159 | =item resign |
|---|
| 160 | Resign the game. |
|---|
| 161 | =item can_hit |
|---|
| 162 | Check to be able to hit the point. |
|---|
| 163 | =item _is_space |
|---|
| 164 | Check that the point is space. |
|---|
| 165 | =item other_side |
|---|
| 166 | return next player. |
|---|
| 167 | =item show |
|---|
| 168 | Show play field. |
|---|
| 169 | =back |
|---|
| 170 | |
|---|
| 171 | =head1 AUTHOR |
|---|
| 172 | |
|---|
| 173 | Junichiro Tobe C<< <junichiro.tobe@gmail.com> >> |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | =head1 LICENCE AND COPYRIGHT |
|---|
| 177 | |
|---|
| 178 | Copyright (c) 2007, Junichiro Tobe C<< <junichiro.tobe@gmail.com> >>. All rights reserved. |
|---|
| 179 | |
|---|
| 180 | This module is free software; you can redistribute it and/or |
|---|
| 181 | modify it under the same terms as Perl itself. See L<perlartistic>. |
|---|