#!/usr/bin/perl
use warnings;
use strict;

my $code=shift @ARGV;
$code=<> unless $code;
chomp($code);

my @grid=();
my @cellbg=();

foreach my $x (1..8)
{
  foreach my $y (1..9)
  {
    $grid[$x][$y]="&nbsp;";
    $cellbg[$x][$y]="";
  }
}

my $turn=1;
my $max=7;

sub this_cell($$)
{
  my ($turn,$x)=@_;
  my $char=($turn%2)?'o':'x';
  my $col=($x==$turn)?'ff':'80';
  my $cellcol=($turn%2)?"0000$col":"${col}0000";
  my $bgcol = ($x==$turn)?' style="background-color:#c0c0c0"':"";
  return("<font color=$cellcol>$char</font>",$bgcol);
}

foreach (split(//,$code))
{
  foreach my $x ($turn..8)
  {
    ($grid[$x][$_],$cellbg[$x][$_])=this_cell($turn,$x);
  }
  $turn++;
}

# Is there a cheat move?
foreach (@ARGV)
{
  $max=8;
  my ($f,$t)=($_=~/(\d)-(\d)/);
  $grid[8][$t]=$grid[7][$f];
  $grid[8][$f]=" ";
  $cellbg[8][$t]=' style="background-color:#d0d000"';
  $cellbg[8][$f]=' style="background-color:#d0d000"';
}

print "<table cols=$max border=1 cellpadding=8px bordercolor=#0000ff>\n";
print "<tr>";
foreach (1..$max)
{
  print "<th valign=bottom align=center>$_</th>\n";
}
print "</tr>\n<tr>";
my $td="td align=center";
foreach (1..$max)
{
  print "<td>\n  <table border=1>\n";
  print "    <tr><$td$cellbg[$_][1]>$grid[$_][1]</td><$td$cellbg[$_][2]>$grid[$_][2]</td><$td$cellbg[$_][3]>$grid[$_][3]</td></tr>\n";
  print "    <tr><$td$cellbg[$_][4]>$grid[$_][4]</td><$td$cellbg[$_][5]>$grid[$_][5]</td><$td$cellbg[$_][6]>$grid[$_][6]</td></tr>\n";
  print "    <tr><$td$cellbg[$_][7]>$grid[$_][7]</td><$td$cellbg[$_][8]>$grid[$_][8]</td><$td$cellbg[$_][9]>$grid[$_][9]</td></tr>\n";
  print "  </table>\n</td>\n";
}
print "</tr></table>\n";
