diff --git a/README.md b/README.md index a0d16ed..2bb3c4f 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ Minesweeper is a classic puzzle game where the objective is to clear a rectangul The board consists of hidden cells, some containing mines. Uncover a cell by selecting its coordinates. If it's a mine, you lose. If it's safe, a number appears indicating how many mines are adjacent to that cell. **Use logic to deduce mine locations and mark them with flags.** +Marking all mines with flags isn't necessary, but helps. ### Clear all non-mine cells to win! @@ -61,4 +62,6 @@ Then rename `Makefile.unix`, press the make button, and a moment later, you got Arrows to move. Z to reveal. X to flag. -R to restart. +R to restart. +P to pause. +Q to quit. diff --git a/src/main.cpp b/src/main.cpp index 144b507..cbbdfcc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,16 +19,22 @@ void startGame(Board &board) bool gameRunning = true; time_t startTime = time(nullptr); - int elapsedTime; + int elapsedTime = 0; + bool isPaused = false; + time_t startPauseTime = 0; + time_t pauseTime = 0; + time_t totalpausetime = 0; while (gameRunning) { + usleep((1000 / MAX_TIME) * 1000); if (board.isGameOver() || board.isGameWon()) { gameRunning = false; } - elapsedTime = difftime(time(NULL), startTime); + if (!isPaused) + elapsedTime = difftime(time(NULL), startTime) - totalpausetime; char flags[5]; char tim[5]; sprintf(flags, "%03d", minesLeft); @@ -36,7 +42,10 @@ void startGame(Board &board) attron(COLOR_PAIR(3)); mvprintw(1, 1, "%s", flags); attroff(COLOR_PAIR(3)); - mvprintw(1, 5, ":)"); + if (!isPaused) + mvprintw(1, 5, ":)"); + else + mvprintw(1, 5, ":|"); attron(COLOR_PAIR(3)); mvprintw(1, 8, "%s", tim); attroff(COLOR_PAIR(3)); @@ -75,6 +84,9 @@ void startGame(Board &board) displayChar = 'X'; } + if (isPaused) + displayChar = '#'; + if (x == cursorX && y == cursorY) { attron(A_REVERSE); @@ -161,9 +173,25 @@ void startGame(Board &board) case 'r': startTime = time(NULL); board.regenerateBoard(); + somethingHasBeenDone = false; + elapsedTime = 0; break; + case 'p': + if (isPaused) + { + pauseTime = difftime(time(NULL), startPauseTime); + totalpausetime += pauseTime; + } + else + { + startPauseTime = time(NULL); + } + isPaused = !isPaused; } + if (isPaused) + continue; + refresh(); }