sea-battle/Assets/Scripts/BotAI/VeryHardBotAI.cs
2026-01-09 08:45:22 +03:00

104 lines
2.7 KiB
C#
Raw Permalink Blame History

using System.Collections.Generic;
using UnityEngine;
public class VeryHardBotAI : IBotAI
{
private EasyBotAI fallbackAI = new EasyBotAI();
private List<Vector2Int> woundedParts = new List<Vector2Int>();
private Vector2Int? lastHit = null;
public Vector2Int MakeMove(GamePole pole)
{
UpdateWoundedParts(pole);
if (woundedParts.Count >= 2)
{
Vector2Int? target = ContinueLineAttack(pole);
if (target.HasValue)
return target.Value;
}
else if (woundedParts.Count == 1)
{
Vector2Int? target = TryCross(pole, woundedParts[0]);
if (target.HasValue)
return target.Value;
}
return fallbackAI.MakeMove(pole);
}
private Vector2Int? TryCross(GamePole pole, Vector2Int center)
{
int[,] directions = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
List<int> order = new List<int> { 0, 1, 2, 3 };
Shuffle(order);
foreach (int i in order)
{
int dx = center.x + directions[i, 0];
int dy = center.y + directions[i, 1];
if (IsValidTarget(pole, dx, dy))
{
lastHit = new Vector2Int(dx, dy);
return new Vector2Int(dx, dy);
}
}
return null;
}
private Vector2Int? ContinueLineAttack(GamePole pole)
{
woundedParts.Sort((a, b) => a.x == b.x ? a.y.CompareTo(b.y) : a.x.CompareTo(b.x));
Vector2Int dir = woundedParts[1] - woundedParts[0];
Vector2Int before = woundedParts[0] - dir;
Vector2Int after = woundedParts[woundedParts.Count - 1] + dir;
if (IsValidTarget(pole, after.x, after.y))
{
lastHit = after;
return after;
}
else if (IsValidTarget(pole, before.x, before.y))
{
lastHit = before;
return before;
}
woundedParts.Clear();
return null;
}
private void UpdateWoundedParts(GamePole pole)
{
woundedParts.Clear();
for (int x = 0; x < pole.LengPole; x++)
{
for (int y = 0; y < pole.LengPole; y++)
{
if (pole.PoleShot[x, y] == 2) // 2 <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
woundedParts.Add(new Vector2Int(x, y));
}
}
}
private bool IsValidTarget(GamePole pole, int x, int y)
{
return x >= 0 && x < pole.LengPole &&
y >= 0 && y < pole.LengPole &&
pole.PoleShot[x, y] == 0;
}
private void Shuffle(List<int> list)
{
for (int i = 0; i < list.Count; i++)
{
int r = Random.Range(i, list.Count);
(list[i], list[r]) = (list[r], list[i]);
}
}
}