-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle_move.h
60 lines (49 loc) · 1.5 KB
/
puzzle_move.h
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
#ifndef PUZZLEMOVE_H
#define PUZZLEMOVE_H
#include "board.h"
using namespace std;
struct PuzzleMove
{
// Data members can be public
int tileMove; // tile moved to reach the Board b
Board *b; // Pointer to a board representing the updated state
int g; // distance from the start board
int h; // heuristic distance to the goal
PuzzleMove *prev; // Pointer to parent PuzzleMove
// Constructor for starting Board (i.e. initial move)
PuzzleMove(Board* board);
// Constructor for subsequent search boards
// (i.e. those returned by Board::potentialMoves() )
PuzzleMove(int tile, Board* board, PuzzleMove *parent);
// Destructor
~PuzzleMove();
};
struct PuzzleMoveScoreComp
{
bool operator()(const PuzzleMove *m1, const PuzzleMove *m2) const
{
// Check if m1's f-score is less than m2's
// If both have the same f-score, break ties by
// checking if m1's h-score is less than m2's.
// If both have the same f and same h score, break ties
// by returning true when m1's tileMove is less than m2's.
if (m1->g + m1->h != m2->g + m2->h){
return m1->g + m1->h < m2->g + m2->h;
}else if (m1->h != m2->h){
return m1->h < m2->h;
}
else {
return m1->tileMove < m2->tileMove;
}
}
};
struct PuzzleMoveBoardComp
{
bool operator()(const PuzzleMove *m1, const PuzzleMove *m2) const
{
// Check if m1's board is "less-than" m2's board by
// using the Board class' operator<()
return (*(m1->b) < *(m2->b));
}
};
#endif