okay so i have this program that is basically like moving the sides to a 2 dimensional rubiks cube. for example
111
222
333
is a 3X3 two dimensional array and when coordinate (0, 1, 2 being 1, 2, 3) 1, 1 (meaning in our number system as 2, 2) would like to be moved up based on the user input, then the numbers in that array move as follows
121
232
313
and so on and so forth
now my problem is that i can figure this out, eventually but the way im doing it is faaaar to tedious, so im reaching out for some help to maybe simply the mess that i have created.
#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;
}
this so far is my code and i already know, its a chaotic mess, but this is mainly due to my absence in the programming world for some time.
and please if any can give code examples id be greatly appreciative. another thing that's troubling me, i know a little about heuristics but not enough to implement it and definitely not enough to use in this mess of a code. any pointers would help greatly, details on that below.
Write a program to solve the 8-Tiles Puzzle using the best first search algorithm. You need to compare between using the following heuristics to estimate the distance to the goal:
* h1= 0 (this will be the breadth first search)
* h2= number of misplaced tiles
* h3= sum of distances heuristic
Your program will prompt the user to enter a starting state (you can use the number zero to represent the blank).
The output of your program will show the required movements to reach the goal state, the total number of states in the search, and the path length.
like i said any help would be greatly appreciated and please use code to give examples, those are easier for me to understand.
thank you greatly for the assist