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.

150 lines
3.1 KiB

  1. package Minesweeper;
  2. import java.awt.Point;
  3. import javax.swing.JOptionPane;
  4. public class Playfield {
  5. private static final int CELLSIZE = 50;
  6. public int Size;
  7. private MinesweeperGame MsG;
  8. public Cell[][] cells;
  9. private int bombAmount;
  10. private int cellsFlooded = 0;
  11. public Playfield(MinesweeperGame _MsG, int _Size, int _bombAmount) {
  12. MsG = _MsG;
  13. Size = _Size;
  14. bombAmount = _bombAmount;
  15. generatePlayfield();
  16. }
  17. public void generatePlayfield() {
  18. cells = new Cell[Size][Size];
  19. int[] bPlacement = new int[bombAmount];
  20. for (int i = 0; i < bPlacement.length; i++) {
  21. bPlacement[i] = (int) (Math.random() * Size * Size);
  22. for (int j = 0; j < i; j++) {
  23. if (bPlacement[i] == bPlacement[j]) {
  24. i--;
  25. break;
  26. }
  27. }
  28. }
  29. for (int i = 0; i < Size; i++) {
  30. for (int j = 0; j < Size; j++) {
  31. cells[i][j] = new Cell(CellType.Number, this, new Point(j, i));
  32. cells[i][j].setBounds(j * CELLSIZE + (MsG.WIDTH / 2 - Size * CELLSIZE / 2),
  33. i * CELLSIZE + (MsG.HEIGTH / 2 - Size * CELLSIZE / 2), CELLSIZE, CELLSIZE);
  34. MsG.add(cells[i][j]);
  35. for (int k = 0; k < bPlacement.length; k++) {
  36. if (bPlacement[k] == i * Size + j) {
  37. cells[i][j].type = CellType.Bomb;
  38. break;
  39. }
  40. }
  41. }
  42. }
  43. for (int i = 0; i < Size; i++) {
  44. for (int j = 0; j < Size; j++) {
  45. if (cells[i][j].type == CellType.Number) {
  46. calculateBombProximity(i, j);
  47. }
  48. }
  49. }
  50. MsG.repaint();
  51. }
  52. public void reset() {
  53. cellsFlooded = 0;
  54. for (int i = 0; i < Size; i++) {
  55. for (int j = 0; j < Size; j++) {
  56. MsG.remove(cells[i][j]);
  57. }
  58. }
  59. MsG.tl.reset();
  60. generatePlayfield();
  61. }
  62. public void calculateBombProximity(int row, int column) {
  63. if (row > 0) {
  64. if (column > 0) {
  65. if (cells[row - 1][column - 1].type == CellType.Bomb) {
  66. cells[row][column].value++;
  67. }
  68. }
  69. if (cells[row - 1][column].type == CellType.Bomb) {
  70. cells[row][column].value++;
  71. }
  72. if (column < cells.length - 1) {
  73. if (cells[row - 1][column + 1].type == CellType.Bomb) {
  74. cells[row][column].value++;
  75. }
  76. }
  77. }
  78. if (row < cells.length - 1) {
  79. if (column > 0) {
  80. if (cells[row + 1][column - 1].type == CellType.Bomb) {
  81. cells[row][column].value++;
  82. }
  83. }
  84. if (cells[row + 1][column].type == CellType.Bomb) {
  85. cells[row][column].value++;
  86. }
  87. if (column < cells.length - 1) {
  88. if (cells[row + 1][column + 1].type == CellType.Bomb) {
  89. cells[row][column].value++;
  90. }
  91. }
  92. }
  93. if (column > 0) {
  94. if (cells[row][column - 1].type == CellType.Bomb) {
  95. cells[row][column].value++;
  96. }
  97. }
  98. if (column < cells.length - 1) {
  99. if (cells[row][column + 1].type == CellType.Bomb) {
  100. cells[row][column].value++;
  101. }
  102. }
  103. }
  104. public void cellFlooded() {
  105. cellsFlooded++;
  106. if (cellsFlooded >= Size * Size - bombAmount) {
  107. revealAllBombs();
  108. JOptionPane.showMessageDialog(MsG, "You won, congratulations!");
  109. reset();
  110. }
  111. }
  112. public void revealAllBombs() {
  113. for (int i = 0; i < Size; i++) {
  114. for (int j = 0; j < Size; j++) {
  115. if(cells[i][j].type == CellType.Bomb) {
  116. cells[i][j].reveal();
  117. }
  118. }
  119. }
  120. MsG.tl.stop();
  121. MsG.repaint();
  122. }
  123. public void cellDried() {
  124. cellsFlooded--;
  125. }
  126. }