-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGameAlgorithm.h
More file actions
139 lines (112 loc) · 2.3 KB
/
GameAlgorithm.h
File metadata and controls
139 lines (112 loc) · 2.3 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#ifndef GAMEALGORITHM_H
#define GAMEALGORITHM_H
#include <QString>
#include <vector>
#include <random>
using namespace ::std::tr1;
const int SIDE_LEN = 64;
const unsigned int DEFAULT_VALUE = 2;
const unsigned int DEFAULT_VALUE4 = 4;
const unsigned int ZERO = 0;
const unsigned int TARGET = 2048; // 16 for test purposes
struct Board
{
unsigned int v;
bool n;
bool m;
std::vector<int> ids;
Board(unsigned int val = NULL, bool newp = false, bool merged = false)
: v(val)
, n(newp)
, m(merged)
, ids()
{
}
Board(const Board& b)
: v(b.v)
, n(b.n)
, m(b.m)
, ids(b.ids)
{
}
const Board& operator = (unsigned int val)
{
v = val;
if (val == ZERO)
ids.clear();
return *this;
}
const Board& operator = (const Board& b)
{
v = b.v;
n = b.n;
m = b.m;
ids = b.ids;
return *this;
}
operator unsigned int () const
{
return v;
}
};
class GameAlgorithm
{
public:
explicit GameAlgorithm(int nDim);
~GameAlgorithm();
QString serializeToString() const;
void deserializeFromString(const QString strImage);
int score() const;
int tile() const;
void setScore(int nScore);
struct Point2D
{
unsigned int x;
unsigned int y;
Board b;
Point2D()
: x(0)
, y(0)
, b(NULL, false, false)
{
}
Point2D(unsigned int cx, unsigned int cy, const Board& cb)
: x(cx)
, y(cy)
, b(cb)
{
}
};
typedef std::vector<Point2D> Point2DArr;
typedef std::pair<Point2DArr, Point2DArr> Point2DArrPair;
enum Move
{
UP,
DOWN,
LEFT,
RIGHT
};
Point2DArr getInitialBoard(void);
Point2DArrPair updateBoard(Move move);
bool hasStillAChance(void);
protected:
void putNextNumbersOnBoard(int nHowMany, Point2DArr& freeSlots);
private:
const int getNextNumberPoint(const Point2DArr& freeSlots);
void moveUp(void);
void moveDown(void);
void moveLeft(void);
void moveRight(void);
void mergeUp(void);
void mergeDown(void);
void mergerLeft(void);
void mergeRight(void);
void clearBoard();
const unsigned int mergePoints(const unsigned int v1, const unsigned int v2) const;
unsigned int defaultValue();
Board m_board[SIDE_LEN][SIDE_LEN];
int m_nSideLen;
unsigned int m_nScore;
mt19937 m_mt19937;
};
#endif // GAMEALGORITHM_H