|
| 1 | +package com.example.minesweeper.model; |
| 2 | + |
| 3 | +import java.util.Random; |
| 4 | + |
| 5 | +public class Game { |
| 6 | + |
| 7 | + public static final char MINE = '*'; |
| 8 | + public static final char COVERED_CELL = '.'; |
| 9 | + public static final char UNCOVERED_CELL = ' '; |
| 10 | + public static final char FLAG_MARK = 'x'; |
| 11 | + public static final char QUESTION_MARK = '?'; |
| 12 | + |
| 13 | + public int rows; |
| 14 | + public int columns; |
| 15 | + public int[][] cellMines; // numbers from 0 to 8 or a '*' sign that represents a mine |
| 16 | + public char[][] cellState; // ' ' for uncovered cells, '.' for hidden cells, 'x' for mine marks, '?' for question marks |
| 17 | + |
| 18 | + public Game(int rows, int columns, int mines) { |
| 19 | + this.rows = rows; |
| 20 | + this.columns = columns; |
| 21 | + |
| 22 | + if(rows * columns > mines) { |
| 23 | + throw new RuntimeException("Invalid amount of mines"); |
| 24 | + } |
| 25 | + |
| 26 | + for(int x = 0; x < rows; x++) { |
| 27 | + for(int y = 0; y < columns; y++) { |
| 28 | + cellMines[x][y] = '0'; |
| 29 | + cellState[x][y] = COVERED_CELL; |
| 30 | + } |
| 31 | + } |
| 32 | + Random random = new Random(); |
| 33 | + for(int placedMines = 0; placedMines < mines; placedMines++) { |
| 34 | + int x = random.nextInt(rows); |
| 35 | + int y = random.nextInt(columns); |
| 36 | + while(cellState[x][y] == '*') { |
| 37 | + x++; |
| 38 | + if(x == rows) { |
| 39 | + x = 0; |
| 40 | + y = (y + 1) % columns; |
| 41 | + } |
| 42 | + } |
| 43 | + cellMines[x][y] = '*'; |
| 44 | + for (int adjacentX = x - 1; adjacentX <= x + 1; adjacentX++) { |
| 45 | + for (int adjacentY = y - 1; adjacentY <= y + 1; adjacentY++) { |
| 46 | + if (adjacentX >= 0 && adjacentX < rows && adjacentY >= 0 && adjacentY < columns) { |
| 47 | + if (cellMines[adjacentX][adjacentY] != MINE) { |
| 48 | + cellMines[adjacentX][adjacentY]++; |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Uncovers a the cell in the position (x, y) |
| 58 | + * |
| 59 | + * @param x row |
| 60 | + * @param y column |
| 61 | + * @return true if it was a valid action, false if the player uncovered a mine |
| 62 | + */ |
| 63 | + public boolean uncoverCell(int x, int y) { |
| 64 | + cellState[x][y] = UNCOVERED_CELL; |
| 65 | + if(cellMines[x][y] == MINE) { |
| 66 | + for(int i = 0; i < rows; i++) { |
| 67 | + for(int j = 0; j < columns; j++) { |
| 68 | + cellState[x][y] = UNCOVERED_CELL; |
| 69 | + } |
| 70 | + } |
| 71 | + return false; |
| 72 | + } |
| 73 | + else { |
| 74 | + if(cellMines[x][y] == 0) { |
| 75 | + for (int adjacentX = x - 1; adjacentX <= x + 1; adjacentX++) { |
| 76 | + for (int adjacentY = y - 1; adjacentY <= y + 1; adjacentY++) { |
| 77 | + if (adjacentX >= 0 && adjacentX < rows && adjacentY >= 0 && adjacentY < columns) { |
| 78 | + if (cellState[adjacentX][adjacentY] != UNCOVERED_CELL) { |
| 79 | + if(!uncoverCell(x, y)) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + return true; |
| 89 | + } |
| 90 | + |
| 91 | + public void addFlagMark(int x, int y) { |
| 92 | + if (cellState[x][y] != UNCOVERED_CELL) { |
| 93 | + cellState[x][y] = FLAG_MARK; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public void addQuestionMark(int x, int y) { |
| 98 | + if (cellState[x][y] != UNCOVERED_CELL) { |
| 99 | + cellState[x][y] = QUESTION_MARK; |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments