Пример #1
0
        /// <summary>Is the move legal. This move doesn't check multiple jumps.</summary>
        /// <param name="board">the board state</param>
        /// <param name="startRow">the start row</param>
        /// <param name="startCol">the start col</param>
        /// <param name="endRow">the end row</param>
        /// <param name="endCol">the end col</param>
        /// <param name="player">the player with the turn</param>
        /// <returns>LEGAL if the move is legal.  Illegal if the move is not legal.  INCOMPLETE if the move results in a jump.</returns>
        private static MoveStatus IsMoveLegal(IBoard board, int startRow, int startCol, int endRow, int endCol, Player player)
        {
            if (!InBounds(startRow, startCol, board) || !InBounds(endRow, endCol, board))
            {
                // out of board bounds
                return(MoveStatus.Illegal);
            }

            Piece startPosition = board[startRow, startCol];
            Piece endPosition   = board[endRow, endCol];

            if ((player == Player.Black && !BoardUtilities.IsBlack(startPosition)) || (player == Player.White && !BoardUtilities.IsWhite(startPosition)))
            {
                // wrong player attempting to make a move
                return(MoveStatus.Illegal);
            }
            else if (!BoardUtilities.IsEmpty(endPosition))
            {// destination is not empty
                return(MoveStatus.Illegal);
            }

            int forwardDirection  = (BoardUtilities.IsBlack(startPosition)) ? 1 : -1;
            int backwardDirection = (!BoardUtilities.IsKing(startPosition)) ? 0 : (BoardUtilities.IsBlack(startPosition)) ? -1 : 1;

            // check for single step along vertical axis
            if (Math.Abs(endRow - startRow) == 1)
            {//possible walk made
                // check if we took a walk when a jump was available
                if (CanJump(board, player))
                {
                    return(MoveStatus.Illegal);
                }

                // one step along the horizontal axis and proper vertical direction movement
                // men can't go backwards but kings can
                if ((Math.Abs(endCol - startCol) == 1) && (startRow + forwardDirection == endRow || startRow + backwardDirection == endRow))
                {
                    return(MoveStatus.Legal);
                }
            }
            else if (Math.Abs(endRow - startRow) == 2)
            {// possible jump made
                int jumpedRow = (endRow + startRow) / 2;
                int jumpedCol = (endCol + startCol) / 2;

                if (BoardUtilities.IsOpponentPiece(player, board[jumpedRow, jumpedCol]))
                {
                    // one step along the horizontal axis and proper vertical direction movement
                    // men can't go backwards but kings can
                    if ((Math.Abs(endCol - startCol) == 2) && (startRow + forwardDirection * 2 == endRow || startRow + backwardDirection * 2 == endRow))
                    {
                        return(MoveStatus.Incomplete);
                    }
                }
            }

            return(MoveStatus.Illegal);
        }
Пример #2
0
        /// <summary>
        /// IsMoveLegal - Это законно
        /// Is the move legal. This move doesn't check multiple jumps.
        /// Это законный ход. Этот ход не проверяет несколько прыжков.
        /// </summary>
        /// <param name="board">
        /// the board state
        /// состояние правления
        /// </param>
        /// <param name="startRow">
        /// the start row
        /// начальный ряд
        /// </param>
        /// <param name="startCol">
        /// the start col
        /// начальный столб
        /// </param>
        /// <param name="endRow">
        /// the end row
        /// конец строки
        /// </param>
        /// <param name="endCol">
        /// the end col
        /// конец колонки
        /// </param>
        /// <param name="player">
        /// the player with the turn
        /// игрок с поворотом
        /// </param>
        /// <returns>
        /// LEGAL if the move is legal.  Illegal if the move is not legal.  INCOMPLETE if the move results in a jump.
        /// ЗАКОННЫЙ, если движение законно. Незаконный, если движение не законно. НЕПОЛНЫЙ, если движение приводит к скачку.
        /// </returns>
        private static MoveStatus IsMoveLegal(IBoard board, int startRow, int startCol, int endRow, int endCol, Player player)
        {
            if (!InBounds(startRow, startCol, board) || !InBounds(endRow, endCol, board))
            {
                // out of board bounds
                //из границ правления
                return(MoveStatus.Illegal);
            }

            Piece startPosition = board[startRow, startCol];
            Piece endPosition   = board[endRow, endCol];

            if ((player == Player.Black && !BoardUtilities.IsBlack(startPosition)) || (player == Player.White && !BoardUtilities.IsWhite(startPosition)))
            {
                // wrong player attempting to make a move
                //неправильный игрок, пытающийся сделать движение
                return(MoveStatus.Illegal);
            }
            else if (!BoardUtilities.IsEmpty(endPosition))
            {// destination is not empty
             //место назначения не пусто
                return(MoveStatus.Illegal);
            }

            int forwardDirection  = (BoardUtilities.IsBlack(startPosition)) ? 1 : -1;
            int backwardDirection = (!BoardUtilities.IsKing(startPosition)) ? 0 : (BoardUtilities.IsBlack(startPosition)) ? -1 : 1;

            // check for single step along vertical axis
            //проверьте на единственный шаг вдоль вертикальной оси
            if (Math.Abs(endRow - startRow) == 1)
            {//possible walk made
             //возможная ходьба сделана
             // check if we took a walk when a jump was available
             //проверьте, прогулялись ли мы, когда скачок был доступен
                if (CanJump(board, player))
                {
                    return(MoveStatus.Illegal);
                }

                // one step along the horizontal axis and proper vertical direction movement
                // один шаг по горизонтальной оси и правильное вертикальное движение
                // men can't go backwards but kings can
                // люди не могут идти назад, но короли могут
                if ((Math.Abs(endCol - startCol) == 1) && (startRow + forwardDirection == endRow || startRow + backwardDirection == endRow))
                {
                    return(MoveStatus.Legal);
                }
            }
            else if (Math.Abs(endRow - startRow) == 2)
            {// possible jump made
             // возможный прыжок сделан
                int jumpedRow = (endRow + startRow) / 2;
                int jumpedCol = (endCol + startCol) / 2;

                if (BoardUtilities.IsOpponentPiece(player, board[jumpedRow, jumpedCol]))
                {
                    // one step along the horizontal axis and proper vertical direction movement
                    // один шаг по горизонтальной оси и правильное вертикальное движение
                    // men can't go backwards but kings can
                    // люди не могут идти назад, но короли могут
                    if ((Math.Abs(endCol - startCol) == 2) && (startRow + forwardDirection * 2 == endRow || startRow + backwardDirection * 2 == endRow))
                    {
                        return(MoveStatus.Incomplete);
                    }
                }
            }

            return(MoveStatus.Illegal);
        }