This repository has been archived by the owner on May 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tetramino.cpp
172 lines (140 loc) · 3.53 KB
/
Tetramino.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
#include "Tetramino.h"
#include "Cell.h"
#include "Game.h"
#include "Utiltities.h"
Tetramino::Tetramino(Game *game, TetraTypes *type, int offsetX, int offsetY, int rotation)
{
_type = type;
_game = game;
_rotation = rotation;
_offsetX = offsetX;
_offsetY = offsetY;
makeCells();
_realrot = 0;
_rotation = 0;
}
Tetramino::Tetramino(Game *game, vector<Cell*> *newCells, int offX, int offY)
{
_game = game;
_cells = *newCells;
_offsetX = offX;
_offsetY = offY;
}
Tetramino::~Tetramino()
{
return;
}
Cell* Tetramino::getFirstCell()
{
for (unsigned int i = 0; i < _cells.size(); i++)
if (_cells[i] != NULL)
return _cells[i];
return NULL;
}
// Still wrong ffs
bool Tetramino::getAdjacentCells(Tetramino *tet, Cell *cell, vector<Cell*> *connectedCells)
{
if (cell == NULL || cell->_visited || cell->_tetramino != tet) return false;
cell->_visited = true;
connectedCells->push_back(cell);
int posX = cell->_posX + cell->_tetramino->_offsetX;
int posY = cell->_posY + cell->_tetramino->_offsetY;
for (int x = posX - 1; x < posX + 1; x += 2)
if (x >= 0 && x < GRID_WIDTH)
getAdjacentCells(tet, _game->_grid[x][posY], connectedCells);
for (int y = posY - 1; y < posY + 1; y += 2)
if (y >= 0 && y < GRID_HEIGHT)
getAdjacentCells(tet, _game->_grid[posX][y], connectedCells);
return true;
}
bool Tetramino::isColliding()
{
bool flag = false;
for (unsigned int i = 0; i < _cells.size(); i++)
{
if (_cells[i] != NULL)
{
int cellPosX = _cells[i]->_posX + _offsetX;
int cellPosY = _cells[i]->_posY + _offsetY;
if (!flag && (cellPosX < 0 || cellPosX >= GRID_WIDTH || cellPosY < 0 || cellPosY >= GRID_HEIGHT))
flag = true;
if (!flag && _game->_grid[cellPosX][cellPosY] != NULL && _game->_grid[cellPosX][cellPosY]->_tetramino != this)
flag = true;
}
}
return flag;
}
bool Tetramino::tryTurn(Direction dir)
{
if (dir == TETRIS_DIR_UP || dir == TETRIS_DIR_DOWN) return false;
int oldreal = _realrot;
int oldrot = _rotation;
if (dir == TETRIS_DIR_LEFT)
_realrot = --_realrot;
else
_realrot = ++_realrot;
_rotation = abs(_realrot) % 4;
updateCellRotation();
if(isColliding())
{
_rotation = oldrot;
_realrot = oldreal;
updateCellRotation();
return false;
}
_game->updateGrid();
return true;
}
bool Tetramino::tryMove(Direction dir)
{
if (dir == TETRIS_DIR_UP) return false;
int oldX = _offsetX;
int oldY = _offsetY;
if (dir == TETRIS_DIR_DOWN)
_offsetY++;
else if (dir == TETRIS_DIR_LEFT)
_offsetX--;
else
_offsetX++;
int size = _game->_tets.size();
int pos = _offsetY;
if(isColliding())
{
_offsetX = oldX;
_offsetY = oldY;
return false;
}
_game->updateGrid();
return true;
}
void Tetramino::updateCellRotation()
{
vector<string> tempPatterns;
Utilities::stringExplode(_type->patterns[_rotation], ',', tempPatterns);
int cellind = 0;
for (unsigned int y = 0; y < tempPatterns.size(); y++)
for (unsigned int x = 0; x < tempPatterns[0].length(); x++)
{
if (tempPatterns[y][x] == '1' && _cells[cellind] != NULL)
{
_cells[cellind]->_posX = x;
_cells[cellind]->_posY = y;
cellind++;
}
}
}
void Tetramino::makeCells()
{
for (unsigned int i = 0; i < _cells.size(); i++)
delete _cells[i];
_cells.clear();
vector<string> tempPatterns;
Utilities::stringExplode(_type->patterns[_rotation], ',', tempPatterns);
for (unsigned int y = 0; y < tempPatterns.size(); y++)
for (unsigned int x = 0; x < tempPatterns[0].length(); x++)
if (tempPatterns[y][x] == '1')
{
Cell *newCell = new Cell(this, _type->tile, x, y);
_cells.push_back(newCell);
}
}