chording support
This commit is contained in:
@@ -40,21 +40,12 @@ Board::Board(int w, int h, short mines) : size(w, h), mines(mines), isFirstClick
|
||||
if (cell.getContent() != Cell::Content::Mine)
|
||||
{
|
||||
int count = 0;
|
||||
for (int dx = -1; dx <= 1; dx++)
|
||||
std::vector<Cell> neighbors = getNeighborsOf(x, y);
|
||||
for (const Cell &neighbor : neighbors)
|
||||
{
|
||||
for (int dy = -1; dy <= 1; dy++)
|
||||
if (neighbor.getContent() == Cell::Content::Mine)
|
||||
{
|
||||
if (dx == 0 && dy == 0)
|
||||
continue;
|
||||
int nx = x + dx;
|
||||
int ny = y + dy;
|
||||
if (nx >= 0 && nx < w && ny >= 0 && ny < h)
|
||||
{
|
||||
if (cells[ny][nx].getContent() == Cell::Content::Mine)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
count++;
|
||||
}
|
||||
}
|
||||
cell.setContent(static_cast<Cell::Content>(count));
|
||||
@@ -63,6 +54,24 @@ Board::Board(int w, int h, short mines) : size(w, h), mines(mines), isFirstClick
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<Cell> Board::getNeighborsOf(int x, int y)
|
||||
{
|
||||
std::vector<Cell> neighbors;
|
||||
for (int dx = -1; dx <= 1; dx++)
|
||||
{
|
||||
for (int dy = -1; dy <= 1; dy++)
|
||||
{
|
||||
if (dx == 0 && dy == 0)
|
||||
continue;
|
||||
int nx = x + dx;
|
||||
int ny = y + dy;
|
||||
if (nx >= 0 && nx < size.x && ny >= 0 && ny < size.y)
|
||||
neighbors.push_back(cells[ny][nx]);
|
||||
}
|
||||
}
|
||||
return neighbors;
|
||||
}
|
||||
|
||||
bool Board::isGameOver()
|
||||
{
|
||||
return b_gameOver;
|
||||
@@ -130,22 +139,30 @@ void Board::revealEmptyCells(int x, int y)
|
||||
|
||||
if (current.getContent() == Cell::Content::Empty)
|
||||
{
|
||||
for (int dx = -1; dx <= 1; dx++)
|
||||
// for (int dx = -1; dx <= 1; dx++)
|
||||
// {
|
||||
// for (int dy = -1; dy <= 1; dy++)
|
||||
// {
|
||||
// if (dx == 0 && dy == 0)
|
||||
// continue;
|
||||
// int nx = pos.x + dx;
|
||||
// int ny = pos.y + dy;
|
||||
// if (nx >= 0 && nx < size.x && ny >= 0 && ny < size.y)
|
||||
// {
|
||||
// Cell &neighbor = cells[ny][nx];
|
||||
// if (neighbor.getState() == Cell::State::Hidden)
|
||||
// {
|
||||
// q.push(Vector2(nx, ny));
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
std::vector<Cell> neighbors = getNeighborsOf(pos.x, pos.y);
|
||||
for (const Cell &neighbor : neighbors)
|
||||
{
|
||||
for (int dy = -1; dy <= 1; dy++)
|
||||
if (neighbor.getState() == Cell::State::Hidden)
|
||||
{
|
||||
if (dx == 0 && dy == 0)
|
||||
continue;
|
||||
int nx = pos.x + dx;
|
||||
int ny = pos.y + dy;
|
||||
if (nx >= 0 && nx < size.x && ny >= 0 && ny < size.y)
|
||||
{
|
||||
Cell &neighbor = cells[ny][nx];
|
||||
if (neighbor.getState() == Cell::State::Hidden)
|
||||
{
|
||||
q.push(Vector2(nx, ny));
|
||||
}
|
||||
}
|
||||
q.push(Vector2(neighbor.position.x, neighbor.position.y));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -229,21 +246,12 @@ void Board::regenerateBoard()
|
||||
if (cell.getContent() != Cell::Content::Mine)
|
||||
{
|
||||
int count = 0;
|
||||
for (int dx = -1; dx <= 1; dx++)
|
||||
std::vector<Cell> neighbors = getNeighborsOf(x, y);
|
||||
for (const Cell &neighbor : neighbors)
|
||||
{
|
||||
for (int dy = -1; dy <= 1; dy++)
|
||||
if (neighbor.getContent() == Cell::Content::Mine)
|
||||
{
|
||||
if (dx == 0 && dy == 0)
|
||||
continue;
|
||||
int nx = x + dx;
|
||||
int ny = y + dy;
|
||||
if (nx >= 0 && nx < size.x && ny >= 0 && ny < size.y)
|
||||
{
|
||||
if (cells[ny][nx].getContent() == Cell::Content::Mine)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
count++;
|
||||
}
|
||||
}
|
||||
cell.setContent(static_cast<Cell::Content>(count));
|
||||
|
||||
@@ -35,4 +35,9 @@ Cell::Content Cell::getContent() const
|
||||
void Cell::setContent(Content newContent)
|
||||
{
|
||||
content = newContent;
|
||||
}
|
||||
|
||||
Vector2 Cell::getPosition() const
|
||||
{
|
||||
return position;
|
||||
}
|
||||
10
src/main.cpp
10
src/main.cpp
@@ -188,6 +188,16 @@ void startGame(Board &board)
|
||||
startPauseTime = time(NULL);
|
||||
}
|
||||
isPaused = !isPaused;
|
||||
break;
|
||||
case 'c':
|
||||
auto neighbors = board.getNeighborsOf(cursorX, cursorY);
|
||||
for (const Cell &neighbor : neighbors)
|
||||
{
|
||||
if (neighbor.getState() == Cell::State::Flagged)
|
||||
continue;
|
||||
board.revealCellAt(neighbor.getPosition().x, neighbor.getPosition().y);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (isPaused)
|
||||
|
||||
Reference in New Issue
Block a user