/// <summary> /// Handles turns for AI players. /// </summary> private IEnumerator AITurn() { // AI delay before continuing yield return(new WaitForSeconds(AIPlayer.AIDelay)); if (state == State.Placement) { // Get a placement move PlacementMove pm = ((AIPlayer)players[turnIndex]).PlacementMove(); placementPiece = pm.PlacementPiece; // Click the placement piece placementPiece.OnMouseUpAsButton(); yield return(new WaitForSeconds(AIPlayer.AIDelay)); // Click the placement tile SelectedTile(pm.Destination); } else { // Get a weighted move WeightedMove wm = players[turnIndex].DoTurn(); // Do the turn SelectedTile(wm.piece.GetNode()); yield return(new WaitForSeconds(AIPlayer.AIDelay)); SelectedTile(wm.dest); } }
/// <summary> /// Ties the WeightedAI together, gets the /// moves, picks one and executes /// </summary> /// <returns>The turn.</returns> public override WeightedMove DoTurn() { moves = GetAllAvailableMoves(); WeightMoves(moves); WeightedMove pickedMove = null; //Attempts before defaulting to random move int Attempts = 10; while (Attempts > 0) { int maxRoll = 999; int roll = new System.Random().Next(maxRoll + 1); if (roll > maxRoll - attackWeight && AttackMoves.Count > 0) { PickAttackMoves(AttackMoves); pickedMove = AttackMoves[new System.Random().Next(0, AttackMoves.Count)]; UnityEngine.Debug.Log(pickedMove.attackValue); UnityEngine.Debug.Log("Attack move picked"); } else if (roll > maxRoll - attackWeight - safeWeight && SafeMoves.Count > 0) { pickedMove = SafeMoves[new System.Random().Next(0, SafeMoves.Count)]; UnityEngine.Debug.Log("Safe move picked"); } else if (roll > maxRoll - attackWeight - safeWeight - wayWeight && WayMoves.Count > 0) { pickedMove = WayMoves[new System.Random().Next(0, WayMoves.Count)]; UnityEngine.Debug.Log("Way move picked"); } else if (roll > maxRoll - attackWeight - safeWeight - wayWeight - neutralWeight && NeutralMoves.Count > 0) { pickedMove = NeutralMoves[new System.Random().Next(0, NeutralMoves.Count)]; UnityEngine.Debug.Log("Way move picked"); } else if (BadMoves.Count > 0) { pickedMove = BadMoves[new System.Random().Next(0, BadMoves.Count)]; UnityEngine.Debug.Log("Bad move picked"); } if (pickedMove != null) { break; } Attempts--; } //Empty out all saved moves BadMoves.Clear(); WayMoves.Clear(); SafeMoves.Clear(); AttackMoves.Clear(); //No valid moves, or failed to choose if (Attempts == 0) { UnityEngine.Debug.Log("WeightedAI could not decide. Random Move."); return(RandomMove()); } else { UnityEngine.Debug.Log("WeightedAI moved."); return(pickedMove); } }