Nur die besten Spiele ;3
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.

129 lines
2.6 KiB

package Minesweeper;
import java.awt.Color;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
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;
setBackground(Color.white);
addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
OnMouseClick();
}
});
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
super.mousePressed(e);
if (e.getButton() == 3) {
OnMouseRightClick();
}
}
});
}
protected void OnMouseClick() {
if (!flagged) {
if (type != CellType.Bomb) {
flood();
} else {
playfield.reset();
}
}
}
protected void OnMouseRightClick() {
if (isEnabled()) {
if (flagged) {
flagged = false;
if (type == CellType.Number) {
setBackground(Color.gray);
} else {
setBackground(Color.red);
}
} else {
flagged = true;
setBackground(Color.cyan);
}
}
}
public void update() {
if (type == CellType.Number) {
setText(String.valueOf(value));
} else {
setBackground(Color.RED);
}
}
public void flood() {
if (type == CellType.Bomb || flagged) {
return;
}
setBackground(Color.LIGHT_GRAY);
setEnabled(false);
playfield.cellFlooded();
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();
}
}
}
}
}