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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
//Main.cpp
int main()
{
const int SIZE = 3;
const int dimension = SIZE * SIZE;
Game g;
int game0[SIZE*SIZE] = { 1, 2, 3, 7, 4, 0, 6, 5, 8 };
g.playGiven("game 0", game0, dimension); //game0 is not passing whole array
}
//inside Game.cpp
void Game::playGiven(std::string boardName, int boardArray[], int size) {
inital.makeBoard(boardArray, size);
solve();
}
//inside Board.cpp
//Create a board from a given set of values
void Board::makeBoard(int values[], int size) {
int c = 0;
for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++) {
if (values[c] == 0) {
blankRow = i;
blankCol = j;
}
board[i][j] = values[c++];
}
}
//inside Game.h
//defines the game class
class Game {
private:
Queue g;
Board inital;
Board final_1;
public:
void playGiven(std::string, int array[], int); //Use a userdefined board
void playRandom(std::string, const int); //From a perfect board, use the random function within board to generate a random board. void solve(); // Solve the puzzle
};
//Board.h
//defines the board class
class Board {
private:
char lastMove;
string boardMoves;
public:
void setMove(char);
void appendBoardMoves(char);
char getMove();
string getBoardMoves();
static const int SIZE = 3;
int board[SIZE][SIZE]; // Values of board
Board(const Board & b); //Create board from another board
void makeBoard(int jumbleCt = 0); //Starting from a perfect board, do jumbleCt moves to alter it
void makeBoard(int values[], int); // Create a board from a set of values specified in row major order
string toString() const; //return a string which represents the contents of the boar
bool operator==(Board &b); //Return true if two boards are equal
int blankRow; // Row location of blank
int blankCol; // Column locatio of blank
Board() { makeBoard(); };
bool slideUp(); // If possible, slides a tile up into the blank position. Returns success of operation.
bool slideDown(); // If possible, slides a tile down into the blank position. Returns success of operation.
bool slideLeft(); // If possible, slides a tile left into the blank position. Returns success of operation.
bool slideRight(); // If possible, slides a tile right into the blank position. Returns success of operation.
void jumble(int ct); //Do jumble moves to alter boardf
char makeMove(char m, char lastMove); //Makes move indicated by m, returns character name of move if done, will not undo previous lastMove
};
| |