added cricket based on framework
authorrealraum <realraum@realraum.at>
Sun, 3 Jul 2011 21:04:33 +0000 (21:04 +0000)
committerrealraum <realraum@realraum.at>
Sun, 3 Jul 2011 21:04:33 +0000 (21:04 +0000)
dart/Dart.pm
dart/dart-cricket.pl [new file with mode: 0755]

index 9f5fd3c..bf501cb 100644 (file)
@@ -26,7 +26,7 @@ sub new
   $self->{round}=1;
   $self->{max_shoots_per_player}=3;
   $self->{current_shoot_count}=0;
-  
+  $self->{callbacks}=$params{callbacks}; 
   $self->callback('init');
   return $self;
 }
@@ -105,6 +105,20 @@ sub shout
   print $fh "$what\n";
 }
 
+sub get_current_player
+{
+  my $self=shift;
+  return $self->get_player($self->{current_player});
+}
+
+sub get_player
+{
+  my $self=shift;
+  my ($player_idx)=@_;
+  die if $player_idx < 0 or $player_idx >= $self->{player_count};
+  return $self->{player}[$player_idx];
+}
+
 sub next_player
 {
   my $self=shift;
@@ -112,7 +126,7 @@ sub next_player
   $self->{current_shoot_count}=0;
   ($self->{current_player},my $new_round)=get_next_active_player($self->{current_player});
   $self->shout("player");
-  $self->shout($self->{player}[$self->{current_player}]{name});
+  $self->shout($self->get_current_player()->{name});
   $self->next_round() if $new_round;
   return $self->callback('next_player');
 }
@@ -174,3 +188,5 @@ sub create_player
   #my $player = {%player_attributes};
   #return $player;
 }
+
+1;
diff --git a/dart/dart-cricket.pl b/dart/dart-cricket.pl
new file mode 100755 (executable)
index 0000000..b026b04
--- /dev/null
@@ -0,0 +1,107 @@
+#!/usr/bin/perl
+# use strict;
+use Dart;
+
+
+
+$|=1;
+my (@player) = @ARGV;
+
+my $dart = new Dart(player_names=>\@player, 
+                    callbacks => {
+                      shoot=>\&shoot,   
+                      next_player=>\&next_player,
+                    }
+                  );
+exit $dart->run(STDIN,STDOUT);
+
+### ===============================
+
+my $sieb =1; # Spielmodus Zahlensieb
+sub gueltig
+{
+  my ($zahl,$mult) = @_;
+  return $zahl>1;
+}
+
+sub shoot
+{
+  my $self=shift;
+  my ($mult,$zahl)=@_;
+  $self->get_current_player()->{score} = {} if not $self->get_current_player()->{score};
+  my $score=$self->get_current_player()->{score};
+
+  if (not gueltig($zahl,$mult))
+  {
+    $self->shout("miss");
+    return;
+  }
+  my ($scho,$scored,$self_scored);
+  while($mult--)
+  {
+
+    if ($score->{$zahl}<3)
+    {
+      $score->{$zahl}++;
+      $self_scored++;
+      if ($sieb && ($score->{$zahl} == 3))
+      {
+        for my $count (2..21)
+        {
+          $count = 25 if $count ==21;
+          if ( ($count % $zahl) == 0)
+          {
+            $score->{$count} = 3;
+          }
+        }
+      }
+    } else {
+      $scho++;
+      for my $player_idx (0..($self->{player_count}-1))
+      {
+        next if not $self->get_player($player_idx)->{active};
+        if ($self->get_player($player_idx)->{score}->{$zahl}<3)
+        {
+          $self->get_player($player_idx)->{score}->{0}+=$zahl;
+          $scored++;
+        }
+      }
+    }
+  }
+  $self->shout_last_shoot() if ($scored || $self_scored);
+  $self->shout("scored") if $scored;
+  $self->shout("scho") if $scho && not $scored;
+  &print_score($self);
+}
+
+sub next_player
+{
+  my $self=shift;
+  &print_score($self);
+}
+
+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 $i (1..21)
+  {
+    for my $player_idx (0..($self->{player_count}-1))
+    {
+      my $zahl = $i>20?25:$i;
+      next if not gueltig($zahl);
+      printf STDERR ("%2d %s    ",$zahl, '#' x $self->get_player($player_idx)->{score}->{$zahl}. '-' x (3-$self->get_player($player_idx)->{score}->{$zahl}));
+    }
+    print STDERR "\n";
+  }
+  for my $player_idx (0..($self->{player_count}-1))
+  {
+    printf STDERR ("%3d\t", $self->get_player($player_idx)->{score}->{0});
+  }
+}