public HitType Shoot(Point p) { if (p.X < 0 || p.X >= BoardSize || p.Y < 0 || p.Y >= BoardSize) { throw new ArgumentOutOfRangeException($"Точка должна быть в пределах доски {BoardSize}x{BoardSize}"); } if (!Board[p.X, p.Y].Equals(CellState.Unknown)) { throw new ArgumentException("Вы уже стреляли в эту точку"); } HitType shoot = Opponent.GetShootIn(p); Board[p.X, p.Y] = shoot.Equals(HitType.Miss) ? CellState.Missed : CellState.Hit; if (shoot == HitType.Kill) { LastHits.Add(p); MissedAroundKilled(); LastHits.Clear(); } else if (shoot == HitType.Hit) { LastHits.Add(p); } LastPoint = p; return(shoot); }
public void MissedAroundKilled() { bool isHorizontal = LastHits.Count == 1 ? true : LastHits[0].X == LastHits[1].X; int xmin, ymin, xmax, ymax, length; if (isHorizontal) { xmin = LastHits[0].X; xmax = xmin; ymin = LastHits.Select(s => s.Y).Min(); ymax = LastHits.Select(s => s.Y).Max(); length = ymax - ymin; } else { ymin = LastHits[0].Y; ymax = ymin; xmin = LastHits.Select(s => s.X).Min(); xmax = LastHits.Select(s => s.X).Max(); length = xmax - xmin; } length++; for (int i = xmin - 1; i < xmin + (isHorizontal ? 2 : length + 1); i++) { for (int j = ymin - 1; j < ymin + (isHorizontal ? length + 1 : 2); j++) { if (i < 0 || i >= BoardSize || j < 0 || j >= BoardSize) { continue; } if (Board[i, j].Equals(CellState.Unknown)) { Board[i, j] = CellState.Missed; } } } }