#!/usr/bin/perl
use strict;
use Dart;
-
+use Term::ANSIColor;
our @x= qw/ 20 1 18 4 13 6 10 15 2 17 /;
our @y = qw/ 3 19 7 16 8 11 14 9 12 5 /;
our %x = array_to_hash(@x);
sub array_to_hash
{
- my @array;
+ my @array=@_;
my %hash;
+ my $i=1;
for my $key (@array)
{
- $hash{$key}=1;
+ $hash{$key}=$i++;
}
return %hash;
}
{
$self->get_player($player_idx)->{sel_x}=0;
$self->get_player($player_idx)->{sel_y}=0;
- for my $x (@x)
+ $self->get_player($player_idx)->{mult_x}=0;
+ $self->get_player($player_idx)->{mult_y}=0;
+ for my $x (@main::x)
{
- for my $y (@y)
+ for my $y (@main::y)
{
$self->get_player($player_idx)->{score}->{$x}{$y}=0;
}
}
- for my $schiff (@schiffe)
+ for my $schiff (@main::schiffe)
{
my $valid=0;
while(not $valid)
{
- my $x_start_idx = int(rand(scalar @x));
- my $y_start_idx = int(rand(scalar @y));
+ my $x_start_idx = int(rand(scalar @main::x));
+ my $y_start_idx = int(rand(scalar @main::y));
my $direction = int(rand(2));
my $x_stop_idx = $x_start_idx + $schiff * $direction;
my $y_stop_idx = $y_start_idx + $schiff * (1-$direction);
- next if ($x_stop_idx > $#x) or ($y_stop_idx > $#y);
+ next if ($x_stop_idx > $#main::x) or ($y_stop_idx > $#main::y);
for my $x_idx ($x_start_idx..$x_stop_idx)
{
for my $y_idx ($y_start_idx .. $y_stop_idx)
{
- next if $self->get_player($player_idx)->{score}->{$x[$x_idx]}{$y[$y_idx]};
+ next if $self->get_player($player_idx)->{score}->{$main::x[$x_idx]}{$main::y[$y_idx]};
}
}
for my $x_idx ($x_start_idx..$x_stop_idx)
{
for my $y_idx ($y_start_idx .. $y_stop_idx)
{
- $self->get_player($player_idx)->{score}->{$x[$x_idx]}{$y[$y_idx]}="s";
+ $self->get_player($player_idx)->{score}->{$main::x[$x_idx]}{$main::y[$y_idx]}="s";
}
}
$valid=1;
sub win_condition
{
my ($self) = @_;
- for my $x (@x)
+ for my $x (@main::x)
{
- for my $y (@y)
+ for my $y (@main::y)
{
return if $self->get_current_player()->{score}->{$x}{$y} eq 's';
}
my ($mult,$zahl)=@_;
my $player=$self->get_current_player();
- if ( (not $x{$zahl}) and (not $y{$zahl}) )
+ if ( (not $main::x{$zahl}) and (not $main::y{$zahl}) )
{
$self->shout("miss");
return;
- } elsif ($x{$zahl}) {
+ } elsif ($main::x{$zahl}) {
$player->{sel_x}=$zahl;
- } elsif ($y{$zahl}) {
+ $player->{mult_x}=$mult;
+ } elsif ($main::y{$zahl}) {
$player->{sel_y}=$zahl;
+ $player->{mult_y}=$mult;
}
$self->shout_last_shoot();
if ($player->{sel_x} && $player->{sel_y})
{
- my $x = $player->{sel_x};
- my $y = $player->{sel_y};
+ my $x_middle = $player->{sel_x};
+ my $y_middle = $player->{sel_y};
+ my $mult_x = $player->{mult_x};
+ my $mult_y = $player->{mult_y};
$player->{sel_x}=0;
$player->{sel_y}=0;
- if ($player->{score}->{$x}{$y} eq 's')
+ $player->{mult_x}=0;
+ $player->{mult_y}=0;
+
+ my $start_x = $main::x{$x_middle}-1 - $mult_x+1;
+ my $stop_x = $main::x{$x_middle}-1 + $mult_x-1;
+ my $start_y = $main::y{$y_middle}-1 - $mult_y+1;
+ my $stop_y = $main::y{$y_middle}-1 + $mult_y-1;
+ my %sound;
+ for my $x_idx ($start_x..$stop_x)
+ {
+ my $x = $x_idx> $#main::x ? $main::x[$x_idx-@main::x] : $main::x[$x_idx];
+ for my $y_idx ($start_y..$stop_y)
+ {
+ my $y = $y_idx> $#main::y ? $main::y[$y_idx-@main::y] : $main::y[$y_idx];
+ if ($player->{score}->{$x}{$y} eq 's')
+ {
+ $sound{scored}++;
+ $player->{score}->{$x}{$y} = 'X';
+ } elsif (not $player->{score}->{$x}{$y}) {
+ $player->{score}->{$x}{$y} = 'o';
+ $sound{miss}++;
+ } elsif ($player->{score}->{$x}{$y} eq 'o') {
+ $sound{scho}++;
+ }
+ }
+ }
+
+ if ($sound{scored})
{
$self->shout("scored");
- $player->{score}->{$x}{$y} = 'X';
- } elsif (not $player->{score}->{$x}{$y}) {
- $player->{score}->{$x}{$y} = 'o';
+ } elsif ($sound{scho}) {
$self->shout("scho");
+ } elsif ($sound{miss}) {
+ $self->shout("miss");
}
+
}
$self->win() if &win_condition($self);
}
sub print_score
{
my ($self)=@_;
+ my $sel_x=$self->get_current_player()->{sel_x};
+ my $sel_y=$self->get_current_player()->{sel_y};
+ my $mult_x=$self->get_current_player()->{mult_x};
+ my $mult_y=$self->get_current_player()->{mult_y};
printf STDERR "Runde\t%d\n\n",$self->{round};
- printf STDERR "Player\t%s\n\n",$self->get_current_player()->{name};
- printf STDERR "x\t%d\n",$self->get_current_player()->{sel_x};
- printf STDERR "y\t%d\n",$self->get_current_player()->{sel_y};
+ printf STDERR "Player\t%s\t\tSchuss\t%d\n\n",$self->get_current_player()->{name},$self->{current_shoot_count};
+ printf STDERR "x: %dx%2d\n",$self->get_current_player()->{mult_x},$self->get_current_player()->{sel_x};
+ printf STDERR "y: %dx%2d\n",$self->get_current_player()->{mult_y},$self->get_current_player()->{sel_y};
+
+ print STDERR " ";
+ for my $y (@main::y)
+ {
+ print STDERR color('bold green') if $sel_y && (abs($main::y{$y} - $main::y{$sel_y}) < $mult_y );
+ printf STDERR " %2d",$y;
+ print STDERR color('reset') if $sel_y && (abs($main::y{$y} - $main::y{$sel_y}) < $mult_y );
- for my $x (@x)
+ }
+ print STDERR "\n";
+ for my $x (@main::x)
{
- for my $y (@y)
+ print STDERR color 'bold green' if $sel_x && (abs($main::x{$x} - $main::x{$sel_x}) < $mult_x );
+ printf STDERR "%2d",$x;
+ for my $y (@main::y)
{
+ print STDERR color('bold green') if $sel_y && (abs($main::y{$y} - $main::y{$sel_y}) < $mult_y );
my $field = $self->get_current_player()->{score}->{$x}{$y};
if ($field eq 'X' or $field eq 'o')
{
- print STDERR $field;
+ print STDERR " $field";
} else {
- print STDERR "?";
+ print STDERR " .";
}
+ print STDERR color('reset') if $sel_y && (abs($main::y{$y} - $main::y{$sel_y}) < $mult_y );
}
+ printf STDERR " %d",$x;
+ print STDERR color 'reset';
print STDERR "\n";
}
+ print STDERR " ";
+ for my $y (@main::y)
+ {
+ print STDERR color('bold green') if $sel_y && (abs($main::y{$y} - $main::y{$sel_y}) < $mult_y );
+ printf STDERR " %2d",$y;
+ print STDERR color('reset') if $sel_y && (abs($main::y{$y} - $main::y{$sel_y}) < $mult_y );
+
+ }
print STDERR "\n\n";
}