(*------------------------------------------------------------
TEAM21
A
234
Target: BDS2006; Platform: Win32; Comment: Borland Developer Studio 2006
------------------------------------------------------------*)
// POVT bot for "Breakthrough" game
// Ural Championship 2010 Game Tour
// Author: POVT team

{$apptype console}
program POVTBot;

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 := 56674567;
	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 getPrice(x, y, ox, oy : integer) : integer;
var price : integer;
    can : Boolean;
begin
    // position
    if currentColor = WHITE then begin
        price := x * 10;
    end else begin
        price := (BOARD_SIZE - x - 1) * 10;
    end;

    // rubka
    if currentColor = WHITE then begin
        if board[x,y] = BLACK then
            Inc(price, (BOARD_SIZE - x - 1) * 15); // TODO constant

    end else begin
        if board[x,y] = WHITE then
            Inc(price, x * 15); // TODO constant
        
    end;


    
    // no wai~
    if currentColor = WHITE then begin
        // have podkrepl
        can := false;
        if (board[x-1, y-1] = WHITE) and ((ox <> x-1) or (oy <> y-1)) then begin
            can := true;
        end;
        if (board[x-1, y+1] = WHITE) and ((ox <> x-1) or (oy <> y+1)) then begin
            can := true;
        end;

        if not can then begin
            // r
            if (y + 1 <= BOARD_SIZE - 1) then begin
                if (board[x+1, y+1] = BLACK) then
                    if not(x in [0..2]) then price := 0
                 else Dec(price, 5);
            end;
            // l
            if (y - 1 >= 0)  then
                if (board[x+1, y-1] = BLACK) then
                    if not(x in [0..2]) then price := 0
                else Dec(price, 5);

        end;


    end else begin

        // est' podkrepl
        can := false;
        if (board[x+1, y-1] = BLACK) and ((ox <> x+1) or (oy <> y-1)) then begin
            can := true;
        end;
        if (board[x+1, y+1] = BLACK) and ((ox <> x+1) or (oy <> y+1)) then begin
            can := true;
        end;

        if not can then begin
            // r
            if (y + 1 <= BOARD_SIZE - 1) then begin
                if (board[x-1, y+1] = WHITE) then
                    if not(x in [BOARD_SIZE - 3..BOARD_SIZE - 1]) then price := 0
                else Dec(price, 5);

            end;
            // l
            if (y - 1 >= 0)  then
                if (board[x-1, y-1] = WHITE) then
                    if not(x in [BOARD_SIZE - 3..BOARD_SIZE - 1]) then price := 0
                else Dec(price, 5);

        end;


    end;


    // win
    if ((currentColor = WHITE) and (x = BOARD_SIZE - 1)) or
       ((currentColor = BLACK) and (x = 0))
       then
       Inc(price, 100000);


    {
    if currentColor = WHITE then begin

    end else begin


    end;
    }

    getPrice := price;

end;



function GetMove : GameMove;
	var	moves : array [0 .. 3 * BOARD_SIZE * BOARD_SIZE - 1] of GameMove;
      movePrice : array [0 .. 3 * BOARD_SIZE * BOARD_SIZE - 1] of integer;
		  moves_count : integer;
		  sx, sy, dy, ex, ey : integer;
      bestMove, i, j, t_price, k: integer;
      t_move : GameMove;

      enemyMin, enemyMinNum, enemyNow : 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 begin
          // gen
					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);

              // calc
              movePrice[moves_count] := getPrice(ex, ey, sx, sy);

              inc(moves_count);
						end;
					end;
        end;

    //sort
    for i := 0 to moves_count - 2 do
      for j := i + 1 to moves_count - 1 do
        if movePrice[i]<movePrice[j] then begin
          t_price := movePrice[i];
          movePrice[i] := movePrice[j];
          movePrice[j] := t_price;
          t_move := moves[i];
          moves[i] := moves[j];
          moves[j] := t_move
        end;


    // get move    
    //kol-vo max
    k := 1;
    while movePrice[0] = movePrice[k] do inc(k);
    // berem samulO pustulO kolonky
    enemyMinNum := -1;
    if currentColor = WHITE then begin
        enemyMin := 23434;
        for i := 0 to k-1 do begin
            enemyNow := 0;
            for j := moves[i].ex + 1 to BOARD_SIZE - 1 do begin
                if board[j, moves[i].ey] = BLACK then begin
                    Inc(enemyNow);
                end;
            end;
            if enemyNow < enemyMin then begin
                enemyMin := enemyNow;
                enemyMinNum := i;
            end;
        end;
    end else begin
        enemyMin := 23434;
        for i := 0 to k-1 do begin
            enemyNow := 0;
            for j := 0 to moves[i].ex - 1 do begin
                if board[j, moves[i].ey] = WHITE then begin
                    Inc(enemyNow);
                end;
            end;
            if enemyNow < enemyMin then begin
                enemyMin := enemyNow;
                enemyMinNum := i;
            end;
        end;
    end;

    GetMove := moves[enemyMinNum];
	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('POVTBot');
			Flush(Output);
			continue;
		end;
		if(command <> 'Start') then
			MakeMove(ParseGameMove(command));
		move := GetMove;
		MakeMove(move);
		Writeln(GameMoveToString(move));
		Flush(Output);
	end;
end.
