-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.cpp
338 lines (314 loc) · 6.52 KB
/
table.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include "cell.h"
#include "table.h"
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>
#include "Cmd.h"
using namespace std;
Table::Table(int sizeX, int sizeY)
{
// crée la table
_sizeX = sizeX;
_sizeY = sizeY;
_lengthAllCoords = _sizeY * _sizeX;
_inGame = 1;
_played = 0;
_score = 0;
_Cells.resize(_sizeY);
for (int j = 0; j < _sizeY; j++)
{
_Cells[j].resize(_sizeX);
}
NextTurn();
}
Table::Table() : Table( 4, 4 )
{
// crée la table XxY à a taille 4x4
}
void Table::resetAllCells()
{
// reset toutes les values des cells à 0
_score = 0;
for (int j = 0; j < _sizeY; j++)
{
for (int i = 0; i < _sizeX; i++)
{
_Cells[j][i].reset();
}
}
}
void Table::Regen()
{
// Regènere la table (fait un nextTurn pour générer les deux valeurs de base)
resetAllCells();
NextTurn();
}
void Table::Gen(int number)
{
// Appelle genereNew sur une case vide au hasard (ou plusieurs,dépend de la valeur number)
int listSize = _list.size();
for (int k = 0; k < number; k++) {
int randomNumber = rand() % (listSize);
_list[randomNumber]->genereNew();
for (int j = randomNumber; j < listSize - 1; j++)
{
_list[j] = _list[j + 1];
}
listSize--;
}
}
void Table::getEmptyCells()
{
// push dans _list toutes les cases vides (value = 0)
_list.clear();
for (int i = 0; i < _sizeY;i++) {
for (int j = 0; j < _sizeX; j++) {
if (_Cells[i][j].getValue() == 0) {
_list.push_back(&_Cells[i][j]);
}
}
}
}
void Table::NextTurn() {
// Appelle gen pour générer sur une ou plusieurs cases(dépend si toutes les cases sont vides ou non
_played = 0;
getEmptyCells();
if (_lengthAllCoords == _list.size()) {
Gen(2);
}
else {
Gen(1);
}
// Appelle gameOver et si gameOver renvoie une valeur "vraie" (en l'occurence 1 ou 2) change la valeur de _inGame
int isTheGameOver = gameOver();
if (isTheGameOver) {
_inGame = 0;
}
_played = 0;
}
int Table::gameOver() {
// Regarde si le joueur a atteint 2048
getEmptyCells();
_played = 0;
for (int i = 0; i < _sizeY; i++)
{
for (int j = 0; j < _sizeX; j++)
{
if (_Cells[i][j].getValue() == 2048) {
cout << endl <<"Victory";
return 2;
}
}
}
// Regarde si des mouvements sont possibles (grip et fusion)
if (_list.size() == 0)
{
for (int j = 0; j < _sizeY; j++)
{
for (int i = 0; i < _sizeX; i++)
{
if (j < _sizeY - 1)
{
if (_Cells[j][i].getValue() == _Cells[j + 1][i].getValue())
{
_played = 1;
}
}
if (i < _sizeX - 1)
{
if (_Cells[j][i].getValue() == _Cells[j][i + 1].getValue())
{
_played = 1;
}
}
}
}
if(!_played)
{
cout << endl << "Defeat";
return 1;
}
}
// Sinon renvoie 0 pour que la partie continue
return 0;
}
void Table::setCell(int x, int y, Cell cell)
{
// set une cell à des coordonnées spécifiques
_Cells[y][x] = cell;
}
void Table::setCells(vector<vector<Cell>> cells)
{
// set un tableau entier de cell
_Cells = cells;
}
Cell Table::getCell(int x, int y)
{
// retoure la cell en coordonée (x, y)
return _Cells[y][x];
}
void Table::RotateGrid(int repeat)
{
// tourne la grille dans le sens horaire
Table actual = Table(_sizeX, _sizeY);
actual.setCells(_Cells);
for (int k = 0; k < repeat; k++)
{
Table otherTable = Table(actual._sizeY, actual._sizeX);
for (int j = 0; j < actual._sizeY; j++)
{
for (int i = 0; i < actual._sizeX; i++)
{
otherTable.setCell(j, i, actual.getCell(i, actual._sizeY - 1 - j));
}
}
actual = otherTable;
}
_sizeX = actual._sizeX;
_sizeY = actual._sizeY;
_Cells = actual._Cells;
}
void Table::grip()
{
// déplace vers la gauche les cases avec une valeur
for (int j = 0; j < _sizeY; j++)
{
int offset = 0;
for (int i = 0; i < _sizeX; i++)
{
if (getCell(i, j).getValue() == 0)
{
offset++;
}
else if (offset != 0)
{
setCell(i - offset, j, getCell(i, j));
_Cells[j][i].reset();
_played = 1;
}
}
}
}
void Table::fusion()
{
// fusionne deux cases : si deux cases sont collées (direction horizontale) et de même valeur,
// la case de gauche s'ajoutera la valeur de celle de droite et cette dernière sera reset
for (int j = 0; j < _sizeY; j++)
{
for (int i = 1; i < _sizeX ; i++)
{
if (_Cells[j][i - 1].getValue() == _Cells[j][i].getValue()&& _Cells[j][i - 1].getValue() != 0)
{
_Cells[j][i - 1].doubl();
_Cells[j][i].reset();
_score += _Cells[j][i - 1].getValue();
_played = 1;
}
}
}
}
void Table::actionLeft(bool lockedWithoutGen)
{
// mouvement vers la gauche (déplacement/fusion/déplacement)
// puis regarde si un coup s'est joué, si oui appelle nextTurn qui regénère une value grâce à Gen
grip();
fusion();
grip();
if (!lockedWithoutGen && _played)
{
NextTurn();
}
}
void Table::actionRight(bool lockedWithoutGen)
{
// Pareil mais vers la droite
// pour cela, on fait tourner la grille de 180° avant et après les actions
RotateGrid(2);
grip();
fusion();
grip();
RotateGrid(2);
if (!lockedWithoutGen && _played)
{
NextTurn();
}
}
void Table::actionUp(bool lockedWithoutGen)
{
// Pareil mais vers le haut
// pour cela, on fait tourner la grille de 270° avant les actions, puis de 90° après
RotateGrid(3);
grip();
fusion();
grip();
RotateGrid(1);
if (!lockedWithoutGen && _played)
{
NextTurn();
}
}
void Table::actionDown(bool lockedWithoutGen)
{
// Pareil mais vers le haut
// pour cela, on fait tourner la grille de 90° avant les actions, puis de 270° après
RotateGrid(1);
grip();
fusion();
grip();
RotateGrid(3);
if (!lockedWithoutGen && _played)
{
NextTurn();
}
}
void Table::createCopy(Table gettingTable)
{
// créer une copie complète de gettingTable vers la table actuel (changement de taille compris dans le processus)
resetAllCells();
_sizeX = gettingTable._sizeX;
_sizeY = gettingTable._sizeY;
_Cells.resize(_sizeY);
for (int j = 0; j < _sizeY; j++)
{
_Cells[j].resize(_sizeX);
}
for (int j = 0; j < _sizeY; j++)
{
for (int i = 0; i < _sizeX; i++)
{
setCell(i, j, Cell(gettingTable.getCell(i, j).getValue()));
}
}
}
bool Table::compare(Table tableToCompare)
{
// vérifie si le contenu de tableToCompare et de la table actuel sont les même, renvoie tru si vrai
if (_sizeX != tableToCompare._sizeX)
{
return false;
}
if (_sizeY != tableToCompare._sizeY)
{
return false;
}
for (int j = 0; j < _sizeY; j++)
{
for (int i = 0; i < _sizeX; i++)
{
if (getCell(i, j).getValue() != tableToCompare.getCell(i, j).getValue())
{
return false;
}
}
}
return true;
}
int Table::getScore()
{
return _score;
}
void Table::ShowGrid(bool cls)
{
Cmd::printOnConsole(this, cls);
}