一组阵列扫雷游戏的另外两个问题
1。)我的代码(与Merlin的辅导)正在输出正确数量的地雷。有时。我怎样才能解决这个问题?的 ANSWERED
示例:
Enter width of board: 5
Enter height of board: 5
Enter number of mines: 5
0 1 2 3 4
0| . . . . .
1| . . . . .
2| . . . . *
3| . . * . .
4| * . . * *
Enter width of board: 5
Enter height of board: 5
Enter number of mines: 5
0 1 2 3 4
0| * . . . .
1| . . * . .
2| . . . . .
3| . * . . .
4| * . . . *
MineField代码:
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 enum State {
/** Cell is mined */
MINED,
/** Player uncovered (hit) mine */
EXPLODED,
/** Cell is not mined but unexplored */
COVERED,
/** Cell is not mined and has been explored */
CLEARED,
/** Cell has been flagged and contains a mine */
FLAGGED,
/** Cell has been flagged but does not contain a mine */
MISFLAGGED;
必填状态字符:
public char toChar(int surroundingMines, boolean showTruth) {
switch (this) {
case EXPLODED: return '+';
case COVERED: return '.';
case CLEARED: return surroundingMines == 0 ? ' ' : String.valueOf(surroundingMines).charAt(0);
case FLAGGED: return 'F';
case MINED: return showTruth ? '*' : '.';
default /* MISFLAGGED */: return showTruth ? 'x' : 'F';
}
}
2。)我如何能够在我的数组输出中显示围绕单元格的地雷:
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 * *
答案 0 :(得分:0)
oops
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)
{
mineField[a][b] = State.MINED;
break;
}
}
}