Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- [System.Serializable]
- public class LevelData
- {
- public int GridXSize;
- public int GridYSize;
- public List<ArrowData> Arrows = new List<ArrowData>();
- public bool CanSolveLevel()
- {
- if (Arrows.Count == 0) return true;
- int totalCells = GridXSize * GridYSize;
- int[] board = new int[totalCells];
- System.Array.Fill(board, -1);
- for (int i = 0; i < Arrows.Count; i++)
- {
- foreach (int idx in Arrows[i].Indices)
- board[idx] = i;
- }
- bool[] removed = new bool[Arrows.Count];
- int removedCount = 0;
- bool changed;
- do
- {
- changed = false;
- for (int i = 0; i < Arrows.Count; i++)
- {
- if (removed[i]) continue;
- if (CanExit(Arrows[i], board))
- {
- foreach (int idx in Arrows[i].Indices)
- board[idx] = -1;
- removed[i] = true;
- removedCount++;
- changed = true;
- }
- }
- } while (changed);
- return removedCount == Arrows.Count;
- }
- private bool CanExit(ArrowData arrow, int[] board)
- {
- int headIdx = arrow.Indices[0];
- int neckIdx = arrow.Indices[1];
- int hx = headIdx % GridXSize;
- int hy = headIdx / GridXSize;
- int nx = neckIdx % GridXSize;
- int ny = neckIdx / GridXSize;
- int dx = hx - nx;
- int dy = hy - ny;
- int cx = hx + dx;
- int cy = hy + dy;
- while (cx >= 0 && cx < GridXSize && cy >= 0 && cy < GridYSize)
- {
- int checkIdx = cy * GridXSize + cx;
- if (board[checkIdx] != -1) return false;
- cx += dx;
- cy += dy;
- }
- return true;
- }
- }
- [System.Serializable]
- public class ArrowData
- {
- public List<int> Indices = new();
- public int ColorIndex = 0;
- }
Advertisement