2026-01-09 08:45:22 +03:00

51 lines
1.4 KiB
C#

using System.Collections.Generic;
using UnityEngine;
public class NormalBotAI : IBotAI
{
protected EasyBotAI fallbackAI = new EasyBotAI();
public virtual Vector2Int MakeMove(GamePole pole)
{
int[] wrecked = pole.SearchWreckedShip();
if (wrecked != null)
{
Vector2Int? shot = ShotCross(pole, wrecked[0], wrecked[1], false);
if (shot.HasValue)
return shot.Value;
}
return fallbackAI.MakeMove(pole);
}
private Vector2Int? ShotCross(GamePole pole, int x, int y, bool twoAttempts)
{
int[,] directions = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
List<int> dirs = new List<int> { 0, 1, 2, 3 };
// Ïåðåìåøèâàåì íàïðàâëåíèÿ ñëó÷àéíûì îáðàçîì
for (int i = 0; i < dirs.Count; i++)
{
int r = Random.Range(i, dirs.Count);
(dirs[i], dirs[r]) = (dirs[r], dirs[i]);
}
foreach (int d in dirs)
{
int dx = x + directions[d, 0];
int dy = y + directions[d, 1];
if (dx >= 0 && dx < pole.LengPole && dy >= 0 && dy < pole.LengPole)
{
if (pole.PoleShot[dx, dy] == 0)
{
return new Vector2Int(dx, dy);
}
}
}
// Åñëè íå íàøëè ïîäõîäÿùåãî âàðèàíòà, âûçûâàåì çàïàñíîé AI
return fallbackAI.MakeMove(pole);
}
}