using System; using System.Collections.Generic; using UnityEngine; public class ShelfState { public readonly List> Layers = new List>(); public int SealChains; public List Front => Layers[0]; public bool IsSealed => SealChains > 0; public int LayerCount => Layers.Count; } public class GameMoveOutcome { public bool Success; public string Message = ""; public int ClearedSets; public int RevealedLayers; public int UnlockedShelves; public int RemovedFruit; } public class GameLevelState { public static readonly int[] FruitOrder = { 1, 2, 4, 6, 11, 5, 8, 10 }; public LevelDefinition Definition { get; private set; } public List Shelves { get; private set; } public int MovesLeft { get; private set; } public float TimeLeft { get; private set; } public int Score { get; private set; } public int Served { get; private set; } public GameMoveOutcome LastOutcome { get; private set; } = new GameMoveOutcome(); public bool Timed => Definition != null && Definition.Timed; public bool Chained => Definition != null && Definition.Chained; public int TotalFruits => Definition != null ? Definition.TotalFruits : 0; public bool IsCleared => RemainingFruits <= 0; public bool IsOutOfMoves => MovesLeft <= 0 && !IsCleared; public bool IsOutOfTime => Timed && TimeLeft <= 0f && !IsCleared; public float TimeLimit => Definition != null ? Definition.TimeLimit : 0f; public int RemainingFruits { get { int total = 0; for (int i = 0; i < Shelves.Count; i++) { for (int layer = 0; layer < Shelves[i].Layers.Count; layer++) total += Shelves[i].Layers[layer].Count; } return total; } } public int SealedShelves { get { int total = 0; for (int i = 0; i < Shelves.Count; i++) { if (Shelves[i].IsSealed) total++; } return total; } } public int TotalChains { get { int total = 0; for (int i = 0; i < Shelves.Count; i++) total += Mathf.Max(0, Shelves[i].SealChains); return total; } } public static GameLevelState Create(LevelDefinition definition, int seed = 0) { var state = new GameLevelState(); state.Definition = definition; var random = seed == 0 ? new System.Random(Guid.NewGuid().GetHashCode()) : new System.Random(seed); state.Build(random); return state; } void Build(System.Random random) { int count = Definition.totalShelves; Shelves = new List(count); for (int i = 0; i < count; i++) Shelves.Add(new ShelfState()); int[] extraLayers = new int[count]; var shelfOrder = new List(); for (int i = 0; i < count; i++) shelfOrder.Add(i); Shuffle(shelfOrder, random); int cursor = 0; for (int i = 0; i < Definition.tripleShelves && cursor < count; i++, cursor++) extraLayers[shelfOrder[cursor]] = 2; for (int i = 0; i < Definition.doubleShelves && cursor < count; i++, cursor++) extraLayers[shelfOrder[cursor]] = 1; int[] frontCounts = new int[count]; for (int i = 0; i < count; i++) frontCounts[i] = 3; var emptyOrder = new List(); for (int i = 0; i < count; i++) emptyOrder.Add(i); Shuffle(emptyOrder, random); if (Definition.emptySlots <= 3) { frontCounts[emptyOrder[0]] = 2; frontCounts[emptyOrder[1]] = 1; } else { frontCounts[emptyOrder[0]] = 2; frontCounts[emptyOrder[1]] = 2; frontCounts[emptyOrder[2]] = 1; frontCounts[emptyOrder[3]] = 1; } for (int i = 0; i < count; i++) { var shelf = Shelves[i]; shelf.Layers.Add(new List(frontCounts[i])); for (int layer = 0; layer < extraLayers[i]; layer++) shelf.Layers.Add(new List(3)); } var frontSlots = new List(); var allSlots = new List(); for (int i = 0; i < count; i++) { var shelf = Shelves[i]; for (int layer = 0; layer < shelf.Layers.Count; layer++) { int slots = layer == 0 ? frontCounts[i] : 3; for (int slot = 0; slot < slots; slot++) { var reference = new SlotRef(i, layer, slot); allSlots.Add(reference); if (layer == 0) frontSlots.Add(reference); } } } var pool = new List(Definition.TotalFruits); int[] playable = new int[Definition.fruitTypes]; for (int i = 0; i < playable.Length; i++) playable[i] = FruitOrder[i]; for (int set = 0; set < Definition.sets; set++) { int fruit = set < playable.Length ? playable[set] : playable[random.Next(playable.Length)]; pool.Add(fruit); pool.Add(fruit); pool.Add(fruit); } Shuffle(pool, random); if (pool.Count != allSlots.Count) { Debug.LogWarning("[GameLevel] Fruit pool and slot count differ: " + pool.Count + " vs " + allSlots.Count); } int limit = Mathf.Min(pool.Count, allSlots.Count); for (int i = 0; i < limit; i++) { var reference = allSlots[i]; var targetLayer = Shelves[reference.Shelf].Layers[reference.Layer]; int candidate = pool[i]; if (targetLayer.Count >= 2 && targetLayer[targetLayer.Count - 1] == candidate && targetLayer[targetLayer.Count - 2] == candidate) { for (int j = i + 1; j < limit; j++) { if (pool[j] == candidate) continue; int swap = pool[j]; pool[j] = candidate; pool[i] = swap; candidate = swap; break; } } targetLayer.Add(candidate); } FixInitialMatches(random); AssignSeals(random, frontCounts); MovesLeft = Definition.moves; TimeLeft = Definition.TimeLimit; Score = 0; Served = 0; LastOutcome = new GameMoveOutcome(); } void AssignSeals(System.Random random, int[] frontCounts) { if (!Definition.Chained || Definition.seals <= 0) return; var candidates = new List(); for (int i = 0; i < Shelves.Count; i++) { if (frontCounts[i] == 3 && Shelves[i].Front.Count == 3) candidates.Add(i); } Shuffle(candidates, random); int seals = Mathf.Min(Definition.seals, candidates.Count); for (int i = 0; i < seals; i++) Shelves[candidates[i]].SealChains = Definition.sealChains; } void FixInitialMatches(System.Random random) { for (int pass = 0; pass < 240; pass++) { bool changed = false; for (int shelfIndex = 0; shelfIndex < Shelves.Count; shelfIndex++) { var shelf = Shelves[shelfIndex]; for (int layer = 0; layer < shelf.Layers.Count; layer++) { var fruits = shelf.Layers[layer]; if (fruits.Count != 3 || fruits[0] != fruits[1] || fruits[1] != fruits[2]) continue; if (TryBreakTriple(shelfIndex, layer, random)) changed = true; } } if (!changed) break; } } bool TryBreakTriple(int shelfIndex, int layer, System.Random random) { var source = Shelves[shelfIndex].Layers[layer]; int fruit = source[0]; for (int pass = 0; pass < 2; pass++) { for (int targetShelf = 0; targetShelf < Shelves.Count; targetShelf++) { var shelf = Shelves[targetShelf]; for (int targetLayer = 0; targetLayer < shelf.Layers.Count; targetLayer++) { if (targetShelf == shelfIndex && targetLayer == layer) continue; var donor = shelf.Layers[targetLayer]; for (int slot = 0; slot < donor.Count; slot++) { int swap = donor[slot]; if (swap == fruit) continue; if (pass == 0) { int same = 0; for (int i = 0; i < donor.Count; i++) { if (i != slot && donor[i] == fruit) same++; } if (same >= 2) continue; } donor[slot] = fruit; source[random.Next(3)] = swap; return true; } } } } return false; } public GameMoveOutcome TryMove(int fromShelf, int fromSlot, int toShelf, int toSlot = -1) { var outcome = new GameMoveOutcome(); if (fromShelf < 0 || fromShelf >= Shelves.Count || toShelf < 0 || toShelf >= Shelves.Count) return outcome; if (fromShelf == toShelf) return outcome; var source = Shelves[fromShelf]; var target = Shelves[toShelf]; if (source.IsSealed || target.IsSealed) { outcome.Message = "game.banner.shelf_chained"; return outcome; } if (fromSlot < 0 || fromSlot >= source.Front.Count) return outcome; if (target.Front.Count >= 3 && toSlot >= target.Front.Count) toSlot = 0; int fruit = source.Front[fromSlot]; if (toSlot >= 0 && toSlot < target.Front.Count) { int swap = target.Front[toSlot]; target.Front[toSlot] = fruit; source.Front[fromSlot] = swap; } else if (target.Front.Count < 3) { source.Front.RemoveAt(fromSlot); target.Front.Add(fruit); } else { int targetIndex = toSlot < 0 ? 0 : toSlot; int swap = target.Front[targetIndex]; target.Front[targetIndex] = fruit; source.Front[fromSlot] = swap; } MovesLeft = Mathf.Max(0, MovesLeft - 1); Score += 2; outcome.Success = true; Resolve(outcome); LastOutcome = outcome; return outcome; } public GameMoveOutcome UseHammer(int shelfIndex) { var outcome = new GameMoveOutcome(); if (shelfIndex < 0 || shelfIndex >= Shelves.Count) return outcome; var shelf = Shelves[shelfIndex]; if (shelf.IsSealed) { outcome.Message = "game.banner.shelf_chained"; return outcome; } if (shelf.Front.Count <= 0) { outcome.Message = "game.banner.shelf_empty"; return outcome; } outcome.RemovedFruit = shelf.Front[0]; shelf.Front.RemoveAt(0); Score += 5; outcome.Success = true; Resolve(outcome); LastOutcome = outcome; return outcome; } public GameMoveOutcome ShuffleVisible(System.Random random) { var outcome = new GameMoveOutcome(); var visible = new List(); for (int i = 0; i < Shelves.Count; i++) { if (Shelves[i].IsSealed) continue; visible.AddRange(Shelves[i].Front); } Shuffle(visible, random); int cursor = 0; for (int i = 0; i < Shelves.Count; i++) { if (Shelves[i].IsSealed) continue; var front = Shelves[i].Front; int count = front.Count; front.Clear(); for (int slot = 0; slot < count; slot++) front.Add(visible[cursor++]); } FixVisibleMatches(random); outcome.Success = true; Resolve(outcome); LastOutcome = outcome; return outcome; } void FixVisibleMatches(System.Random random) { for (int pass = 0; pass < 60; pass++) { bool changed = false; for (int i = 0; i < Shelves.Count; i++) { if (Shelves[i].IsSealed) continue; var front = Shelves[i].Front; if (front.Count != 3 || front[0] != front[1] || front[1] != front[2]) continue; for (int j = 0; j < Shelves.Count && !changed; j++) { if (i == j || Shelves[j].IsSealed) continue; var other = Shelves[j].Front; for (int slot = 0; slot < other.Count; slot++) { if (other[slot] == front[0]) continue; int swap = other[slot]; other[slot] = front[0]; front[random.Next(3)] = swap; changed = true; break; } } if (changed) break; } if (!changed) break; } } public bool UseMasterKey(out string message) { for (int i = 0; i < Shelves.Count; i++) { if (!Shelves[i].IsSealed) continue; Shelves[i].SealChains = 0; var outcome = new GameMoveOutcome(); Resolve(outcome); LastOutcome = outcome; message = "game.banner.chain_broken"; return true; } for (int i = 0; i < Shelves.Count; i++) { if (Shelves[i].Front.Count <= 0) continue; Shelves[i].Front.Clear(); var outcome = new GameMoveOutcome(); Resolve(outcome); LastOutcome = outcome; message = "game.banner.shelf_cleared"; return true; } message = "game.banner.nothing_to_unlock"; return false; } public void AddMoves(int amount) { MovesLeft = Mathf.Max(0, MovesLeft + amount); } public bool Tick(float deltaTime) { if (!Timed || IsCleared) return false; TimeLeft = Mathf.Max(0f, TimeLeft - Mathf.Max(0f, deltaTime)); return TimeLeft <= 0f; } public Dictionary CountVisibleTypes(bool allLayers = true) { var counts = new Dictionary(); for (int i = 0; i < Shelves.Count; i++) { var shelf = Shelves[i]; int layerLimit = allLayers ? shelf.Layers.Count : 1; for (int layer = 0; layer < layerLimit; layer++) { var fruits = shelf.Layers[layer]; for (int slot = 0; slot < fruits.Count; slot++) { int fruit = fruits[slot]; if (!counts.ContainsKey(fruit)) counts[fruit] = 0; counts[fruit]++; } } } return counts; } void Resolve(GameMoveOutcome outcome) { int guard = 0; bool changed = true; while (changed && guard++ < 40) { changed = false; for (int i = 0; i < Shelves.Count; i++) { var shelf = Shelves[i]; var front = shelf.Front; if (front.Count == 3 && front[0] == front[1] && front[1] == front[2]) { front.Clear(); Served++; Score += 100; outcome.ClearedSets++; if (Timed) TimeLeft = Mathf.Min(TimeLimit, TimeLeft + 2f); if (Chained) { for (int s = 0; s < Shelves.Count; s++) { if (Shelves[s].SealChains <= 0) continue; Shelves[s].SealChains--; if (Shelves[s].SealChains == 0) outcome.UnlockedShelves++; } } changed = true; } } for (int i = 0; i < Shelves.Count; i++) { var shelf = Shelves[i]; if (shelf.Front.Count == 0 && shelf.Layers.Count > 1) { shelf.Layers.RemoveAt(0); outcome.RevealedLayers++; changed = true; } } } } static void Shuffle(IList list, System.Random random) { for (int i = list.Count - 1; i > 0; i--) { int j = random.Next(i + 1); T temp = list[i]; list[i] = list[j]; list[j] = temp; } } struct SlotRef { public readonly int Shelf; public readonly int Layer; public readonly int Slot; public SlotRef(int shelf, int layer, int slot) { Shelf = shelf; Layer = layer; Slot = slot; } } }