root/lang/perl/Games-Go/trunk/lib/Games/Go/Play.pm @ 16405

Revision 16405, 4.0 kB (checked in by junichiro, 6 years ago)

デバッグライトをコメントアウトごと除去(それと、git の練習)

Line 
1package Games::Go::Play;
2
3use warnings;
4use strict;
5use Carp;
6use YAML::Syck;
7use Data::Dumper;
8
9# use Games::Go::Coordinate;
10use version; our $VERSION = qv('0.0.1');
11
12use base qw/Class::Accessor::Fast/;
13__PACKAGE__->mk_accessors(qw(space black white out pass));
14
15our $B_SIZE = 19;
16our $SPACE  = '0';
17our $BLACK  = '1';
18our $WHITE  = '2';
19our $OUT    = '9';
20
21sub 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
48sub 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
64sub 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
89sub 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
100sub 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
107sub _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
115sub _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
123sub 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
133sub other_side {
134    my ( $self, $symbol ) = @_;
135    return ( $symbol eq $BLACK ) ? $WHITE : $BLACK;
136}
137
138sub end {
139    my ($self) = @_;
140    print "[TODO] end.\n";
141}
142
1431;
144__END__
145
146=head1 NAME
147
148Games::Go::Play - Play 'go' on Perl.
149
150
151=head1 VERSION
152
153This 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
167This module make you to be able to play 'go' on perl.
168
169#       
170
171=head2 Methods
172=over
173=item new
174new Constructor of play field(board).
175=item init
176Initialize play field(board).
177=item hit
178Do one action(itte utu).
179=item pass
180Pass.
181=item resign
182Resign the game.
183=item can_hit
184Check to be able to hit the point.
185=item other_side
186return next player.
187=item show
188Show play field.
189=item end
190End the game.
191=back
192
193=head1 AUTHOR
194
195Junichiro Tobe  C<< <junichiro.tobe@gmail.com> >>
196
197
198=head1 LICENCE AND COPYRIGHT
199
200Copyright (c) 2007, Junichiro Tobe C<< <junichiro.tobe@gmail.com> >>. All rights reserved.
201
202This module is free software; you can redistribute it and/or
203modify it under the same terms as Perl itself. See L<perlartistic>.
Note: See TracBrowser for help on using the browser.