Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. package simple;
  2.  
  3. import java.util.LinkedList;
  4.  
  5. /**
  6. * Created by Max on 6/24/2019.
  7. */
  8. public class MineSweeper {
  9.  
  10. int[][] currentGameBoard;
  11.  
  12. int[][] gameBoard;
  13.  
  14. LinkedList<Node> queue;
  15.  
  16. int rows;
  17.  
  18. int columns;
  19.  
  20. public MineSweeper() {
  21. queue = new LinkedList<>();
  22. gameBoard = new int[][] {{9, 2, 9},
  23. {1, 1, 1},
  24. {0, 0, 0}
  25. };
  26. currentGameBoard = new int[][] {
  27. {10, 10, 10},
  28. {10, 10, 10},
  29. {10, 10, 10}
  30. };
  31. rows = gameBoard.length;
  32. columns = gameBoard[0].length;
  33. }
  34.  
  35. public int digAtLocation(Node position) {
  36.  
  37. if (gameBoard[position.x][position.y] == 9) {
  38. return 9;
  39. }
  40.  
  41. currentGameBoard[position.x][position.y] = gameBoard[position.x][position.y];
  42.  
  43. fillZeros(position);
  44.  
  45. return gameBoard[position.x][position.y];
  46. }
  47.  
  48. public int flagLocation(Node position) {
  49.  
  50. if (currentGameBoard[position.x][position.y] == 10) {
  51. currentGameBoard[position.x][position.y] = -1;
  52. }
  53.  
  54. return gameBoard[position.x][position.y];
  55. }
  56.  
  57. public int[][] getBoard() {
  58. return currentGameBoard;
  59. }
  60.  
  61. public void fillZeros(Node position) {
  62. queue.add(position);
  63. int[] dr = {-1, 1, 0, 0, -1, -1, 1, 1};
  64. int[] dc = {0, 0, 1, -1, -1, 1, -1, 1};
  65.  
  66. while (!queue.isEmpty()) {
  67. Node curr = queue.poll();
  68. for (int i=0; i<8; i++) {
  69. Node adj = new Node(curr.x + dr[i], curr.y + dc[i]);
  70. if (isValid(adj)) {
  71. currentGameBoard[adj.x][adj.y] = 0;
  72. queue.add(adj);
  73. revealNeighboringCells(adj);
  74. }
  75. }
  76. }
  77. }
  78.  
  79. // This function reveals the neighboring cells of a "empty space"
  80. public void revealNeighboringCells(Node node) {
  81. int[] dr = {-1, 1, 0, 0, -1, -1, 1, 1};
  82. int[] dc = {0, 0, 1, -1, -1, 1, -1, 1};
  83.  
  84. for (int i=0; i< 8; i++) {
  85. Node adj = new Node(node.x+dr[i], node.y+dc[i]);
  86. if (adj.x >= 0 && adj.x < rows && adj.y >= 0 && adj.y < columns && currentGameBoard[adj.x][adj.y] == 10) {
  87. currentGameBoard[adj.x][adj.y] = gameBoard[adj.x][adj.y];
  88. }
  89. }
  90. }
  91.  
  92. // This function checks to see if the cell is within the bounds of the board and it supposed to be "empty space"
  93. // as well as not having been processed yet
  94. public boolean isValid(Node node) {
  95. if (node.x >= 0 && node.x < rows && node.y >= 0 && node.y < columns && gameBoard[node.x][node.y] == 0 && currentGameBoard[node.x][node.y] == 10) {
  96. return true;
  97. }
  98.  
  99. return false;
  100. }
  101.  
  102. public static class Node{
  103. int x;
  104. int y;
  105.  
  106. public Node(int x, int y) {
  107. this.x = x;
  108. this.y = y;
  109. }
  110. }
  111.  
  112. public static void main(String[] args) {
  113. MineSweeper ms = new MineSweeper();
  114. int[][] board = ms.getBoard();
  115. printBoard(board);
  116. // 101010
  117. // 101010
  118. // 101010
  119. Node bomb = new Node(0,0);
  120. int digResult = ms.digAtLocation(bomb);
  121. System.out.println(digResult); //9
  122.  
  123. ms.flagLocation(bomb);
  124. printBoard(board);
  125. // -11010
  126. // 101010
  127. // 101010
  128.  
  129. Node validSpot = new Node(2, 2);
  130. ms.digAtLocation(validSpot);
  131. board = ms.getBoard();
  132. printBoard(board);
  133. // -11010
  134. // 111
  135. // 000
  136.  
  137. Node validSpot2 = new Node(0, 1);
  138. ms.digAtLocation(validSpot2);
  139. board = ms.getBoard();
  140. printBoard(board);
  141. // -1210
  142. // 111
  143. // 000
  144. }
  145.  
  146. public static void printBoard(int[][] board) {
  147. for(int i=0; i<board.length; i++) {
  148. for(int j=0; j<board[0].length;j++) {
  149. System.out.print(board[i][j]);
  150. }
  151. System.out.println("");
  152. }
  153. }
  154.  
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement