Array Swapping Game Help

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;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	const int num_elements = 2;
	int array[num_elements][num_elements] = { {1, 2}, {3, 4} };

	//swap

	int temp = array[0][0];
	//temp is 1

	array[0][0] = array[1][1];
	//array[0][0] is no longer 1, it's 4.

	array[1][1] = temp;
	//array[1][1], which used to be 4, is now 1.

	//The values are effectively swapped. 


Another thing to keep in mind:

Imagine a rubiks cube. If you simply peal off the colored squares and rearrange them randomly on the cube, it's likely you'll have a cube that cannot be solved, since the arrangement of colored cubes wouldn't otherwise ever appear naturally if the cube were just scrambled. You'd have an impossible configuration.

If you purely randomize the positions of the squares - as you're doing in the code you posted - more often than not, you'll end up with a grid that cannot be solved. Instead, you should simulate random valid moves to "shuffle" the grid first, so that it can be "unshuffled" and solvable.
Last edited on
This was my old code I posted the wrong one can you please check out my new post? http://www.cplusplus.com/forum/beginner/186972/ Thank you very much!
Topic archived. No new replies allowed.