Saturday, April 19, 2008

Reversi-3x3 games

The ROOT software is great to make histograms and plots of data.

Here is a plot showing the scores of all possible games on a 3x3 board. There are 8 entries in the histogram, one for each of the possible different games. For all entries player 2 wins. In four of the possible games the board is completely filled with 9 disks of player 2 in the end (represented by the red square in the histogram). For three of the possible games player 2 has eight disks on the board in the end and one field is left empty (yellow square in the histogram). And there is only one game that ends with the board containing eight disks of player 2 and one disk of player one. None of the other combinations are possible.



And here is the ROOT macro that generated the plot:
void p(char* fname) {
gStyle->SetPalette(1);
gStyle->SetOptStat(0);
gStyle->SetCanvasColor(0);
gStyle->SetCanvasBorderMode(0);
gStyle->SetCanvasBorderSize(0);
gStyle->SetTitleFillColor(0);
gStyle->SetTitleBorderSize(1);
ifstream in;
in.open(fname);

string s;
int r,c;
long n;
TH2F *h2 = new TH2F("h2","Scores for Reversi games;Player 1; Player 2",
37,-0.5,36.5,
37,-0.5,36.5);

while (s!="Row") {
in >> s;
//cout << s << endl;
if (!in.good()) break;
}
s = "";
while (s!="Maximum") {
c = 0;
in >> r;
cout << "Reading row " << r << endl;
in >> s;
while (!(s=="Row" || s=="Maximum")) {
n = atoi(s.c_str());
h2->Fill(r,c,n);
//cout << r << c << n << endl;
++c;
in >> s;
}
}
in.close();
TCanvas* cnv = new TCanvas("c","cnv",400,400);
h2->GetXaxis()->SetRange(0,c);
h2->GetYaxis()->SetRange(0,c);
stringstream t;
int size = (int)sqrt(c);
t << "Scores for Reversi-" << size << "x" << size;
h2->SetTitle(t.str().c_str());
h2->Draw("colz");
}

No comments: