kfkama
3 years ago
2 changed files with 78 additions and 11 deletions
@ -1,24 +1,89 @@ |
|||||
package Minesweeper; |
package Minesweeper; |
||||
|
|
||||
|
import java.awt.Color; |
||||
|
import java.awt.Point; |
||||
|
import java.awt.event.ActionEvent; |
||||
|
import java.awt.event.ActionListener; |
||||
|
|
||||
import javax.swing.JButton; |
import javax.swing.JButton; |
||||
|
|
||||
enum CellType{Number, Bomb} |
|
||||
|
enum CellType { |
||||
|
Number, Bomb |
||||
|
} |
||||
|
|
||||
public class Cell extends JButton { |
public class Cell extends JButton { |
||||
|
|
||||
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
||||
|
private Playfield playfield; |
||||
|
|
||||
public CellType type; |
public CellType type; |
||||
|
public Point cord; |
||||
public boolean flagged = false; |
public boolean flagged = false; |
||||
public int value = 0; |
public int value = 0; |
||||
|
|
||||
public Cell(CellType _type) { |
|
||||
|
public Cell(CellType _type, Playfield _playfield, Point _cord) { |
||||
type = _type; |
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() { |
public void update() { |
||||
if (type == CellType.Number) { |
if (type == CellType.Number) { |
||||
setText(String.valueOf(value)); |
setText(String.valueOf(value)); |
||||
} else { |
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
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(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
} |
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue