public override void Act(StateController controller) { AIUtilities.AIMove = Vector2.zero; List <Vector2Int> possibleMoves = controller.bm.redSelectedPiece.PossibleMoves(); if (possibleMoves.Count == 0) { return; } Vector2Int bestMove = possibleMoves[0]; Vector2Int targetLocation = AIUtilities.FindTargetLocation(controller.shortTermTarget); foreach (Vector2Int move in possibleMoves) { if ((targetLocation - move).magnitude <= (targetLocation - bestMove).magnitude) { bestMove = move; } } Vector2 direction = bestMove - controller.bm.redSelection; direction.Normalize(); AIUtilities.AIMove = direction; }
bool CanPieceMove(int piece) { Vector2Int pieceLocation = AIUtilities.FindTargetLocation(piece); bool canMove = false; if (piece == 20 || piece == 21) { return(true); } else { for (int y = Mathf.Max(0, pieceLocation.y - 1); y <= Mathf.Min(7, pieceLocation.y + 1); y++) { for (int x = Mathf.Max(0, pieceLocation.x - 1); x <= Mathf.Min(7, pieceLocation.x + 1); x++) { if (!(x == pieceLocation.x && y == pieceLocation.y)) { ChessPiece p = Utilities.chessBoard[pieceLocation.x, pieceLocation.y].GetComponent <ChessPiece>(); if (p.IsMovePossible(x, y, Utilities.chessBoard[x, y])) { canMove = true; } } } } } return(canMove); }
public override bool Decide(StateController controller) { Vector2Int targetLocation = AIUtilities.FindTargetLocation(controller.shortTermPieceToControl); if (targetLocation == controller.bm.redSelection) { AIUtilities.AIPressed = false; return(true); } return(false); }
public override void Act(StateController controller) { AIUtilities.AIMove = new Vector2(0, 0); Vector2Int targetLocation = AIUtilities.FindTargetLocation(controller.shortTermPieceToControl); Vector2 move = targetLocation - controller.bm.redSelection; move.Normalize(); AIUtilities.AIPressed = true; AIUtilities.AIMove = move; }
void FindShortTermTarget(int longTermTarget) { Vector2Int longTermTargetLocation = AIUtilities.FindTargetLocation(longTermTarget); bool targetFound = false; if (longTermTargetLocation.x < 0 || longTermTargetLocation.y < 0) { // target not found on board } for (int distance = 1; distance < 6; distance++) { for (int y = Mathf.Max(0, longTermTargetLocation.y - distance); y <= Mathf.Min(7, longTermTargetLocation.y + distance); y++) { for (int x = Mathf.Max(0, longTermTargetLocation.x - distance); x <= Mathf.Min(7, longTermTargetLocation.x + distance); x++) { if (Mathf.Abs(longTermTargetLocation.x - x) == distance || Mathf.Abs(longTermTargetLocation.y - y) == distance) { if (Utilities.chessBoard[x, y] == null) { openingTarget = new Vector2Int(x, y); Vector2Int checkShortTerm = Vector2Int.zero; if (openingTarget.x > longTermTargetLocation.x) { checkShortTerm.x = -1; } else if (openingTarget.x < longTermTargetLocation.x) { checkShortTerm.x = 1; } else { checkShortTerm.x = 0; } if (openingTarget.y > longTermTargetLocation.y) { checkShortTerm.y = -1; } else if (openingTarget.y < longTermTargetLocation.y) { checkShortTerm.y = 1; } else { checkShortTerm.y = 0; } shortTermTarget = Utilities.chessBoard[openingTarget.x + checkShortTerm.x, openingTarget.y + checkShortTerm.y] .GetComponent <ChessPiece>().id; targetFound = true; } } } } if (targetFound) { break; } } if (!targetFound) { shortTermTarget = longTermTarget; } }