Skip to content

Commit

Permalink
Added comments to code
Browse files Browse the repository at this point in the history
  • Loading branch information
kodlayanfahriye committed Feb 28, 2022
1 parent feba2e9 commit 669387f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
5 changes: 5 additions & 0 deletions Header/NPuzzleSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#define MAX_NO_OF_LEVELS 25


// Constructor for NPuzzleSolver.
NPuzzleSolver::NPuzzleSolver(const std::string &puzzle) {
std::stringstream ss(puzzle);
unsigned short n;
Expand All @@ -19,13 +21,15 @@ NPuzzleSolver::NPuzzleSolver(const std::string &puzzle) {
}


// Prints a line separator to split the rows of the N-puzzle on the terminal.
void NPuzzleSolver::printSeparator(const char &ch, const unsigned short &boxWidth, const unsigned short &boardSize) {
for(unsigned short i = 1; i < boardSize; i++)
std::cout << std::string(boxWidth, ch) << "|";
std::cout << std::string(boxWidth, ch) << std::endl;
}


// Prints the N-puzzle on the terminal.
void NPuzzleSolver::printPuzzle() const {
const unsigned short boxWidth = std::to_string(puzzleBoard.size()).length() + 2, boardSize = std::sqrt(puzzleBoard.size());
std::cout << "Board size: " << boardSize << std::endl;
Expand Down Expand Up @@ -53,6 +57,7 @@ void NPuzzleSolver::printPuzzle() const {
}


// Main method to solve the N-puzzle by adding a layer to the decision tree until either the solution is found or the MAX_NO_OF_LEVELS threshold is reached.
void NPuzzleSolver::solve() {
std::string solution = "";
const unsigned short emptySlotIndex = distance(puzzleBoard.begin(), std::find(puzzleBoard.begin(), puzzleBoard.end(), 0)), boardSize = std::sqrt(puzzleBoard.size());
Expand Down
2 changes: 2 additions & 0 deletions Header/NPuzzleSolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <string>
#include <vector>


// NPuzzleSolver class declaration.
class NPuzzleSolver {
private:
std::vector<unsigned short> puzzleBoard;
Expand Down
8 changes: 8 additions & 0 deletions Header/PathNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <set>


// An enum containing all possible directions of the movement.
typedef enum {
LEFT = 'L',
RIGHT = 'R',
Expand All @@ -11,13 +12,15 @@ typedef enum {
} Directions;


// Creates a subtree connected to the current node with directions as their child nodes.
void PathNode::createSubtree() {
static std::set<char> moves = {LEFT, RIGHT, UP, DOWN};
for(auto i = moves.begin(); i != moves.end(); i++)
childNodeList.emplace(*i, PathNode());
}


// Adds a level to the bottom of the decision tree.
void PathNode::addLevel() {
if(childNodeList.empty()) {
createSubtree();
Expand All @@ -28,6 +31,7 @@ void PathNode::addLevel() {
}


// Deletes the last level at the bottom of the decision tree.
void PathNode::clearLastLevel() {
if(childNodeList.begin()->second.childNodeList.empty()) {
childNodeList.clear();
Expand All @@ -38,11 +42,13 @@ void PathNode::clearLastLevel() {
}


// Deletes the whole decision tree altogether when it is called from the root, otherwise deletes the current node's whole subtree instead.
void PathNode::clearTree() {
childNodeList.clear();
}


// Returns TRUE if the given solution path is valid, FALSE otherwise.
bool PathNode::checkSolution(const std::string &path, const std::vector<unsigned short> &puzzleBoard) {
for(size_t i = 0; i < puzzleBoard.size() - 1; i++)
if(puzzleBoard[i] != i + 1)
Expand All @@ -52,6 +58,8 @@ bool PathNode::checkSolution(const std::string &path, const std::vector<unsigned
}


// A recursive, backtracking algorithm which traverses the whole decision tree to find a valid solution path for the N-puzzle.
// Returns TRUE if such solution path exists, FALSE otherwise.
bool PathNode::checkAllPaths(std::string &path, std::vector<unsigned short> &puzzleBoard, const unsigned short &boardSize, unsigned short &emptySlotIndex) {
for(auto i = childNodeList.begin(); i != childNodeList.end(); i++) {
unsigned short changedElementIndex;
Expand Down
1 change: 1 addition & 0 deletions Header/PathNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <vector>


// PathNode class declaration.
class PathNode {
private:
std::map<char, PathNode> childNodeList;
Expand Down
19 changes: 7 additions & 12 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,34 @@

int main() {
NPuzzleSolver TwoToTwo("1-2-0-3");

NPuzzleSolver ThreeToThree_ez("0-1-2-4-6-3-7-5-8");
NPuzzleSolver ThreeToThree_avg("5-1-2-4-6-3-0-7-8");
NPuzzleSolver ThreeToThree_hard("1-2-0-3-6-4-5-7-8");
NPuzzleSolver ThreeToThree_very_hard("4-6-7-3-2-1-5-0-8");
NPuzzleSolver ThreeToThree_impossible("1-2-3-4-5-6-8-7-0");

NPuzzleSolver FourToFour("2-10-4-0-1-6-3-7-5-14-12-8-9-13-11-15");

NPuzzleSolver FiveToFive("1-2-4-5-10-6-7-3-14-9-0-11-12-8-15-16-17-13-18-20-21-22-23-19-24");


TwoToTwo.printPuzzle();
TwoToTwo.solve();

NPuzzleSolver ThreeToThree_ez("0-1-2-4-6-3-7-5-8");
ThreeToThree_ez.printPuzzle();
ThreeToThree_ez.solve();

NPuzzleSolver ThreeToThree_avg("5-1-2-4-6-3-0-7-8");
ThreeToThree_avg.printPuzzle();
ThreeToThree_avg.solve();

NPuzzleSolver ThreeToThree_hard("1-2-0-3-6-4-5-7-8");
ThreeToThree_hard.printPuzzle();
ThreeToThree_hard.solve();

NPuzzleSolver ThreeToThree_very_hard("4-6-7-3-2-1-5-0-8");
ThreeToThree_very_hard.printPuzzle();
ThreeToThree_very_hard.solve();

NPuzzleSolver ThreeToThree_impossible("1-2-3-4-5-6-8-7-0");
ThreeToThree_impossible.printPuzzle();
ThreeToThree_impossible.solve();

NPuzzleSolver FourToFour("2-10-4-0-1-6-3-7-5-14-12-8-9-13-11-15");
FourToFour.printPuzzle();
FourToFour.solve();

NPuzzleSolver FiveToFive("1-2-4-5-10-6-7-3-14-9-0-11-12-8-15-16-17-13-18-20-21-22-23-19-24");
FiveToFive.printPuzzle();
FiveToFive.solve();

Expand Down

0 comments on commit 669387f

Please sign in to comment.