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

Revision 6878, 3.5 kB (checked in by junichiro, 5 years ago)

lang/perl/Games-Go: 盤上に石を打てるようにした

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));
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    };
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
47sub 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
63sub 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
87sub 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
97sub can_hit {
98    my ( $self, $points, $symbol ) = @_;
99    return 0 unless ($self->_is_space($points));
100    return 1;
101}
102
103sub _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
111sub pass {
112    my ($self) = @_;
113    $self->{'teban'} = $self->other_side( $self->{'teban'} );
114}
115
116sub other_side {
117    my ( $self, $symbol ) = @_;
118    return ( $symbol eq $BLACK ) ? $WHITE : $BLACK;
119}
120
1211;
122__END__
123
124=head1 NAME
125
126Games::Go::Play - Play 'go' on Perl.
127
128
129=head1 VERSION
130
131This 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
145This module make you to be able to play 'go' on perl.
146
147#       
148
149=head2 Methods
150=over
151=item new
152new Constructor of play field(board).
153=item init
154Initialize play field(board).
155=item hit
156Do one action(itte utu).
157=item pass
158Pass.
159=item resign
160Resign the game.
161=item can_hit
162Check to be able to hit the point.
163=item _is_space
164Check that the point is space.
165=item other_side
166return next player.
167=item show
168Show play field.
169=back
170
171=head1 AUTHOR
172
173Junichiro Tobe  C<< <junichiro.tobe@gmail.com> >>
174
175
176=head1 LICENCE AND COPYRIGHT
177
178Copyright (c) 2007, Junichiro Tobe C<< <junichiro.tobe@gmail.com> >>. All rights reserved.
179
180This module is free software; you can redistribute it and/or
181modify it under the same terms as Perl itself. See L<perlartistic>.
Note: See TracBrowser for help on using the browser.