コード例 #1
0
        void CheckForcedMove(Piece piece)
        {
            //Get the cell location of piece
            Vector2Int cell = piece.cell;

            //Loop through adjacent cells of cell
            for (int x = -1; x <= 1; x += 2)
            {
                for (int y = -1; y <= 1; y += 2)
                {
                    //Create offset cell from index
                    Vector2Int offset = new Vector2Int(x, y);
                    //Create a new x from piece coordinates using offset
                    Vector2Int desiredCell = cell + offset;

                    #region Check #01 - Correct Direction?
                    //Is the piece not king?
                    if (!piece.isKing)
                    {
                        //If the piece white?
                        if (piece.isWhite)
                        {
                            //Is the piece moving backwards?
                            if (desiredCell.y < cell.y)
                            {
                                //Invalid - Check next one
                                continue;
                            }
                        }
                        //If the piece red?
                        else
                        {
                            //Is the piece moving backwards?
                            if (desiredCell.y > cell.y)
                            {
                                //Invalid move, check the next one
                                continue;
                            }
                        }
                    }
                    #endregion
                    #region Check #02 - Is the adjacent cell out of bounds?
                    //Is desired cell out of bounds?
                    if (IsOutOfBounds(desiredCell))
                    {
                        //Invalid- Check next one
                        continue;
                    }
                    #endregion
                    Piece detectedPiece = GetPiece(desiredCell);
                    #region Check #03 - Is the desired cell empty?
                    //Is there a detected piece?
                    if (detectedPiece == null)
                    {
                        //Invalid, check again
                        continue;
                    }
                    #endregion
                    #region Check #04 - Is the detected piece the same color?
                    if (detectedPiece.isWhite == piece.isWhite)
                    {
                        //Invalid - Check the next one
                        continue;
                    }
                    #endregion
                    Vector2Int jumpCell = cell + (offset * 2);
                    #region Check #05 - Is the jump cell out of bounds?
                    if (IsOutOfBounds(jumpCell))
                    {
                        continue;
                    }
                    #endregion
                    #region Check #06 - Is there a piece at the jump cell?
                    //Get piece next to the one we want to jump
                    detectedPiece = GetPiece(jumpCell);
                    if (detectedPiece)
                    {
                        continue;
                    }
                    #endregion

                    #region Store Forced Move
                    //Check if forced moves contains the piece we're currently checking
                    if (!forcedMoves.ContainsKey(piece))
                    {
                        //Add it to list of forced moves
                        forcedMoves.Add(piece, new List <Vector2Int>());
                    }
                    forcedMoves[piece].Add(jumpCell);
                    #endregion
                }
            }
        }
コード例 #2
0
        // ------------------------------------------------- //

        /// <summary>Detect if there is a forced move for a given piece.</summary>
        void CheckForcedMove(Piece piece)
        {
            // Get cell location of piece
            Vector2Int cell = piece.cell;

            // Loop through adjacent cells of a cell
            for (int x = -1; x <= 1; x += 2)
            {
                for (int y = -1; y <= 1; y += 2)
                {
                    // Create offset cell from index
                    Vector2Int offset = new Vector2Int(x, y);

                    // Create a new X from piece coordinates using offset
                    Vector2Int desiredCell = cell + offset;

                    #region Check #01 - Correct Direction?

                    // Is the piece not king?
                    if (!piece.isKing)
                    {
                        // Is the piece white?
                        if (piece.isWhite)
                        {
                            // Is the piece moving backwards? If yes, invalid move. Check next.
                            if (desiredCell.y < cell.y)
                            {
                                continue;
                            }
                        }
                        else
                        {
                            // The piece is red.
                            // Is the piece moving backwards? If yes, invalid move. Check next.
                            if (desiredCell.y > cell.y)
                            {
                                continue;
                            }
                        }
                    }

                    #endregion

                    #region Check #02 - Is the adjacent cell out of bounds?

                    // Is desired cell out of bounds?
                    if (IsOutOfBounds(desiredCell))
                    {
                        continue;
                    }

                    #endregion

                    // Try getting the piece at coordinates
                    Piece detectedPiece = GetPiece(desiredCell);

                    #region Check #03 - Is the desired cell empty?

                    // Is there a detected piece? If not, check next.
                    if (detectedPiece == null)
                    {
                        continue;
                    }

                    #endregion

                    #region Check #04 - Is the detected piece the same color?

                    // Is the detected piece the same color? If yes, invalid. Check next.
                    if (detectedPiece.isWhite == piece.isWhite)
                    {
                        continue;
                    }

                    #endregion

                    // Try getting the diagonal cell next to detected piece
                    Vector2Int jumpCell = cell + (offset * 2);

                    #region Check #05 - Is the jump cell out of bounds?

                    // Is the detination cell out of bounds? If yes, invalid. Check the next one.
                    if (IsOutOfBounds(jumpCell))
                    {
                        continue;
                    }

                    #endregion

                    #region Check #06 - Is there a piece at the jump cell?

                    // Get piece next to the one we want to jump
                    detectedPiece = GetPiece(jumpCell);

                    // Is there a piece there? If yes, invalid. Check the next one.
                    if (detectedPiece)
                    {
                        continue;
                    }

                    #endregion

                    // If code execution makes it to this point, a forced move has been detected!

                    #region Store Forced Move

                    // Check if forced moves contains the piece we're currently holding
                    if (!forcedMoves.ContainsKey(piece))
                    {
                        // Add it to list of forced moves
                        forcedMoves.Add(piece, new List <Vector2Int>());
                    }

                    // Add the jump cell to the piece's forced moves
                    forcedMoves[piece].Add(jumpCell);

                    #endregion
                }
            }
        }