-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMineModel.java
More file actions
154 lines (143 loc) · 2.9 KB
/
MineModel.java
File metadata and controls
154 lines (143 loc) · 2.9 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
//John Tran
//This class does all of the calulations and functionality of the game
import java.util.*;
import java.io.Serializable;
public class MineModel implements Serializable
{
public static final int SAFE = 4;
public static final int MINE_FLAG = 3;
public static final int FLAG = 2;
public static final int MINE = 1;
public static final int NO_MINE = 0;
public static final int C = 10;
public static final int R = 10;
public static int sqauresClicked = 0;
private boolean gameStatus = false;
private boolean gameWon = false;
public static int bombs = 30;
private int[][] board = new int[R][C];
private int[][] neighbors = new int [R][C];
//sets up the board with mines
public MineModel()
{
for(int i = 0; i < bombs; i++)
{
Random rand = new Random();
int r = rand.nextInt(R);
int c = rand.nextInt(C);
board[r][c] = MINE;
}
for(int j = 0; j < R; j++)
{
for(int k = 0; k < C; k++)
{
if(board[j][k] == MINE)
{
neighbors[j][k] = MINE;
}
else
{
board[j][k] = NO_MINE;
neighbors[j][k] = (findNeighbors(j, k));
}
}
}
}
//finds nearby mines
public int findNeighbors(int r, int c)
{
int numOfMines = 0;
for (int i = r - 1; i <= r + 1; i++)
{
for (int j = c - 1; j <= c + 1; j++)
{
if (((i < 0) || (i >= R) || (j < 0) || (j >= C)))
{
continue;
}
if (board[i][j] == MINE)
{
numOfMines++;
}
}
}
return numOfMines;
}
//flags a square and gives it a certain value if it's a mine or not
public void flagSquare(int r, int c)
{
if (board[r][c] == MINE)
{
board[r][c] = MINE_FLAG;
}
else if (board[r][c] == MINE_FLAG)
{
board[r][c] = MINE;
}
if(board[r][c] == NO_MINE)
{
board[r][c] = FLAG;
}
else if (board[r][c] == FLAG)
{
board[r][c] = NO_MINE;
}
}
//this reveals a square. If all squares were successfully cleared the game is won
//if there is a mine, the game is lost
public void revealSquare(int r, int c)
{
if(board[r][c] == NO_MINE)
{
board[r][c] = SAFE;
sqauresClicked++;
System.out.println(sqauresClicked);
if(sqauresClicked == R*C - bombs)
{
gameWon = true;
}
}
if (board[r][c] == MINE)
{
setStatus();
for (int i = 0; i < R; i++)
{
for(int j = 0; j < C; j++)
{
if(board[i][j] == MINE_FLAG)
{
board[i][j] = MINE;
}
}
}
}
}
public void setSquares()
{
sqauresClicked = 0;
}
public void setBombs(int amount)
{
bombs = amount;
}
public boolean getWin()
{
return gameWon;
}
public void setStatus()
{
gameStatus = true;
}
public boolean getStatus()
{
return gameStatus;
}
public int getMine(int x, int y)
{
return board[x][y];
}
public int getNeighbor(int x, int y)
{
return neighbors[x][y];
}
}