You need to sign in or sign up before continuing.
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.logging.Level;
import java.util.logging.Logger;
public Boolean calculateDone;
public ArrayList<int[]> nulls;
public int mines;
public Boolean isGameOver;
public Model(Controller controller)
{
this.controller = controller;
squares = new JButton[controller.settings.size][controller.settings.size];
int mines = 0;
isGameOver = false;
public int getTimeLeft(){ return this.timeLeft; }
public int getMines() { return this.mines; }
public int getScore() { return this.score; }
public int getUncoveredFields()
{
int uncovered = 0;
for (int i = 0; i < this.controller.settings.size; i++)
{
for (int j = 0; j < this.controller.settings.size; j++)
{
if((this.squares[i][j].getText().length() == 0) || this.squares[i][j].getText().equals(" ") || this.squares[i][j].getText().equals("?") )
{
uncovered++;
}
}
}
return uncovered;
}
public 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;
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
}
private Boolean isGameOverAndWinner()
{
Boolean isWinner = true ;
for (int i = 0; i < this.controller.settings.size; i++)
{
for (int j = 0; j < this.controller.settings.size; j++)
{
if( this.isMined(i, j) )
{
JButton actualField = this.squares[i][j];
if( this.isFlagged(i, j) && !this.isGameOver )
isWinner = isWinner;
else
isWinner = false;
}
}
}
return isWinner;
}
public Boolean isFlagged(int x, int y)
{
if( this.squares[x][y].getText().equals("?") )
return true;
return false;
}
public void showAllMines(Color color)
{
for (int i = 0; i < this.controller.settings.size; i++)
{
for (int j = 0; j < this.controller.settings.size; j++)
{
JButton actField = this.squares[i][j];
if( this.isMined(i, j) )
{
actField.setText("*");
actField.setBackground(color);
}
}
}
public void calculateButtonStyle(int[] pos)
{
if( isGameOver )
return;
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("*");
this.controller.gameOver();
if( isGameOver )
{
return;
}
}
else
{
int mines = this.calculateMinesAround(pos[0], pos[1]);
if( mines == 0 )
{
//ArrayList<int[]> nulls = this.getNullFieldAround(pos[0], pos[1], null,null);
RecursiveNullFinder nullFinder = new RecursiveNullFinder(pos[0], pos[1], null, null,controller);
this.calculateDone=false;
nullFinder.start();
while( !this.calculateDone )
{
synchronized(this)
{
try {
wait(100);
} catch (InterruptedException ex) {
Logger.getLogger(Model.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
for( int[] nullMiner : this.nulls )
{
this.squares[nullMiner[0]][nullMiner[1]].setText("0");
String[] neighbours = this.getNeighbours(nullMiner);
for (int i = 0; i < 9; i++)
{
String[] posN = neighbours[i].split(":");
int x = Integer.parseInt(posN[0]);
int y = Integer.parseInt(posN[1]);
if( x >= 0 && y >= 0)
{
int actMines = this.calculateMinesAround(x,y);
System.out.println(x + ":" + y);
this.squares[x][y].setText(Integer.toString(actMines));
}
}
}
}
else
{
squares[pos[0]][pos[1]].setText(Integer.toString(mines));
}
}
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
}
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;
}
public int calculateMinesAround(int x,int y)
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
{
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 )
{
this.mines++;
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));
squares[i][j].addMouseListener(new ButtonActionListener(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(" ");
this.controller.view.flagged = this.mines;
public void colorField(int x,int y, Color color, Color colorF)
{
this.squares[x][y].setBackground(color);
this.squares[x][y].setForeground(colorF);
}
if( isGameOver )
return;
String text = squares[pos[0]][pos[1]].getText();
if( text.equals("?"))
{
squares[pos[0]][pos[1]].setText("");
this.controller.view.flagChanged(1);
}
else if( text.equals(""))
{
squares[pos[0]][pos[1]].setText("?");
this.controller.view.flagChanged(-1);
this.colorField(pos[0], pos[1], Color.BLUE, Color.YELLOW);
}
else if( text.equals(" "))
{
squares[pos[0]][pos[1]].setText("?");
this.controller.view.flagChanged(-1);
this.colorField(pos[0], pos[1], Color.BLUE, Color.YELLOW);
if( isGameOverAndWinner() )
{
this.isWinner = true;
this.isGameOver = true;
this.controller.gameOver();
}