Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Random;
  3.  
  4.  
  5.  
  6. class Utils {
  7. void linkHorizontalCells(Cell a, Cell b) {
  8. a.cells.set(4, b);
  9. b.cells.set(0, a);
  10. }
  11.  
  12. void linkVerticalCells(Cell top, Cell bottom) {
  13. top.cells.set(6, bottom);
  14. bottom.cells.set(2, top);
  15. }
  16.  
  17. }
  18.  
  19. int rows = 5;
  20. int columns = 5;
  21. int mines = 5;
  22. int windowX = 1000;
  23. int windowY = 1000;
  24.  
  25. class Cell {
  26. ArrayList<Cell> cells;
  27. int value; // 1 represents mine, 0 represents nothing
  28. boolean revealed;
  29. Cell(ArrayList<Cell> cells, int value) {
  30. this.cells = cells;
  31. this.value = value;
  32. this.revealed = false;
  33. }
  34.  
  35. // method to count the number of surrounding mines of this cell
  36. int countMines() {
  37. int counter = 0;
  38. for (int i = 0; i < 8; i++) {
  39. if (this.cells.get(i).value == 1) {
  40. counter++;
  41. }
  42. }
  43. return counter;
  44. }
  45.  
  46. void reveal() {
  47. this.revealed = true;
  48. for (int i = 0; i < 8; i ++) {
  49. if(this.cells.get(i).value == 0 && this.cells.get(i).countMines() == 0 && this.cells.get(i).revealed == false) {
  50. this.cells.get(i).reveal();
  51. }
  52. if(this.cells.get(i).value == 0 && this.cells.get(i).countMines() > 0 && this.cells.get(i).revealed == false) {
  53. this.cells.get(i).revealed = true;
  54. }
  55. }
  56. }
  57.  
  58.  
  59. }
  60.  
  61. ArrayList<ArrayList<Cell>> initBoard(int rows, int columns) {
  62. ArrayList<ArrayList<Cell>> grid = new ArrayList<ArrayList<Cell>>(rows);
  63. for (int i = 0; i < rows; i++) {
  64. ArrayList<Cell> horizontals = new ArrayList<Cell>(columns);
  65. for (int j = 0; j < columns; j++) {
  66. Cell newCell = new Cell(new ArrayList<Cell>(8), 0);
  67. for (int k = 0; k < 8; k++) {
  68. newCell.cells.add(k, new Cell(new ArrayList<Cell>(0), -1));// dummy iniatializer
  69. }
  70. horizontals.add(newCell);
  71. }
  72. grid.add(horizontals);
  73. }
  74. return grid;
  75. }
  76.  
  77.  
  78.  
  79. ArrayList<ArrayList<Cell>> initMines(ArrayList<ArrayList<Cell>> grid) {
  80. int minesInserted = 0;
  81. Random a = new Random();
  82. int m = a.nextInt(rows);
  83. int n = a.nextInt(columns);
  84. while (this.mines > minesInserted) {
  85. if (grid.get(m).get(n).value == 0) {
  86. grid.get(m).get(n).value = 1;
  87. minesInserted++;
  88. }
  89. m = a.nextInt(rows);
  90. n = a.nextInt(columns);
  91. }
  92. return grid;
  93. }
  94.  
  95. ArrayList<ArrayList<Cell>> initLinks(ArrayList<ArrayList<Cell>> grid) {
  96.  
  97. for (int i = 0; i < rows; i++) {
  98. for (int j = 0; j < columns; j++) {
  99. if (j - 1 >= 0) {
  100. grid.get(i).get(j).cells.set(0, grid.get(i).get(j - 1)); // left
  101. }
  102. if (i - 1 >= 0 && j - 1 >= 0) {
  103. grid.get(i).get(j).cells.set(1, grid.get(i - 1).get(j - 1)); // top left
  104. }
  105. if (i - 1 >= 0) {
  106. grid.get(i).get(j).cells.set(2, grid.get(i - 1).get(j)); // top
  107. }
  108. if (i - 1 >= 0 && j + 1 < columns) {
  109. grid.get(i).get(j).cells.set(3, grid.get(i - 1).get(j + 1)); // top right
  110. }
  111. if (j + 1 < columns) {
  112. grid.get(i).get(j).cells.set(4, grid.get(i).get(j + 1)); // right
  113. }
  114. if (i + 1 < rows && j + 1 < columns) {
  115. grid.get(i).get(j).cells.set(5, grid.get(i + 1).get(j + 1)); // bottom right
  116. }
  117. if (i + 1 < rows) {
  118. grid.get(i).get(j).cells.set(6, grid.get(i + 1).get(j)); // bottom
  119. }
  120. if (i + 1 < rows && j - 1 >= 0) {
  121. grid.get(i).get(j).cells.set(7, grid.get(i + 1).get(j - 1)); // bottom left
  122. }
  123. }
  124. }
  125.  
  126. // Add mines
  127. // generate list of all unique i, j coordinate pairs
  128. // list of arrays of len 2
  129.  
  130. return grid;
  131. }
  132.  
  133. ArrayList<ArrayList<Cell>> board = new ArrayList<ArrayList<Cell>>();
  134. boolean alive ;
  135. void setup() {
  136. size(1000,1000);
  137. board = initBoard(rows, columns);
  138. board = initLinks(board);
  139. board = initMines(board);
  140. fill(255);
  141. }
  142.  
  143. void draw() {
  144.  
  145. for (int i = 0; i < rows; i ++) {
  146. for (int j = 0; j < columns ; j++) {
  147.  
  148. Cell currentCell = board.get(i).get(j);
  149. int nearbyMines = currentCell.countMines();
  150. if (currentCell.revealed) {
  151. if (currentCell.value == 1) {
  152. fill (255, 0, 0);
  153. square(windowX/columns * i, windowY/rows * j, windowX/rows);
  154. noLoop();
  155.  
  156. }
  157. else {
  158.  
  159. fill(255);
  160. square(windowX/columns * i, windowY/rows * j, windowX/rows);
  161. fill(0);
  162. textSize(32);
  163. int dx = windowX/columns/2;
  164. int dy = windowY/rows/2;
  165. int posX = (windowY/columns * i) + dx;
  166. int posY = (windowX/rows* j) + dy;
  167. text(nearbyMines, posX, posY);
  168.  
  169. }
  170. }
  171. else {
  172. fill(255);
  173. square(windowX/columns * i, windowY/rows * j, windowX/rows);
  174. }
  175.  
  176. }
  177. }
  178. }
  179.  
  180. void mousePressed() {
  181. float posX = mouseX;
  182. float posY = mouseY;
  183. float widthX = windowX;
  184. float widthY = windowY;
  185. int x = (int) (posX/widthX * columns);
  186. int y = (int) (posY/widthY * rows);
  187. board.get(x).get(y).reveal();
  188.  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement