private void ChooseGeneralSafePoint() { ChessPiece general = ArmyAI.Find(piece => piece.type == "King"); int[] positionHolder = new int[] { general.CurrentX, general.CurrentY }; List <int[]> safeDestinations = new List <int[]>(); BoardManager.Instance.SelectPiece(general.CurrentX, general.CurrentY); chosenPiece = general; List <AIMove> moves = Utils.GetPieceDestinations(general); for (int i = 0; i < moves.Count; i++) { ChessPiece destinationPiece = TemporaryBoard[moves[i].AttackX, moves[i].AttackY]; Utils.UpdateSlot(general.CurrentX, general.CurrentY); Utils.RepositionPiece(general, new int[] { moves[i].AttackX, moves[i].AttackY }); if (Utils.CheckPointSafe(moves[i].AttackX, moves[i].AttackY)) { safeDestinations.Add(new int[] { moves[i].AttackX, moves[i].AttackY }); } general.SetPosition(positionHolder[0], positionHolder[1]); Utils.UpdateSlot(positionHolder[0], positionHolder[1], general); Utils.UpdateSlot(moves[i].AttackX, moves[i].AttackY, destinationPiece); RefreshBoardData(); } chosenDestination = safeDestinations.Count > 0 ? safeDestinations[Random.Range(0, safeDestinations.Count)] : null; }
private void HandleAIMove() { chosenPiece = GetPiece(); if (chosenPiece != null) { BoardManager.Instance.SelectPiece(chosenPiece.CurrentX, chosenPiece.CurrentY); } if (chosenMove != null && (chosenMove.Weight > 0 || initialSequenceCompleted)) { int destX = chosenMove.Defender != null ? chosenMove.Defender.CurrentX : chosenMove.AttackX; int destY = chosenMove.Defender != null ? chosenMove.Defender.CurrentY : chosenMove.AttackY; chosenDestination = new int[2] { destX, destY }; } else if (!initialSequenceCompleted) { chosenDestination = GetDestination(); } if (chosenDestination == null) { RunForQueen(); } if (chosenDestination == null || (chosenDestination != null && Utils.CheckGeneralThreatened(chosenPiece, chosenDestination[0], chosenDestination[1]))) { ChooseGeneralSafePoint(); } if (chosenDestination == null || (chosenDestination != null && Utils.CheckGeneralThreatened(chosenPiece, chosenDestination[0], chosenDestination[1]))) { ProtectGeneral(); } ChessPiece general = ArmyAI.Find(piece => piece.type == "King"); if (chosenDestination != null) { BoardManager.Instance.MovePiece(chosenDestination[0], chosenDestination[1]); } else { BoardManager.Instance.WhiteTurn(); initialSequenceCompleted = false; BoardManager.Instance.EndGame(); } }