#!/usr/bin/perl

$turn = "";
$turn_name = "";

while (<>) {
    if (/^\s+(BLACK|WHITE)\s+\([*O]\).*$/) {
	if ($1 eq "BLACK") {
	    $turn = "Board.Black";
	} else {
	    $turn = "Board.White";
	}
    } elsif (/^.*([1-8]) ([*-O][*-O ]+[*-O]) \1/) {
	$rownum = $1;
	$row = $2;
	for ($i = 0; $i < length($row); $i += 2) {
	    $sq = substr($row, $i, 1);
	    $ind = ($rownum - 1) * 8 + $i/2;
	    if ($sq eq "*") {
		$brd[$ind] = "Some Board.Black";
	    } elsif ($sq eq "O") {
		$brd[$ind] = "Some Board.White";
	    } elsif ($sq eq "-") {
		$brd[$ind] = "None";
	    } else {
		die "Unexpected board square: $sq\n";
	    }
	}
    } elsif (/\s+TURN\s+A B C D E F G H\s+\[\d+\]: (..).*$/) {
	$lastmove = $1;
	if ($lastmove eq "PA") {
	    $lastmove = "Board.Pass";
	} elsif (lc($lastmove) =~ /([a-h])([1-8])/) {
	    $movenum = ($2 - 1) * 8 + (ord($1) - ord("a"));
	    $lastmove = "(Board.Square $movenum)";
	} else {
	    die "Unrecognized move: $lastmove\n";
	}
    }
}


# output info
print "let brda = [| ";
for ($i = 0; $i < 7; $i++) {
    for ($j = 0; $j < 8; $j++) {
	print $brd[$i * 8 + $j] . "; ";
    }
    print "\n";
}

# last row
for ($j = 0; $j < 7; $j++) {
    print $brd[7*8 + $j] . "; ";
}
print $brd[7*8 + 7] . " |];;\n";

print "let brd = Board.of_array brda $turn $lastmove;;\n";
print "let mp = Machine.machinePlayer 5 10 10;;\n";

if ($turn eq "Board.Black") {
    print "Othello.play mp Othello.humanPlayer brd true;;\n";
} else {
    print "Othello.play Othello.humanPlayer mp brd true;;\n";
}
