(*------------------------------------------------------------
TEAM39
A
221
Target: BDS2006; Platform: Win32; Comment: Borland Developer Studio 2006
------------------------------------------------------------*)
// Random bot for "Breakthrough" game
// Ural Championship 2010 Game Tour
// Author: Alexander Ipatov

{$apptype console}
program HeHey;

const BOARD_SIZE = 8;
type

	TCell = (EMPTY, WHITE, BLACK);

	GameMove = record
		sx, sy, ex, ey : integer;
	end;

var
	board : array[0 .. BOARD_SIZE - 1, 0 .. BOARD_SIZE - 1] of TCell;
	currentColor : TCell;

	function CreateGameMove(sx, sy, ex, ey : integer) : GameMove;
	var	res : GameMove;
	begin
		res.sx := sx;
		res.sy := sy;
		res.ex := ex;
		res.ey := ey;
		CreateGameMove := res;
	end;

	function ParseGameMove(move : string) : GameMove;
	begin
		ParseGameMove := CreateGameMove(ord(move[2]) - ord('1'), ord(move[1]) - ord('a'), ord(move[4]) - ord('1'), ord(move[3]) - ord('a'));
	end;

	function GameMoveToString (move : GameMove) : string;
	begin
		GameMoveToString := char(ord('a') + move.sy) + char(ord('1') + move.sx) + char(ord('a') + move.ey) + char(ord('1') + move.ex);
	end;

	procedure Init;
	var	cell : TCell;
		x , y : integer;
	begin
		for x := 0 to BOARD_SIZE - 1 do
		begin
			if (x <= 1) then
				cell := WHITE
			else
			if (x >= BOARD_SIZE - 2) then
				cell := BLACK
			else
				cell := EMPTY;
			for y := 0 to BOARD_SIZE - 1 do
				board[x, y] := cell;
		end;
		currentColor := WHITE;
		RandSeed := 314159265;
	end;

	procedure MakeMove(move : GameMove);
	begin
		board[move.ex, move.ey] := board[move.sx, move.sy];
		board[move.sx, move.sy] := EMPTY;
		if (currentColor = WHITE) then
			currentColor := BLACK
		else
			currentColor := WHITE;
	end;

  function getCell(cx,cy: integer) : TCell;
  begin
    if ((cx>=0)and (cy>=0) and
        (cx<BOARD_SIZE)and(cy<BOARD_SIZE))
        then  getCell:=board[cx,cy]
        else  getCell:= EMPTY;

  end;

  function checkmove(sx, sy, ex, ey:integer):integer;
    var prio:integer;
      koof:shortint;
      EnemyColor:TCell;
  begin
    koof:=-1;
    EnemyColor:=WHITE;
    if board[sx,sy]=WHITE then
    begin
      koof:=1;
      EnemyColor:=BLACK;
    end;

    prio:=0;
    if ((ey=0) and(currentColor=BLACK))
    then
       prio:=1000;

    if (ey=1)then
      if (currentColor=BLACK)then
       prio:=500
      else
       prio:=-200;

    if (ey=BOARD_SIZE-2)then
      if(currentColor=WHITE) then
       prio:=500
      else
      prio:=-200;

    if (ey=BOARD_SIZE-1)and(currentColor=WHITE)
      then
         prio:=1000;

    if((board[ex,ey]=EMPTY) and
        (getCell(ex+koof,ey+koof)=EMPTY) and
        (getCell(ex-koof,ey+koof)=EMPTY))then
      prio:=prio+200;

    if((board[ex,ey]=EMPTY) and(
        (getCell(ex+koof,ey+koof)=EnemyColor) or
        (getCell(ex-koof,ey+koof)=EnemyColor)))then
      prio:=prio-450;

    if((board[ex,ey]=EnemyColor) and
        (getCell(ex+koof,ey+koof)<>EnemyColor) and
        (getCell(ex-koof,ey+koof)<>EnemyColor))then
      prio:=prio+300;

    if((board[ex,ey]=EnemyColor) and(
        (getCell(ex+koof,ey+koof)=EnemyColor) or
        (getCell(ex-koof,ey+koof)=EnemyColor)))then
      prio:=prio-450;

   if(  (getCell(sx+koof,sy+koof)=EnemyColor) or
        (getCell(sx-koof,sy+koof)=EnemyColor))then
      prio:=prio +650;

     checkmove:=prio;
  end;

	function HeHeyGameMove : GameMove;
	var	moves : array [0 .. 3 * BOARD_SIZE * BOARD_SIZE - 1] of GameMove;
    priority : array[0 .. 3 * BOARD_SIZE * BOARD_SIZE - 1] of integer;
		moves_count, mx, i : integer;
		sx, sy, dy, ex, ey : integer;
	begin
		moves_count := 0;
		for sx := 0 to BOARD_SIZE - 1 do
			for sy := 0 to BOARD_SIZE - 1 do
				if(board[sx, sy] = currentColor) then
					for dy := -1 to 1 do
					begin
						if (currentColor = WHITE) then
							ex := sx + 1
						else
							ex := sx - 1;
						ey := sy + dy;
						if (ex >= 0) and (ex < BOARD_SIZE) and (ey >= 0) and (ey < BOARD_SIZE) and
							 (((dy <> 0) and (board[ex, ey] <> currentColor)) or (board[ex, ey] = EMPTY)) then
						begin
							moves[moves_count] := CreateGameMove(sx, sy, ex, ey);
              priority[moves_count]:=checkmove(sx, sy, ex, ey);
							inc(moves_count);
						end;
					end;
          mx:=0;
          for I := 0 to moves_count - 1 do
            if priority[mx]<priority[i] then mx:=i;
		HeHeyGameMove := moves[mx];
	end;

var	command : string;
	move : GameMove;
begin
	Init;
	while (true) do
	begin
		readln (command);
		if(command = 'Quit') then
			break;
		if(command = 'Name') then
		begin
			Writeln('HeHey');
			Flush(Output);
			continue;
		end;
		if(command <> 'Start') then
			MakeMove(ParseGameMove(command));
		move := HeHeyGameMove;
		MakeMove(move);
		Writeln(GameMoveToString(move));
		Flush(Output);
	end;
end.
