我试图创建一个10x10 Minesweeper-esque板,由10个随机放置为*。不涉及实际的游戏玩法,只需要制作棋盘。我已经能够(有些)成功地随机地放置了地雷,但我无法让附近的矿井计数方面发挥作用。我尝试了很多不同的东西,但这是我迄今为止所提出的。
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();
}
}
}
答案 0 :(得分:1)
尝试此代码(不是面向对象的方法),但很容易理解:
import java.util.Random;
public class Mines {
public static final int BOARD_SIZE = 5;
enum Space {
Empty, Mine
};
public static void main(String[] args) {
Random rand = new Random();
// Creates board
System.out.println("Empty 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.print("_");
}
System.out.println();
}
// Sets mines
for (int i = 0; i < BOARD_SIZE; i++) {
int mX = rand.nextInt(BOARD_SIZE);
int mY = rand.nextInt(BOARD_SIZE);
// Condition if random number combination [mX, mY] generated previously. Guarantees BOARD_SIZE mines always.
if(Space.Mine.equals(board[mX][mY])) {
i--;
continue;
}
board[mX][mY] = Space.Mine;
}
System.out.println("\nPlacing mines");
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("\nCounting mines");
for (int y = 0; y < board.length; y++) {
for (int x = 0; x < board.length; x++) {
if(Space.Mine.equals(board[y][x])) {
System.out.print("*");
} else {
System.out.print(findAdjCount(y, x, board));
}
}
System.out.println();
}
}
private static int findAdjCount(int row, int col, Space[][] board) {
int cnt = 0;
// Check 8 adjacent positions
for (int i = row - 1; i <= row + 1; i++) {
for (int j = col - 1; j <= col + 1; j++) {
if(i >= 0 && i < BOARD_SIZE && j >= 0 && j < BOARD_SIZE) {
if(Space.Mine.equals(board[i][j])) {
cnt++;
}
}
}
}
return cnt;
}
}