summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--Makefile2
-rw-r--r--README.md7
-rw-r--r--TODO.txt3
-rw-r--r--src/Game.cpp170
-rw-r--r--src/Game.hpp4
-rw-r--r--src/main.cpp4
7 files changed, 115 insertions, 77 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..931307c
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+*.o
+minesweeper
diff --git a/Makefile b/Makefile
index d94defb..4c5a947 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
CXX = g++
-CXXFLAGS = -std=c++17 -Wall -Wextra -O2
+CXXFLAGS = -std=c++17 -g -O0 -Wall -Wextra -O2
SRC = src/Game.cpp src/main.cpp
OBJ = $(SRC:.cpp=.o)
diff --git a/README.md b/README.md
index b2ac4e4..3b5ec6f 100644
--- a/README.md
+++ b/README.md
@@ -2,6 +2,9 @@
# minesweeper
-A reimplementation of minesweeper game in C++ in terminal.
+A reimplementation of minesweeper
+game in C++ in terminal.
-Written in ![https://www.vim.org/](assets/vim_on_fire.gif) and VS Codium.
+Written in
+![Vim](assets/vim_on_fire.gif)
+and VS Codium.
diff --git a/TODO.txt b/TODO.txt
new file mode 100644
index 0000000..c693be6
--- /dev/null
+++ b/TODO.txt
@@ -0,0 +1,3 @@
+Things to do:
+1. fix tile revealing
+2. add flag placing
diff --git a/src/Game.cpp b/src/Game.cpp
index a703913..d98c5e9 100644
--- a/src/Game.cpp
+++ b/src/Game.cpp
@@ -12,7 +12,7 @@ void Game::initDiff(int diff){
boardHeight = 9;
mines = 9;
break;
- case 1:
+ case 1:
boardWidth = 16;
boardHeight = 16;
mines = 12;
@@ -22,41 +22,39 @@ void Game::initDiff(int diff){
boardHeight = 16;
mines = 16;
break;
- case 3:
- std::cout << "Editing game parameters\n";
- break;
}
}
void Game::initBoard(){
- // Bomb map (boolean): 0 or false is no bomb, 1 or true is bomb;
- // clearing bomb map
- for(int i=0; i < boardHeight; i++){
- for(int j=0; j < boardWidth; j++){
- bombMap[i][j] = 0;
+ for(int y=0; y < boardHeight; y++){
+ for(int x=0; x < boardWidth; x++){
+ bombMap[y][x] = false;
+ revealedMap[y][x] = false;
+ tileMap[y][x] = 0;
+ flagMap[y][x] = false;
}
}
- // initializing bomb map
-
- for(int i=0; i < boardHeight; i++){
- for(int j=0; j < boardWidth; j++){
- for(int k = 0; k < mines; k++){
- bombMap[i][j] = rand() % 2;
- }
+ int placed = 0;
+ while(placed < mines){
+ int x = rand() % boardWidth;
+ int y = rand() % boardHeight;
+
+ if(!bombMap[y][x]){
+ bombMap[y][x] = true;
+ placed++;
}
}
+}
- // initializing tile map for display
-
- for(int i=0; i < boardHeight; i++){
- for(int j=0; j < boardWidth; j++){
- tileMap[i][j] = 0;
+void Game::updateBoard(){
+ for(int y=0; y < boardHeight; y++){
+ for(int x=0; x < boardWidth; x++){
+ if(!revealedMap[y][x]) continue;
+ else if(flagMap[y][x] == true) continue;
- // DEBUG: seeing bombs
- if(bombMap[i][j] == true){
- tileMap[i][j] = 11;
- }
+ int bombs = bombCheck(x, y);
+ tileMap[y][x] = bombs == 0 ? 1 : bombs + 1;
}
}
}
@@ -115,58 +113,84 @@ void Game::displayBoard() {
system("clear");
#endif
- for(int i=0; i < boardHeight; i++){
- for(int j = 0; j < boardWidth; j++){
- if(tileMap[i][j] == 0){
- putchar('#');
- } else if(tileMap[i][j] == 1){
- putchar('.');
- } else if(tileMap[i][j] == 2){
- putchar('1');
- } else if(tileMap[i][j] == 3){
- putchar('2');
- } else if(tileMap[i][j] == 4){
- putchar('3');
- } else if(tileMap[i][j] == 5){
- putchar('4');
- } else if(tileMap[i][j] == 6){
- putchar('5');
- } else if(tileMap[i][j] == 7){
- putchar('6');
- } else if(tileMap[i][j] == 8){
- putchar('7');
- } else if(tileMap[i][j] == 9){
- putchar('8');
- } else if(tileMap[i][j] == 10){
- putchar('X');
- } else if(tileMap[i][j] == 11){
- putchar('B');
- }
+ for(int y=0; y < boardHeight; y++){
+ for(int x = 0; x < boardWidth; x++){
+ if(tileMap[y][x] == 0) putchar('#');
+ else if(tileMap[y][x] == 1) putchar('.');
+ else if(tileMap[y][x] == 10) putchar('F');
+ else putchar('0' + tileMap[y][x] - 1);
}
putchar('\n');
}
}
+int Game::bombCheck(int x, int y){
+ int count = 0;
+
+ for(int dy = -1; dy <= 1; dy++){
+ for(int dx = -1; dx <= 1; dx++){
+ if(dx == 0 && dy == 0) continue;
+
+ int nx = x + dx;
+ int ny = y + dy;
+
+ if(nx >= 0 && nx < boardWidth && ny >= 0 && ny < boardHeight && bombMap[ny][nx]){
+ count++;
+ }
+ }
+ }
+ return count;
+}
+
void Game::input(){
- std::string input;
-
- std::cout << "game>";
- std::cin >> input;
-
- if(input == "q"){
- inGame = false;
-
- } else if(input == "d"){
- int x, y;
- std::cout << "Enter column and row where to dig: ";
- std::cin >> x >> y;
- tileMap[x][y] = 1;
- } else if(input == "f"){
- int x, y;
- std::cout << "Enter column and row where to place flag: ";
- std::cin >> x >> y;
- tileMap[x][y] = 10;
- } else {
- std::cout << "Unknown command '" << input << "'.\n";
+ std::string cmd;
+ std::cout << "game> ";
+ std::cin >> cmd;
+
+ if(std::cin.fail()){
+ std::cout << "Input failed." << std::endl;
+ inGame = false;
+ return;
}
-}
+
+ if(cmd == "q"){
+ inGame = false;
+ return;
+ }
+
+ int x, y;
+ std::cin >> x >> y;
+
+ if(x < 0 || x >= boardWidth || y < 0 || y >= boardHeight) return;
+ if(cmd == "d"){
+ if(bombMap[y][x]){
+ std::cout << "BOOM! Game over\n";
+ inGame = false;
+ return;
+ }
+
+ if(!revealedMap[y][x]){
+ revealedMap[y][x] = true;
+ if(bombCheck(x, y) == 0){
+ for(int dy = -1; dy <= 1; dy++){
+ for(int dx = -1; dx <= 1; dx++){
+ int nx = x + dx;
+ int ny = y + dy;
+
+ if(nx >= 0 && nx < boardWidth && ny >= 0 && ny < boardHeight && !revealedMap[ny][nx]){
+ revealedMap[ny][nx] = true;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ if(cmd == "f"){
+ if(revealedMap[y][x]){
+ std::cout << "You can't place flag here.\n";
+ } else {
+ flagMap[y][x] = true;
+ }
+ }
+}
diff --git a/src/Game.hpp b/src/Game.hpp
index 61a788e..599259f 100644
--- a/src/Game.hpp
+++ b/src/Game.hpp
@@ -10,6 +10,8 @@ private:
bool bombMap[MAX_H][MAX_W];
int tileMap[MAX_H][MAX_W];
+ bool revealedMap[MAX_H][MAX_W];
+ bool flagMap[MAX_H][MAX_W];
int mines;
@@ -19,7 +21,9 @@ public:
int editDiff();
void initDiff(int diff);
void initBoard();
+ void updateBoard();
void displayBoard();
+ int bombCheck(int x, int y);
void input();
};
diff --git a/src/main.cpp b/src/main.cpp
index d4dab15..afea063 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -7,7 +7,7 @@ int main(){
game.inGame = false;
int choice;
- cout << "Welcome to minesweeper 0.1.0\n";
+ cout << "Welcome to minesweeper v0.2.0\n";
cout << "\n";
cout << "Select difficulty:\n"
"1. Easy\n"
@@ -31,11 +31,13 @@ int main(){
game.inGame = true;
}
+ srand(time(nullptr));
game.initBoard();
while(game.inGame){
game.displayBoard();
game.input();
+ game.updateBoard();
}
return 0;