| 1 | package Game::Battleship; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 2 | our $AUTHORITY = 'cpan:GENE'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | # ABSTRACT: "You sunk my battleship!" | ||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.0602'; | |||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Game::Battleship::Player; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | has players => ( | ||||||||||||||||||||||||||||||||||||||||||
| 14 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 15 | 0 | ▶ | isa => sub { croak 'Invalid players list' unless ref($_[0]) eq 'HASH' }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | ); | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 3 | ▶ | sub add_player { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_player: called CC=10 SCAR=28.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 3 | my ($self, $player, $i) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | # If we are not given a number to use... | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 3 | ▶ | unless ($i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | # ..find the least whole number that is not already used. | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | my @nums = sort { $a <=> $b } | |||||||||||||||||||||||||||||||||||||||||
| 26 | 3 | grep { s/^player_(\d+)$/$1/ } | |||||||||||||||||||||||||||||||||||||||||
| 27 | 3 | keys %{ $self->{players} }; | |||||||||||||||||||||||||||||||||||||||||
| 28 | 3 | my $n = 1; | |||||||||||||||||||||||||||||||||||||||||
| 29 | 3 | for (@nums) { | |||||||||||||||||||||||||||||||||||||||||
| 30 | 3 | ▶ | last if $n > $_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 3 | $n++; | |||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 3 | $i = $n; | |||||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | # Make the name to use for our object. | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 3 | my $who = "player_$i"; | |||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | # Return undef if we are trying to add an existing player. | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 3 | ▶ | if ( exists $self->{players}{$who} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | warn "A player number $i already exists\n"; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | # Set the player name unless we already have a player. | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 3 | ▶ | $player = $who unless $player; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | # We are given a player object. | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 3 | ▶ | if (ref ($player) eq 'Game::Battleship::Player') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | $self->{players}{$who} = $player; | |||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | # We are given the guts of a player. | ||||||||||||||||||||||||||||||||||||||||||
| 53 | elsif (ref ($player) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||
| 54 | $self->{players}{$who} = Game::Battleship::Player->new( | ||||||||||||||||||||||||||||||||||||||||||
| 55 | id => $player->{id} || $i, | ||||||||||||||||||||||||||||||||||||||||||
| 56 | name => $player->{name} || $who, | ||||||||||||||||||||||||||||||||||||||||||
| 57 | fleet => $player->{fleet}, | ||||||||||||||||||||||||||||||||||||||||||
| 58 | dimensions => $player->{dimensions}, | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
$player->{'id'} || $i
$player->{'name'} || $who
MC/DC0%0/2
$player->{'id'} || $i
$player->{'id'}
$i
MC/DC0%0/2
$player->{'name'} || $who
$player->{'name'}
$who
Truth table0%0/3
A || B
A= $player->{'id'}B= $i
Truth table0%0/3
A || B
A= $player->{'name'}B= $who
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | # We are just given a name. | ||||||||||||||||||||||||||||||||||||||||||
| 62 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 3 | $self->{players}{$who} = Game::Battleship::Player->new( | |||||||||||||||||||||||||||||||||||||||||
| 64 | id => $i, | ||||||||||||||||||||||||||||||||||||||||||
| 65 | name => $player, | ||||||||||||||||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | # Hand the player object back. | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 3 | return $self->{players}{$who}; | |||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 10 | ▶ | sub player { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
player: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 10 | my ($self, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 10 | my $player; | |||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | # Step through each player... | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 10 | for (keys %{ $self->{players} }) { | |||||||||||||||||||||||||||||||||||||||||
| 80 | # Are we looking at the same player by name, key or id? | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 20 | ▶ | if( $_ eq $name || | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$_ eq $name or $self->{"players"}{$_}->name eq $name or $self->{"players"}{$_}->id eq $name
$_ eq $name or $self->{"players"}{$_}->name eq $name
MC/DC100%3/3
$_ eq $name or $self->{"players"}{$_}->name eq $name or $self->{"players"}{$_}->id eq $name
$_ eq $name
$self->{"players"}{$_}->name eq $name
$self->{"players"}{$_}->id eq $name
Truth table100%4/4
A or B or C
A= $_ eq $nameB= $self->{"players"}{$_}->name eq $nameC= $self->{"players"}{$_}->id eq $name
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | $self->{players}{$_}->name eq $name || | ||||||||||||||||||||||||||||||||||||||||||
| 83 | $self->{players}{$_}->id eq $name | ||||||||||||||||||||||||||||||||||||||||||
| 84 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 85 | # Set the player object to return. | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 9 | $player = $self->{players}{$_}; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 9 | last; | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 10 | ▶ | warn "No such player '$name'\n" unless $player; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 10 | return $player; | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | sub play { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
play: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | my $winner = 0; | |||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | while (not $winner) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | # Take a turn per live player. | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | for my $player (values %{ $self->{players} }) { | |||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | next unless $player->life; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | # Strike each opponent. | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | for my $opponent (values %{ $self->{players} }) { | |||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | next if $opponent->name eq $player->name || | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$opponent->name eq $player->name or not $opponent->life
MC/DC0%0/2
$opponent->name eq $player->name or not $opponent->life
$opponent->name eq $player->name
not $opponent->life
Truth table0%0/3
A or not(B)
A= $opponent->name eq $player->nameB= not $opponent->life
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | !$opponent->life; | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | my $res = -1; # "duplicate strike" flag. | |||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | while ($res == -1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | $res = $player->strike( | |||||||||||||||||||||||||||||||||||||||||
| 113 | $opponent, | ||||||||||||||||||||||||||||||||||||||||||
| 114 | $self->_get_coordinate($opponent) | ||||||||||||||||||||||||||||||||||||||||||
| 115 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | # Do we have a winner? | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | my @alive = grep { $self->{players}{$_}->life } keys %{ $self->{players} }; | |||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | $winner = @alive == 1 ? shift @alive : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | #warn $winner->name ." is the winner!\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | return $winner; | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | # Return a coordinate from a player's grid. | ||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | sub _get_coordinate { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_coordinate: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | my ($self, $player) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | my ($x, $y); | |||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | # Return random coordinates... | ||||||||||||||||||||||||||||||||||||||||||
| 136 | ($x, $y) = ( | ||||||||||||||||||||||||||||||||||||||||||
| 137 | int 1 + rand $player->{grid}->{dimension}[0], | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | int 1 + rand $player->{grid}->{dimension}[1] | |||||||||||||||||||||||||||||||||||||||||
| 139 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | # warn "$x, $y\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | return $x, $y; | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | __END__ | ||||||||||||||||||||||||||||||||||||||||||