/*------------------------------------------------------------
TEAM41
A
230
Target: CS35; Platform: Win32; Comment: C Sharp (.Net 3.5)
------------------------------------------------------------*/
// Random bot for "Breakthrough" game
// Ural Championship 2010 Game Tour
// Author: Vladimir Yakovlev

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

public class Me
{
	public Me()
	{
		board = new Cell[BoardSize, BoardSize];
		for(int x = 0; x < BoardSize; x++)
		{
            Cell cell = new Cell();
            cell.Type = (x <= 1 ? CellType.WHITE : (x >= BoardSize - 2 ? CellType.BLACK : CellType.EMPTY));
			for(int y = 0; y < BoardSize; y++)
				board[x, y] = cell;
		}
		currentColor = CellType.WHITE;
		random = new Random();
	}

	private static void Main()
	{
		new Me().Run();
    }

#if NE_TROGAT
    [DllImport("user32.dll")]
    static extern int GetWindowTextA(IntPtr Wnd, StringBuilder txt, int k);
    
    [DllImport("user32.dll")]
    static extern IntPtr FindWindowA([MarshalAs(UnmanagedType.LPStr)]string cap, [MarshalAs(UnmanagedType.LPStr)]string cls);

    [DllImport("user32.dll")]
    static extern int SetWindowTextA(IntPtr Wnd, [MarshalAs(UnmanagedType.LPStr)]string cap);
#endif

    IntPtr pWnd;
    private string In()
    {
#if NE_TROGAT
        if (pWnd == IntPtr.Zero)
        {
            do
            {
                pWnd = FindWindowA(null, "Connector::");
                Thread.Sleep(100);
            }
            while (pWnd == IntPtr.Zero);

            SetWindowTextA(pWnd, "GO!");
            Thread.Sleep(100);
        }

        Thread.Sleep(20);
        StringBuilder r;
        do
        {
            r = new StringBuilder(1024);
            GetWindowTextA(pWnd, r, r.Capacity + 1);
            Thread.Sleep(20);
        } while (r.ToString() == "wait" || r.ToString() == "GO!");

        return r.ToString();
#else
        return Console.ReadLine();
#endif
    }

    private void Out(string s)
    {
        //MessageBox.Show(s);
#if NE_TROGAT
        Thread.Sleep(20);
        SetWindowTextA(pWnd, s);
        Thread.Sleep(20);
#else
        Console.WriteLine(s);
		Console.Out.Flush();
#endif
    }

	private void Run()
	{
		while(true)
		{
			string command = In();
			if(command == "Quit")
				break;
			if(command == "Name")
			{
                Out("Debug Annigilator");
				continue;
			}
			if(command != "Start")
				MakeMove(GameMove.Parse(command));
            Calc();
			GameMove move = GetRandomMove();
			MakeMove(move);
            Out(move.ToOutput());
		}
	}

    private void Calc()
    {
        for (int x = 0; x < BoardSize; x++)
        {
            for (int y = 0; y < BoardSize; y++)
            {
                int dx = currentColor == CellType.WHITE ? 1 : -1;
                board[x, y].Hit = HaveTarget(x + dx, y - 1) || HaveTarget(x + dx, y + 1);
                if (currentColor == CellType.WHITE)
                    board[x, y].Dist = x;
                else
                    board[x, y].Dist = 7 - x;
            }
        }
    }

    private bool HaveTarget(int x, int y)
    {
        if (x < 0 || y < 0) return false;
        if (x >= BoardSize || y >= BoardSize) return false;

        return board[x, y].Type != CellType.EMPTY && board[x, y].Type != currentColor;
    }

	private void MakeMove(GameMove move)
	{
		board[move.ex, move.ey].Type = board[move.sx, move.sy].Type;
		board[move.sx, move.sy].Type = CellType.EMPTY;
        currentColor = (currentColor == CellType.WHITE ? CellType.BLACK : CellType.WHITE);
	}

	private GameMove GetRandomMove()
	{
		List<GameMove> moves = new List<GameMove>();
		for(int sx = 0; sx < BoardSize; sx++)
		{
			for(int sy = 0; sy < BoardSize; sy++)
			{
				if(board[sx, sy].Type == currentColor)
				{
					for(int dy = -1; dy <= 1; dy++)
					{
                        int ex = sx + (currentColor == CellType.WHITE ? 1 : -1);
						int ey = sy + dy;
						if((ex >= 0 && ex < BoardSize && ey >= 0 && ey < BoardSize) &&
                           (dy != 0 && board[ex, ey].Type != currentColor || board[ex, ey].Type == CellType.EMPTY))
							moves.Add(new GameMove(sx, sy, ex, ey));
					}
				}
			}
		}

        moves.Sort(GameMove.Cmp);

		return moves[0];
	}

    private GameMove GetSmartMove()
    {
        return new GameMove(0, 0, 0, 0);
    }

	private readonly Random random;
	public static Cell[,] board;
    private CellType currentColor;
	private const int BoardSize = 8;

    public enum CellType
	{
		EMPTY,
		WHITE,
		BLACK
	}

    public struct Cell
    {
        public CellType Type { get; set; }
        public bool Hit { get; set; }
        public int Dist { get; set; }

        public override string ToString()
        {
            return Type.ToString() + " " + Hit.ToString() + " " + Dist.ToString();
        } 
    }

	private class GameMove
	{
		public GameMove(int sx, int sy, int ex, int ey)
		{
			this.sx = sx;
			this.sy = sy;
			this.ex = ex;
			this.ey = ey;
            Calc();
		}

		public static GameMove Parse(string move)
		{
			return new GameMove(move[1] - '1', move[0] - 'a', move[3] - '1', move[2] - 'a');
		}

		public override string ToString()
		{
			return string.Format("{0}{1}-{2}{3} {4}", (char)(sy + 'a'), sx + 1, (char)(ey + 'a'), ex + 1, W);
		}

        public string ToOutput()
        {
            return string.Format("{0}{1}{2}{3}", (char)(sy + 'a'), sx + 1, (char)(ey + 'a'), ex + 1);
        }

        private void Calc()
        {
            W = 0;
            if (!To.Hit) W += 0.4;
            if (To.Type!=CellType.EMPTY && To.Type!=From.Type) W += 0.6;
            W += To.Dist * 0.05;
            if (To.Dist == 7) W += 10;
        }

        public static int Cmp(GameMove x, GameMove y)
        {
            int r = x.W.CompareTo(y.W);
            return -r;
        }

        public Cell From
        {
            get
            {
                return Me.board[sx, sy];
            }
        }

        public Cell To
        {
            get
            {
                return Me.board[ex, ey];
            }
        }

		public readonly int sx;
		public readonly int sy;
		public readonly int ex;
		public readonly int ey;

        public double W;
	}
}
