Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package simple;
- import java.util.LinkedList;
- /**
- * Created by Max on 6/24/2019.
- */
- public class MineSweeper {
- int[][] currentGameBoard;
- int[][] gameBoard;
- LinkedList<Node> queue;
- int rows;
- int columns;
- public MineSweeper() {
- queue = new LinkedList<>();
- gameBoard = new int[][] {{9, 2, 9},
- {1, 1, 1},
- {0, 0, 0}
- };
- currentGameBoard = new int[][] {
- {10, 10, 10},
- {10, 10, 10},
- {10, 10, 10}
- };
- rows = gameBoard.length;
- columns = gameBoard[0].length;
- }
- public int digAtLocation(Node position) {
- if (gameBoard[position.x][position.y] == 9) {
- return 9;
- }
- currentGameBoard[position.x][position.y] = gameBoard[position.x][position.y];
- fillZeros(position);
- return gameBoard[position.x][position.y];
- }
- public int flagLocation(Node position) {
- if (currentGameBoard[position.x][position.y] == 10) {
- currentGameBoard[position.x][position.y] = -1;
- }
- return gameBoard[position.x][position.y];
- }
- public int[][] getBoard() {
- return currentGameBoard;
- }
- public void fillZeros(Node position) {
- queue.add(position);
- int[] dr = {-1, 1, 0, 0, -1, -1, 1, 1};
- int[] dc = {0, 0, 1, -1, -1, 1, -1, 1};
- while (!queue.isEmpty()) {
- Node curr = queue.poll();
- for (int i=0; i<8; i++) {
- Node adj = new Node(curr.x + dr[i], curr.y + dc[i]);
- if (isValid(adj)) {
- currentGameBoard[adj.x][adj.y] = 0;
- queue.add(adj);
- revealNeighboringCells(adj);
- }
- }
- }
- }
- // This function reveals the neighboring cells of a "empty space"
- public void revealNeighboringCells(Node node) {
- int[] dr = {-1, 1, 0, 0, -1, -1, 1, 1};
- int[] dc = {0, 0, 1, -1, -1, 1, -1, 1};
- for (int i=0; i< 8; i++) {
- Node adj = new Node(node.x+dr[i], node.y+dc[i]);
- if (adj.x >= 0 && adj.x < rows && adj.y >= 0 && adj.y < columns && currentGameBoard[adj.x][adj.y] == 10) {
- currentGameBoard[adj.x][adj.y] = gameBoard[adj.x][adj.y];
- }
- }
- }
- // This function checks to see if the cell is within the bounds of the board and it supposed to be "empty space"
- // as well as not having been processed yet
- public boolean isValid(Node node) {
- 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) {
- return true;
- }
- return false;
- }
- public static class Node{
- int x;
- int y;
- public Node(int x, int y) {
- this.x = x;
- this.y = y;
- }
- }
- public static void main(String[] args) {
- MineSweeper ms = new MineSweeper();
- int[][] board = ms.getBoard();
- printBoard(board);
- // 101010
- // 101010
- // 101010
- Node bomb = new Node(0,0);
- int digResult = ms.digAtLocation(bomb);
- System.out.println(digResult); //9
- ms.flagLocation(bomb);
- printBoard(board);
- // -11010
- // 101010
- // 101010
- Node validSpot = new Node(2, 2);
- ms.digAtLocation(validSpot);
- board = ms.getBoard();
- printBoard(board);
- // -11010
- // 111
- // 000
- Node validSpot2 = new Node(0, 1);
- ms.digAtLocation(validSpot2);
- board = ms.getBoard();
- printBoard(board);
- // -1210
- // 111
- // 000
- }
- public static void printBoard(int[][] board) {
- for(int i=0; i<board.length; i++) {
- for(int j=0; j<board[0].length;j++) {
- System.out.print(board[i][j]);
- }
- System.out.println("");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement