We created an ADT called SqrMatrix in class.  You can download our code from www.cs.csi.cuny.edu/~zelikovi/csc326/data/SqrMatrixfiles.htm  This contains the header file SqrMatrix.h with the class declaration, and the implementation file, SqrMatrix.cpp.

 

This assignment is meant to give you experience in using an ADT. 

 

  1. Add a new public member function to the class SqrMatrix.  This function, Compare, accepts a SqrMatrix as a parameter and compares self to the parameter, returning true if the two are the same and false otherwise.  Remember, that if the two matrices are not the same size, then they cannot be equal.

 

  1. Let us create client code that uses the SqrMatrix to play the 8 puzzle.  The 8 puzzle is a 3 by 3 grid of numbers 1-8 with one empty space.  For example, a desired state of the 8 puzzle might be:

 

1

2

3

4

 

5

6

7

8

 

 

The game is played by moving tiles into the blank space until a final goal configuration is found. 

 

Create one SqrMatrix called goal that is initialized to the values in the above example.  You may use a 0 to denote the blank space.

 

A text file is at the bottom of the SqrMatrix code file that you downloaded. Create another SqrMatrix called game, that reads in the initial values that are on the first three lines of  the data file.   This is the configuration of the tiles when the person starts playing the game.

 

Use the public member function Compare to see if game is the same as goal.  While it is not, read the next three lines of the file into game, as that is the configuration of the game after one move.  We are assuming that these consecutive snapshots of the game are available in the file.  Keep track of the number of moves it takes until the goal is reached.

 

Print out each consecutive game board, as well as the total number of moves that it took to reach the goal.