Skip to content

Commit

Permalink
Update Game.java
Browse files Browse the repository at this point in the history
  • Loading branch information
kek-Sec committed Feb 7, 2022
1 parent 436772f commit c267d63
Showing 1 changed file with 7 additions and 34 deletions.
41 changes: 7 additions & 34 deletions minesweeper/src/gr/hmu/tp4768/Models/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -388,43 +388,16 @@ public boolean checkAdjacentSquares(int row, int column, Square[][] grid) {
* @return true if game is over, false if not
*/
public boolean checkGameOver(Square[][] grid) {
// set max number of rows
int maxRows = grid.length;
// set max number of columns
int maxColumns = grid[0].length;
// set number of mines
int mines = 0;
// set number of revealed squares
int revealedSquares = 0;
// set number of flagged squares
int flaggedSquares = 0;
// loop through the grid
for (int i = 0; i < maxRows; i++) {
for (int j = 0; j < maxColumns; j++) {
// check if the square is a mine
if (grid[i][j].isMine()) {
mines++;
}
// check if the square is revealed
if (grid[i][j].isRevealed()) {
revealedSquares++;
}
// check if the square is flagged
if (grid[i][j].isFlagged()) {
flaggedSquares++;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
// check if the square is not revealed or flagged
if (!grid[i][j].isRevealed() && !grid[i][j].isFlagged()) {
return false;
}
}
}
// check if the number of mines equals the number of flagged squares
if (mines == flaggedSquares) {
// check if the number of revealed squares equals the number of squares
// minus the number of mines
if (revealedSquares == (maxRows * maxColumns) - mines) {
this.isGameOver = true;
return true;
}
}
this.isGameOver = false;
return false;
this.isGameOver = true;
return true;
}
}

0 comments on commit c267d63

Please sign in to comment.