| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- public class ShelfState
- {
- public readonly List<List<int>> Layers = new List<List<int>>();
- public int SealChains;
- public List<int> 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<ShelfState> 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<ShelfState>(count);
- for (int i = 0; i < count; i++)
- Shelves.Add(new ShelfState());
- int[] extraLayers = new int[count];
- var shelfOrder = new List<int>();
- 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<int>();
- 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<int>(frontCounts[i]));
- for (int layer = 0; layer < extraLayers[i]; layer++)
- shelf.Layers.Add(new List<int>(3));
- }
- var frontSlots = new List<SlotRef>();
- var allSlots = new List<SlotRef>();
- 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<int>(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<int>();
- 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<int>();
- 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<int, int> CountVisibleTypes(bool allLayers = true)
- {
- var counts = new Dictionary<int, int>();
- 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<T>(IList<T> 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;
- }
- }
- }
|