added rot/blau dart
authorrealraum <realraum@realraum.at>
Fri, 11 Nov 2011 21:17:29 +0000 (21:17 +0000)
committerrealraum <realraum@realraum.at>
Fri, 11 Nov 2011 21:17:29 +0000 (21:17 +0000)
dart/Dart.pm
dart/dart-red-101.pl [new symlink]
dart/dart-red-x01.pl [new file with mode: 0755]

index cb654bd..d9b3795 100644 (file)
@@ -25,6 +25,18 @@ sub new
 }
 
 
+sub get_color
+{
+  my $self=shift;
+  my ($mul,$zahl)=@_;
+  my @zahlen =  qw/20 1 18 4 13 6 10 15 2 17 3 19 7 16 8 11 14 9 12 5/;
+  my $counter=0;
+  $counter++ while($zahl != shift @zahlen and @zahlen);
+  $mul=0 if $mul >1;
+  my $result = ($counter+$mul )%2;
+  return $result;
+}
+
 sub init
 {
   my $self=shift;
diff --git a/dart/dart-red-101.pl b/dart/dart-red-101.pl
new file mode 120000 (symlink)
index 0000000..eb00505
--- /dev/null
@@ -0,0 +1 @@
+dart-red-x01.pl
\ No newline at end of file
diff --git a/dart/dart-red-x01.pl b/dart/dart-red-x01.pl
new file mode 100755 (executable)
index 0000000..56b4cb2
--- /dev/null
@@ -0,0 +1,78 @@
+#!/usr/bin/perl
+use strict;
+use Dart;
+
+$|=1;
+my $maxScore = $0;
+$maxScore =~ s/.*\-(\d+).pl$/\1/;
+my (@player) = @ARGV;
+
+my $dart = new Dart(player_names=>\@player, 
+                    callbacks => {
+                      shoot=>\&shoot,   
+                      next_player=>\&next_player,
+                      before_shoot=>\&print_score,
+                      init=>\&init,
+                    }
+                  );
+exit $dart->run();
+
+### ===============================
+
+sub init
+{
+  my $self=shift;
+
+  for my $player_idx (0..($self->{player_count}-1))
+  {
+    $self->get_player($player_idx)->{score} = $maxScore;
+  }
+}
+
+sub win_condition
+{
+  my ($self) = @_;
+  return $self->get_current_player()->{score} == 0;
+}
+
+sub shoot
+{
+  my $self=shift;
+  my ($mult,$zahl)=@_;
+  my $color = $self->get_color($mult,$zahl)?1:-1;  
+  if ($self->get_current_player()->{score} >= $mult * $zahl)
+  {
+    $self->get_current_player()->{score} -= $color *$mult * $zahl;
+    $self->shout_last_shoot();
+    $self->win() if &win_condition($self);
+  }
+  else
+  {
+    $self->shout("miss");
+    $self->get_current_player()->{score} = $self->get_current_player()->{last_score};
+    $self->finish_player_round();
+  }
+}
+
+sub next_player
+{
+  my $self=shift;
+  $self->get_current_player()->{last_score} = $self->get_current_player()->{score};
+}
+
+sub print_score
+{
+  my ($self)=@_;
+  printf STDERR "\n\n";
+  printf STDERR "Runde\t%d\n\n",$self->{round};
+  for my $player_idx (0..($self->{player_count}-1))
+  {
+    printf STDERR "%s\t", ($player_idx == $self->{current_player})?"(".$self->get_player($player_idx)->{name}.")":$self->get_player($player_idx)->{name};
+  }
+  print STDERR "\n";
+  for my $player_idx (0..($self->{player_count}-1))
+  {
+    printf STDERR "%s\t", $self->get_player($player_idx)->{score};
+  }
+  print STDERR "\n";
+}