/// <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 Methods // //------------------------------------------------------ /// <summary> /// Update puzzle based on move string and add moves counter /// </summary> /// <param name="move"></param> public void MakeMove(string move) { try { puzzle.UpdatePuzzle(move); this.moves += 1; } catch (Exception ex) { ; } }