You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
1.9 KiB
91 lines
1.9 KiB
package Minesweeper;
|
|
|
|
import java.awt.Color;
|
|
import java.awt.Point;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionListener;
|
|
|
|
import javax.swing.JButton;
|
|
|
|
enum CellType {
|
|
Number, Bomb
|
|
}
|
|
|
|
public class Cell extends JButton {
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
private Playfield playfield;
|
|
|
|
public CellType type;
|
|
public Point cord;
|
|
public boolean flagged = false;
|
|
public int value = 0;
|
|
|
|
public Cell(CellType _type, Playfield _playfield, Point _cord) {
|
|
type = _type;
|
|
cord = _cord;
|
|
playfield = _playfield;
|
|
|
|
addActionListener(new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
OnMouseClick();
|
|
}
|
|
});
|
|
}
|
|
|
|
protected void OnMouseClick() {
|
|
if (type != CellType.Bomb) {
|
|
flood();
|
|
}
|
|
}
|
|
|
|
public void update() {
|
|
if (type == CellType.Number) {
|
|
setText(String.valueOf(value));
|
|
} else {
|
|
setBackground(Color.RED);
|
|
}
|
|
}
|
|
|
|
public void flood() {
|
|
if (type == CellType.Bomb) {
|
|
return;
|
|
}
|
|
setBackground(Color.LIGHT_GRAY);
|
|
setEnabled(false);
|
|
|
|
if (value == 0) {
|
|
if (cord.y > 0) {
|
|
if (playfield.cells[cord.y - 1][cord.x].type == CellType.Number
|
|
&& playfield.cells[cord.y - 1][cord.x].isEnabled()) {
|
|
playfield.cells[cord.y - 1][cord.x].flood();
|
|
}
|
|
}
|
|
|
|
if (cord.x < playfield.Size - 1) {
|
|
if (playfield.cells[cord.y][cord.x + 1].type == CellType.Number
|
|
&& playfield.cells[cord.y][cord.x + 1].isEnabled()) {
|
|
playfield.cells[cord.y][cord.x + 1].flood();
|
|
}
|
|
}
|
|
|
|
if (cord.y < playfield.Size - 1) {
|
|
if (playfield.cells[cord.y + 1][cord.x].type == CellType.Number
|
|
&& playfield.cells[cord.y + 1][cord.x].isEnabled()) {
|
|
playfield.cells[cord.y + 1][cord.x].flood();
|
|
}
|
|
}
|
|
|
|
if (cord.x > 0) {
|
|
if (playfield.cells[cord.y][cord.x - 1].type == CellType.Number
|
|
&& playfield.cells[cord.y][cord.x - 1].isEnabled()) {
|
|
playfield.cells[cord.y][cord.x - 1].flood();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|