chording support
This commit is contained in:
parent
dda721f8e9
commit
c1e89c5122
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,3 +2,4 @@ lib
|
||||
.vscode
|
||||
bin
|
||||
build
|
||||
Makefile
|
19
README.md
19
README.md
@ -10,6 +10,16 @@ Marking all mines with flags isn't necessary, but helps.
|
||||
|
||||
### Clear all non-mine cells to win!
|
||||
|
||||
## Controls
|
||||
|
||||
Arrows to move.
|
||||
Z to reveal.
|
||||
X to flag.
|
||||
C to chord.
|
||||
R to restart.
|
||||
P to pause.
|
||||
Q to quit.
|
||||
|
||||
## Compilation
|
||||
|
||||
By default, the Makefile compiles in debug mode, so to compile both, use `make both`.
|
||||
@ -56,12 +66,3 @@ sudo dnf install ncurses-devel
|
||||
Search engines are your best friend.
|
||||
|
||||
Then rename `Makefile.unix`, press the make button, and a moment later, you got it.
|
||||
|
||||
## Controls
|
||||
|
||||
Arrows to move.
|
||||
Z to reveal.
|
||||
X to flag.
|
||||
R to restart.
|
||||
P to pause.
|
||||
Q to quit.
|
||||
|
@ -20,6 +20,7 @@ public:
|
||||
int getMineCount() const;
|
||||
void regenerateBoard();
|
||||
bool isGameWon() const;
|
||||
std::vector<Cell> getNeighborsOf(int x, int y);
|
||||
|
||||
private:
|
||||
Vector2 size;
|
||||
|
@ -38,6 +38,7 @@ public:
|
||||
void toggleFlag();
|
||||
Content getContent() const;
|
||||
void setContent(Content newContent);
|
||||
Vector2 getPosition() const;
|
||||
|
||||
private:
|
||||
State state = State::Hidden;
|
||||
|
@ -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));
|
||||
|
@ -36,3 +36,8 @@ 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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user