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.

113 lines
2.4 KiB

  1. package Minesweeper;
  2. import java.awt.Point;
  3. public class Playfield {
  4. private static final int CELLSIZE = 50;
  5. public int Size;
  6. private MinesweeperGame MsG;
  7. public Cell[][] cells;
  8. public Playfield(MinesweeperGame _MsG, int _Size, int _bombAmount) {
  9. MsG = _MsG;
  10. Size = _Size;
  11. generatePlayfield(_bombAmount);
  12. }
  13. public void generatePlayfield(int _bombAmount) {
  14. cells = new Cell[Size][Size];
  15. int[] bPlacement = new int[_bombAmount];
  16. for (int i = 0; i < bPlacement.length; i++) {
  17. bPlacement[i] = (int) (Math.random() * Size * Size);
  18. for (int j = 0; j < i; j++) {
  19. if (bPlacement[i] == bPlacement[j]) {
  20. i--;
  21. break;
  22. }
  23. }
  24. }
  25. for (int i = 0; i < Size; i++) {
  26. for (int j = 0; j < Size; j++) {
  27. cells[i][j] = new Cell(CellType.Number, this, new Point(j, i));
  28. cells[i][j].setBounds(j * CELLSIZE + (MsG.WIDTH / 2 - Size * CELLSIZE / 2),
  29. i * CELLSIZE + (MsG.HEIGTH / 2 - Size * CELLSIZE / 2), CELLSIZE, CELLSIZE);
  30. MsG.add(cells[i][j]);
  31. for (int k = 0; k < bPlacement.length; k++) {
  32. if (bPlacement[k] == i * Size + j) {
  33. cells[i][j].type = CellType.Bomb;
  34. cells[i][j].update();
  35. break;
  36. }
  37. }
  38. }
  39. }
  40. for (int i = 0; i < Size; i++) {
  41. for (int j = 0; j < Size; j++) {
  42. if (cells[i][j].type == CellType.Number) {
  43. calculateBombProximity(i, j);
  44. }
  45. }
  46. }
  47. }
  48. public void calculateBombProximity(int row, int column) {
  49. if (row > 0) {
  50. if (column > 0) {
  51. if (cells[row - 1][column - 1].type == CellType.Bomb) {
  52. cells[row][column].value++;
  53. }
  54. }
  55. if (cells[row - 1][column].type == CellType.Bomb) {
  56. cells[row][column].value++;
  57. }
  58. if (column < cells.length - 1) {
  59. if (cells[row - 1][column + 1].type == CellType.Bomb) {
  60. cells[row][column].value++;
  61. }
  62. }
  63. }
  64. if (row < cells.length - 1) {
  65. if (column > 0) {
  66. if (cells[row + 1][column - 1].type == CellType.Bomb) {
  67. cells[row][column].value++;
  68. }
  69. }
  70. if (cells[row + 1][column].type == CellType.Bomb) {
  71. cells[row][column].value++;
  72. }
  73. if (column < cells.length - 1) {
  74. if (cells[row + 1][column + 1].type == CellType.Bomb) {
  75. cells[row][column].value++;
  76. }
  77. }
  78. }
  79. if (column > 0) {
  80. if (cells[row][column - 1].type == CellType.Bomb) {
  81. cells[row][column].value++;
  82. }
  83. }
  84. if (column < cells.length - 1) {
  85. if (cells[row][column + 1].type == CellType.Bomb) {
  86. cells[row][column].value++;
  87. }
  88. }
  89. cells[row][column].update();
  90. }
  91. }