-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
172 lines (151 loc) · 4.3 KB
/
Board.java
File metadata and controls
172 lines (151 loc) · 4.3 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
public class Board {
//public static ArrayList<Color> colorBoard;
public static int size = 7;
private static int[][] key;
public static int[][] gameBoard;
private static int totalMines;
public Board(){
key = new int[size][size];
gameBoard = new int[size][size];
//colorList();
MMethod();
totalMines = (int)((size * size) * 0.15625);
putBombs();
fillKey();
}
//fills the board with mines randomly
public void putBombs(){
int row = (int)(size * Math.random());
int col = (int)(size * Math.random());
for(int i = 0; i < totalMines; i++){
while(key[row][col] == 10)
{
row = (int)(size * Math.random());
col = (int)(size * Math.random());
}
key[row][col] = 10;
}
}
public void fillKey(){
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
if(key[i][j] != 10){
key[i][j] = findNumberOfMines(i,j);
}
}
}
}
public int findNumberOfMines(int row, int col)
{
int numberOfBomb = 0;
if( row -1 >= 0 &&
col -1 >= 0 &&
key[row-1][col-1] == 10)
{
numberOfBomb++;
}
if( row -1 >= 0 &&
col >= 0 &&
key[row-1][col] == 10)
{
numberOfBomb++;
}
if( row -1 >= 0 &&
col +1 < key[0].length &&
key[row-1][col+1] == 10)
{
numberOfBomb++;
}
if( row >= 0 &&
col -1 >= 0 &&
key[row][col-1] == 10)
{
numberOfBomb++;
}
if( row >= 0 &&
col +1 < key[0].length &&
key[row][col+1] ==10)
{
numberOfBomb++;
}
if( row + 1 < key.length &&
col -1 >= 0 &&
key[row+1][col-1] == 10)
{
numberOfBomb++;
}
if( row +1 < key.length &&
col >= 0 &&
key[row+1][col] == 10)
{
numberOfBomb++;
}
if( row +1 < key.length &&
col +1 < key[0].length &&
key[row+1][col+1] ==10)
{
numberOfBomb++;
}
//changing the int to a string
//return numberOfBombs;
return numberOfBomb;
}
public static void keyToBoard(int row, int col){
if(gameBoard[row][col] != 11) gameBoard[row][col] = key[row][col];
}
public static void printGameBoard(){
for(int i =0; i < size; i++){
for(int j=0; j < size;j++){
System.out.print(gameBoard[i][j] + " ");
}
System.out.println();
}
}
public static void MMethod(){
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
gameBoard[i][j] = -1;
}
}
}
public static int getIndex(int row, int col){
return key[row][col];
}
public static void clickedBomb(){
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
gameBoard[i][j] = 10;
key[i][j] = 10;
}
}
}
public static void putFlag(int row, int col){
//11 means flag
gameBoard[row][col]= 11;
}
public static boolean checkWinOrLost(){
int bombToFlag = 0;
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
if(key[i][j] == 10 && gameBoard[i][j] ==11){
bombToFlag++;
}
}
}
return bombToFlag == totalMines;
}
public static int checkFlags(){
int flagsPlaced = 0;
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
if(gameBoard[i][j] ==11){
flagsPlaced++;
}
}
}
return flagsPlaced;
}
public static int numOfMines(){
return totalMines;
}
}