~ubuntu-branches/ubuntu/natty/dealer/natty

« back to all changes in this revision

Viewing changes to Examples/convert.pl

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Berg
  • Date: 2005-09-07 23:32:20 UTC
  • Revision ID: james.westby@ubuntu.com-20050907233220-e7bsghnrwg1ncye4
Tags: upstream-0.20040530
ImportĀ upstreamĀ versionĀ 0.20040530

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
 
 
3
# This script reformats the output of Descr.test_dealer into a nice
 
4
# table, with the theoretical values next to the hcp and suit distributions
 
5
# for the 4 players
 
6
 
 
7
# This is not the best piece of Perl that I've ever written.
 
8
 
 
9
#
 
10
# Frequency of suit lengths and hcp help by players.  From Frederic H. Frost,
 
11
# "Bridge Odds Complete".
 
12
#
 
13
@suitlen = (1.27, 8.01, 20.59, 28.63, 23.87, 12.47, 4.16, 0.88, 0.12, 0.01, 0,0,0);
 
14
@rawhcp = (0.36, 0.79, 1.35, 2.46, 3.85, 5.19, 6.55, 8.03, 8.89, 9.36, 9.41, 8.94, 8.03, 6.91, 5.69, 4.42, 3.31, 2.36, 1.61, 1.04, 0.64, 0.38, 0.21, 0.11, 0.06, 0.03, 0.01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
 
15
 
 
16
$player_to_int{"N"} = 0;
 
17
$player_to_int{"E"} = 1;
 
18
$player_to_int{"S"} = 2;
 
19
$player_to_int{"W"} = 3;
 
20
$suit_to_int{"Spades"}   = 0;
 
21
$suit_to_int{"Hearts"}   = 1;
 
22
$suit_to_int{"Diamonds"} = 2;
 
23
$suit_to_int{"Clubs"}    = 3;
 
24
$suit_to_int{"S"} = 0;
 
25
$suit_to_int{"H"} = 1;
 
26
$suit_to_int{"D"} = 2;
 
27
$suit_to_int{"C"} = 3;
 
28
$rank_to_int{"T"} = 10;
 
29
$rank_to_int{"J"} = 11;
 
30
$rank_to_int{"Q"} = 12;
 
31
$rank_to_int{"K"} = 13;
 
32
$rank_to_int{"A"} = 14;
 
33
 
 
34
 
 
35
while (<STDIN>) {
 
36
   chop;
 
37
   chop;
 
38
   if ($_ =~ "HCP") {
 
39
      ($dummy, $dummy, $player) = split (/\s+/, $_);
 
40
      $player  = $player_to_int{$player};
 
41
      for ($i=0; $i<38; $i++) {
 
42
         $_ = <STDIN>;
 
43
         ($dummy, $dummy, $freq) = (split /\s+/,  $_);
 
44
         $hcp[$i][$player] = $freq;
 
45
      }
 
46
   } elsif ($_ =~ "Frequency Card") {
 
47
      ($dummy, $dumy, $player, $card) = (split /\s+/, $_);
 
48
      $player = $player_to_int{$player};
 
49
      $suit   = $suit_to_int  {substr($card, 0, 1)};
 
50
      $rank = substr($card, 1, 1);
 
51
      if (defined $rank_to_int{$rank}) {
 
52
         $rank = $rank_to_int{$rank};
 
53
      } 
 
54
      $_ = <STDIN>;
 
55
      $_ = <STDIN>;
 
56
      ($dummy, $dummy, $count) = split (/\s+/, $_);
 
57
      $hascard[$player][$suit][$rank] = $count;
 
58
   } elsif ($_ =~ "Frequency") {
 
59
      ($dummy, $suit, $player) = split (/\s+/, $_);
 
60
      $player  = $player_to_int{$player};
 
61
      $suit    = $suit_to_int{$suit};
 
62
      for ($i=0 ; $i<13; $i++) {
 
63
         $_ = <STDIN>;
 
64
         ($dummy, $dummy, $freq) = split (/\s+/, $_);
 
65
         $suit[$i][$player][$suit] = $freq;
 
66
      }
 
67
   } elsif ($_ =~ "Generated") {
 
68
      ($dummy, $constant, $dummy) = split (/\s+/, $_);
 
69
   }
 
70
}
 
71
 
 
72
print "Generated $constant hands\n\n";
 
73
$constant /= 100;
 
74
 
 
75
print "HCP Distribution\n================\n";
 
76
print " #     N            E            S            W\n";
 
77
print "       %   diff     %   diff     %   diff     %   diff\n";
 
78
 
 
79
for ($i=0 ; $i<38 ; $i++) {
 
80
  printf ("%2d ", $i);
 
81
  for ($j=0 ; $j<4; $j++) {
 
82
     printf ("%5.2f ", $hcp[$i][$j]/$constant);
 
83
     printf ("%6.3f ", $hcp[$i][$j]/$constant- $rawhcp[$i]);
 
84
  }
 
85
  printf ("%5.2f", $rawhcp[$i]);
 
86
  print "\n";
 
87
}
 
88
 
 
89
foreach $k ("Spades", "Hearts", "Diamonds", "Clubs") {
 
90
  print "\n$k Distribution\n=====================\n";
 
91
  print " #     N            E            S            W\n";
 
92
  print "       %   diff     %   diff     %   diff     %   diff\n";
 
93
  $k1 = $suit_to_int{$k};
 
94
  for ($i=0 ; $i<14 ; $i++) {
 
95
    printf ("%2d ", $i);
 
96
    for ($j=0 ; $j<4; $j++) {
 
97
       printf ("%5.2f ", $suit[$i][$j][$k1]/$constant);
 
98
       printf ("%6.3f ", $suit[$i][$j][$k1]/$constant-$suitlen[$i]);
 
99
    }
 
100
    printf ("%5.2f", $suitlen[$i]);
 
101
    print "\n";
 
102
  }
 
103
}
 
104
 
 
105
print "\nIndividual Cards\n================\n";
 
106
 
 
107
print "  Spades                  Hearts                  Diamonds                Clubs\n";
 
108
print "#     N     E     S     W     N     E     S     W     N     E     S     W     N     E     S     W\n";
 
109
 
 
110
for ($rank = 2 ; $rank < 15 ; $rank++) {
 
111
 
 
112
   printf ("%s ", substr("23456789TJQKA", $rank-2, 1));
 
113
   for ($suit = 0 ; $suit < 4; $suit++) {
 
114
      
 
115
      for ($player = 0 ; $player < 4 ; $player++) {
 
116
         printf ("%5.2f ", $hascard [$player][$suit][$rank]/$constant-25.00);
 
117
      }
 
118
   }
 
119
 
 
120
   print "\n";
 
121
}