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

Revision 3798, 2.7 kB (checked in by junichiro, 6 years ago)

lang/perl/Games-Go: Change method name. 'display' -> 'show'

Line 
1package Games::Go::Play;
2
3use warnings;
4use strict;
5use Carp;
6use YAML::Syck;
7use Data::Dumper;
8# use Games::Go::Coordinate;
9use version; our $VERSION = qv('0.0.1');
10
11use base qw/Class::Accessor::Fast/;
12__PACKAGE__->mk_accessors(qw(space black white out));
13
14our $B_SIZE = 19;
15our $SPACE  = '0';
16our $BLACK  = '1';
17our $WHITE  = '2';
18our $OUT    = '9';
19
20sub new {
21    my ( $class, $size ) = shift;
22    $size ||= $B_SIZE;
23    my $self = {
24        'size'    => $size,
25        'board'   => [],
26        'lastman' => $WHITE,
27    };
28
29    # my $c1 = Games::Go::Coordinate->new( x => 4, y => 3 );
30    # my $c2 = Games::Go::Coordinate->new( x => 4, y => 10 );
31    # print $c1;
32    # print $c2;
33    bless $self, $class;
34    $self->{'board'} = $self->init();
35    return $self;
36}
37
38sub init {
39    my @board;
40    for ( my $i = 0 ; $i <= $B_SIZE ; $i++ ) {
41        $board[0][$i] = $OUT;
42        $board[$i][0] = $OUT;
43    }
44    for ( my $i = 1 ; $i <= $B_SIZE ; $i++ ) {
45        for ( my $j = 1 ; $j <= $B_SIZE ; $j++ ) {
46            $board[$i][$j] = $SPACE;
47        }
48    }
49    return \@board;
50}
51
52sub show {
53    my ($self) = @_;
54    my $symbol = {
55        $BLACK => ' *',
56        $WHITE => ' o',
57        $SPACE => ' +',
58        $OUT   => '  ',
59    };
60    print "    ";
61    for ( my $x = 1 ; $x <= $B_SIZE ; $x++ ) {
62        printf( "% 2s", $x );
63    }
64    print "\n";
65    my $i = 0;
66    foreach my $x ( @{ $self->{'board'} } ) {
67        printf( "% 2s", $i ) if ($i);
68        foreach ( @{$x} ) {
69            print $symbol->{$_};
70        }
71        print "\n";
72        $i++;
73    }
74}
75
76sub hit {
77    my ( $self, $points, $symbol ) = @_;
78    $symbol ||= $self->other_side( $self->{'lastman'});
79    $self->{'board'}[ $points->[1] ][ $points->[0] ] = $symbol;
80    $self->{'lastman'} = $symbol;
81}
82
83sub other_side {
84    my ($self, $symbol) = @_;
85    return ( $symbol eq $BLACK ) ? $WHITE : $BLACK;
86}
87
881;
89__END__
90
91=head1 NAME
92
93Games::Go::Play - Play 'go' on Perl.
94
95
96=head1 VERSION
97
98This document describes Games::Go::Play version 0.0.1
99
100
101=head1 SYNOPSIS
102
103    use Games::Go::Play;
104    my $go = Games::Go::Play->new('19');
105    $go->hit([2,3]);
106    $go->hit([3,3]);
107    $go->hit([3,4]);
108    $go->show;
109
110=head1 DESCRIPTION
111
112This module make you to be able to play 'go' on perl.
113
114#       
115
116=head2 Methods
117=over
118=item new
119new Constructor of play field(board).
120=item init
121Initialize play field(board).
122=item hit
123Do one action(itte utu).
124=item other_side
125return next player.
126=item show
127Show play field.
128=back
129
130=head1 AUTHOR
131
132Junichiro Tobe  C<< <junichiro.tobe@gmail.com> >>
133
134
135=head1 LICENCE AND COPYRIGHT
136
137Copyright (c) 2007, Junichiro Tobe C<< <junichiro.tobe@gmail.com> >>. All rights reserved.
138
139This module is free software; you can redistribute it and/or
140modify it under the same terms as Perl itself. See L<perlartistic>.
Note: See TracBrowser for help on using the browser.