public bool CheckChildren(PuzzleState state) { if (state.checkForWin()) { return(true); } List <PuzzleState> children = state.generateChildren(); if (children.Count == 0) { return(false); } foreach (var child in children) { if (visitedStates.Contains(child.ToString())) { continue; } visitedStates.Add(child.ToString()); if (CheckChildren(child)) { child.Print(); return(true); } } return(false); }
public void Print(PuzzleState previousState) { for (int i = colums[0].balls.Length - 1; i >= 0; i--) { for (int j = 0; j < colums.Length; j++) { bool changed = colums[j].balls[i] != previousState.colums[j].balls[i]; if (colums[j].balls[i] == '\0') { if (changed) { Console.BackgroundColor = ConsoleColor.Green; } Console.Write(" "); } else { if (changed) { Console.BackgroundColor = ConsoleColor.DarkRed; } Console.Write(colums[j].balls[i]); } Console.BackgroundColor = ConsoleColor.Black; Console.Write(" "); } Console.WriteLine(); } }
public bool CheckChildrenNonRecursive(PuzzleState state, ColorPalet colors = null) { Stack <PuzzleState> stack = new Stack <PuzzleState>(); stack.Push(state); while (stack.Count > 0) { var curr = stack.Peek(); if (curr.checkForWin()) { break; } List <PuzzleState> children = curr.generateChildren(); if (children.Count > 0) { bool allVisited = true; foreach (var child in children) { if (visitedStates.Contains(child.ToString())) { //skip } else { stack.Push(child); visitedStates.Add(child.ToString()); allVisited = false; break; } } if (allVisited) { stack.Pop(); } } else { //dead stack.Pop(); } } Console.WriteLine(stack.Count + " Steps"); var prev = stack.Pop(); while (stack.Count > 0) { var curr = stack.Pop(); curr.PrintFancy(prev, colors); prev = curr; Console.WriteLine(); } return(true); }
public PuzzleState(PuzzleState parentState, int SourceIndex, int destinationIndex) { colums = new Colum[parentState.colums.Length]; for (int i = 0; i < colums.Length; i++) { colums[i] = new Colum(parentState.colums[i]); } colums[destinationIndex].addChar(colums[SourceIndex].takeTopChar()); }
public void PrintFancy(PuzzleState previousState, ColorPalet palet = null) { if (palet == null) { palet = ColorPalet.defaultColors; } int sourceIndex = 0; int destinationIndex = 0; for (int i = colums[0].balls.Length - 1; i >= 0; i--) { for (int j = 0; j < colums.Length; j++) { bool changed = colums[j].balls[i] != previousState.colums[j].balls[i]; if (colums[j].balls[i] == '\0') { if (changed) { destinationIndex = j; Console.BackgroundColor = palet.destinationColor; } Console.Write(" "); } else { if (changed) { sourceIndex = j; Console.BackgroundColor = palet.sourceColor; } Console.ForegroundColor = palet.Get(colums[j].balls[i]); Console.Write(colums[j].balls[i]); } Console.BackgroundColor = palet.defaultBG; Console.ForegroundColor = palet.defaultText; Console.Write(" "); } Console.WriteLine(); } }
public PuzzleGraph(PuzzleState initalState) { startingState = initalState; }
static void Main(string[] args) { #region char definition char a = 'a'; char b = 'b'; char c = 'c'; char d = 'd'; char e = 'e'; char f = 'f'; char g = 'g'; char h = 'h'; char i = 'i'; char j = 'j'; char k = 'k'; char l = 'l'; char lightGreen = a; char gray = b; char orange = c; char yellow = d; char red = e; char purple = f; char khaki = g; char lightBlue = h; char pink = i; char brown = j; char darkBlue = k; char darkGreen = l; char empty = '\0'; #endregion ColorPalet colors = new ColorPalet(sourceC: ConsoleColor.Gray, destinationC: ConsoleColor.White); colors.Add(lightGreen, ConsoleColor.Green); colors.Add(gray, ConsoleColor.DarkGray); colors.Add(orange, ConsoleColor.DarkYellow); colors.Add(yellow, ConsoleColor.Yellow); colors.Add(red, ConsoleColor.Red); colors.Add(purple, ConsoleColor.DarkMagenta); colors.Add(khaki, ConsoleColor.DarkCyan); colors.Add(lightBlue, ConsoleColor.Cyan); colors.Add(pink, ConsoleColor.Magenta); colors.Add(brown, ConsoleColor.DarkRed); colors.Add(darkBlue, ConsoleColor.DarkBlue); colors.Add(darkGreen, ConsoleColor.DarkGreen); Colum[] cols0 = new Colum[] { new Colum(), new Colum(), new Colum(), new Colum(), new Colum(), new Colum(), new Colum(), new Colum(), new Colum(), new Colum(), new Colum(), new Colum(), new Colum(), new Colum() }; Colum[] cols1 = new Colum[] { new Colum(c, b, a, a), new Colum(c, b, c, b), new Colum(a, c, b, a), new Colum(), new Colum() }; Colum[] cols2 = new Colum[] { new Colum(a, a, a, b), new Colum(b, b, b, a), new Colum() }; Colum[] cols3 = new Colum[] { new Colum(a, a, a, empty), new Colum(b, b, b, b), new Colum(a, empty, empty, empty) }; Colum[] cols4 = new Colum[] { new Colum(yellow, orange, gray, lightGreen), new Colum(lightBlue, khaki, purple, red), new Colum(purple, lightGreen, pink, khaki), new Colum(brown, red, pink, yellow), new Colum(purple, brown, darkBlue, khaki), new Colum(yellow, purple, gray, khaki), new Colum(darkGreen, lightBlue, gray, orange), new Colum(pink, lightBlue, darkGreen, red), new Colum(lightGreen, lightBlue, darkBlue, darkBlue), new Colum(orange, pink, brown, darkGreen), new Colum(orange, yellow, lightGreen, gray), new Colum(darkBlue, darkGreen, brown, red), new Colum(), new Colum() }; Colum[] cols5 = new Colum[] { new Colum(gray, brown, orange, lightBlue), new Colum(purple, lightGreen, yellow, gray), new Colum(lightBlue, pink, khaki, orange), new Colum(yellow, red, yellow, darkBlue), new Colum(yellow, orange, purple, brown), new Colum(darkGreen, khaki, lightGreen, darkGreen), new Colum(darkBlue, red, darkBlue, khaki), new Colum(red, brown, lightGreen, brown), new Colum(purple, lightBlue, lightGreen, pink), new Colum(pink, orange, khaki, purple), new Colum(darkGreen, darkGreen, pink, gray), new Colum(darkBlue, lightBlue, red, gray), new Colum(), new Colum() }; PuzzleState pState1 = new PuzzleState(cols1); PuzzleState pState2 = new PuzzleState(cols2); PuzzleGraph pGraph1 = new PuzzleGraph(pState1); PuzzleGraph pGraph2 = new PuzzleGraph(pState2); PuzzleGraph lvl107 = new PuzzleGraph(new PuzzleState(cols4)); PuzzleGraph lvl109 = new PuzzleGraph(new PuzzleState(cols5)); lvl107.Solve(colors); }