【发布时间】:2014-05-20 09:27:46
【问题描述】:
我仍然坚持我之前提出的问题,但我想我会发一个新帖子来清理一下(抱歉,如果这很麻烦)。
我正在努力展示一个/细胞的“周围地雷”。我需要做的是让用户清除一个单元格,如果附近有地雷,则显示有多少个,例如: 我如何能够在我的数组输出中显示围绕单元格的地雷:
替换:
0 1 2 3 4
0| . . . . .
1| . . . . .
2| . . . . *
3| . . * . .
4| * . . * *
与:
0 1 2 3 4
0| . . . . .
1| . . . 1 1
2| . . 1 2 *
3| 1 2 * 2 2
4| * 1 2 * *
我用来放置地雷的代码是:
public MineField(int w, int h, int m)
{
Random r = new Random();
mineField = new State[w][h];
surroundingMines = new int[w][h];
initialiseMineField = new int[w][h];
traceOn = true; //set to false before submitting
width = w;
height = h;
mineCount = m;
for (int i = 0; i < w; i++)
{
for (int j = 0; j < h; j++)
{
mineField[i][j] = State.COVERED;
}
}
for (int k = 0; k < m; k++)
{
while (true)
{
int a = r.nextInt(w);
int b = r.nextInt(h);
if (mineField[a][b] != State.MINED)
{
break;
}
}
mineField[r.nextInt(w)][r.nextInt(h)] = State.MINED;
}
}
我通过以下方式展示我的“雷区”:
public void displayField(boolean showTruth)
{
System.out.print(" ");
for (int col = 0; col < width; col++)
{
System.out.print(" " + col);
}
System.out.println();
for (int row = 0; row < height; row++)
{
System.out.print("" + row + "|");
for (int col = 0; col < width; col++)
{
//TODO: You need to complete this method by printing the correct character for the current field cell
if (mineField[row][col] == State.MINED)
{
System.out.print(" " + '*' + " " );
}
if (mineField[row][col] == State.EXPLODED)
{
System.out.print(" " + '+' + " " );
}
if (mineField[row][col] == State.COVERED)
{
System.out.print(" " + '.' + " " );
}
if (mineField[row][col] == State.CLEARED)
{
System.out.print(" " + ' ' + " " );
}
if (mineField[row][col] == State.FLAGGED)
{
System.out.print(" " + 'F' + " " );
}
if (mineField[row][col] == State.MISFLAGGED)
{
System.out.print(" " + 'F' + " " );
}
}
System.out.println();
}
}
感谢您的帮助,谢谢!
【问题讨论】: