From 3b8ec6158ad0a96d4a610272cf8910df5394c9fd Mon Sep 17 00:00:00 2001 From: realraum Date: Thu, 7 Jul 2011 22:06:04 +0000 Subject: [PATCH] version with colour --- dart/dart-schiffe.pl | 120 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 91 insertions(+), 29 deletions(-) diff --git a/dart/dart-schiffe.pl b/dart/dart-schiffe.pl index 9b72464..537a7a9 100755 --- a/dart/dart-schiffe.pl +++ b/dart/dart-schiffe.pl @@ -1,7 +1,7 @@ #!/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); @@ -23,11 +23,12 @@ exit $dart->run(); 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; } @@ -41,36 +42,38 @@ sub init { $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; @@ -82,9 +85,9 @@ sub init 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'; } @@ -98,30 +101,62 @@ sub shoot 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); } @@ -129,24 +164,51 @@ sub shoot 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"; } -- 1.7.10.4