-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.cpp
151 lines (127 loc) · 3.15 KB
/
game.cpp
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
140
141
142
143
144
145
146
147
148
149
150
151
#include <cstdlib>
#include <ctime>
#include "game.h"
using namespace std;
namespace game {
const double PROB_DRAW2 = 0.9;
Tiles::Tiles (size_t sz)
: _done (false), _score (0), _max (0), _size (sz)
{
// init board
size_t n = _size*_size;
_grid.reserve (n);
for (size_t i=0;i<n;++i)
_grid.emplace_back (new Cell);
// init row pointers
for (size_t r=0;r<_size;++r) {
Line row;
for (size_t c=0;c<_size;++c)
row.push_back (_grid[r*_size+c]);
_rows.push_back (row);
}
// init col pointers
for (size_t c=0;c<_size;++c) {
Line col;
for (size_t r=0;r<_size;++r)
col.push_back (_grid[r*_size+c]);
_cols.push_back (col);
}
// let's play
insert_random ();
}
Tiles::~Tiles ()
{
for (auto& c : _grid) delete c;
}
void Tiles::move (Direction d)
{
if (_done) return;
bool moved = false;
switch (d) {
case LEFT:
for (auto& r : _rows)
moved |= collapse (&r, r.begin (), r.end ());
break;
case RIGHT:
for (auto& r : _rows)
moved |= collapse (&r, r.rbegin (), r.rend ());
break;
case UP:
for (auto& c : _cols)
moved |= collapse (&c, c.begin (), c.end ());
break;
case DOWN:
for (auto& c : _cols)
moved |= collapse (&c, c.rbegin (), c.rend ());
break;
}
if (moved) insert_random ();
else check_status ();
}
template<class T>
bool Tiles::collapse (Line* l, T begin, T end)
{
bool moved = false;
auto lit = begin, rit = lit;
while (lit != end) {
while (rit != end && (*rit)->val==0) ++rit;
if (rit == end) return moved;
Cell *lhs = *lit, *rhs = *rit;
if (lit == rit) {
++rit;
}
else if (*lhs == 0) {
lhs->val = rhs->val;
rhs->val = 0;
moved = true;
++rit;
}
else if (*lhs == *rhs) {
lhs->val += rhs->val;
rhs->val = 0;
_score += lhs->val;
if (lhs->val > _max) _max = lhs->val;
moved = true;
++lit;
rit = lit;
}
else {
++lit;
}
}
return moved;
}
vector<Cell*> Tiles::available ()
{
vector<Cell*> cells;
for (auto& r : _rows)
for (auto& c : r)
if (!c->val) cells.push_back (c);
return cells;
}
bool Tiles::collapsable (Line *l)
{
for (auto it=l->begin ();it!=--l->end ();++it) {
Cell *lhs = *it, *rhs = *(it+1);
if (*lhs == *rhs) return true;
else if (*lhs == 0 || *rhs == 0) return true;
}
return false;
}
void Tiles::check_status ()
{
for (auto& r : _rows)
if (collapsable (&r)) return;
for (auto& c : _cols)
if (collapsable (&c)) return;
_done = true;
}
void Tiles::insert_random ()
{
vector<Cell*> cells = available ();
if (cells.empty ()) return;
Cell *c = cells[rand () % cells.size ()];
double p = double (rand ())/double (RAND_MAX);
c->val = (p < PROB_DRAW2) ? 2 : 4;
}
} // game