コード例 #1
0
ファイル: Grid.cs プロジェクト: threepat1/checkers
 bool IsForcedMove(Piece selected, Vector2Int desiredCell)
 {
     // Does the selected piece have a forced move?
     if (forcedMoves.ContainsKey(selected))
     {
         // Is there any forced moves for this piece?
         if (forcedMoves[selected].Contains(desiredCell))
         {
             // It is a forced move
             return(true);
         }
     }
     // It is not a forced move
     return(false);
 }
コード例 #2
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
                }
            }
        }