#!/usr/bin/perl

($EXP, $DEV, $SET, $TRA, $TWO, $PRO, $STA, $ROB) = (1,2,3,4,5,6,7,8);

@dnm = ('!!!', 'Exp', 'Dev', 'Set', 'C$$', 'Cx2', 'Pro', '***', 'ROB');

int(rand(3));

@die1 = ($ROB, $ROB, $PRO, $TRA, $TWO, $STA);
@die2 = ($EXP, $DEV, $DEV, $SET, $STA, $STA);

@mws = (1, 10, 11, 1, 3, 1, 1, 1);
@ws = (3, 20, 15, 6, 1, 3, 1);  # 7 is DOOMED
@ds = (2, 25, 14, 2, 15); # 5 is 6-devs
$mws = 0; foreach (@mws) { $mws += $_; }
$ws = 0; foreach (@ws) { $ws += $_; }
$ds = 0; foreach (@ds) { $ds += $_; }
@types = ("Military World", "Non-Military World", "Development");

$DEBUG = 0;

# $SETTLE_BOOST = $ARGV[0];
$SETTLE_BOOST = 1;
  
# print "SETTLE BOOST is $SETTLE_BOOST\n";

sub random_card {
  my $val = int(rand($mws+$ws+$ds));
  my $vp;
  my $type;
  if ($val < $mws) {
    $type = 0;
    $vp = 0;
    while ($mws[$vp] < $val) {
      $val -= $mws[$vp];
      $vp++;
    }
  } elsif ($val < $mws + $ws) {
    $val -= $mws;
    $type = 1;
    $vp = 0;
    while ($ws[$vp] < $val) {
      $val -= $ws[$vp];
      $vp++;
    }
    $vp = -1 if ($vp == 7);
  } else {
    $val -= $mws;
    $val -= $ws;
    $type = 2;
    $vp = 0;
    while ($ws[$vp] < $val) {
      $val -= $ds[$vp];
      $vp++;
    }
    $vp = 6 if ($vp == 5);
  }
  print "DEBUG: Flipped a $types[$type] worth $vp points\n" if ($DEBUG);
  return ($type, $vp);
}

sub flip_to_find {
  my ($type_wanted, $mon_needed) = @_;
  my $found = 0;
  my $vp_found = 0;
  while (not $found and $stack_size > 0) {
    my ($type, $vp) = random_card();
    if ($type == $type_wanted) {
      $found = 1;
      $vp_found = $vp;
      if ($types[$type] eq "Development" and $vp == 6 and $mon == 0) {
        $found = 0;
      }
    }
    $stack_size --;
  }
  return ($found, $vp_found);
}

@humans = ( [
$SET, $TRA, $DEV, $SET, $SET, $TRA, $DEV, $TRA, $DEV, $SET, $DEV, $SET, $EXP, $TRA, $DEV, $SET,
], [
$SET, $TRA, $SET, $SET, $DEV, $TRA, $DEV, $PRO, $TWO, $PRO,
], [
$SET, $TRA, $SET, $SET, $EXP, $DEV, $SET, $PRO, $SET, $TRA, $DEV, $TWO,
], [
$SET, $SET, $TRA, $PRO, $EXP, $DEV, $DEV, $SET, $SET, $SET, $SET, $SET, $DEV, $SET,
], [
$EXP, $TRA, $DEV, $SET, $EXP, $PRO, $DEV, $SET, $SET, $TRA, $DEV, $PRO, $SET, $TWO,
], [
$EXP, $TRA, $DEV, $DEV, $SET, $TRA, $EXP, $DEV, $DEV, $TRA, $SET, $SET, $EXP, $TRA, $DEV, $SET, 
], [
$TRA, $PRO, $DEV, $TRA, $EXP, $DEV, $EXP, $SET, $TRA, $PRO, $SET, $TWO, $SET, $PRO, $SET, $PRO,
], [
$EXP, $EXP, $EXP, $DEV, $EXP, $DEV, $EXP, $TRA, $DEV, $SET, $DEV, $SET, $DEV, $SET, $EXP, $SET, $EXP, $SET,
], [
$EXP, $TRA, $DEV, $SET, $SET, $TRA, $DEV, $SET, $DEV, $TWO, $TRA, $TWO, $SET, $PRO, $DEV, $TWO,
], [
$EXP, $DEV, $EXP, $PRO, $EXP, $TRA, $DEV, $SET, $EXP, $TRA, $SET, $TRA, $SET, $PRO, $SET, $TWO, $SET, $PRO,
], [
$EXP, $SET, $SET, $TRA, $DEV, $SET, $SET, $TRA, $SET, $SET, $SET, $TRA, $SET, $PRO, $TRA, $TWO, $SET, $PRO, $SET, $TWO,
], [
$SET, $TRA, $EXP, $PRO, $TRA, $PRO, $EXP, $DEV, $SET, $SET, $TWO, $PRO, $DEV, $TWO, $DEV, $TWO, $SET, $TWO,
] ); 
@name = (
  "New Sparta, perfect hand kinda",
  "Alpha Centauri, fast crank",
  "Alpha Centauri, avoiding VP break",
  "New Sparta, big military",
  "Alpha Centauri, kinda improved logistics",
  "Alpha Centauri, ATI NGO dense tableau",
  "Ancient Race, partial crank",
  "New Sparta, partial tableau run",
  "Alpha Centauri, produce/consume",
  "Earth's Lost Colony, not quite crank",  # 1, 2, 5, 6, 8, 9, 15, 16, 30, 36
  "New Sparta, Trisha, Alien crank",
  "Epsilon Eridani, crank leech",
);

@human_scores = (
  54, 24, 26, 38, 33, 34, 43, 52, 44, 36, 47, 38,
);

$vp_lim = 15;

@opponents = (' OE', ' EE', ' AC', ' NS', 'ELC', ' SC', ' AR', 'DAF', ' DW');
@start_mon = (1, 1, 2, 1, 1, 1, 0, 1, 1);
@start_eco = (0, 1, 0, 0, 1, 0, 0, 0, 0);
@start_rob = ($TRA, $STA, $SET, $SET, $PRO, $EXP, $TRA, $PRO, $SET);
@start_vp = (2, 1, 0, 1, 1, 1, 1, 2, -1);

$cur_rob;
$turn;
$funky_state;

sub roll {
  my ($hum1, $hum2) = @_;
  my $d1 = $die1[int(rand(6))];
  my $d2 = $die2[int(rand(6))];
  print "DEBUG: Robot actually Rolls $dnm[$d1] $dnm[$d2]\n" if ($DEBUG);
  if ($d1 == $ROB) {
    if ($cur_rob == 0) {
      if ($funky_state % 2 == 0) {
        $d1 = $TRA;
      } else {
        $d1 = $TWO;
      }
    } elsif ($cur_rob == 7) {
      if ($funky_state == 0) {
        $d1 = $PRO;
      } else {
        $d1 = $TRA;
      }
    } else {
      $d1 = $start_rob[$cur_rob];
    }
  }
  if ($d1 == $STA and $d2 == $STA) {
    $d1 = $hum1; $d2 = $hum2;
  } elsif ($d1 == $STA) {
    if ($hum1 == $d2) {
      $d1 = $hum2;
    } elsif ($hum2 == $d2) {
      $d1 = $hum1;
    } elsif ($hum1 > $hum2) {
      $d1 = $hum1;
    } else {
      $d1 = $hum2;
    }
  } elsif ($d2 == $STA) {
    if ($hum1 == $d1) {
      $d2 = $hum2;
    } elsif ($hum2 == $d1) {
      $d2 = $hum1;
    } elsif ($hum1 > $hum2) {
      $d2 = $hum1;
    } else {
      $d2 = $hum2;
    }
  }
  if ($cur_rob == 0) {
    if ($funky_state == 0) {
      if ($d1 == $TRA or $d2 == $TRA) {
        $funky_state = 1;
      }
    }
  }
  if ($cur_rob == 7) {
    if ($funky_state == 0) {
      if ($d1 == $PRO or $d2 == $PRO) {
        $funky_state = 1;
      }
    } elsif ($funky_state % 2 == 1) {
      if ($d1 == $TRA or $d2 == $TRA) {
        $funky_state = 0;
      }
    }
  }
  return ($d1, $d2) if ($d1 < $d2);
  return ($d2, $d1);
}

# srand($ARGV[0]);

$gc = 3000;
# $gc = 1;

for $cur_human (0..10) {
# for $cur_human (0..$#humans) {

@human = @{$humans[$cur_human]};
$foofoo = (scalar @human) / 2;
print "Trying $foofoo turn Human $cur_human ( $name[$cur_human] ) $human_scores[$cur_human] ...\n";


for $cur_rob (0..8) {
# for $cur_rob (2, 3) {
  $min_score = 999;
  $max_score = -999;
  for $game (1..$gc) {

    $vps = 0;
    $tableau_vps = $start_vp[$cur_rob];
    $tableau_6s = 0;
    $tableau_size = 1;
    $devel_count = 0;
    $world_count = 0;
    $turn = 0;
    $funky_state = 0;
    $stack_size = 4;
    $stack_size = 3 if ($cur_rob == 6);
    $super_devs = 0;
    $mon = $start_mon[$cur_rob];
    $eco = $start_eco[$cur_rob];
    $num_flips = 0;
    $num_finds = 0;
    $num_fails = 0;

    print "DEBUG:\n" if ($DEBUG);
    print "DEBUG: Robot is $opponents[$cur_rob]\n" if ($DEBUG);
    print "DEBUG: Stack size is now $stack_size\n" if ($DEBUG);
    while ($turn * 2 < scalar @human) {
      print "DEBUG: Turn $turn\n" if ($DEBUG);
      my @hums = @human[$turn*2..$turn*2+1];
      my @robs = roll(@hums);
      print "DEBUG: Human Roll $dnm[$hums[0]] $dnm[$hums[1]]\n" if ($DEBUG);
      print "DEBUG: Robot Roll $dnm[$robs[0]] $dnm[$robs[1]]\n" if ($DEBUG);

      # EXPLORE
      if (scalar @robs >= 2 and $robs[0] == $EXP and $robs[1] == $EXP) {
        $stack_size += ($opponents[$cur_rob] eq ' SC') ? 7 : 3;
        $mon++; print "DEBUG:   Adding one cash to $mon.\n" if ($DEBUG);
        shift @robs; shift @robs;
        shift @hums if (scalar @hums > 0 and $hums[0] == $EXP);
        print "DEBUG: Explore\n" if ($DEBUG);
        print "DEBUG:   Stack size is now $stack_size\n" if ($DEBUG);
      } elsif (scalar @robs >= 1 and $robs[0] == $EXP) {
        $stack_size += ($opponents[$cur_rob] eq ' SC') ? 3 : 1;
        $mon++; print "DEBUG:   Adding one cash to $mon.\n" if ($DEBUG);
        shift @robs;
        shift @hums if (scalar @hums > 0 and $hums[0] == $EXP);
        print "DEBUG: Explore\n" if ($DEBUG);
        print "DEBUG:   Stack size is now $stack_size\n" if ($DEBUG);
      } elsif (scalar @hums >= 1 and $hums[0] == $EXP) {
        $stack_size += ($opponents[$cur_rob] eq ' SC') ? 3 : 1;
        print "DEBUG: Explore\n" if ($DEBUG);
        print "DEBUG:   Stack size is now $stack_size\n" if ($DEBUG);
      }

$OLD_DEVELOP_RULES = 0;


if ($OLD_DEVELOP_RULES) {

} else {

      # DEVELOP ROBOT
      for $subphase (1..2) {
        if (scalar @robs >= 1 and $robs[0] == $DEV) {
          shift @robs;
          shift @hums if (scalar @hums > 0 and $hums[0] == $DEV);
          print "DEBUG: Develop Robot $subphase\n" if ($DEBUG);

          if ($mon >= 3) {
            $super_devs++; print "DEBUG:   Has 3 cash, getting super_dev to $super_devs\n" if ($DEBUG);
            $mon--; print "DEBUG:   Removing one cash to $mon.\n" if ($DEBUG);
            $mon--; print "DEBUG:   Removing one cash to $mon.\n" if ($DEBUG);
            $mon--; print "DEBUG:   Removing one cash to $mon.\n" if ($DEBUG);
            $tableau_size++; print "DEBUG:   Tableau size becomes $tableau_size.\n" if ($DEBUG);
          } else {
            $stack_size ++; print "DEBUG:   Stack size is now $stack_size\n" if ($DEBUG);
            my ($found, $vp_found) = flip_to_find(2, 0);
            $num_flips++;
            while ($found and $vp_count == 6 and $mon < 2) {
              ($found, $vp_found) = flip_to_find(2, 0);
            }
            if ($found) {
              print "DEBUG:   Found Develop worth $vp_found\n" if ($DEBUG);
              if ($vp_found == 6 and $mon >= 2) {
                $mon--; print "DEBUG:   Removing one cash to $mon.\n" if ($DEBUG);
                $mon--; print "DEBUG:   Removing one cash to $mon.\n" if ($DEBUG);
                $tableau_6s ++; print "DEBUG:   Tableau 6s is now $tableau_6s.\n" if ($DEBUG);
              } else {
                $tableau_vps += $vp_found; print "DEBUG:   Tableau vps is now $tableau_vps.\n" if ($DEBUG);
              }
              $tableau_size++; print "DEBUG:   Tableau size becomes $tableau_size.\n" if ($DEBUG);
              $devel_count++; print "DEBUG:   Devel count becomes $devel_count.\n" if ($DEBUG);
              $num_finds++;
            } else {
              print "DEBUG:   Nothing found.\n" if ($DEBUG);
              $num_fails++;
            }
          }
        }
      }

      # DEVELOP HUMAN
      for $subphase (1..2) {
        if (scalar @hums >= 1 and $hums[0] == $DEV) {
          shift @hums;
          print "DEBUG: Develop Human $subphase\n" if ($DEBUG);
          if ($mon == 0) {
            print "DEBUG:   No action; no money.\n" if ($DEBUG);
          } else {
            if ($mon >= 3) {
              $super_devs++; print "DEBUG:   Has 2 cash, getting super_dev to $super_devs\n" if ($DEBUG);
              $mon--; print "DEBUG:   Removing one cash to $mon.\n" if ($DEBUG);
              $mon--; print "DEBUG:   Removing one cash to $mon.\n" if ($DEBUG);
              $mon--; print "DEBUG:   Removing one cash to $mon.\n" if ($DEBUG);
              $tableau_size++; print "DEBUG:   Tableau size becomes $tableau_size.\n" if ($DEBUG);
            } else {
              my ($found, $vp_found) = flip_to_find(2, 0);
              $num_flips++;
              if ($found) {
                print "DEBUG:   Found Develop worth $vp_found\n" if ($DEBUG);
                $mon--; print "DEBUG:   Removing one cash to $mon.\n" if ($DEBUG);
                if ($vp_found == 6) {
                  $tableau_6s ++; print "DEBUG:   Tableau 6s is now $tableau_6s.\n" if ($DEBUG);
                  $mon--; print "DEBUG:   Removing one cash to $mon.\n" if ($DEBUG);
                } else {
                  $tableau_vps += $vp_found; print "DEBUG:   Tableau vps is now $tableau_vps.\n" if ($DEBUG);
                }
                $tableau_size++; print "DEBUG:   Tableau size becomes $tableau_size.\n" if ($DEBUG);
                $devel_count++; print "DEBUG:   Devel count becomes $devel_count.\n" if ($DEBUG);
                $num_finds++;
              } else {
                print "DEBUG:   Nothing found.\n" if ($DEBUG);
                $num_fails++;
              }
            }
          }
        }
      }

}

$NEW_SPARTA_BONUS = 1;

      # SETTLE ROBOT
      for $subphase (1..2) {
        if (scalar @robs >= 1 and $robs[0] == $SET) {
          shift @robs;
          shift @hums if (scalar @hums > 0 and $hums[0] == $SET);
          print "DEBUG: Settle Robot $subphase\n" if ($DEBUG);
          if ($cur_rob == 8 and $funky_state == 0) {
            $funky_state = 1;
            print "DEBUG:   Special Doomed World Settle\n" if ($DEBUG);
            my ($type, $vp_found) = (0, 0);
            while ($type != 1 or $vp_found < 3) {
              ($type, $vp_found) = random_card();
            }
            print "DEBUG:   Found World worth $vp_found\n" if ($DEBUG);
            $tableau_vps += $vp_found + 1; print "DEBUG:   Tableau vps is now $tableau_vps.\n" if ($DEBUG);
            print "DEBUG:   Tableau size becomes $tableau_size.\n" if ($DEBUG);
            $world_count++; print "DEBUG:   World count becomes $world_count.\n" if ($DEBUG);
            $eco++; print "DEBUG:   Adding one eco to $eco.\n" if ($DEBUG);
          } else {
            $stack_size += 3; print "DEBUG:   Stack size is now $stack_size\n" if ($DEBUG);
            $stack_size += 1 if ($cur_rob == 2);
            $stack_size += 2 if ($cur_rob == 3 and $NEW_SPARTA_BONUS);
            $milm = 1;
            $milm = 0 if ($cur_rob == 1 or $cur_rob == 3 or $cur_rob == 5);
            my ($found, $vp_found) = flip_to_find($milm, 0);
            $num_flips++;
            if ($found) {
              print "DEBUG:   Found World worth $vp_found\n" if ($DEBUG);
              $tableau_vps += $vp_found; print "DEBUG:   Tableau vps is now $tableau_vps.\n" if ($DEBUG);
              $tableau_size++; print "DEBUG:   Tableau size becomes $tableau_size.\n" if ($DEBUG);
              $world_count++; print "DEBUG:   World count becomes $world_count.\n" if ($DEBUG);
              $num_finds++;
            } else {
              $num_fails++;
            }
          }
        }
      }

      # SETTLE HUMAN 
      for $subphase (1..2) {
        if (scalar @hums >= 1 and $hums[0] == $SET) {
          shift @hums;
          print "DEBUG: Settle Human $subphase\n" if ($DEBUG);
          if ($cur_rob == 1 or $cur_rob == 3 or $cur_rob == 5 or $cur_rob == 2) {
            $milm = 0;
            $milm = 1 if ($cur_rob == 2);
            $stack_size += 2; print "DEBUG:   Stack size is now $stack_size\n" if ($DEBUG);
            $stack_size += 2 if ($cur_rob == 3 and $NEW_SPARTA_BONUS);
            my ($found, $vp_found) = flip_to_find($milm, 0);
            $num_flips++;
            if ($found) {
              print "DEBUG:   Found Military World worth $vp_found\n" if ($DEBUG);
              $tableau_vps += $vp_found; print "DEBUG:   Tableau vps is now $tableau_vps.\n" if ($DEBUG);
              $tableau_size++; print "DEBUG:   Tableau size becomes $tableau_size.\n" if ($DEBUG);
              $world_count++; print "DEBUG:   World count becomes $world_count.\n" if ($DEBUG);
              $num_finds++;
            } else {
              $num_fails++;
            }
          } elsif ($mon > 0) {
            $milm = 1;
            $stack_size += 2; print "DEBUG:   Stack size is now $stack_size\n" if ($DEBUG);
            my ($found, $vp_found) = flip_to_find($milm, 0);
            $num_flips++;
            if ($found) {
              print "DEBUG:   Found World worth $vp_found\n" if ($DEBUG);
              $mon--; print "DEBUG:   Removing one cash to $mon.\n" if ($DEBUG);
              $tableau_vps += $vp_found; print "DEBUG:   Tableau vps is now $tableau_vps.\n" if ($DEBUG);
              $tableau_size++; print "DEBUG:   Tableau size becomes $tableau_size.\n" if ($DEBUG);
              $world_count++; print "DEBUG:   World count becomes $world_count.\n" if ($DEBUG);
              $num_finds++;
            } else {
              $num_fails++;
            }
          }
        }
      }

      # CONSUME
      if (scalar @robs >= 2 and $robs[0] == $TRA and $robs[1] == $TWO) {
        shift @robs; shift @robs;
        shift @hums if (scalar @hums > 0 and $hums[0] == $TRA);
        shift @hums if (scalar @hums > 0 and $hums[0] == $TWO);
        print "DEBUG: Consume\n" if ($DEBUG);
        $mon++; print "DEBUG:   Adding one cash to $mon.\n" if ($DEBUG);
        $mon++; print "DEBUG:   Adding one cash to $mon.\n" if ($DEBUG);
        $vps += 2*($eco); print "DEBUG:   Adding double $eco VP, now $vps.\n" if ($DEBUG);
        $eco++; print "DEBUG:   Adding one eco to $eco.\n" if ($DEBUG);
      } elsif (scalar @robs >= 1 and $robs[0] == $TRA) {
        shift @robs;
        shift @hums if (scalar @hums > 0 and $hums[0] == $TRA);
        shift @hums if (scalar @hums > 0 and $hums[0] == $TWO);
        print "DEBUG: Consume\n" if ($DEBUG);
        $mon++; print "DEBUG:   Adding one cash to $mon.\n" if ($DEBUG);
        $mon++; print "DEBUG:   Adding one cash to $mon.\n" if ($DEBUG);
        $vps += $eco; print "DEBUG:   Adding $eco VP, now $vps.\n" if ($DEBUG);
      } elsif (scalar @robs >= 1 and $robs[0] == $TWO) {
        shift @robs;
        shift @hums if (scalar @hums > 0 and $hums[0] == $TRA);
        shift @hums if (scalar @hums > 0 and $hums[0] == $TWO);
        print "DEBUG: Consume\n" if ($DEBUG);
        $vps += 2*($eco); print "DEBUG:   Adding double $eco VP, now $vps.\n" if ($DEBUG);
        $eco++; print "DEBUG:   Adding one eco to $eco.\n" if ($DEBUG);
      } elsif (scalar @hums >= 1 and ($hums[0] == $TRA or $hums[0] == $TWO)) {
        shift @hums if (scalar @hums > 0 and $hums[0] == $TRA);
        shift @hums if (scalar @hums > 0 and $hums[0] == $TWO);
        print "DEBUG:   Consume\n" if ($DEBUG);
        $vps += $eco; print "DEBUG:   Adding $eco VP, now $vps.\n" if ($DEBUG);
      }

      # PRODUCE
      if (scalar @robs >= 1 and $robs[0] == $PRO) {
        print "DEBUG: Produce\n" if ($DEBUG);
        $eco++; print "DEBUG:   Adding one eco to $eco.\n" if ($DEBUG);
      }

      if ($mon > 4) {
        $mon = 4; print "DEBUG: Woop, reverting mon to 4.\n" if ($DEBUG);
        $total_mon_bounce ++;
      }

      if ($eco > 5) {
        $eco = 5; print "DEBUG: Woop, reverting eco to 6.\n" if ($DEBUG);
        $total_eco_bounce ++;
      }

      $turn++;
    }

    $vps = 20 if ($vps > 20);

    print "DEBUG: Game over.\n" if ($DEBUG);
    print "DEBUG:   $vps VP chips, $tableau_vps normal VPs in tableau,\n" if ($DEBUG);
    print "DEBUG:   $tableau_6s dev-6s in tableau, $super_devs dev-chips,\n" if ($DEBUG);
    print "DEBUG:   $tableau_size tableau_size, $turn turns,\n" if ($DEBUG);
    print "DEBUG:   $mon final money, $eco final economy.\n" if ($DEBUG);

    $sha = ($super_devs > 1) ? 1 : $super_devs;
    $e_score = $vps + $tableau_vps + $tableau_6s * 6 + $sha * 6;
    $sha = ($super_devs > 2) ? 2 : $super_devs;
    $m_score = $vps + $tableau_vps + $tableau_6s * 6 + $sha * 6;
    $sha = ($super_devs > 3) ? 3 : $super_devs;
    $h_score = $vps + $tableau_vps + $tableau_6s * 9 + $sha * 9;
# print "$e_score $m_score $h_score $human_scores[$cur_human]\n";
    $total_e_wins++ if ($human_scores[$cur_human] > $e_score);
    $total_m_wins++ if ($human_scores[$cur_human] > $m_score);
    $total_h_wins++ if ($human_scores[$cur_human] > $h_score);

    $total_flips += $num_flips;
    $total_finds += $num_finds;
    $total_fails += $num_fails;
    $total_vps += $vps;
    $total_tab_vps += $tableau_vps;
    $total_tab_6s += $tableau_6s;
    $total_tab_size += $tableau_size;
    $total_devel_count += $devel_count++;
    $total_world_count += $world_count++;
    $maxi = 3;
#    $maxi = 1;
    $super_devs = $maxi if ($super_devs > $maxi);
    $total_supers += $super_devs;
    $score = $vps + $tableau_vps + $tableau_6s * 9 + $super_devs * 9;
#    $score = $vps + $tableau_vps + $tableau_6s * 6 + $super_devs * 6;
    $min_score = $score if ($min_score > $score);
    $max_score = $score if ($max_score < $score);
    $total_turns += $turn;
    push @bot_scores, $score;
    $total_super_devs[$super_devs]++;
    $tableau_size = 12 if ($tableau_size > 12);
    $total_tab_sizes[$tableau_size]++;

  }

  print "$opponents[$cur_rob]: ";

  @sorted = sort @bot_scores;

  $mean = (($total_tab_vps + $total_vps + $total_tab_6s * 9 + $total_supers * 9)/$gc);
  $variance = 0;
  foreach (@bot_scores) {
    $variance += ($mean - $_) * ($mean - $_);
  }
  $variance /= $gc;
  

  printf "fff %05.2f %05.2f %04.2f %04.2f monb %05.2f tabsz %05.2f (8) %04.2f %04.2f %04.2f %04.2f %04.2f dev %05.2f wor %05.2f tbVP %05.2f chp %05.2f sxs %04.2f sprs %04.2f %04.2f %04.2f %04.2f %04.2f %04.2f mean %05.2f med %02d stdev %05.2f wns %04.2f %04.2f %04.2f\n",
    ($total_flips/$gc),
    ($total_finds/$gc),
    ($total_fails/$gc),
    ($total_fails/$total_flips),
    ($total_mon_bounce/$gc),
    ($total_tab_size/$gc),
    ($total_tab_sizes[8]/$gc),
    ($total_tab_sizes[9]/$gc),
    ($total_tab_sizes[10]/$gc),
    ($total_tab_sizes[11]/$gc),
    ($total_tab_sizes[12]/$gc),
    ($total_devel_count/$gc),
    ($total_world_count/$gc),
    ($total_tab_vps/$gc),
    ($total_vps/$gc),
    ($total_tab_6s/$gc),
    ($total_supers/$gc),
    ($total_super_devs[0]/$gc),
    ($total_super_devs[1]/$gc),
    ($total_super_devs[2]/$gc),
    ($total_super_devs[3]/$gc),
    ($total_super_devs[4]/$gc),
    $mean,
    $sorted[int($#sorted/2)],
    sqrt($variance),
    ($total_e_wins/$gc),
    ($total_m_wins/$gc),
    ($total_h_wins/$gc),
  ;

  $total_mon_bounce = 0;
  $total_eco_bounce = 0;
  $total_vps = 0;
  $total_tab_vps = 0;
  $total_tab_6s = 0;
  $total_supers = 0;
  $total_tab_size = 0;
  $total_devel_count = 0;
  $total_world_count = 0;
  $min_score = 999;
  $max_score = -999;
  $total_turns = 0;
  @total_super_devs = ();
  @total_tab_sizes = ();
  @bot_scores = ();
  $total_flips = 0;
  $total_finds = 0;
  $total_fails = 0;
  $total_e_wins = 0;
  $total_m_wins = 0;
  $total_h_wins = 0;
}

}
