Hi I have some programming homework and I've got the main idea but I am not sure how to swap the numbers in the array.
This was the question.
This game has 9 squares with the numerals 1 through 8 on them, and a 9th square with some special symbol printed. The squares are arranged in 3 rows and 3 columns. For example, an arrangement might look like this:
1 * 3
4 2 6
7 5 8
You can swap the special symbol with the numeral printed on any of its neighbours (the squares immediately to its left or right, above, or below it). For example, starting with the arrangement above, I can restore the “natural” arrangement (below) by swapping the * with the 2, then with the 5,and then with the 8:
1 2 3
4 5 6
7 8 *
The object of the game is to begin with the squares in a random order, and to get them into the “natural” order shown above, using only valid swaps between the special symbol and neighbouring numerals. Valid input is the digits 1-8 and valid moves are the horizontal or vertical squares adjacent to the special symbol. Invalid swaps will not change the order of any squares. Use the project name
Here is my code.
Thanks in advance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include <iostream>
#include <array>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int random_matrix[3][3];
int i;
int j;
srand ( time(NULL) ); // initialize once only
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
random_matrix[i][j]= rand() % 10;
}
for(int x=0;x<3;x++) // loop 3 times for three lines
{
for(int y=0;y<3;y++) // loop for the three elements on the line
{
if (random_matrix[x][y] ==0)
{
remove(0);
0=='*';
cout<<'*';
}
cout<<random_matrix[x][y]; // display the current element out of the array
}
cout<<endl; // when the inner loop is done, go to a new line
}
return 0;
}
|