-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.cpp
214 lines (182 loc) · 4.58 KB
/
grid.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
#include <SFML/Graphics.hpp>
#include <numeric>
#include "grid.hpp"
#include <iostream>
#include <stdexcept>
namespace tetris
{
// Constructors
GameBoard::GameBoard() {}
GameBoard::GameBoard(int &height, int &width) : grid(height, std::vector<int>(width, 0)),
m_height(height), m_width(width), score(0), lines_cleared(0)
{
try
{
if (height < 5 || height > 50 || width < 5 || width > 50)
{
throw std::exception();
}
}
catch (const std::exception &e)
{
std::cerr << "Cannot build a board of " << width << " x " << height << " " << std::endl;
}
}
void GameBoard::generate_new_piece()
{
std::srand(std::time(nullptr));
block = std::rand() % 7 + 1;
b_x = std::rand() % (m_width - 4);
b_y = 0;
current_piece = shapes.at(block);
}
bool GameBoard::in_bounds()
{
for (int y = 0; y < 4; ++y)
{
for (int x = 0; x < 4; ++x)
{
if (current_piece[y][x] == 0)
continue;
int grid_x = x + b_x;
int grid_y = y + b_y;
if (grid_x < 0 || grid_x >= m_width || grid_y < 0 || grid_y >= m_height)
return false;
if (grid_y >= 0 && grid[grid_y][grid_x])
return false;
if (grid_x < 0)
return false; // Check if the piece is too far to the left
}
}
return true;
}
bool GameBoard::has_hit_pile()
{
for (int y = 0; y < 4; ++y)
{
for (int x = 0; x < 4; ++x)
{
if (current_piece[y][x] == 0)
continue;
int gridY = y + b_y;
int gridX = x + b_x;
if (gridY < 0 || gridY >= m_height || gridX < 0 || gridX >= m_width || grid[gridY][gridX])
return true;
}
}
return false;
}
// clears the lines
void GameBoard::shift_down()
{
int deleted_line = m_height - 1;
int linesCleared = 0; // Initialize linesCleared to count cleared lines
for (int undeleted_line = m_height - 1; undeleted_line >= 0; undeleted_line--)
{
int count_width = 0;
for (int x = 0; x < m_width; ++x)
{
if (grid[undeleted_line][x])
count_width++;
}
if (count_width < m_width)
{
for (int x = 0; x < m_width; ++x)
{
grid[deleted_line][x] = grid[undeleted_line][x];
}
--deleted_line;
}
else
{
++linesCleared;
sf::sleep(sf::milliseconds(20)); // Increment linesCleared for each cleared line
}
}
lines_cleared += linesCleared;
score += (linesCleared * linesCleared) * 100;
// Update your score here with the number of lines cleared
// For example: score += linesCleared;
}
bool GameBoard::move_down()
{
// moves the piece down
++b_y;
if (has_hit_pile())
{
// moves it back up
--b_y;
for (int y = 0; y < 4; ++y)
{
for (int x = 0; x < 4; ++x)
{
if (current_piece[y][x])
{
int grid_row = b_y + y;
int grid_col = b_x + x;
// Check boundary conditions before accessing grid elements
if (grid_row >= 0 && grid_row < m_height && grid_col >= 0 && grid_col < m_width)
{
// this aids for color generate of the pile
grid[grid_row][grid_col] = block;
}
}
}
}
shift_down();
generate_new_piece();
return false;
}
return true;
}
void GameBoard::rotate()
{
// create a temp 2d vector for the rotated_piece
std::vector<std::vector<int>> rotated_block(4, std::vector<int>(4, 0));
for (int y = 0; y < 4; ++y)
{
for (int x = 0; x < 4; ++x)
{
if (current_piece[y][x])
{
rotated_block[3 - x][y] = 1;
}
}
}
// this makes the current_piece rotated
current_piece = std::move(rotated_block);
}
Grid &GameBoard::getGameState()
{
return grid;
}
const int &GameBoard::getBlock()
{
return block;
}
bool GameBoard::is_game_over()
{
for (int y = 0; y < 4; y++)
{
for (int x = 0; x < 4; x++)
{
if (current_piece[y][x] == 1)
{
int world_x = b_x + x;
int world_y = b_y + y;
// Check if the block is out of the top boundary
if (world_y < 0)
{
return true;
}
// Check if the block collides with existing blocks in the world
if (world_y >= 0 && grid[world_y][world_x] != 0)
{
return true;
}
}
}
}
return false;
}
}