sega f32d7dd69b Как вот это вот описать...
Было очень много экстренных ситуаций.
И по мне стреляли, и я упал в лужу нахуй блять, и лежал нахуй
2026-07-29 15:05:01 +03:00

197 lines
6.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
public enum CellState
{
Empty, Player1, Player2
}
public static class FieldChecker
{
static GameManager gm => GameManager.instance;
public static Chip[,] FieldArray => Board.Instance.Grid;
public static int Height => Board.Instance.Rows;
public static int Width => Board.Instance.Columns;
public static int TargetLineLength = 4;
public static CheckResult Check(CellState lastPlayer)
{
HashSet<(int row, int col)> WinningIndexes = new();
var rowsResult = checkAllRows(lastPlayer);
var colsResult = checkAllColumns(lastPlayer);
var diagonalsResult = checkAllDiagonals(lastPlayer);
bool hasWin = rowsResult.hasWin ||
colsResult.hasWin ||
diagonalsResult.hasWin;
if (hasWin)
{
WinningIndexes.UnionWith(rowsResult.winningIndexes);
WinningIndexes.UnionWith(colsResult.winningIndexes);
WinningIndexes.UnionWith(diagonalsResult.winningIndexes);
return new CheckResult(end: true,
isDraw: false,
WinningIndexes);
}
else if (checkDraw())
{
return new CheckResult(end: true,
isDraw: true,
WinningIndexes);
}
else
{
return new CheckResult(end: false,
isDraw: false,
WinningIndexes);
}
}
private static (bool hasWin, HashSet<(int row, int col)> winningIndexes) checkAllRows(CellState targetTeam)
{
var winningCells = new HashSet<(int row, int col)>();
for (int row = 0; row < Height; row++)
{
int start = -1;
for (int col = 0; col <= Width; col++) // теперь проходим до Width включительно
{
// Проверяем, не вышли ли за границы массива
var chip = FieldArray[col, row];
bool isMatch = col < Width && chip != null && chip.Team == targetTeam;
if (isMatch && start == -1)
start = col;
if ((!isMatch || col == Width) && start != -1)
{
int length = col - start;
if (length >= TargetLineLength)
{
for (int c = start; c < col; c++)
winningCells.Add((row, c));
}
start = -1;
}
}
}
return (winningCells.Count > 0, winningCells);
}
private static (bool hasWin, HashSet<(int row, int col)> winningIndexes) checkAllColumns(CellState targetTeam)
{
var winningCells = new HashSet<(int row, int col)>();
for (int col = 0; col < Width; col++)
{
int start = -1;
for (int row = 0; row <= Height; row++)
{
var chip = FieldArray[col, row];
bool isMatch = row < Height && chip != null && chip.Team == targetTeam;
if (isMatch && start == -1)
start = row;
if ((!isMatch || row == Height) && start != -1)
{
int length = row - start;
if (length >= TargetLineLength)
{
for (int r = start; r < row; r++)
winningCells.Add((r, col));
}
start = -1;
}
}
}
return (winningCells.Count > 0, winningCells);
}
private static (bool hasWin, HashSet<(int row, int col)> winningIndexes) checkAllDiagonals(CellState targetCellState)
{
var winningCells = new HashSet<(int row, int col)>();
// Проверка главных диагоналей
for (int row = 0; row < Height; row++)
{
CheckDiagonalStartingAt(row, 0, 1, 1, targetCellState, winningCells);
}
for (int col = 1; col < Width; col++)
{
CheckDiagonalStartingAt(0, col, 1, 1, targetCellState, winningCells);
}
// Проверка побочных диагоналей
for (int row = 0; row < Height; row++)
{
CheckDiagonalStartingAt(row, Width - 1, 1, -1, targetCellState, winningCells);
}
for (int col = Width - 2; col >= 0; col--)
{
CheckDiagonalStartingAt(0, col, 1, -1, targetCellState, winningCells);
}
return (winningCells.Count > 0, winningCells);
}
private static void CheckDiagonalStartingAt(int startRow, int startCol, int rowStep, int colStep,
CellState targetCellState, HashSet<(int, int)> winningCells)
{
int rows = Height;
int cols = Width;
int currentRow = startRow;
int currentCol = startCol;
int sequenceLength = 0;
int sequenceStartRow = startRow;
int sequenceStartCol = startCol;
while (currentRow >= 0 && currentRow < rows &&
currentCol >= 0 && currentCol < cols)
{
var chip = FieldArray[currentCol, currentRow];
bool isMatch = chip != null && chip.Team == targetCellState;
if (isMatch)
{
sequenceLength++;
if (sequenceLength >= TargetLineLength)
{
// Добавляем ВСЕ ячейки из текущей последовательности
for (int i = 0; i < sequenceLength; i++)
{
int r = sequenceStartRow + i * rowStep;
int c = sequenceStartCol + i * colStep;
winningCells.Add((r, c));
}
}
}
else
{
sequenceLength = 0;
sequenceStartRow = currentRow + rowStep;
sequenceStartCol = currentCol + colStep;
}
currentRow += rowStep;
currentCol += colStep;
}
}
public static bool checkDraw()
{
for (int col = 0; col < Width; col++)
{
if (Board.Instance.GetLowestEmptyRow(col) != -1) return false;
}
return true;
}
}