public bool Remove(PuzzleState cc, int index) { if (Final) { return(true); } int val = cc[index]; var child = next[val]; if (child == null) { return(false); } bool ret = child.Remove(cc, index + 1); if (ret) { if (child.count == 0) { next[val] = null; } count--; } return(ret); }
public static Puzzle TutorialPuzzleOne(PuzzleState initial) { var solved = initial.Flip(new Move(0, Side.Right)); Level level = Level.TutorialLevel(solved, new int[] { 1 }, 1); return(new Puzzle(level, initial)); }
public bool Add(PuzzleState cc, T extra, int index) { if (Final) { if (this.extra.Equals(default(T))) { this.extra = extra; } return(false); } int val = cc[index]; var child = next[val]; bool noChild = child == null; if (noChild) { child = new CCNode <T>(cc, index + 1); next[val] = child; } bool wasAdded = child.Add(cc, extra, index + 1) || noChild; if (wasAdded) { count++; } return(wasAdded); }
private static List <PuzzleState> ReadConfigs(Level level) { var textAsset = Resources.Load <TextAsset>(level.dataFile + "." + level.puzzleMoves); if (textAsset == null) { return(null); } var lines = textAsset.text.Split('\n'); var configs = new List <PuzzleState>(); foreach (var line in lines) { var str = line.Trim(); if (str.Length == 0) { continue; } if (level.Sided) { str = str.Substring(1); } configs.Add(PuzzleState.FromCompactString(str, level.solved.numColors, level.Sided)); } return(configs); }
public CCNode(PuzzleState sample, int index) { if (sample.Count != index) { next = new CCNode <T> [sample.CoinValueRange]; } }
internal static Puzzle BuildTutorialLevel(string solvedString, string initialString, int numMoves) { var solved = PuzzleState.FromString(solvedString); var initial = PuzzleState.FromString(initialString); Level level = Level.TutorialLevel(solved, new int[] { 1 }, numMoves); return(new Puzzle(level, initial)); }
public List <Move> FindSolutionPath(PuzzleState current) { if (!initialized || tree == null) { Debug.LogFormat("Cannot find solution path for level {0}. Init: {1}, Tree: {2}", this, initialized, (tree == null) ? "no" : "yes"); return(null); } return(tree.ReCreatePath(current, solved)); }
public static GameState FromString(string str) { var parts = str.Split(SEPARATOR); GameState gs = new GameState( PuzzleState.FromString(parts[0]), Move.ListFromString(parts[1]) ); return(gs); }
public bool Contains(PuzzleState cc, int index) { if (Final) { return(true); } int val = cc[index]; var child = next[val]; return(child != null && child.Contains(cc, index + 1)); }
public override bool Equals(object obj) { if (!(obj is PuzzleState) || obj == null) { return(false); } PuzzleState other = obj as PuzzleState; bool ret = sided == other.sided && Enumerable.SequenceEqual(pieces, other.pieces); return(ret); }
public bool GetExtra(PuzzleState cc, out T extra, int index) { extra = this.extra; if (Final) { return(true); } int val = cc[index]; var child = next[val]; return(child != null && child.GetExtra(cc, out extra, index + 1)); }
public Move NextMove(PuzzleState cc) { if (!initialized || tree == null) { Debug.LogWarning("Asked for next move on non-initialized Level"); return(Move.NoMove); } Move move = new Move(); tree.GetExtra(cc, out move); return(move); }
public Level(LevelType type, PuzzleState solved, string textureFile, string dataFile, int[] puzzlesPerLevel, int puzzleMoves, int starsRequired, bool tutorialLevel, bool forceLoadTree) { this.type = type; this.solved = solved; this.textureFile = textureFile; this.dataFile = dataFile; this.puzzlesPerLevel = puzzlesPerLevel; this.starsRequired = starsRequired; this.puzzleMoves = puzzleMoves; this.tutorialLevel = tutorialLevel; this.forceLoadTree = forceLoadTree; }
public static LevelRef FromCompactString(string str) { LevelRef lr = new LevelRef(); var ss = str.Trim(); if (ss.Length > 0) { var parts = str.Split('/'); var type = (Level.LevelType)Enum.Parse(typeof(Level.LevelType), parts[0]); var cc = PuzzleState.FromCompactString(parts[1]); lr = new LevelRef(type, cc); } return(lr); }
public Puzzle(Level level, PuzzleState initial) { if (level == null) { throw new ArgumentNullException("Level cannot be null for puzzle"); } if (initial == null) { throw new ArgumentNullException("Initial PuzzleState cannot be null for puzzle"); } this.level = level; this.initial = initial; }
/// <summary> /// Reads a solution tree from a text file. /// </summary> /// TODO: has side effects, should be reprogrammed -- Seu /// <returns></returns> private CCTrie LoadTree() { var file = Resources.Load <TextAsset>(dataFile); if (file == null) { return(null); } var tree = new CCTrie(solved); int numBeforeHardest = puzzleMoves == 1 ? 0 : puzzlesPerLevel.Take(puzzleMoves - 1).Aggregate((ppl, sum) => ppl + sum); var initialStates = new List <PuzzleState>(); int read = 0; int first = Sided ? 1 : 0; int numPieces = solved.Count; string[] leaves = file.text.Split('\n'); foreach (var leaf in leaves) { string str = leaf.Trim(); if (str.Length == 0) { continue; } PuzzleState cc = PuzzleState.FromCompactString(str.Substring(first, numPieces), solved.numColors, Sided); Move move = Move.FromChar(str[first + numPieces]); tree.Add(cc, move); if (read >= numBeforeHardest && read < numBeforeHardest + puzzlesPerLevel[puzzleMoves - 1]) { initialStates.Add(cc); } read++; } if (initialStates.Count == 0) { Debug.LogErrorFormat("No initial states from Tree on level {0}", this.Ref); } InitialConfigs = initialStates.ToArray(); return(tree); }
public List <Move> ReCreatePath(PuzzleState from, PuzzleState to, int max = 100) { if (!Contains(from)) { return(null); } List <Move> moves = new List <Move>(); while (!from.Equals(to) && max > 0) { Move move = new Move(); GetExtra(from, out move); moves.Add(move); from = from.Flip(move); max--; } return(moves); }
/// <summary> /// Loads all level classes from metadata file /// format /// type, image file, solved config, configs move level 1, configs move level 2, ... configs move level N /// </summary> /// <param name="metaText"></param> private void LoadLevelDefs(string metaText) { levels.Clear(); var json = JSON.Parse(metaText); foreach (JSONClass typeData in json["types"].AsArray) { var type = (Level.LevelType)Enum.Parse(typeof(Level.LevelType), typeData["type"]); var typeTexture = typeData["texture"]; foreach (JSONClass levelData in typeData["levels"].AsArray) { var textureFile = levelData["texture"] ?? typeTexture; var pieces = levelData["pieces"]; var indexesStr = levelData["indexes"].AsArray; var indexes = new int[indexesStr.Count]; for (int cs = 0; cs < indexesStr.Count; cs++) { indexes[cs] = int.Parse(indexesStr[cs]); } var playIndex = levelData["moves"].AsInt; var starsRequired = levelData["starsRequired"].AsInt; bool forceLoadTree = levelData["forceLoadTree"] != null; bool tutorial = levelData["tutorial"] != null; Level level = new Level(type, PuzzleState.FromString(pieces), textureFile, LEVELS_BASE_DIR + pieces, indexes, playIndex, starsRequired, tutorial, forceLoadTree); if (tutorial) { tutorialLevelRef = level.Ref; } AddLevel(level); } } availableLevels = new List <Level>(); foreach (var ll in levels.Values) { availableLevels.AddRange(ll); } availableLevels.Sort(); }
internal bool IsSolution(PuzzleState state) { return(level.IsSolution(state)); }
public LevelRef(LevelType type, PuzzleState cc) { this.type = type; this.cc = cc; }
public Level(LevelType type, PuzzleState solved, string textureFile, string dataFile, int[] puzzlesPerLevel, int puzzleMoves, int starsRequired) : this(type, solved, textureFile, dataFile, puzzlesPerLevel, puzzleMoves, starsRequired, false, false) { }
public LevelRef(Level level) { this.type = level.type; this.cc = level.solved; }
internal bool IsSolution(PuzzleState current) { return(current.Equals(solved)); }
public CCTrie(PuzzleState sample) { this.root = new CCNode <Move>(sample); this.sample = sample; }
public static Level TutorialLevel(PuzzleState solved, int[] puzzlesPerLevel, int puzzleMoves) { return(new Level(solved.sided ? LevelType.sided : LevelType.simple, solved, null, null, puzzlesPerLevel, puzzleMoves, 0, true, false)); }
public CCNode(PuzzleState sample) { next = new CCNode <T> [sample.CoinValueRange]; }
public bool GetExtra(PuzzleState cc, out Move extra) { return(root.GetExtra(cc, out extra, 0)); }
public bool Remove(PuzzleState cc) { return(root.Remove(cc, 0)); }
public bool Add(PuzzleState cc, Move extra) { return(root.Add(cc, extra, 0)); }
public bool Contains(PuzzleState cc) { return(root.Contains(cc, 0)); }