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.

106 lines
2.3 KiB

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