#!/usr/bin/perl

my @board;
for $i (0 .. 18) {
	$board[$i] = [ split / /, ('. ' x 19) ];
}

my %letter = (
	"Benjamin" => B,
	"Blest Lax Monk Pal" => W,
	"Dave" => D,
	"Jeff Schroeder" => S,
	"Joel" => J,
	"Joerg" => G,
	"M'cachessilnath" => M,
	"Poulenc" => C,	
	"PurpleBob" => P,
	"The Kid" => K,
	"Zagarna" => Z
);

my %name = reverse %letter;
my %allies;

my $data = join '', <STDIN>;
$data =~ s/\nnweek \d+\n{2}//gs;
$data =~ s/^.+?\t//gm;

my @datum = split /\n/, $data;
my @offer = ();

print textboard(@board), "\n";

foreach (@datum) {
	my ($move, $name) = split /\t/;
	
	if ($move =~ m/.o./) { push @offer, $move; }
	elsif (my ($a, $b) = $move =~ m/(.)a(.)/) {
		$allies{"$name"} .= $b;
		$allies{"$name{$b}"} .= $a;

 		@offer = grep !/${b}o$a/, @offer;
	}
	else {
		my ($col, $row) = unpack 'aa2', $move;
		$col = ord($col)-96;

		$board[$row-1][$col-1] = $letter{$name};
		print textboard(@board), "\n";

		print "Active Alliances:\n";
		while (my ($key, $val) = each %allies) {
			print $letter{$key}, "  $key: $val\n";
		}

		print "\nOffered Alliances:\n";
	
		foreach (@offer) {
			my ($a, $b) = m/(.)o(.)/;
			print "$a  $name{$a}: $b\n";
		}

		print "\n";
	}
}

sub textboard (@) {
	my @board = @_;
	my ($i, $j);

	my $out = '';

	for $i (0 .. 19) {
		for $j (0 .. 19) {
			if ($i) {
				if ($j) { $out .= "$board[$i-1][$j-1] "; }
				else { $out .= sprintf("%2d ", $i); }
			}
			elsif ($j) { $out .= chr($j+96) . ' '; }
			else { $out .= "   "; }
		}
		$out .= "\n";
	}

	return $out;
}
