-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCell.java
More file actions
197 lines (175 loc) · 7.27 KB
/
Cell.java
File metadata and controls
197 lines (175 loc) · 7.27 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
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class Cell extends JButton {
public final int row;
public final int col;
public int neighboringBombs;
public boolean isBomb;
public boolean isFlagged;
public boolean isRevealed;
/**
* The markUnknown function sets the background color of the cell to yellow if it's unsure.
*/
void markUnknown() {
this.setForeground(Color.BLACK);
this.setBackground(Color.YELLOW);
}
/**
* The markSafe() function sets the background color of the cell to green if it is clear.
*/
void markSafe() {
this.setForeground(Color.BLACK);
this.setBackground(Color.GREEN);
}
/**
* The markBomb() function changes the background color to red if it is a bomb.
*/
void markBomb() {
this.setForeground(Color.BLACK);
this.setBackground(Color.RED);
}
/**
* The function "makeBomb" sets the "isBomb" variable to true.
*/
void makeBomb() {
this.isBomb = true;
}
/**
* The function sets the number of neighboring bombs and updates the icon of the cell based on the
* number of neighboring bombs.
*
* @param neighboringBombs The parameter `neighboringBombs` represents the number of neighboring
* bombs around a particular cell in a game.
*/
void setNeighboringBombs(int neighboringBombs) {
ImageIcon icon;
this.neighboringBombs = neighboringBombs;
if(this.neighboringBombs != 0) {
switch(neighboringBombs){
case 1:
icon = new ImageIcon(getClass().getResource("/res/1.png"));
setIcon(new ImageIcon(icon.getImage().getScaledInstance(20, 20, java.awt.Image.SCALE_FAST)));
break;
case 2:
icon = new ImageIcon(getClass().getResource("/res/2.png"));
setIcon(new ImageIcon(icon.getImage().getScaledInstance(20, 20, java.awt.Image.SCALE_FAST)));
break;
case 3:
icon = new ImageIcon(getClass().getResource("/res/3.png"));
setIcon(new ImageIcon(icon.getImage().getScaledInstance(20, 20, java.awt.Image.SCALE_FAST)));
break;
case 4:
icon = new ImageIcon(getClass().getResource("/res/4.png"));
setIcon(new ImageIcon(icon.getImage().getScaledInstance(20, 20, java.awt.Image.SCALE_FAST)));
break;
case 5:
icon = new ImageIcon(getClass().getResource("/res/5.png"));
setIcon(new ImageIcon(icon.getImage().getScaledInstance(20, 20, java.awt.Image.SCALE_FAST)));
break;
case 6:
icon = new ImageIcon(getClass().getResource("/res/6.png"));
setIcon(new ImageIcon(icon.getImage().getScaledInstance(20, 20, java.awt.Image.SCALE_FAST)));
break;
case 7:
icon = new ImageIcon(getClass().getResource("/res/7.png"));
setIcon(new ImageIcon(icon.getImage().getScaledInstance(20, 20, java.awt.Image.SCALE_FAST)));
break;
case 8:
icon = new ImageIcon(getClass().getResource("/res/8.png"));
setIcon(new ImageIcon(icon.getImage().getScaledInstance(20, 20, java.awt.Image.SCALE_FAST)));
break;
default:
return;
}
}
}
/**
* The `reveal` function updates the appearance of a component based on its properties, such as
* setting an icon if it represents a bomb or changing the background color and border.
*/
void reveal() {
this.isRevealed = true;
if(this.isBomb) {
ImageIcon icon = new ImageIcon(getClass().getResource("/res/bomb.png"));
setIcon(new ImageIcon(icon.getImage().getScaledInstance(20, 20, java.awt.Image.SCALE_FAST)));
return;
}
setBackground(Color.LIGHT_GRAY);
setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
setText("");
}
/**
* The function toggles a flag on a cell and updates its icon accordingly.
*/
void toggleFlag() {
if(this.isRevealed){
return;
}
if (!this.isFlagged) {
this.isFlagged = true;
ImageIcon icon = new ImageIcon(getClass().getResource("/res/flag.png"));
setIcon(new ImageIcon(icon.getImage().getScaledInstance(20, 20, java.awt.Image.SCALE_FAST)));
}
else if(this.isFlagged){
this.isFlagged = false;
setIcon(null);
}
}
/**
* The function calculates the probability of a bomb being present at a given position in a grid
* based on the distance from the current position and the size of the grid. Explanation of the probability
* of a bomb: https://www.desmos.com/calculator/b3lcshvkvg
*
* @param x The x-coordinate of the cell for which we want to calculate the probability of a bomb.
* @param y The y-coordinate of the current position on the grid.
* @param gridSize The gridSize parameter represents the size of the grid. It is used in the
* calculation of the probability of a bomb being present at a certain location.
* @param maxProbability The maximum probability of a bomb being present in the grid. It is given
* as a percentage, so it should be a value between 0 and 100.
* @return The method is returning the probability of a bomb being present at a given position on a
* grid.
*/
double calculateProbabilityOfBomb(int x, int y, int gridSize, int maxProbability) {
// we use the simple grid distance because
// it is computationally faster than pythagora
double distance = Math.abs(y - this.row) + Math.abs(x - this.col);
double probability = (maxProbability / 100.0) * (2 / Math.PI) * Math.atan((distance * distance) / gridSize);
return probability;
}
Cell(int row, int col) {
this.row = row;
this.col = col;
setBackground(new Color(180, 180, 180));
setBorder(BorderFactory.createRaisedBevelBorder());
setText("");
}
/**
* The equals() function checks if two Cell objects have the same row and column values.
*
* @param obj The "obj" parameter is an object that is being compared to the current object for
* equality.
* @return The method is returning a boolean value, which indicates whether the current object is
* equal to the object being compared.
*/
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass())
return false;
Cell cell = (Cell) obj;
return row == cell.row &&
col == cell.col;
}
/**
* The hashCode() function in Java returns the hash code value for the object based on the values
* of the row and col variables.
*
* @return The method is returning the hash code of the combination of the "row" and "col"
* variables.
*/
@Override
public int hashCode() {
return Objects.hash(row, col);
}
}