Skip to content

Instantly share code, notes, and snippets.

@nambrot
Created August 15, 2018 02:21
Show Gist options
  • Select an option

  • Save nambrot/6cc9f319ec237bd24e9d8cb9a3fc11d1 to your computer and use it in GitHub Desktop.

Select an option

Save nambrot/6cc9f319ec237bd24e9d8cb9a3fc11d1 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.*;
/*
* To execute Java, please define "static void main" on a class
* named Solution.
*
* If you need more classes, simply define them inline.
*/
class Solution {
public static void main(String[] args) {
Minesweeper game = new Minesweeper(10, 10);
game.spreadMines(15);
game.printUserBoard();
Scanner scanner = new Scanner(System.in);
while(!game.isFinished()) {
System.out.println("Enter (m)ark bomb or (r)eveal: ");
String selection = scanner.nextLine();
System.out.println("Enter row of your selection: ");
int row = scanner.nextInt();
System.out.println("Enter column of your selection: ");
int col = scanner.nextInt();
game.open(selection, row, col);
scanner.nextLine();
}
}
}
class Minesweeper {
boolean[][] board; // false - no mine, true - mine
int[][] userBoard; // number of surrounding mines, or -1; for a mine itself
int[][] visible; // 1 - visible, 0 - not visible, 2 - marked
int n;
int m;
Random random = new Random();
boolean finished;
int numberOfMines;
int visibleFields = 0;
static int[][] directions = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
static int[][] sdir = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
public Minesweeper(int n, int m) {
board = new boolean[n][m];
userBoard = new int[n][m];
visible = new int[n][m];
this.n = n;
this.m = m;
}
public void spreadMines(int numberOfMines) {
// int[] indicies = new int[n*m];
// for (int i=0;i<n*m;i++) indicies[i] = i;
// Collections.shuffle(indicies);
// for (int i=0;i<numberOfMines;i++) {
// board[row(indicies[i])][col(indicies[i])] = true;
// }
this.numberOfMines = numberOfMines;
if (numberOfMines > n*m) throw new RuntimeException("too many mines");
for (int i=0;i<numberOfMines;i++) {
int pos = random.nextInt(n*m);
while (isMine(pos)) pos = random.nextInt(n*m);
board[row(pos)][col(pos)] = true;
}
for (int row=0;row<n;row++) {
for (int col=0;col<m;col++) {
if (isMine(row, col)) userBoard[row][col] = -1;
else {
int count = 0;
for (int[] dir : directions) {
if (isMineUnbounded(row+dir[0], col+dir[1])) count++;
}
userBoard[row][col] = count;
}
}
}
return;
}
private boolean isMine(int pos) { return board[row(pos)][col(pos)]; }
private boolean isMine(int row, int col) { return board[row][col]; }
private boolean isMineUnbounded(int row, int col) {
if (!validRowCol(row, col)) return false;
return isMine(row, col);
}
private boolean isNotVisible(int row, int col) { return visible[row][col] == 0; }
private boolean isVisible(int row, int col) { return visible[row][col] == 1; }
private boolean isMarked(int row, int col) { return visible[row][col] == 2; }
private boolean validRowCol(int row, int col) { return !(row < 0 || row >= n || col < 0 || col >= m); }
private int row(int pos) { return pos / m; }
private int col(int pos) { return pos % m; }
private boolean isRevealing(String selection) { return selection.equals("r"); }
private boolean isMarking(String selection) { return selection.equals("m"); }
public boolean open(String selection, int row, int col) {
if (!validRowCol(row, col)) {
System.out.println("Invalid selection");
printUserBoard();
return true;
}
if (isVisible(row, col)) {
System.out.println("Already visible");
printUserBoard();
return true;
}
if (!isMarking(selection) && !isRevealing(selection)) {
System.out.println("Invalid action: " + selection);
printUserBoard();
return true;
}
if (isMarking(selection)) {
switch (visible[row][col]) {
case 0:
visible[row][col] = 2;
break;
case 1:
break;
case 2:
visible[row][col] = 0;
break;
}
printUserBoard();
return true;
}
else {
if (isMine(row, col)) {
finished = true;
visible[row][col] = 1;
System.out.println("Boom! Game Over");
printUserBoard();
return false;
}
expandFrom(row, col);
printUserBoard();
// expand from here if we opened a 0 spot
if (visibleFields == n*m - numberOfMines) {
finished = true;
System.out.println("You win!");
}
}
return true;
}
private void expandFrom(int row, int col) {
if (!validRowCol(row, col)) return;
if (isVisible(row, col)) return;
visible[row][col] = 1;
visibleFields++;
if (userBoard[row][col] != 0) return;
for (int[] dir : sdir) {
expandFrom(row+dir[0], col+dir[1]);
}
return;
}
public void printTrueBoard() {
System.out.print(" ");
for (int j=0;j<m;j++) System.out.print(j + " ");
System.out.println("");
for (int i=0;i<n;i++) {
System.out.print(i + " ");
for (int j=0;j<m;j++) System.out.print(isMine(i,j) ? "x " : "_ ");
System.out.println("");
}
return;
}
public void printUserBoard() {
System.out.print(" ");
for (int j=0;j<m;j++) System.out.print(j + " ");
System.out.println("");
for (int i=0;i<n;i++) {
System.out.print(i + " ");
for (int j=0;j<m;j++) {
switch(visible[i][j]) {
case 0:
System.out.print("_ ");
break;
case 1:
System.out.print(isMine(i, j) ? "x " : userBoard[i][j] + " ");
break;
case 2:
System.out.print("? ");
break;
}
}
System.out.println("");
}
return;
}
public boolean isFinished() {
return finished;
}
}
/*
Your previous Python 2 content is preserved below:
This is what your interview environment will look like (except in a real interview you will also have audio).
Use the language dropdown near the top right to select the language you would like to use.
You can run code by hitting the 'Run' button, which will appear near the top left once you've selected a language.
There is also a whiteboard you can use for any sketching you need to do. Click on 'Draw' in the bottom center.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment