so basically i need help on taking an existing array and moving a specific number in that array that is 3X3 and move it up down, etc.., after its moved the values around it need to move accordingly.
example
123
321
123
i want to move spot (3, 2) to the left
123
213
123
also the same for up and down elements
the program will also keep up with the spot you in and change them accordingly
my program will need to keep track of the coordinates too, that i have yet to figure out how to implement.
#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>0 && i<3 && j>=0 && j<3)
{
swapValues(array, i, j, i-1, j);
swapValues(array, i, j, i+1, j);
}
else
{
cout << "wrong move!" << endl;
}
//swapValues(array, i, j, i, j-1);
//swapValues(array, i, j, i, j+1);
}
void movedown(int (&array)[3][3], int i, int j)
{
swapValues(array, i+1, j, i, j);
swapValues(array, i-1, j, i, j);
}
void moveright(int (&array)[3][3], int i, int j)
{
swapValues(array, i, j+1, i, j);
swapValues(array, i, j-1, i, j);
}
void moveup(int (&array)[3][3], int i, int j)
{
//here you MUST check the indexes to make sure you CAN move left
//int lim = (3*3)-1;
//for(int x = lim; x >= 1; x--)
//{
swapValues(array, i, j, i-1, j);
swapValues(array, i, j, i+1, 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 on the x axis(left to right)" << endl;
cin >> start1;
cout << "now enter the point on the y axis(up or down)" << 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;
}
please give me code examples, explanations are good, but never clear enough for me.
not asking you to give answer, just help me see the right answer
- so i have updated my code and it sorta kinda works, the only problem is that when i use any other coordinates besides the ones (1, 1) then i get memory addresses instead of array numbers. Any explanation on this would be greatly appreciated, i have no idea how to fix a problem like this because im not really sure how the problem is even there?
also that code is completely compilable and ready to go, so run to see what i mean if i didnt explain well enough.
This is a possible solution
Use this function to swap the values at given indexes
Then, for example, your
moveLeftwill be like thisNote that I pass the array as reference, otherwise your modifications won’t be visible in your
mainfunction