Skip to content

Commit b190dce

Browse files
committed
Fix bug with showing number of mines. Looks like I had to check for MINE_SEALED state in GetAdjacentCells().
Add TextComponent to all cells. I don't see the point of adding it to only adjacent mine cells because we are also resetting the game that may change some normal cells to adjacent mine cells which need the TextComponent.
1 parent 91837f1 commit b190dce

File tree

3 files changed

+45
-20
lines changed

3 files changed

+45
-20
lines changed

src/Cell.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "Grid.h"
99
#include "RectangleMeshComponent.h"
1010
#include "LineMeshComponent.h"
11+
#include "TextComponent.h"
1112

1213
Cell::Cell(Game *game, Grid *grid)
1314
:Actor(game)
@@ -58,6 +59,21 @@ void Cell::SetIndex(int x, int y)
5859
mIndex.y = y;
5960
}
6061

62+
void Cell::Reset()
63+
{
64+
mCellType = UNEXPOSE;
65+
auto textComp = (TextComponent*)(GetComponent("TextComponent"));
66+
if (textComp)
67+
{
68+
textComp->SetIsShow(false);
69+
}
70+
else
71+
{
72+
printf("TextComponent is null!!!\n");
73+
}
74+
mNumOfMines = 0;
75+
}
76+
6177

6278
//
6379
//Cell::Cell(Game* game)

src/Cell.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class Cell : public Actor
4141
// Static cell length
4242
static const int LENGTH = 100;
4343

44+
void Reset();
45+
4446
private:
4547
CellType mCellType;
4648
class Grid* mGrid;

src/Grid.cpp

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ void Grid::Initialize()
5151
mesh->SetWidth(Cell::LENGTH);
5252
mesh->SetHeight(Cell::LENGTH);
5353

54+
auto textComp = new TextComponent("TextComponent", cell, 2);
55+
textComp->SetColor(RAYWHITE);
56+
textComp->SetFont(GetFontDefault());
57+
textComp->SetFontSize(40.f);
58+
textComp->SetSpacing(0.f);
59+
textComp->SetText(std::to_string(cell->GetNumOfMines()));
60+
textComp->SetIsShow(false);
61+
5462
// Add line mesh component for the sealed state of cells
5563
auto lineMesh = new LineMeshComponent("LineMeshComponent", cell, 2);
5664
lineMesh->SetThickness(3.f);
@@ -70,15 +78,11 @@ void Grid::Reset()
7078
{
7179
for (auto cell : row)
7280
{
73-
cell->SetCellType(UNEXPOSE);
74-
// Reset TextComponent
75-
auto textComp = reinterpret_cast<TextComponent*>(cell->GetComponent("TextComponent"));
76-
if (textComp)
77-
{
78-
textComp->SetIsShow(false);
79-
}
81+
// Reset cell to original state
82+
cell->Reset();
8083
}
8184
}
85+
// Re-setup mines randomly
8286
SetMines();
8387
GetGame()->SetGameState(Game::PLAYING);
8488
}
@@ -144,7 +148,7 @@ void Grid::SetMines()
144148
i = GetRandomValue(0, mRows - 1);
145149
j = GetRandomValue(0, mColumns - 1);
146150
// Set Mine cell
147-
if (mCellList[i][j]->GetCellType() != MINE)
151+
if (mCellList[i][j]->GetCellType() == UNEXPOSE)
148152
{
149153
mCellList[i][j]->SetCellType(MINE);
150154
mMineList.push_back(mCellList[i][j]);
@@ -173,7 +177,7 @@ int Grid::GetAdjacentCells(Cell *cell, std::vector<Cell*>& adjacentCells)
173177
{
174178
// Valid adjacent cell
175179
adjacentCells.push_back(mCellList[i][j]);
176-
if (mCellList[i][j]->GetCellType() == MINE)
180+
if (mCellList[i][j]->GetCellType() == MINE || mCellList[i][j]->GetCellType() == MINE_SEALED)
177181
{
178182
numOfMines++;
179183
}
@@ -182,7 +186,7 @@ int Grid::GetAdjacentCells(Cell *cell, std::vector<Cell*>& adjacentCells)
182186
{
183187
// Valid adjacent cell
184188
adjacentCells.push_back(mCellList[z][j]);
185-
if (mCellList[z][j]->GetCellType() == MINE)
189+
if (mCellList[z][j]->GetCellType() == MINE || mCellList[z][j]->GetCellType() == MINE_SEALED)
186190
{
187191
numOfMines++;
188192
}
@@ -198,7 +202,7 @@ int Grid::GetAdjacentCells(Cell *cell, std::vector<Cell*>& adjacentCells)
198202
if ((y - 1) >= 0)
199203
{
200204
adjacentCells.push_back(mCellList[x][y - 1]);
201-
if (mCellList[x][y - 1]->GetCellType() == MINE)
205+
if (mCellList[x][y - 1]->GetCellType() == MINE || mCellList[x][y - 1]->GetCellType() == MINE_SEALED)
202206
{
203207
numOfMines++;
204208
}
@@ -207,7 +211,7 @@ int Grid::GetAdjacentCells(Cell *cell, std::vector<Cell*>& adjacentCells)
207211
if ((y + 1) < mColumns)
208212
{
209213
adjacentCells.push_back(mCellList[x][y + 1]);
210-
if (mCellList[x][y + 1]->GetCellType() == MINE)
214+
if (mCellList[x][y + 1]->GetCellType() == MINE || mCellList[x][y + 1]->GetCellType() == MINE_SEALED)
211215
{
212216
numOfMines++;
213217
}
@@ -243,16 +247,19 @@ void Grid::Expose(Cell *cell)
243247
cell->SetNumOfMines(numOfMines);
244248
// Check if this cell already has a TextComponent
245249
auto textComp = reinterpret_cast<TextComponent*>(cell->GetComponent("TextComponent"));
246-
if (!textComp)
250+
if (textComp)
247251
{
248-
textComp = new TextComponent("TextComponent", cell, 2);
252+
textComp->SetColor(RAYWHITE);
253+
textComp->SetFont(GetFontDefault());
254+
textComp->SetFontSize(40.f);
255+
textComp->SetSpacing(0.f);
256+
textComp->SetText(std::to_string(cell->GetNumOfMines()));
257+
textComp->SetIsShow(true);
258+
}
259+
else
260+
{
261+
printf("TextComponent is null!!!\n");
249262
}
250-
textComp->SetColor(RAYWHITE);
251-
textComp->SetFont(GetFontDefault());
252-
textComp->SetFontSize(40.f);
253-
textComp->SetSpacing(0.f);
254-
textComp->SetText(std::to_string(cell->GetNumOfMines()));
255-
textComp->SetIsShow(true);
256263
}
257264
else
258265
{

0 commit comments

Comments
 (0)