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.

137 lines
2.8 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. package Minesweeper;
  2. import java.awt.Color;
  3. import java.awt.Point;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.MouseAdapter;
  7. import java.awt.event.MouseEvent;
  8. import javax.swing.JButton;
  9. import javax.swing.JOptionPane;
  10. enum CellType {
  11. Number, Bomb
  12. }
  13. public class Cell extends JButton {
  14. private static final long serialVersionUID = 1L;
  15. private Playfield playfield;
  16. public CellType type;
  17. public Point cord;
  18. public boolean flagged = false;
  19. public int value = 0;
  20. public Cell(CellType _type, Playfield _playfield, Point _cord) {
  21. type = _type;
  22. cord = _cord;
  23. playfield = _playfield;
  24. setBackground(Color.white);
  25. addActionListener(new ActionListener() {
  26. @Override
  27. public void actionPerformed(ActionEvent e) {
  28. OnMouseClick();
  29. }
  30. });
  31. addMouseListener(new MouseAdapter() {
  32. @Override
  33. public void mousePressed(MouseEvent e) {
  34. // TODO Auto-generated method stub
  35. super.mousePressed(e);
  36. if (e.getButton() == 3) {
  37. OnMouseRightClick();
  38. }
  39. }
  40. });
  41. }
  42. protected void OnMouseClick() {
  43. if (!flagged) {
  44. reveal();
  45. if (type != CellType.Bomb) {
  46. flood();
  47. } else {
  48. JOptionPane.showMessageDialog(getParent(),"KABOOM! Try again!");
  49. playfield.reset();
  50. }
  51. }
  52. }
  53. protected void OnMouseRightClick() {
  54. if (isEnabled()) {
  55. if (flagged) {
  56. flagged = false;
  57. if (type == CellType.Number) {
  58. setBackground(Color.gray);
  59. playfield.cellDried();
  60. } else {
  61. setBackground(Color.red);
  62. }
  63. } else {
  64. flagged = true;
  65. setBackground(Color.cyan);
  66. if (type == CellType.Number) {
  67. playfield.cellFlooded();
  68. }
  69. }
  70. }
  71. }
  72. public void reveal() {
  73. if (type == CellType.Number) {
  74. setText(String.valueOf(value));
  75. } else {
  76. setBackground(Color.RED);
  77. }
  78. }
  79. public void flood() {
  80. if (type == CellType.Bomb || flagged) {
  81. return;
  82. }
  83. setBackground(Color.LIGHT_GRAY);
  84. setEnabled(false);
  85. playfield.cellFlooded();
  86. if (value == 0) {
  87. if (cord.y > 0) {
  88. if (playfield.cells[cord.y - 1][cord.x].type == CellType.Number
  89. && playfield.cells[cord.y - 1][cord.x].isEnabled()) {
  90. playfield.cells[cord.y - 1][cord.x].flood();
  91. }
  92. }
  93. if (cord.x < playfield.Size - 1) {
  94. if (playfield.cells[cord.y][cord.x + 1].type == CellType.Number
  95. && playfield.cells[cord.y][cord.x + 1].isEnabled()) {
  96. playfield.cells[cord.y][cord.x + 1].flood();
  97. }
  98. }
  99. if (cord.y < playfield.Size - 1) {
  100. if (playfield.cells[cord.y + 1][cord.x].type == CellType.Number
  101. && playfield.cells[cord.y + 1][cord.x].isEnabled()) {
  102. playfield.cells[cord.y + 1][cord.x].flood();
  103. }
  104. }
  105. if (cord.x > 0) {
  106. if (playfield.cells[cord.y][cord.x - 1].type == CellType.Number
  107. && playfield.cells[cord.y][cord.x - 1].isEnabled()) {
  108. playfield.cells[cord.y][cord.x - 1].flood();
  109. }
  110. }
  111. }
  112. }
  113. }