/// <summary> /// Generate a solution string for a puzzle /// Updates the puzzle and returns a move string /// </summary> /// <returns></returns> public string SolvePuzzle() { string move = ""; //Check if this puzzle is already solved if (this.CheckSolved(0, 0)) { return(""); } int rows = this.Height - 1; int cols = this.Width - 1; //# Get the puzzle's dimensions if (this.GetNumber(rows, cols) != 0) /////????????? { int currentRow = this.CurrentPosition(0, 0)[0]; int currentCol = this.CurrentPosition(0, 0)[1]; int deltaRow = this.Delta(rows, cols, currentRow, currentCol)[0]; int deltaCol = this.Delta(rows, cols, currentRow, currentCol)[1]; move += string.Concat(Enumerable.Repeat("d", deltaRow)) + string.Concat(Enumerable.Repeat("r", deltaCol)); this.UpdatePuzzle(move); } //Iterate over the bottom rows in reverse order, i.e. bottom right corner first for (int row = rows; row > 1; row--) { for (int col = cols; col > 0; col--) { move += this.SolveInteriorTile(row, col); } move += this.SolveCol0Tile(row); } //Iterate over outermost colums in the top two rows in reverse order for (int col = cols; col > 1; col--) { move += this.SolveRow1Tile(col); move += this.SolveRow0Tile(col); } //Add the move string from solve_2x2() move += this.Solve2x2(); Puzzle newPuzzle = this.Clone(); newPuzzle.UpdatePuzzle(move); if (!newPuzzle.CheckSolvedAll()) { //add final string move += "drul"; } return(move); }
public void CheckSolvedAll() { Puzzle puzzle = new Puzzle(4, 4, new int[,] { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 }, { 12, 13, 14, 15 } }); bool result = puzzle.CheckSolvedAll(); Assert.AreEqual(true, result, "Test #50 Checksolvd"); }