Пример #1
0
        public static Move ReadMove(TextReader reader, TextWriter writer, bool retry)
        {
            String input     = reader.ReadLine();
            bool   validMove = false;
            Move   move      = null;

            do
            {
                String[] positions = input.Trim().Split(' ');
                move = new Move();

                foreach (String position in positions)
                {
                    if (!String.IsNullOrEmpty(position))
                    {
                        int numericPosition;
                        if (int.TryParse(position, out numericPosition))
                        {
                            move.AddMoves(numericPosition);
                        }
                        else
                        {
                            validMove = false;
                            writer.WriteLine("Error parsing move.  Moves should be numeric");
                            break;
                        }
                    }
                }
                validMove = true;
            } while (!validMove && retry);

            return(move);
        }
Пример #2
0
        /// <summary>
        /// GetCaptures - Получить захваты
        /// Generate captures list for the piece at the given location
        /// Создать список снимков для произведения в заданном месте
        /// </summary>
        /// <param name="moves">
        /// stores the list of moves generated
        /// сохраняет список сгенерированных ходов
        /// </param>
        /// <param name="locations">
        /// list of parent locations
        /// список родительских локаций
        /// </param>
        /// <param name="board">
        /// the board state
        ///  состояние правления
        /// </param>
        /// <param name="piece">
        /// the piece
        /// кусок
        /// </param>
        /// <param name="row">
        /// the row of the piece
        ///  строка произведения
        /// </param>
        /// <param name="col">
        /// the column of the piece
        /// столбец произведения
        /// </param>
        /// <param name="dx">
        /// the horizontal direction
        ///  горизонтальное направление
        /// </param>
        /// <param name="dy">
        /// the vertical direction
        /// вертикальное направление
        /// </param>
        /// <returns><c>true</c>
        /// if capture available
        /// если захват доступен
        /// </returns>
        private static bool GetCaptures(ICollection <Move> moves, IList <Location> locations, IBoard board, Piece piece, int row, int col, int dx, int dy)
        {
            int endRow  = row + dy * 2;
            int endCol  = col + dx * 2;
            int jumpRow = row + dy;
            int jumpCol = col + dx;

            // jump available
            // прыжок доступен
            if (InBounds(endRow, endCol, board) && BoardUtilities.AreOpponents(piece, board[jumpRow, jumpCol]) && BoardUtilities.IsEmpty(board[endRow, endCol]))
            {
                locations.Add(new Location(endRow, endCol));
                board[row, col]         = Piece.None;
                board[jumpRow, jumpCol] = Piece.None;
                board[endRow, endCol]   = piece;

                bool  captureAvailable = false;
                int[] DIRECTIONS       = { -1, 1 }; // {down/right, up/left} // {вниз / вправо, вверх / влево}
                int   Y_START_INDEX    = (BoardUtilities.IsKing(piece) || BoardUtilities.IsWhite(piece)) ? 0 : 1;
                int   Y_END_INDEX      = (BoardUtilities.IsKing(piece) || BoardUtilities.IsBlack(piece)) ? 1 : 0;

                for (int idxY = Y_START_INDEX; idxY <= Y_END_INDEX; idxY++)
                {
                    for (int idxX = 0; idxX < DIRECTIONS.Length; idxX++)
                    {
                        bool result = GetCaptures(
                            moves, new List <Location>(locations),
                            board.Copy(), piece,
                            endRow, endCol, DIRECTIONS[idxX], DIRECTIONS[idxY]
                            );
                        captureAvailable = captureAvailable || result;
                    }
                }


                if ((!captureAvailable) && (locations.Count > 1))
                {
                    Move move = new Move();
                    foreach (Location location in locations)
                    {
                        move.AddMoves(location);
                    }

                    moves.Add(move);
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }
        /// <summary>Generate captures list for the piece at the given location</summary>
        /// <param name="moves">stores the list of moves generated</param>
        /// <param name="locations">list of parent locations</param>
        /// <param name="board">the board state</param>
        /// <param name="piece">the piece</param>
        /// <param name="row">the row of the piece</param>
        /// <param name="col">the column of the piece</param>
        /// <param name="dx">the horizontal direction</param>
        /// <param name="dy">the vertical direction</param>
        /// <returns><c>true</c> if capture available</returns>
        private static bool GetCaptures(ICollection <Move> moves, IList <Location> locations, IBoard board, Piece piece, int row, int col, int dx, int dy)
        {
            int endRow  = row + dy * 2;
            int endCol  = col + dx * 2;
            int jumpRow = row + dy;
            int jumpCol = col + dx;

            // jump available
            if (InBounds(endRow, endCol, board) && BoardUtilities.AreOpponents(piece, board[jumpRow, jumpCol]) && BoardUtilities.IsEmpty(board[endRow, endCol]))
            {
                locations.Add(new Location(endRow, endCol));
                board[row, col]         = Piece.None;
                board[jumpRow, jumpCol] = Piece.None;
                board[endRow, endCol]   = piece;

                bool  captureAvailable = false;
                int[] directions       = { -1, 1 }; // {down/right, up/left}
                int   yStartIndex      = (BoardUtilities.IsKing(piece) || BoardUtilities.IsWhite(piece)) ? 0 : 1;
                int   yEndIndex        = (BoardUtilities.IsKing(piece) || BoardUtilities.IsBlack(piece)) ? 1 : 0;

                for (int idxY = yStartIndex; idxY <= yEndIndex; idxY++)
                {
                    foreach (int t in directions)
                    {
                        bool result = GetCaptures(
                            moves, new List <Location>(locations),
                            board.Copy(), piece,
                            endRow, endCol, t, directions[idxY]
                            );
                        captureAvailable = captureAvailable || result;
                    }
                }


                if ((!captureAvailable) && (locations.Count > 1))
                {
                    Move move = new Move();
                    foreach (Location location in locations)
                    {
                        move.AddMoves(location);
                    }

                    moves.Add(move);
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }