I'm trying to create a 10x10 Minesweeper-esque board made of _'s, with 10 mines randomly placed as *'s. No actual gameplay is involved, just the making of the board. I've been able to (somewhat) successfully place the mines randomly but I can't get the nearby mine counting aspect to work. I've tried many different things but this is what I've come up with so far.
import java.util.Random;
public class Mines {
public static final int BOARD_SIZE = 10;
enum Space {Empty, Mine, MineCount};
public static void main(String[] args) {
Random rand = new Random();
//Creates board
Space[][] board = new Space[BOARD_SIZE][BOARD_SIZE];
for (int y=0;y<board.length;y++)
{
for (int x=0;x<board.length;x++)
{
board[x][y] = Space.Empty;
}
}
System.out.println("Creating empty board");
//Draws the board
for(int y=0;y<board.length;y++)
{
for(int x=0;x<board.length;x++)
{
switch(board[y][x])
{
case Empty:
System.out.print("_");
break;
default:
System.out.println("?");
}
}
System.out.println();
}
System.out.println("Placing mines");
//Sets mines
for(int i=0;i<board.length;i++)
{
int mX = rand.nextInt(BOARD_SIZE);
int mY = rand.nextInt(BOARD_SIZE);
if(board[mX][mY] == Space.Empty)
{
board[mX][mY] = Space.Mine;
}
}
for(int y=0;y<board.length;y++)
{
for(int x=0;x<board.length;x++)
{
switch(board[y][x])
{
case Empty:
System.out.print("_");
break;
case Mine:
System.out.print("*");
break;
}
}
System.out.println();
}
//Count mines
System.out.println("Counting the mines");
//Prints board again
for(int y=0;y<board.length;y++)
{
for(int x=0;x<board.length;x++)
{
switch(board[y][x])
{
case Empty:
System.out.print("_");
break;
case Mine:
System.out.print("*");
break;
case MineCount:
int mineCount = 0;
if(board[x-1][y-1] == Space.Mine)
{
mineCount++;
board[y][x] = Space.MineCount;
System.out.print(mineCount);
}
}
}
System.out.println();
}
}
}