-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameLogic.java
More file actions
216 lines (191 loc) · 6.15 KB
/
Copy pathGameLogic.java
File metadata and controls
216 lines (191 loc) · 6.15 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
211
212
213
214
215
216
import java.util.*;
public class GameLogic {
public String[][] board;
int row,col,bombs,safe;
Set<String> location;
int noOfFlag;
public GameLogic(int row,int col){
this.row=row;
this.col=col;
board=new String[row][col];
double percentage=getMinePercenatge(row,col);
bombs=(int)Math.round(row*col*percentage);
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
board[i][j]="E";
}
}
location=setBombs(row,col,bombs);
safe=(row*col)-location.size();
noOfFlag=location.size();
//display(board);
//System.out.println(location);
//System.out.println("No of safes:"+safe);
//startGame(board,location);
}
private double getMinePercenatge(int row, int col) {
int mull=row*col;
if(mull==81)return 0.12;
if(mull==256)return 0.16;
else return 0.21;
}
public int[] breakDownCell(String s){
String[] part=s.split(",");
return new int[]{Integer.parseInt(part[0]),Integer.parseInt(part[1])};
}
public void startGame(String[][] board,Set<String> locations){
Scanner input=new Scanner(System.in);
while(this.safe!=0) {
int x, y;
System.out.println("ENTER-X:");
x = input.nextInt();
System.out.println("Enter-Y");
y = input.nextInt();
String click = x + "," + y;
int[] loc = breakDownCell(click);
//System.out.println("The cell u clicked is "+loc[0]+" "+loc[1]+" and the cell is "+board[loc[0]][loc[1]]);
if(checkBomb(click,locations)){return;}
if (board[loc[0]][loc[1]].equals("E")) {
int count=findAdj(board, loc[0], loc[1]);
//System.out.println(count+"is the adj COUNT FOR:"+loc[0]+":"+loc[1]);
if ( count!=0) {
this.safe--;
board[x][y] = String.valueOf(count);
} else {
BFS(board, x, y);
}
}
//display(board);
}
gameOver(true);
}
public Boolean checkBomb(String click, Set<String> locations) {
if (locations.contains(click)) {
int[] loc=breakDownCell(click);
board[loc[0]][loc[1]] = "X";
gameOver(false);
return true;
}
return false;
}
private void BFS(String[][] board, int x, int y) {
int[][] directions={{0,1},{1,0},{-1,0},{0,-1},{1,1},{-1,-1},{1,-1},{-1,1}};
Set<String> visited=new HashSet<>();
visited.add(x+","+y);
Queue<String> q=new LinkedList<>();
q.add(x+","+y);
if(board[x][y].equals("E")){
this.safe--;
}
while(!q.isEmpty()){
int l= q.size();
for(int i=0;i<l;i++){
String node=q.poll();
int[] loc=breakDownCell(node);
int count=findAdj(board,loc[0],loc[1]);
if(count>0){
board[loc[0]][loc[1]]= String.valueOf(count);
}
else {
board[loc[0]][loc[1]]="B";
for (int[] dir : directions) {
int nx = dir[0] + loc[0];
int ny = dir[1] + loc[1];
if (nx < 0 || ny < 0 || nx >= row || ny >= col || visited.contains(nx + "," + ny)) {
continue;
} else {
if(board[nx][ny].equals("E")){
this.safe--;
}
q.add(nx+","+ny);
visited.add(nx+","+ny);
}
}
}
}
}
}
int findAdj(String[][] board,int x,int y){
int count=0;
int[][] directions={{0,1},{1,0},{-1,0},{0,-1},{1,1},{-1,-1},{1,-1},{-1,1}};
for(int[] dir:directions){
int nx=x+dir[0];
int ny=y+dir[1];
if(nx<0||ny<0||nx>=row||ny>=col){
continue;
}
if(location.contains(nx+","+ny)){
count++;
}
}
return count;
}
public void gameOver(boolean b){
if(!b){
System.out.println("BOOM!");
}
else{
System.out.println("U HAVE WON!!");
}
}
public void display(String[][] board){
for(int i=0;i<board.length;i++){
for(int j=0;j<board[0].length;j++){
System.out.print(board[i][j]+" ");
}
System.out.println();
}
System.out.println(this.safe);
}
public Set<String> setBombs(int row, int col, int bombs) {
Random rand=new Random();
Set<String> bombSet=new HashSet<>();
while(bombs!=0){
int x=rand.nextInt(row);
int y=rand.nextInt(col);
String new_string=x+","+y;
if(!bombSet.contains(new_string)){
bombSet.add(new_string);
this.board[x][y]="M";
bombs--;
}
}
return bombSet;
}
public String[][] getBoard() {
return this.board;
}
public void flagIt(int x, int y) {
if(board[x][y].equals("E") || board[x][y].equals("M")){
board[x][y]="F";
noOfFlag--;
}else if(board[x][y].equals("F")){
if(location.contains(x+","+y)){
board[x][y]="M";
}
else {
board[x][y]="E";
}
noOfFlag++;
}
}
public Boolean revealCell(int x, int y) {
if(board[x][y].equals("M")){
return false;
}
if (board[x][y].equals("E")) {
int count=findAdj(board,x,y);
if ( count!=0) {
this.safe--;
board[x][y] = String.valueOf(count);
} else {
BFS(board, x, y);
}
}
return true;
}
public int fetchSafe() {
//System.out.println(this.safe);
return this.safe;
}
}