Skip to content

Instantly share code, notes, and snippets.

@dmamills
Created December 7, 2012 03:44
Show Gist options
  • Save dmamills/4230601 to your computer and use it in GitHub Desktop.
Save dmamills/4230601 to your computer and use it in GitHub Desktop.
Minesweeper revisions, generate a minesweeper board of any size, and number of mines
/*
Challenge #108
http://www.reddit.com/r/dailyprogrammer/comments/126905/10272012_challenge_108_intermediate_minesweeper/
Daniel Mills
Create a 15x15, 20 Mine Minesweeper Field
Revision: updated to be able to increase boardsize and mines
*/
import java.util.Random;
public class MinesweepField {
private class BoardPoint {
int x;
int y;
public BoardPoint(int x,int y) {
this.x = x;
this.y = y;
}
}
static Random rdm = new Random();
private int SIZE = 20;
private final char MINE = 'M';
private final int MINE_COUNT = 20;
private BoardPoint[] minelocations = new BoardPoint[MINE_COUNT];
private BoardPoint[] checkLocations;
private char[][] field;
private int[][] values;
public void printField() {
for(int i=0; i < SIZE;i++) {
for(int j =0; j < SIZE;j++) {
System.out.print("| "+field[i][j] + " ");
}
System.out.print("|");
//make and print divider line
String line = "\n";
for(int x =0; x < SIZE*4;x++)
line +="-";
System.out.println(line);
}
}
public void calculateNonMineValues() {
//for each mine
for(int i =0; i < minelocations.length;i++) {
//for each check spot around mine
for(int j=0; j < checkLocations.length;j++) {
BoardPoint temp = new BoardPoint(minelocations[i].x+checkLocations[j].x, minelocations[i].y+checkLocations[j].y);
//if checkLocation inside board
if( (temp.x > -1 && temp.x < SIZE) && (temp.y > -1 && temp.y < SIZE )) {
//if not another mine at location
if(field[temp.x][temp.y] != MINE) {
//increase value at location
values[temp.x][temp.y]++;
}
}
}
}
}
public void addValuesToBoard() {
//check each spot on board
for(int i=0;i < SIZE;i++) {
for(int j=0;j< SIZE;j++) {
//if location not a mine or has no value
if(field[i][j] != MINE && values[i][j] != 0) {
//put value in string and put on display field
String x = ""+values[i][j];
field[i][j] = x.charAt(0);
}
}
}
}
public MinesweepField() {
//check locations are the 9 spots around a mine to check values
checkLocations = new BoardPoint[9];
checkLocations[0] = new BoardPoint(-1,-1);checkLocations[1] = new BoardPoint(-1,0);checkLocations[2] = new BoardPoint(-1,1);
checkLocations[3] = new BoardPoint(0,-1);checkLocations[4] = new BoardPoint(0,0);checkLocations[5] = new BoardPoint(0,1);
checkLocations[6] = new BoardPoint(1,-1);checkLocations[7] = new BoardPoint(1,0);checkLocations[8] = new BoardPoint(1,1);
//create field empty field
field = new char[SIZE][SIZE];
for(int i =0; i < SIZE;i++)
for(int j=0;j< SIZE;j++)
field[i][j] = ' ';
//create values field and set all values to 0
values = new int[SIZE][SIZE];
for(int i =0; i < SIZE;i++)for(int j =0;j< SIZE;j++) values[i][j]=0;
//set number of mines and place on field
int mines = MINE_COUNT;
while(mines > 0) {
int rx = rdm.nextInt(SIZE);
int ry = rdm.nextInt(SIZE);
//if rx,ry == free space place mine
if(field[rx][ry] == ' ') {
field[rx][ry] = MINE;
//store mine location
minelocations[mines-1] = new BoardPoint(rx,ry);
mines--;
}
}
//set values around mines
calculateNonMineValues();
//add numbers to board
addValuesToBoard();
}
public static void main(String[] args) {
MinesweepField ms = new MinesweepField();
ms.printField();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment