void Randomize(Matchspace space) { List <int> types = new List <int>(); for (int b = 0; b < buckets.Length; b++) { for (int i = 0; i < buckets[b]; i++) { types.Add(b); } } for (int i = types.Count - 1; i > 0; i--) { int buffer = types[i]; int random = types[Random.Range(0, i)]; types[i] = types[random]; types[random] = buffer; } int count = 0; foreach (Matchable match in space.grid) { if (count < types.Count) { match.type = types[count]; count++; } else { match.type = -1; } } }
public void StartLevel(int index) { this.index = index; if (currentLevel) { Destroy(currentLevel.gameObject); } currentLevel = levels[index].Generate(); onLevelStart?.Invoke(currentLevel); levelActive = true; }
public override Matchspace Generate() { int count = 0; Matchspace space = Matchspace.Create(spaceSize); do { count++; Randomize(space); space.Step(); } while (!EquilibriumFinder.Search(space)); Debug.Log("Iterations until solution found: " + count); return(space); }
public override Matchspace Generate() { Matchspace space = Matchspace.Create(spaceSize); for (int x = 0; x < space.width; x++) { for (int y = 0; y < space.height; y++) { for (int z = 0; z < space.depth; z++) { space.grid[x, y, z].type = (x + y + z) % types; } } } return(space); }
static IEnumerable <SwapPair> PossibleSwaps(Matchspace space) { var surfaceNoMatch = from Matchable match in space.grid where !match.Hidden() && !match.AnyMatch() select match; foreach (Matchable first in surfaceNoMatch.Where(first => first.AnyAlmost())) { foreach (Matchable second in surfaceNoMatch) { if (SwapPair.MatchingSwap(first, second)) { yield return(new SwapPair(first, second)); } } } }
public static void SaveToTypeMap(Matchspace space) { if (!Directory.Exists(SAVE_FOLDER)) { Directory.CreateDirectory(SAVE_FOLDER); } int count = 0; while (File.Exists(SAVE_FOLDER + fileName + count + ".json")) { count++; } TypeMap saveMap = new TypeMap(space); string json = JsonUtility.ToJson(saveMap); Debug.Log("Saving to: " + SAVE_FOLDER); File.WriteAllText(SAVE_FOLDER + fileName + count + ".json", json); }
static bool Depthsearch(Matchspace space, Stack <SwapPair> path) { foreach (SwapPair step in PossibleSwaps(space)) { step.Swap(); space.Step(); path.Push(step); if (space.IsSolvable() && Depthsearch(space, path)) { break; } else { path.Pop().Swap(); space.Step(); } } return(space.InEquilibrium()); }
public override Matchspace Generate() { Vector3Int size = new Vector3Int(typeMap.width, typeMap.height, typeMap.depth); Matchspace space = Matchspace.Create(size); int[,,] types = typeMap.GetTypes(); for (int x = 0; x < space.width; x++) { for (int y = 0; y < space.height; y++) { for (int z = 0; z < space.depth; z++) { space.grid[x, y, z].type = types[x, y, z]; } } } space.Step(); return(space); }
public TypeMap(Matchspace space) { width = space.width; height = space.height; depth = space.height; List <int> typelist = new List <int>(); for (int x = 0; x < space.width; x++) { for (int y = 0; y < space.height; y++) { for (int z = 0; z < space.depth; z++) { typelist.Add(space.grid[x, y, z].type); } } } types = typelist.ToArray(); }
public static bool Search(Matchspace space, out List <SwapPair> steps) { Stack <SwapPair> path = new Stack <SwapPair>(); if (Depthsearch(space, path)) { steps = path.ToList(); steps.Reverse(); while (path.Count > 0) { path.Pop().Swap(); space.Step(); } return(true); } else { steps = null; return(false); } }
public static bool Search(Matchspace space) { return(Search(space, out List <SwapPair> list)); }
public void NewTarget(Matchspace target) { targetObject = target.gameObject; }
public void ShowSpace(Matchspace space) { currentLevel = space; StartCoroutine(PresentSpace()); }
public void Setup(Matchspace space) { currentSpace = space; }