-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackEnd.java
More file actions
122 lines (102 loc) · 2.66 KB
/
BackEnd.java
File metadata and controls
122 lines (102 loc) · 2.66 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
public class BackEnd {
private Minefield field = null;
private int[][] realGrid;
private boolean firstRun = true;
private int spacesUncovered = 0;
private int D = 9;
private int numberOfMines = 10;
private GameScreen gameScreen = null;
private boolean gameScreenLinked = false;
private boolean handlersEnabled = true;
public void linkToGameScreen(){
if (!gameScreenLinked){
gameScreen = FrontEnd.gameScreen();
gameScreenLinked = true;
}
}
public void setD(int D){
if (field == null){
if (Minefield.DIsValid(D)){
this.D = D;
}
else {
this.D = 9;
}
}
else {
System.out.println("Minefield already created. Cannot modify D.");
}
}
public int getD(){
return this.D;
}
public void setNumberOfMines(int numberOfMines){
if (field == null){
if (Minefield.mineNumberIsValid(this.D, numberOfMines)){
this.numberOfMines = numberOfMines;
}
else {
numberOfMines = Minefield.defaultNumberOfMines(this.D);
}
}
else {
System.out.println("Minefield already created. Cannot modify D.");
}
}
public int getNumberOfMines(){
return numberOfMines;
}
public void newGrid(int x, int y){
if (firstRun){
field = new Minefield(D, numberOfMines, x, y);
realGrid = field.getMinefield();
firstRun = false;
}
int neighboringMines = realGrid[x][y];
if (neighboringMines == -1){
gameScreen.setVisible(false);
gameScreen.dispose();
Songs.song1.stop();
new GameOver();
}
if (neighboringMines > 0){
gameScreen.uncoverBox(x, y, neighboringMines);
spacesUncovered++;
}
if (neighboringMines == 0){
gameScreen.uncoverBox(x, y, MinesweeperIcons.NULL);
spacesUncovered++;
pointIsZero(x, y);
}
if (spacesUncovered == (field.getD() * field.getD() - field.getNumberOfMines())){
handlersEnabled = false;
new WinScreen();
}
}
public boolean handlersEnabled(){
return handlersEnabled;
}
private void pointIsZero(int x, int y){
int[] xPosition = Minefield.getPositionX(x);
int[] yPosition = Minefield.getPositionY(y);
for (int counter = 0; counter < 8; counter++){
int currentX = xPosition[counter];
int currentY = yPosition[counter];
if (gameScreen.getButtonNumber(currentX, currentY) == MinesweeperIcons.QUESTION){
int neighboringMines = realGrid[currentX][currentY];
if (neighboringMines > 0){
gameScreen.uncoverBox(currentX, currentY, neighboringMines);
spacesUncovered++;
}
if (neighboringMines == 0){
gameScreen.uncoverBox(currentX, currentY, MinesweeperIcons.NULL);
spacesUncovered++;
pointIsZero(currentX, currentY);
}
}
}
}
public Minefield getMinefield(){
return field;
}
}