Newer
Older
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package GUI.game;
import java.util.Random;
import javax.swing.JButton;
public class Model
{
Controller controller;
public JButton[][] squares;
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
public Model(Controller controller)
{
this.controller = controller;
squares = new JButton[controller.settings.size][controller.settings.size];
}
/*
* 012
* 345
* 678
*/
private String[] getNeighbours(int[] pos)
{
int actX = (pos[0]);
int actY = (pos[1]);
String[] neighbours = new String[9];
neighbours[0] = (Integer.toString(actX-1) + ":" + Integer.toString(actY+1));
neighbours[1] = (Integer.toString(actX) + ":" + Integer.toString(actY+1));
neighbours[2] = (Integer.toString(actX+1) + ":" + Integer.toString(actY+1));
neighbours[3] = (Integer.toString(actX-1) + ":" + Integer.toString(actY));
neighbours[4] = (Integer.toString(actX) + ":" + Integer.toString(actY));
neighbours[5] = (Integer.toString(actX+1) + ":" + Integer.toString(actY));
neighbours[6] = (Integer.toString(actX-1) + ":" + Integer.toString(actY-1));
neighbours[7] = (Integer.toString(actX) + ":" + Integer.toString(actY-1));
neighbours[8] = (Integer.toString(actX+1) + ":" + Integer.toString(actY-1));
if( (actX + 1) >= this.controller.settings.size)
{
neighbours[2] = "-1:-1";
neighbours[5] = "-1:-1";
neighbours[8] = "-1:-1";
}
if( (actY+1) >= this.controller.settings.size )
{
neighbours[0] = "-1:-1";
neighbours[1] = "-1:-1";
neighbours[2] = "-1:-1";
}
return neighbours;
}
public void calculateButtonStyle(int[] pos)
{
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
if( this.isMined(pos[0], pos[1]))
{
for (int i = 0; i < this.controller.settings.size; i++)
{
for (int j = 0; j < this.controller.settings.size; j++)
{
if( isMined(i, j))
{
squares[i][j].setText("*");
}
else
{
}
}
}
}
else
{
int mines = this.calculateMinesAround(pos[0], pos[1]);
if( mines == 0 )
{
ArrayList<int[]> nulls = this.getNullFieldAround(pos[0], pos[1], null,null);
for( int[] nullMiner : nulls )
{
this.squares[nullMiner[0]][nullMiner[1]].setText("0");
}
}
else
{
squares[pos[0]][pos[1]].setText(Integer.toString(mines));
}
}
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
// System.out.println("Actual:" + pos[0] + ":" + pos[1]);
// for (int i = 0; i < 9; i++) {
// System.out.println(i + "-" + this.getNeighbours(pos)[i]);
// }
}
public int[] getPosFromString(String pos)
{
String[] splitted;
splitted = pos.split(":");
int[] po = new int[2];
po[0] = Integer.parseInt(splitted[0]);
po[1] = Integer.parseInt(splitted[1]);
return po;
}
private boolean isMined(int x, int y)
{
if( x < 0 || y < 0 || x > this.controller.settings.size || y > this.controller.settings.size )
{
return false;
}
String name = this.squares[x][y].getName();
String[] splitted = name.split(":");
if( splitted[2].equals("b") )
{
return true;
}
return false;
}
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
private ArrayList<int[]> getNullFieldAround(int x, int y,ArrayList<int[]> nulls,ArrayList<int[]> wasThere)
{
if( wasThere == null )
wasThere = new ArrayList<>();
ArrayList<int[]> nullsAround;
if( nulls != null )
nullsAround = nulls;
else
nullsAround = new ArrayList<>();
int pos[] = new int[2];
pos[0] = x;
pos[1] = y;
String[] neighbours = this.getNeighbours(pos);
for (int i = 0; i < 9; i++)
{
int[] posN = getPosFromString(neighbours[i]);
if( posN[0] == -1 || posN[1] == -1 )
{
int dummy = 10;
}
else if( this.calculateMinesAround(posN[0], posN[1]) == 0 )
{
if( !this.isContains(wasThere, posN) )
{
nullsAround.add(posN);
wasThere.add(posN);
ArrayList<int[]> newNulls = this.getNullFieldAround(posN[0], posN[1], nullsAround,wasThere);
nullsAround.addAll(newNulls);
for( int[] actNull : newNulls )
{
this.addUnique(nullsAround, actNull);
}
}
}
}
return nullsAround;
}
private ArrayList<int[]> addUnique(ArrayList<int[]> to, int[] add)
{
for (int[] actTo : to )
{
if( actTo == add )
return to;
}
to.add(add);
return to;
}
private Boolean isContains(ArrayList<int[]> container, int[] position)
{
for( int[] act : container)
{
if( act[0] == position[0] && act[1] == position[1] )
return true;
}
return false;
}
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
private int calculateMinesAround(int x,int y)
{
int pos[] = new int[2];
pos[0] = x;
pos[1] = y;
String[] neighbours = this.getNeighbours(pos);
int minesAround = 0;
for (int i = 0; i < 9; i++)
{
int[] positionOfNeighbour = getPosFromString(neighbours[i]);
if( isMined(positionOfNeighbour[0], positionOfNeighbour[1]))
{
minesAround++;
}
}
return minesAround;
}
public boolean generateMines(int minesYet)
{
Random random = new Random();
int rand=0;
if( this.controller.settings.level == 1 )
rand = 8;
else if( this.controller.settings.level == 2 )
rand = 5;
else if( this.controller.settings.level == 3)
rand = 3;
if( random.nextInt(10000)%rand == 1 )
{
return true;
}
return false;
}
public void build()
{
int minesYet = 0;
for(int i=0;i<controller.settings.size;i++)
{
for(int j=0;j<controller.settings.size;j++)
{
squares[i][j] = new JButton();
squares[i][j].setSize(400,400);
String name;
if( this.generateMines(minesYet))
{
//tegyunk be egyet
name = Integer.toString(i) + ":" + Integer.toString(j) + ":b";
minesYet++;
}
else
{
name = Integer.toString(i) + ":" + Integer.toString(j) + ":0";
}
squares[i][j].setName(name);
squares[i][j].addActionListener(new ButtonActionListener(this.controller));
}
}
for(int i=0;i<controller.settings.size;i++)
{
for(int j=0;j<controller.settings.size;j++)
{
int minesAround = 0;
minesAround = this.calculateMinesAround(i,j);
String name = squares[i][j].getName();
squares[i][j].setName(name + ":" + minesAround);
// if( !(squares[i][j].getText() == "*") )
// squares[i][j].setText(Integer.toString(minesAround));
// squares[i][j].setText(" ");