-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinesweeperGame.java
138 lines (128 loc) · 4.69 KB
/
MinesweeperGame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
Description: The actual mine sweeper game.
Author: Theo N.
Contributors: n/a
Date: 2/12/19
*/
public class MinesweeperGame {
private int rows;
private int columns;
private int mines;
private int board[][];
private int visibleBoard[][];
public MinesweeperGame(int inputRows, int inputColumns, int inputMines) { // Service Constructor
rows = inputRows;
columns = inputColumns;
mines = inputMines;
visibleBoard = new int[rows][columns];
board = this.createBoard();
board = populateBoard(board);
visibleBoard();
}
public int playerInteract(int inputRow, int inputColumn) { // Determines the result of an interaction with the board and changes all components appropriately
if (board[inputRow][inputColumn] == 9) {
return 2;
}
else {
visibleBoard[inputRow][inputColumn] = board[inputRow][inputColumn];
}
for (int i = 0; i< rows; i++) { // Checks for a win, if they haven't won yet, then they need to keep playing
for (int j = 0; j < columns; j++) {
if (board[i][j] != 9) {
if (visibleBoard[i][j] == board[i][j]) {
visibleBoard[i][j] = visibleBoard[i][j];
}
else {
return 0;
}
}
}
}
return 1;
}
public void getVisibleBoard() {
printBoard(visibleBoard);
} // Calls the "visible" board, i.e. what the player can see
public void getBoard() {
printBoard(board);
} // Calls the master board with all of the answers
private int[][] createBoard(){ // Helper Method to instantiate the board
int temp[][] = new int[rows][columns];
return temp;
}
private int[][] populateBoard(int inputBoard[][]) { // Helper Method to randomly place mines
while (mines != 0) {
int a = (int)(Math.random()*rows);
int b = (int)(Math.random()*columns);
if (inputBoard[a][b] == 0) {
inputBoard[a][b] = 9;
mines -= 1;
}
}
for(int i = 0; i < rows; i++) { // This section then assigns each space of number based on the surrounding mines.
for (int j = 0; j < columns; j++) {
if (inputBoard[i][j] != 9) {
int tempMines = 0;
if ((j - 1) != -1) {
if (inputBoard[i][j - 1] == 9) {
tempMines += 1;
}
}
if ((i - 1) != -1) {
if (inputBoard[i - 1][j] == 9) {
tempMines += 1;
}
}
if ((i - 1) != -1 && (j - 1) != -1) {
if (inputBoard[i - 1][j - 1] == 9) {
tempMines += 1;
}
}
if ((i - 1) != -1 && (j + 1) != columns) {
if (inputBoard[i - 1][j + 1] == 9) {
tempMines += 1;
}
}
if ((i + 1) != rows) {
if (inputBoard[i + 1][j] == 9) {
tempMines += 1;
}
}
if ((j + 1) != columns) {
if (inputBoard[i][j + 1] == 9) {
tempMines += 1;
}
}
if ((i + 1) != rows && (j + 1) != columns) {
if (inputBoard[i + 1][j + 1] == 9) {
tempMines += 1;
}
}
if ((i + 1) != rows && (j - 1) != -1) {
if (inputBoard[i + 1][j - 1] == 9) {
tempMines += 1;
}
}
inputBoard[i][j] = tempMines;
}
}
}
return (inputBoard);
}
private void printBoard(int inputBoard[][]) { // Prints the board in a neat matrix fashion
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(inputBoard[i][j] + " ");
}
System.out.println();
}
}
private void visibleBoard() { // Generates a blank "visible board"
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
visibleBoard[i][j] = -1;
}
}
printBoard(visibleBoard);
}
}