Games::Go::Coordinate - Represents a board coordinate in the game of Go


Games-Go-Coordinate documentation Contained in the Games-Go-Coordinate distribution.

Index


Code Index:

NAME

Top

Games::Go::Coordinate - Represents a board coordinate in the game of Go

VERSION

Top

version 1.100860

SYNOPSIS

Top

    use Games::Go::Coordinate;

    my $c1 = Games::Go::Coordinate->new(x => 4, y => 3);
    my $c2 = Games::Go::Coordinate->new(x => 4, y => 10);
    if ($c2 gt $c1) {
        # ...
    }

DESCRIPTION

Top

This class represents a board coordinate in the game of Go. Coordinate objects can be compared (as strings) to see whether two ranks are equal or whether one rank is higher than the other. Coordinate objects stringify to the SGF notation (for example, (4,10) stringifies to dj.

METHODS

Top

set_sgf_coordinate

    $coord->set_sgf_coordinate('cf');

Takes a coordinate in SGF notation and sets x() and y() from it.

new_from_sgf_coordinate

    my $cord = Games::Go::Coordinate->new_from_sgf_coordinate('cf');

Alternative constructor that accepts an SGF coordinate and sets x() and y() from it.

to_sgf

Returns the coordinate in SGF notation. This is also how the coordinate object stringifies.

as_list

Returns the coordinate in (x,y) notation. For example, it might return a string (16,17).

translate

    $coord->translate(2, -3);

Takes as arguments - in that order - a horizontal delta and a vertical delta and translates the coordinate by those deltas.

str_cmp

FIXME

stringify

FIXME

INSTALLATION

Top

See perlmodinstall for information and options on installing Perl modules.

BUGS AND LIMITATIONS

Top

No bugs have been reported.

Please report any bugs or feature requests through the web interface at http://rt.cpan.org/Public/Dist/Display.html?Name=Games-Go-Coordinate.

AVAILABILITY

Top

The latest version of this module is available from the Comprehensive Perl Archive Network (CPAN). Visit http://www.perl.com/CPAN/ to find a CPAN site near you, or see http://search.cpan.org/dist/Games-Go-Coordinate/.

The development version lives at http://github.com/hanekomu/Games-Go-Coordinate/. Instead of sending patches, please fork this project using the standard git and github infrastructure.

AUTHOR

Top

  Marcel Gruenauer <marcel@cpan.org>

COPYRIGHT AND LICENSE

Top


Games-Go-Coordinate documentation Contained in the Games-Go-Coordinate distribution.

use 5.008;
use strict;
use warnings;

package Games::Go::Coordinate;
our $VERSION = '1.100860';
# ABSTRACT: Represents a board coordinate in the game of Go
use Moose;
use overload
  '""'  => 'stringify',
  'cmp' => 'str_cmp';
has [qw/x y/] => (is => 'rw', isa => 'Int');

# accept something like 'ac' and set x=1, y=3
sub set_sgf_coordinate {
    my ($self, $coord) = @_;
    my ($x, $y) = map { ord($_) - 96 } split // => lc($coord);
    $self->x($x);
    $self->y($y);
}

sub new_from_sgf_coordinate {
    my ($class, $coord) = @_;
    my $self = $class->new;
    $self->set_sgf_coordinate($coord);
    $self;
}

sub to_sgf {
    my $self = shift;
    join '' => map { chr($_ + 96) } $self->x, $self->y;
}

sub as_list {
    my $self = shift;
    sprintf '(%d,%d)', $self->x, $self->y;
}

sub stringify {
    my $self = shift;
    $self->to_sgf;
}

sub str_cmp {
    my ($lhs, $rhs, $reversed) = @_;
    $_ = "$_" for $lhs, $rhs;
    ($lhs, $rhs) = ($rhs, $lhs) if $reversed;
    $lhs cmp $rhs;
}

sub translate {
    my ($self, $dx, $dy) = @_;
    $self->x($self->x + $dx);
    $self->y($self->y + $dy);
}
1;


__END__