-
Notifications
You must be signed in to change notification settings - Fork 0
/
aialgorithm.h
58 lines (46 loc) · 1.32 KB
/
aialgorithm.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
#ifndef AIALGORITHM_H
#define AIALGORITHM_H
#include <QObject>
#include <QList>
#include <QMap>
#include <QVector>
#include <QMap>
#include "aiproblem.h"
typedef QList<int> AiState;
struct AiNode
{
int cost; // Cost of this state
int totalCost; // Total cost until this state
int step; // The index of this state in solution path
AiState state; // State of the board stored in this struct
AiNode* previous; // Back-pointer
AiNode* next; // Next step in optimum solution path. Use this to track the path from start to goal.
bool operator== (const AiNode &node) const {
return this->state == node.state;
}
bool operator== (const AiState &state) const {
return this->state == state;
}
bool operator< (const AiNode &state) const {
return this->totalCost < state.totalCost;
}
};
class AiAlgorithm : public QObject
{
Q_OBJECT
public:
AiAlgorithm(AiProblem *problem, QObject *parent = 0)
: QObject(parent), mProblem(problem) { }
virtual ~AiAlgorithm() { };
public slots:
virtual AiNode run() = 0;
protected slots:
virtual bool foundGoalAt(AiNode& node) { return node == mProblem->mGoal; };
virtual void open(AiNode&) = 0;
virtual int getCostOfGoingTo(const AiState&) const = 0;
protected:
QVector<AiNode> mSetOfOpenNodes;
QMap<int, AiNode> mPriorityQueue;
AiProblem *mProblem;
};
#endif // AIALGORITHM_H