I need some serious assistence with finding the solution to this problem, it has plagued me for some time now and im just not getting the big picture.
I need to move the elements in a 2d array according to the user input, but the way i have figured out is too difficult and could probably be much easier, but i havent found much on it so bare with me.
Secondly I need to figure out a breadth first algorithm to find the number of moves it will take for the user to get to the solution state, this is also something that feels down right impossible but im sure if i get some guidance it will all come to light.

If anyone thinks they can help with this it would be sweet, cause i have found a similar problem online BUT it is in java and since im only c++ its sorta makes sence in places but then looses me at times.

Also if you could give a few code examples to show me what your talking about if you explain then that would be even better, i like to see how the code is built that way i can understand it a little better.

This is my main code (forgive my lack of comments)

``````#include <iostream>
#include <iomanip>
#include <array>

using namespace std;

void printarray(int num[3][3])
{
for(int i = 0;i < 3; i++)
{
for(int j = 0; j< 3; j++)
{
cout << num[i][j] << " ";
}
cout << "\n";
}
}

void swapValues(int (&array)[3][3], int i1,int j1,int i2,int j2)
{
int temp = array[i1][j1];
array[i1][j1] = array[i2][j2];
array[i2][j2] = temp;
}

void moveleft(int (&array)[3][3], int i, int j)
{
if(i==1&&j==1)
{
swapValues(array, i, j, i-1, j);
swapValues(array, i, j, i+1, j);
}
else if(i==1&&j==2)
{
swapValues(array, i, j, i, j-1);
swapValues(array, i, j, i, j-2);
}
else if(i==0&&j==0)
{
swapValues(array, i, j+2, i, j);
swapValues(array, i, j+1, i, j);
}
else if(i==0&&j==2)
{
swapValues(array, i, j, i, j);
}
else
swapValues(array, i, j, i-1, j);
swapValues(array, i, j, i+1, j);

}

void movedown(int (&array)[3][3], int i, int j)
{
if(i==0&&j==0)
{
swapValues(array, i, j, i+1, j);
swapValues(array, i, j, i+2, j);
}
else if(i==1&&j==1)
{
swapValues(array, i+1, j, i, j);
swapValues(array, i-1, j, i, j);
}
else if(i==0&&j==2)
{
swapValues(array, i, j, i+1, j);
}
else
swapValues(array, i, j, i-1, j);
swapValues(array, i, j, i+1, j);
}

void moveright(int (&array)[3][3], int i, int j)
{
if(i==0&&j==0)
{
swapValues(array, i, j+1, i, j);
swapValues(array, i, j+2, i, j);
}
else if(i==1&&j==1)
{
swapValues(array, i, j+1, i, j);
swapValues(array, i, j-1, i, j);
}
else if(i==1&&j==2)
{
swapValues(array, i, j-2, i, j);
swapValues(array, i, j-1, i, j);
}
else
swapValues(array, i, j, i-1, j);
swapValues(array, i, j, i+1, j);
}

void moveup(int (&array)[3][3], int i, int j)
{
if(i==0&&j==0)
{
swapValues(array, i, j, i+2, j);
swapValues(array, i, j, i+1, j);
}
else if(i==1&&j==1)
{
swapValues(array, i, j, i-1, j);
swapValues(array, i, j, i+1, j);
}
else if(i==0&&j==2)
{
swapValues(array, i, j, i+1, j);
swapValues(array, i, j, i+2, j);
swapValues(array, i, j, i+1, j);
}
else
swapValues(array, i, j, i-1, j);
swapValues(array, i, j, i+2, j);
//int coord1d = j * 3 + i;
}

class Puzzle
{
public:
Puzzle();
void swapValues(int num[3][3], int i1, int j1, int i2, int j2);
//void moveleft(int[3][3], int start1, int start2);
void moveup(int (&array)[3][3], int i, int j);
void moveright(int (&array)[3][3], int i, int j);
void moveleft(int (&array)[3][3], int i, int j);
void movedown(int (&array)[3][3], int i, int j);
void printarray(int[3][3]);
};

int main()
{
int state1[3][3] = {{2, 8, 3}, {1, 6, 4}, {7, 0, 5}};
int state2[3][3] = {{2, 8, 1}, {4, 6, 3}, {0, 7, 5}};
int gstate[3][3] = {{1, 2, 3}, {8, 0, 4}, {7, 6, 5}};
cout << "this is state 1" << endl;
printarray(state1);
cout << "\n\n";
cout << "now this is state 2" << endl;
printarray(state2);
cout << "\n\n";
cout << "now this is the goal state" << endl;;
printarray(gstate);
int start1, start2;
cout << endl << endl << "please enter your starting point in the states listed above going up or down" << endl;
cin >> start1;
cout << "now enter the point going left to right" << endl;
cin >> start2;
//moveleft(state1, start1, start2);
cout << "this is moving coordinate (1, 1) upwards" << endl;
moveup(state1, start1, start2);
printarray(state1);
cout << endl;
cout << "this is moving coordinate (1, 1) to the left" << endl;
moveleft(state1, start1, start2);
printarray(state1);
cout << endl;
cout << "this is moving coordinate (1, 1) downwards" << endl;
movedown(state1, start1, start2);
printarray(state1);
cout << endl;
cout << "this is moving coordinate (1, 1) to the right" << endl;
moveright(state1, start1, start2);
printarray(state1);
cout << endl;

cin.get();
cin.get();
return 0;
}
``````

Firstly, I notice a very obvious problem in your if-else-if statements in that you end with

``````else
Code Statement 1;
Code Statement 2;
``````

Remembering that C++ doesn't care about whitespace you can rewrite that to be more clear on its function like this:

``````else
Code Statement 1;
Code Statement 2;
``````

Note that even if one of the if statements triggers, the last line will still be executed.

As for the search algorithm, all you really need to define is getAllMoves(array) and isDone(array), the rest is taken care of in the algorithm. Using an std::vector may help deal with getAllMoves.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.