Robot Battle Perl round robin script
1 #!/bin/perl -w
2
3 # Create list of bots in current dir.
4 $i = 0;
5 opendir(DIRHANDLE, ".") or die "couldn't open . : $!";
6 while ( defined ($filename = readdir(DIRHANDLE)) ) {
7 if ($filename =~ /\.prg$/i){
8 $bots[$i][0] = $filename;
9 $i++;
10 }
11 }
12 closedir(DIRHANDLE);
13
14 # Second array for 'vs' matching
15 @bots2 = @bots;
16
17 if(1==1){
18 # display list of bots
19 print "List of ", scalar(@bots), " bots to compete:\n";
20 $x = 0;
21 while($x < $i){
22 print $bots2[$x][0], "\n";
23 $x++;
24 }}
25
26 $a=0;
27 $s=0;
28 # score array: full score line, bot name, 3 scores. so 5 elements.
29 #$scores[0][0]
30 # match bots together in a round robin tourny
31 print "\nRun round-robin with all bots...\n";
32 for($x = 0, $i2 = $i; $x < $i;$x++){
33
34 # remove first bot from second array each iteration
35 shift @bots2;
36 $i2--;
37
38 for($x2 = 0; $x2 < $i2;$x2++){
39 # here we would run the actual RB program, currently
40 # just print the command
41 print "SYSTEM: linrob $bots[$x][0] \t$bots2[$x2][0]\n";
42 system("rm score.log");
43 system("./linrob -width 700 -height 700 -games 5 $bots[$x][0] $bots2[$x2][0] > output.txt\n");
44
45 open(SCORE, "score.log");
46 while(<SCORE>){
47
48 #print "\t" . $_ . "";
49 if($_ =~ /$bots[$x][0]/){
50 chomp;
51 print "=====" . $_ . $s . "====\n";
52 $scores[$s][0] = $_;
53 $scores[$s][1] = $bots[$x][0];
54 $' =~ /,/;
55 $' =~ /,/;
56 $scores[$s][2] = $`;
57 $' =~ /,/;
58 $' =~ /,/;
59 $scores[$s][3] = $`;
60 $' =~ /,/;
61 chomp($scores[$s][4] = $');
62 $s++;
63 }
64 if($_ =~ /$bots2[$x2][0]/){
65 chomp;
66 print "=====" . $_ . $s . "====\n";
67 $scores[$s][0] = $_;
68 $scores[$s][1] = $bots2[$x2][0];
69 $' =~ /,/;
70 $' =~ /,/;
71 $scores[$s][2] = $`;
72 $' =~ /,/;
73 $' =~ /,/;
74 $scores[$s][3] = $`;
75 $' =~ /,/;
76 chomp($scores[$s][4] = $');
77 $s++;
78 }
79 }
80 close(SCORE);
81
82
83 $a++;
84 }
85
86 }
87 $scores_ct = @scores;
88 print "\n###", $a, " matches!", $scores_ct, "scores \n";
89
90 $x = 0;
91 while($x < $scores_ct){
92 print "$scores[$x][1] scores:\n";
93 print " $scores[$x][2] $scores[$x][3] $scores[$x][4] \n";
94 $x++;
95 }