-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMineSweeper.java
More file actions
210 lines (202 loc) · 6.22 KB
/
Copy pathMineSweeper.java
File metadata and controls
210 lines (202 loc) · 6.22 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import java.util.Scanner;
public class MineSweeper
{
//2D Arrays for mine placement and covered / uncovered status
private int[][] board;
private boolean[][] flipped;
private boolean[][] marked;
public boolean gameOver = false;
private int numMines = 0;
private int numFlipped = 0;
public static Scanner scan = new Scanner(System.in);
//Constructor
public MineSweeper(int rows, int cols)
{
board = new int[rows][cols];
flipped = new boolean[rows][cols];
marked = new boolean[rows][cols];
fillMines();
}
public void fillMines()
{
for(int row = 0; row < board.length; row++)
{
for(int tile = 0; tile < board[0].length; tile++)
{
int bit = (int) (Math.random()*9);
if(bit == 1)
{
board[row][tile] = -1;
numMines++;
}
}
}
}
public void fillTiles()
{
for(int row = 0; row < board.length; row++)
{
for(int tile = 0; tile < board[0].length; tile++)
{
if(board[row][tile] < 0)
{
for(int subRow = row-1; subRow <= row +1; subRow++)
{
for(int subTile = tile-1; subTile <= tile+1; subTile++)
{
if(subRow >= 0 && subRow < board.length && subTile >= 0 && subTile < board[0].length && board[subRow][subTile] >= 0)
{
board[subRow][subTile]++;
}
}
}
}
}
}
}
public void printBoard()
{
System.out.println();
for(int r = -1; r < board.length; r++)
{
for(int t = -1; t < board[0].length; t++)
{
if(r == -1)
{
System.out.print(t+1+" ");
}
else if(t == -1)
{
System.out.print(r+1+" ");
}
else if(flipped[r][t] == true)
{
if(board[r][t]==0)
{
System.out.print(" ");
}
else
{
System.out.print(board[r][t]+" ");
}
}
else if(marked[r][t] == true)
{
System.out.print("M ");
}
else
{
System.out.print("X ");
}
}
System.out.println();
}
System.out.println();
}
public void unCover(int r, int c)
{
//if flipped tile is a mine, game over
if(board[r][c]==-1)
{
if(marked[r][c] == false)
{
gameOver = true;
flipped[r][c]=true;
System.out.println("Game Over!!");
}
else
{
System.out.println("What are you crazy?? You marked that tile as a mine!!");
}
}
//if flipped tile has no adjacent mines, check surrounding tiles and flip as appropriate
else if(board[r][c] == 0)
{
flipped[r][c]=true;
numFlipped++;
for(int subRow = r-1; subRow <= r+1; subRow++)
{
for(int subCol = c-1; subCol <= c+1; subCol++)
{
if(subRow >= 0 && subRow < board.length && subCol >=0 && subCol < board[0].length //checking to make sure adjacent cell is within bounds
&& board[subRow][subCol] > -1 && flipped[subRow][subCol] == false) //making sure adjacent cell is not a mine & not flipped
{
unCover(subRow,subCol); //recursively call the unCover method on appropriate tiles adjacent to "0" tiles
}
}
}
}
//Tile gets flipped no matter what (unless it's a marked mine)
else {
flipped[r][c]=true;
numFlipped++;
}
printBoard();
if(numFlipped == (board.length*board[0].length)-numMines)
{
checkWin();
}
}
public void pickTile()
{
System.out.println("Pick a tile by typing the row number and column number separated by a space. To mark or unmark a tile as a mine, type \"M\" afterward. Then press enter.");
String tile = scan.nextLine();
tile = tile.toUpperCase();
String row = tile.substring(0,tile.indexOf(" "));
String col = tile.substring(tile.indexOf(" ")+1,tile.indexOf(" ")+2);
int r = Integer.parseInt(row)-1;
int c = Integer.parseInt(col)-1;
if(tile.indexOf("M")>=0)
{
if(marked[r][c] == false)
{
marked[r][c] = true;
printBoard();
if(numFlipped == (board.length*board[0].length)-numMines)
{
checkWin();
}
}
else {
marked[r][c] = false;
printBoard();
}
}
else
{
unCover(r,c);
}
}
//Checks for win condition and displays text when it is met.
public void checkWin()
{
boolean won = true;
for(int r = 0; r < board.length; r++)
{
for(int c =0; c < board[r].length; c++)
{
if(board[r][c]==-1 && !marked[r][c])
{
won = false;
}
}
}
if(won)
{
gameOver = true;
System.out.println("You did it! You marked all the mines! Good luck on the walk home! :)");
}
}
public static void main(String[] args)
{
System.out.println("Welcome to Minesweeper!");
MineSweeper mine = new MineSweeper(6,6);
mine.fillMines();
mine.fillTiles();
mine.printBoard();
while(!mine.gameOver)
{
mine.pickTile();
}
}
}