die "Missing player_names" if not ref $params{player_names} eq 'ARRAY';
die "Missing player_names" if not @{$params{player_names}};
$self->{player}=[];
- $self->{current_player}=0;
- my $player_counter=0;
for my $player_name (@{$params{player_names}})
{
- $player_counter++;
$self->add_player(&create_player(name=>$player_name,rank=>undef,active=>1));
}
- $self->{active_player_count}=$player_counter;
- $self->{player_count}=$player_counter;
- $self->{round}=1;
+ $self->{callbacks}=$params{callbacks};
+ $self->init();
+ return $self;
+}
+
+
+sub init
+{
+ my $self=shift;
+ $self->{round}=0;
$self->{max_shoots_per_player}=3;
$self->{current_shoot_count}=0;
- $self->{callbacks}=$params{callbacks};
+ $self->{current_player}=0;
+ $self->{player_count}=@{$self->{player}};
+ $self->{active_player_count}=$self->{player_count};
$self->callback('init');
- return $self;
}
sub reset_game
{
my $self=shift;
+ my @sort_player = sort { $a->{rank} <=> $b->{rank} } @{$self->{player}};
+ $self->{player}=[];
+ for my $player (@sort_player)
+ {
+ $self->add_player(&create_player(name=>$player->{name},rank=>undef,active=>1));
+ }
+ $self->init();
}
sub callback
pop @history;
$self= pop @history;
$self->callback('undo');
+ } elsif ($mult eq 'reset') {
+ $self->reset_game();
}
push @history, Clone::clone($self);
$self->callback('before_shoot');
my $self=shift;
$self->callback('before_next_player');
$self->{current_shoot_count}=0;
- ($self->{current_player},my $new_round)=get_next_active_player($self->{player},$self->{current_player});
+ ($self->{current_player},my $new_round)=$self->get_next_active_player();
$self->shout("player");
$self->shout($self->get_current_player()->{name});
$self->next_round() if $new_round;
sub get_next_active_player
{
- my ($players_ref,$current_player)=@_;
+ my $self=shift;
+ my $players_ref = $self->{player};
+ my $current_player = $self->{current_player};
my $num_players=@$players_ref;
my $new_round=0;
do
$current_player++;
if($current_player>=$num_players)
{
- die "Error no remaining active players" if $new_round;
+ $self->reset_game() if $new_round;
$current_player=0;
$new_round=1;
}
shoot=>\&shoot,
next_player=>\&next_player,
before_shoot=>\&print_score,
+ init=>\&init,
}
);
exit $dart->run();
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;
$self->shout_last_shoot() if ($scored || $self_scored);
$self->shout("scored") if $scored;
$self->shout("scho") if $scho && not $scored;
+ $self->win() if &win_condition($self);
}
sub next_player