/*------------------------------------------------------------
TEAM16
A
226
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;

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

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

    private void Run()
    {
        while (true)
        {
            string command = Console.ReadLine();
            if (command == "Quit")
                break;
            if (command == "Name")
            {
                Console.WriteLine("OSU_Stronghold");
                Console.Out.Flush();
                continue;
            }
            if (command == "Start")
                GameMove.st = 1;
            if (command != "Start")
            {
                MakeMove(GameMove.Parse(command));
            }
            GameMove move = new GameMove(0, 0, 1, 1);
            switch (GameMove.st)
            {
                case 0: move = GetBlackMove(); break;
                case 1: move = GetWhiteMove(); break;
            }
            MakeMove(move);
            Console.WriteLine(move.ToString());
            Console.Out.Flush();
        }
    }

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

    private GameMove GetBlackMove()
    {
        /*
         * Атакована клетка или нет
         */
        for (int i = 0; i < 8; i++)
            for (int j = 0; j < 8; j++)
                atack[i, j] = false;
        for (int i = 0; i < 8; i++)
            for (int j = 0; j < 8; j++)
            {
                if (board[i, j] == Cell.WHITE)
                {
                    if(j<7)
                        atack[i + 1, j + 1] = true;
                    if(j>0)
                        atack[i + 1, j - 1] = true;
                }
            }

        /*
         * Баланс
        for (int i = 0; i < 8; i++)
            for (int j = 0; j < 8; j++)
                balance[j] = false;
        for (int i = 0; i < 8; i++)
            for (int j = 0; j < 8; j++)
            {
                if (board[i, j] == Cell.WHITE)
                {
                    if (j < 7)
                        atack[i + 1, j + 1] = true;
                    if (j > 0)
                        atack[i + 1, j - 1] = true;
                }
            }

         */

        /*
         * Есть ли рубить
         */
        for (int sx = BOARD_SIZE-1; sx >0; sx--)
            for (int sy = 0; sy < BOARD_SIZE; sy++)
            {
                if (board[sx, sy] == currentColor)
                {
                    if(sy<7&&(board[sx-1, sy+1]==Cell.WHITE))
                        return new GameMove(sx, sy, sx - 1, sy + 1);
                    if (sy>0&&(board[sx - 1, sy - 1] == Cell.WHITE))
                        return new GameMove(sx, sy, sx - 1, sy - 1);
                }

            }

        /*
         * Прорыв
         */
        for (int sx = 1; sx <= 3; sx++)
            for (int sy = 0; sy < BOARD_SIZE; sy++)
            {
                if (board[sx, sy] == currentColor)
                {
                    if ((board[sx - 1, sy] == Cell.EMPTY) && !atack[sx - 1, sy])
                        return new GameMove(sx, sy, sx - 1, sy);
                    if (((sy +1)< BOARD_SIZE) && (board[sx - 1, sy + 1] == Cell.EMPTY) && !atack[sx - 1, sy + 1])
                        return new GameMove(sx, sy, sx - 1, sy+1);
                    if ((sy  > 0) && (board[sx - 1, sy - 1] == Cell.EMPTY) && !atack[sx - 1, sy - 1])
                        return new GameMove(sx, sy, sx - 1, sy-1);
                }

            }

        /*
         * Устранение дыр
         */
        for (int sx = BOARD_SIZE - 1; sx > 1; sx--)
            for (int sy = 0; sy < BOARD_SIZE; sy++)
            {
                if (board[sx, sy] == currentColor)
                {
                    if (
                        (sy == 1 || sy == 3 || sy == 4 || sy == 6) &&
                        ((board[sx - 1, sy] == Cell.EMPTY) 
                            &&
                        (board[sx - 2, sy] == currentColor)) 
                        && !atack[sx - 1, sy])
                        return new GameMove(sx, sy, sx - 1, sy);
                }
            }

        /*
         * Выравнивание строя
         */
        for (int sx = BOARD_SIZE-1; sx >0; sx--)
            for (int sy = 0; sy < BOARD_SIZE; sy++)
            {
                if (board[sx, sy] == currentColor)
                {
                    if ((sy == 0||sy==2) && (board[sx - 1, sy + 1] != currentColor)&& !atack[sx - 1, sy + 1])
                        return new GameMove(sx, sy, sx - 1, sy + 1);
                    if ((sy == 7 || sy == 5) && (board[sx - 1, sy - 1] != currentColor) && !atack[sx - 1, sy - 1])
                        return new GameMove(sx, sy, sx - 1, sy - 1);
                }
            }
        for (int sx = BOARD_SIZE - 1; sx > 0; sx--)
            for (int sy = 0; sy < BOARD_SIZE; sy++)
            {
                if (board[sx, sy] == currentColor)
                {
                    if ((sy == 0 || sy == 2) && (board[sx - 1, sy] == Cell.EMPTY) && !atack[sx - 1, sy])
                        return new GameMove(sx, sy, sx - 1, sy);
                    if ((sy == 7 || sy == 5) && (board[sx - 1, sy] == Cell.EMPTY) && !atack[sx - 1, sy])
                        return new GameMove(sx, sy, sx - 1, sy );
                }
            }

        /*
         * Продвижение вредёд
         */
        for (int sx = BOARD_SIZE - 1; sx > 0; sx--)
            for (int sy = 0; sy < BOARD_SIZE; sy++)
            {
                if (board[sx, sy] == currentColor)
                {
                    if ((sy == 1 || sy == 3) && (board[sx - 1, sy - 1] != currentColor) && !atack[sx - 1, sy-1])
                        return new GameMove(sx, sy, sx - 1, sy - 1);
                    if ((sy == 4 || sy == 6) && (board[sx - 1, sy + 1] != currentColor) && !atack[sx - 1, sy-1])
                        return new GameMove(sx, sy, sx - 1, sy + 1);
                }
            }


        /*
         * Великий рандом
         */

        List<GameMove> moves = new List<GameMove>();
        for (int sx = 0; sx < BOARD_SIZE; sx++)
        {
            for (int sy = 0; sy < BOARD_SIZE; sy++)
            {
                if (board[sx, sy] == currentColor)
                {
                    for (int dy = -1; dy <= 1; dy++)
                    {
                        int ex = sx + (currentColor == Cell.WHITE ? 1 : -1);
                        int ey = sy + dy;
                        if ((ex >= 0 && ex < BOARD_SIZE && ey >= 0 && ey < BOARD_SIZE) &&
                           (dy != 0 && board[ex, ey] != currentColor || board[ex, ey] == Cell.EMPTY))
                            if (board[ex, ey] == Cell.BLACK)
                            {
                                return new GameMove(sx, sy, ex, ey);
                            }
                            else
                            {
                                moves.Add(new GameMove(sx, sy, ex, ey));
                            }
                    }
                }
            }
        }
        return moves[random.Next(moves.Count)];


    }

    private GameMove GetWhiteMove()
    {
        for (int i = 0; i < 8; i++)
            for (int j = 0; j < 8; j++)
                atack[i, j] = false;
        for (int i = 0; i < 8; i++)
            for (int j = 0; j < 8; j++)
            {
                if (board[i, j] == Cell.BLACK)
                {
                    if (j < 7)
                        atack[i - 1, j + 1] = true;
                    if (j > 0)
                        atack[i - 1, j - 1] = true;
                }
            }

        for (int sx = 0; sx <7; sx++)
            for (int sy = 0; sy < BOARD_SIZE; sy++)
            {
                if (board[sx, sy] == currentColor)
                {
                    if (sy < 7 && (board[sx + 1, sy + 1] == Cell.BLACK))
                        return new GameMove(sx, sy, sx + 1, sy + 1);
                    if (sy > 0 && (board[sx + 1, sy - 1] == Cell.BLACK))
                        return new GameMove(sx, sy, sx + 1, sy - 1);
                }

            }

        /*
         * Прорыв
         */
        for (int sx = 7; sx >=4; sx--)
            for (int sy = 0; sy < BOARD_SIZE; sy++)
            {
                if (board[sx, sy] == currentColor)
                {
                    if ((board[sx + 1, sy] == Cell.EMPTY) && !atack[sx + 1, sy])
                        return new GameMove(sx, sy, sx + 1, sy);
                    if (((sy + 1) < BOARD_SIZE) && (board[sx + 1, sy + 1] == Cell.EMPTY) && !atack[sx + 1, sy + 1])
                        return new GameMove(sx, sy, sx + 1, sy + 1);
                    if ((sy > 0) && (board[sx + 1, sy - 1] == Cell.EMPTY) && !atack[sx + 1, sy - 1])
                        return new GameMove(sx, sy, sx + 1, sy - 1);
                }

            }


        for (int sx = 0; sx < 6; sx++)
            for (int sy = 0; sy < BOARD_SIZE; sy++)
            {
                if (board[sx, sy] == currentColor)
                {
                    if (
                        (sy == 1 || sy == 3 || sy == 4 || sy == 6)
                            && 
                         ((board[sx + 1, sy] == Cell.EMPTY)
                            &&
                        (board[sx + 2, sy] == currentColor))
                            && 
                         !atack[sx + 1, sy])
                        return new GameMove(sx, sy, sx + 1, sy);
                }
            }

        for (int sx = 0; sx < 7; sx++)
            for (int sy = 0; sy < BOARD_SIZE; sy++)
            {
                if (board[sx, sy] == currentColor)
                {
                    if ((sy == 0 || sy == 2) && (board[sx + 1, sy + 1] != currentColor) && !atack[sx + 1, sy + 1])
                        return new GameMove(sx, sy, sx + 1, sy + 1);
                    if ((sy == 7 || sy == 5) && (board[sx + 1, sy - 1] != currentColor) && !atack[sx + 1, sy - 1])
                        return new GameMove(sx, sy, sx + 1, sy - 1);
                }
            }
        for (int sx = 0; sx < 7; sx++)
            for (int sy = 0; sy < BOARD_SIZE; sy++)
            {
                if (board[sx, sy] == currentColor)
                {
                    if ((sy == 0 || sy == 2) && (board[sx + 1, sy] == Cell.EMPTY) && !atack[sx + 1, sy])
                        return new GameMove(sx, sy, sx + 1, sy);
                    if ((sy == 7 || sy == 5) && (board[sx + 1, sy] == Cell.EMPTY) && !atack[sx + 1, sy])
                        return new GameMove(sx, sy, sx + 1, sy);
                }
            }
        for (int sx = 0; sx < 7; sx++)
            for (int sy = 0; sy < BOARD_SIZE; sy++)
            {
                if (board[sx, sy] == currentColor)
                {
                    if ((sy == 1 || sy == 3) && (board[sx + 1, sy - 1] != currentColor) && !atack[sx + 1, sy - 1])
                        return new GameMove(sx, sy, sx + 1, sy - 1);
                    if ((sy == 4 || sy == 6) && (board[sx + 1, sy + 1] != currentColor) && !atack[sx + 1, sy - 1])
                        return new GameMove(sx, sy, sx + 1, sy + 1);
                }
            }


        List<GameMove> moves = new List<GameMove>();
        for (int sx = 0; sx < BOARD_SIZE; sx++)
        {
            for (int sy = 0; sy < BOARD_SIZE; sy++)
            {
                if (board[sx, sy] == currentColor)
                {
                    for (int dy = -1; dy <= 1; dy++)
                    {
                        int ex = sx + (currentColor == Cell.WHITE ? 1 : -1);
                        int ey = sy + dy;
                        if ((ex >= 0 && ex < BOARD_SIZE && ey >= 0 && ey < BOARD_SIZE) &&
                           (dy != 0 && board[ex, ey] != currentColor || board[ex, ey] == Cell.EMPTY))
                            if (board[ex, ey] == Cell.BLACK)
                            {
                                return new GameMove(sx, sy, ex, ey);
                            }
                            else
                            {
                                moves.Add(new GameMove(sx, sy, ex, ey));
                            }
                    }
                }
            }
        }
        return moves[random.Next(moves.Count)];


    }

    private readonly Random random;
    private readonly Cell[,] board;
    private Cell currentColor;
    private const int BOARD_SIZE = 8;
    public bool[,] atack;
    private enum Cell
    {
        EMPTY,
        WHITE,
        BLACK
    }

    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;
        }

        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}", (char)(sy + 'a'), sx + 1, (char)(ey + 'a'), ex + 1);
        }

        public static int st = 0;

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