-
Notifications
You must be signed in to change notification settings - Fork 0
/
SliderPuzzle.hpp
58 lines (48 loc) · 1.74 KB
/
SliderPuzzle.hpp
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
#ifndef _SLIDERPUZZLE_HPP
#define _SLIDERPUZZLE_HPP
#include <iostream>
#include <string>
using namespace std;
/*
SliderPuzzle.hpp
Popular puzzle where you slide tiles around a board, e.g.,
the "15 Puzzle", the "8 Puzzle", etc.
*/
class SliderPuzzle : public MazeState {
public:
SliderPuzzle(int, int, string);
SliderPuzzle(const SliderPuzzle&); // Deep copy constructor
~SliderPuzzle();
// returns true if this MazeState is a solution for the puzzle
bool isSolution();
// returns a vector of possible next positions for the puzzle.
vector<MazeState*> getSuccessors();
// If you want to use BestFS, you must assign a priority value to
// all puzzle states. (If you don't want to use BestFS, you
// can just return 0 for all MazeStates.)
//
// returns a non-negative integer representing a guess of how far we are
// from a solution. Bigger means farther from solution.
int getBadness();
// For many dictionary implementations, it's convenient to have
// a unique ID for each state (so we can sort them, compare them,
// hash them, etc.)
//
// Returns a unique string for any state
string getUniqId(void);
// print the puzzle state
void print (ostream& out);
private:
const int rows; // number of rows
const int cols; // number of columns
int *board; // array of size rows*cols to hold the board
int empty_row; // row of the empty space
int empty_col; // col of the empty space
string uniqId; // memoized copy of unique ID
int badness; // memoized copy of badness
void slide_down(); // slide a tile down into empty space
void slide_up(); // slide a tile up into empty space
void slide_right(); // slide a tile right into empty space
void slide_left(); // slide a tile left into empty space
};
#endif