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.

141 lines
3.0 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
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.GRAY;
  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(Color.white);
  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. JOptionPane.showMessageDialog(getParent(),"KABOOM! Try again!");
  53. playfield.reset();
  54. }
  55. }
  56. }
  57. protected void OnMouseRightClick() {
  58. if (isEnabled()) {
  59. if (flagged) {
  60. flagged = false;
  61. if (type == CellType.Number) {
  62. setBackground(HIDDENCOLOR);
  63. playfield.cellDried();
  64. } else {
  65. setBackground(MINECOLOR);
  66. }
  67. } else {
  68. flagged = true;
  69. setBackground(FLAGCOLOR);
  70. if (type == CellType.Number) {
  71. playfield.cellFlooded();
  72. }
  73. }
  74. }
  75. }
  76. public void reveal() {
  77. if (type == CellType.Number) {
  78. setText(String.valueOf(value));
  79. } else {
  80. setBackground(MINECOLOR);
  81. }
  82. }
  83. public void flood() {
  84. if (type == CellType.Bomb || flagged) {
  85. return;
  86. }
  87. setBackground(FLOODEDCOLOR);
  88. setEnabled(false);
  89. playfield.cellFlooded();
  90. if (value == 0) {
  91. if (cord.y > 0) {
  92. if (playfield.cells[cord.y - 1][cord.x].type == CellType.Number
  93. && playfield.cells[cord.y - 1][cord.x].isEnabled()) {
  94. playfield.cells[cord.y - 1][cord.x].flood();
  95. }
  96. }
  97. if (cord.x < playfield.Size - 1) {
  98. if (playfield.cells[cord.y][cord.x + 1].type == CellType.Number
  99. && playfield.cells[cord.y][cord.x + 1].isEnabled()) {
  100. playfield.cells[cord.y][cord.x + 1].flood();
  101. }
  102. }
  103. if (cord.y < playfield.Size - 1) {
  104. if (playfield.cells[cord.y + 1][cord.x].type == CellType.Number
  105. && playfield.cells[cord.y + 1][cord.x].isEnabled()) {
  106. playfield.cells[cord.y + 1][cord.x].flood();
  107. }
  108. }
  109. if (cord.x > 0) {
  110. if (playfield.cells[cord.y][cord.x - 1].type == CellType.Number
  111. && playfield.cells[cord.y][cord.x - 1].isEnabled()) {
  112. playfield.cells[cord.y][cord.x - 1].flood();
  113. }
  114. }
  115. }
  116. }
  117. }