Games::Risk::Country - map country


Games-Risk documentation Contained in the Games-Risk distribution.

Index


Code Index:

NAME

Top

Games::Risk::Country - map country

VERSION

Top

version 3.103040

SYNOPSIS

Top

    my $country = Games::Risk::Country->new(\%params);

DESCRIPTION

Top

This module implements a map country, with all its characteristics.

METHODS

Top

Constructor

* my $country = Games::Risk::Country->new( \%params )

Create a new country. Mandatory params are name, continent, greyval, x and y (see below in Accessors section for a quick definition of those params). Other attributes are optional, but can be supplied anyway.

Accessors

The following accessors (acting as mutators, ie getters and setters) are available for Games::Risk::Country objects:

* armies()

number of armies currently in the country.

* continent()

a Games::Risk::Continent object in which the country is located.

* greyval()

an integer between 1 and 254 corresponding at the grey (all RGB values set to greyval()) used to draw the country on the grey-scale map.

* id()

alias for greyval().

* name()

country name.

* owner()

a Games::Risk::Player object currently owning the country.

* coordx()

the x location of the country capital.

* coordy()

the y location of the country capital.

Methods

* $country->chown( $player )

Change the owner of the $country to be $player. This implies updating cross-reference for previous owner and new one.

* $country->destroy()

Remove all circular references of $country, to prevent memory leaks.

* my $bool = $country->is_neighbour( $c )

Return true if $country is a neighbour of country $c, false otherwise.

* my @neighbours = $country->neighbours()

Return the list of $country's neighbours.

* $country->neighbour_add( $c )

Add $c to the list of $country's neighbours. This is not reciprocical.

SEE ALSO

Top

Games::Risk.

AUTHOR

Top

  Jerome Quelin

COPYRIGHT AND LICENSE

Top


Games-Risk documentation Contained in the Games-Risk distribution.

#
# This file is part of Games-Risk
#
# This software is Copyright (c) 2008 by Jerome Quelin.
#
# This is free software, licensed under:
#
#   The GNU General Public License, Version 3, June 2007
#
use 5.010;
use strict;
use warnings;

package Games::Risk::Country;
BEGIN {
  $Games::Risk::Country::VERSION = '3.103040';
}
# ABSTRACT: map country

use List::MoreUtils qw{ any };

use base qw{ Class::Accessor::Fast };
__PACKAGE__->mk_accessors( qw{ armies continent greyval name owner coordx coordy
    _neighbours } );


#--
# METHODS

# -- public methods

#
# $country->chown( $player );
#
# Change the owner of the $country to be $player. This implies updating
# cross-reference for previous owner and new one.
#
sub chown {
    my ($self, $player) = @_;

    # remove old owner
    my $previous = $self->owner;
    $previous->country_del($self) if defined $previous;

    # store new owner
    $self->owner($player);
    $player->country_add($self);
}


#
# $country->destroy;
#
# Remove all circular references of $country, to prevent memory leaks.
#
#sub DESTROY { say "destroy: $_[0]"; }
sub destroy {
    my ($self) = @_;
    $self->continent(undef);
    $self->owner(undef);
    $self->_neighbours([]);
}


#
# my $id = $country->id;
#
# For all intents & purposes, id is an alias to greyval
#
*id = \&greyval;


#
# my $bool = $country->is_neighbour($c);
#
# Return true if $country is a neighbour of country $c, false
# otherwise.
#
sub is_neighbour {
    my ($self, $c) = @_;
    return any { $_ eq $c } $self->neighbours;
}


#
# $country->neighbour_add( $c );
#
# Add $c to the list of $country's neighbours. This is not reciprocical.
#
sub neighbour_add {
    my ($self, $c) = @_;
    my @neighbours = $self->neighbours;
    push @neighbours, $c;
    $self->_neighbours( \@neighbours );
}


#
# my @neighbours = $country->neighbours;
#
# Return the list of the country's neighbours.
#
sub neighbours {
    my ($self) = @_;
    my $neighbours = $self->_neighbours // []; #//padre
    return @$neighbours;
}


1;




__END__