X-Git-Url: https://git.realraum.at/?a=blobdiff_plain;f=dart%2Fdart-cricket.pl;h=c3199996f5344546de38917671377422ceba1eef;hb=ae63af1c2a2512719f2d31a9ac87518edab6ca4d;hp=de18e0ef1097f896df899809d8baf90fec2559f0;hpb=3311c2a78c87d6428b8aeca2b14e8d86f49505fd;p=svn42.git diff --git a/dart/dart-cricket.pl b/dart/dart-cricket.pl index de18e0e..c319999 100755 --- a/dart/dart-cricket.pl +++ b/dart/dart-cricket.pl @@ -12,6 +12,8 @@ my $dart = new Dart(player_names=>\@player, callbacks => { shoot=>\&shoot, next_player=>\&next_player, + before_shoot=>\&print_score, + init=>\&init, } ); exit $dart->run(); @@ -24,6 +26,36 @@ sub gueltig return $zahl>1; } +sub init +{ + my $self=shift; + for my $i (0..21) + { + for my $player_idx (0..($self->{player_count}-1)) + { + my $zahl = $i>20?25:$i; + next if $zahl > 0 and not gueltig($zahl); + $self->get_player($player_idx)->{score}->{$zahl}=0; + } + } +} + +sub win_condition +{ + my ($self) = @_; + for my $i (keys %{$self->get_current_player()->{score}}) + { + next if not $i; + return if $self->get_current_player()->{score}->{$i}<3; + } + my $score = $self->get_current_player()->{score}->{0}; + for my $player_idx (0..($self->{player_count}-1)) + { + return if $score > $self->get_player($player_idx)->{score}->{0}; + } + return 1; +} + sub shoot { my $self=shift; @@ -71,13 +103,12 @@ sub shoot $self->shout_last_shoot() if ($scored || $self_scored); $self->shout("scored") if $scored; $self->shout("scho") if $scho && not $scored; - &print_score($self); + $self->win() if &win_condition($self); } sub next_player { my $self=shift; - &print_score($self); } sub print_score