Пример #1
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);
            }
        }
Пример #3
0
        /// <summary>
        /// CanJump - Может прыгать
        /// Check the piece at the postion can jump any pieces on the board
        /// Проверьте фигуру в позиции, можете прыгать любые фигуры на доске
        /// </summary>
        /// <param name="board">
        /// the board state
        /// состояние правления
        /// </param>
        /// <param name="row">
        /// the row the piece is on
        /// строка, на которой стоит произведение
        /// </param>
        /// <param name="col">
        /// the column the piece is on
        /// колонка, на которой находится произведение
        /// </param>
        /// <param name="verticalDirection">
        /// the vertical direction to move in
        /// вертикальное направление движения
        /// </param>
        /// <param name="horizontalDirection">
        /// the horizontal direction to move in
        /// горизонтальное направление движения
        /// </param>
        /// <returns><code>true</code>
        /// if the piece can make a jump
        /// если кусок может совершить прыжок
        /// </returns>
        private static bool CanJump(IBoard board, int row, int col, int verticalDirection, int horizontalDirection)
        {
            int   newRow = row + verticalDirection;
            int   newCol = col + horizontalDirection;
            Piece piece  = board[row, col];

            if (!InBounds(newRow, newCol, board))
            {//not within board bounds
                return(false);
            }

            if (BoardUtilities.AreOpponents(piece, board[newRow, newCol]))
            {// check if you can jump enemy
                int endRow = newRow + verticalDirection;
                int endCol = newCol + horizontalDirection;
                return(InBounds(endRow, endCol, board) && BoardUtilities.IsEmpty(board[endRow, endCol]));
            }

            return(false);
        }