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.

143 lines
3.1 KiB

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