Skip to content

Instantly share code, notes, and snippets.

@dav-s
Created April 23, 2015 02:41
Show Gist options
  • Save dav-s/cba59a4973431aa20690 to your computer and use it in GitHub Desktop.
Save dav-s/cba59a4973431aa20690 to your computer and use it in GitHub Desktop.
A small little minesweeper implementation in Java.
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
* Created by Davis Robertson on 4/22/15
*
* @author Davis Robertson
*/
public class MinesweeperPlayer {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
Minesweeper ms = new Minesweeper(10, 10, 10);
while (!ms.gameOver()){
System.out.println(ms);
System.out.println();
ms.uncover(scn.nextByte()-1, scn.next().charAt(0)-'A');
}
System.out.println(ms.toStringReveal());
if (ms.gameWon()){
System.out.println("YOU WON!");
}else{
System.out.println("YOU LOST!");
}
}
}
class Minesweeper{
private int rows, columns;
private boolean gameOver;
private boolean gameWon;
private byte[][] board;
private boolean[][] uncovered;
private static final byte BOMB = -1, FLAG = -2, COVERED = -4;
private static Map<Byte, Character> byteCharacterMap = new HashMap<>();
private int numberUncovered = 0, numberBombs;
static {
byteCharacterMap.put(BOMB, 'X');
byteCharacterMap.put(FLAG, '?');
byteCharacterMap.put(COVERED, '_');
byteCharacterMap.put((byte)0, ' ');
for (byte i = 1; i < 8; i++) {
byteCharacterMap.put(i, (char)(i+'0'));
}
}
public Minesweeper(int rows, int columns, int bombs){
if (rows*columns<bombs){
throw new IllegalArgumentException("There can not be more bombs than spaces on the grid.");
}
this.rows=rows;
this.columns=columns;
numberBombs = bombs;
board = new byte[rows][columns];
uncovered = new boolean[rows][columns];
for (int i = 0; i < bombs; i++) {
int[] bombpos = getRandomPosition();
while (board[bombpos[0]][bombpos[1]]==BOMB){
bombpos = getRandomPosition();
}
placeBomb(bombpos[0], bombpos[1]);
}
}
public int[] getRandomPosition(){
return new int[]{(int)(Math.random()*rows), (int)(Math.random()*columns)};
}
private void placeBomb(int row, int col){
if(!isValidPosition(row, col)){
throw new IllegalArgumentException("That is not a valid spot on the board.");
}
board[row][col] = BOMB;
for (int rowadd = -1; rowadd <= 1; rowadd++) {
for (int coladd = -1; coladd <= 1; coladd++) {
if (isValidPosition(row+rowadd, col+coladd) && board[row+rowadd][col+coladd]!=BOMB){
board[row+rowadd][col+coladd]++;
}
}
}
}
public boolean isValidPosition(int row, int col){
return row>=0 && row<rows && col>=0 && col<columns;
}
public void uncover(int row, int col){
if (!isValidPosition(row, col)){
throw new IllegalArgumentException("A valid position is required.");
}if(uncovered[row][col]){
throw new IllegalArgumentException("This position has already been uncovered.");
}
if (board[row][col]==BOMB){
gameOver=true;
}
uncovered[row][col]=true;
numberUncovered++;
if (board[row][col]==0){
for (int rowadd = -1; rowadd <= 1; rowadd++) {
for (int coladd = -1; coladd <= 1; coladd++) {
if (isValidPosition(row+rowadd, col+coladd) && !uncovered[row+rowadd][col+coladd]){
if (board[row+rowadd][col+coladd]!=0){
uncovered[row+rowadd][col+coladd]=true;
numberUncovered++;
}else{
uncover(row+rowadd, col+coladd);
}
}
}
}
}
if (rows*columns-numberUncovered==numberBombs){
gameWon=true;
gameOver=true;
}
}
public boolean gameOver(){
return gameOver;
}
public boolean gameWon(){
return gameWon;
}
public String toString(){
String result = "+ ";
for (int i = 0; i < columns; i++) {
result+=" "+((char) (i+'A'));
}
result+="\n";
for (int i = 0; i < rows; i++) {
result+=String.format(" %2d|", (i+1));
for (int j = 0; j < columns; j++) {
byte current = COVERED;
if (uncovered[i][j]){
current = board[i][j];
}
result+=" "+byteCharacterMap.get(current);
}
result+="\n";
}
return result;
}
public String toStringReveal(){
String result = "+ ";
for (int i = 0; i < columns; i++) {
result+=" "+((char) (i+'A'));
}
result+="\n";
for (int i = 0; i < rows; i++) {
result+=String.format(" %2d|", (i+1));
for (int j = 0; j < columns; j++) {
result+=" "+byteCharacterMap.get(board[i][j]);
}
result+="\n";
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment