我正在尝试用 Java 编写一个简单的游戏,它使用 2d 数组创建一个点网格,供用户继续前进。我已经这样做了,我已经到了要求用户选择他们的动作的地步,但是,在那之后,我想重新打印网格,而他们的旧空间是空的,并且在他们搬到的空间。我不确定如何重新打印网格,因为当我第一次打印它时,我使用了 if 语句来告诉循环不要在哪里放置点。我将在下面发布代码,非常感谢提示!
import java.util.Scanner;
import java.util.Random;
public class main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
char[][] gridArray = new char[10][10];
int randomRow;
int randomCol;
int randomRow2;
int randomCol2;
String moveSelect;
boolean validInput = true;
int min = 0;
int max = 9;
Random tRNG = new Random();
randomRow = tRNG.nextInt(max - min + 1) + min;
randomCol = tRNG.nextInt(max - min + 1) + min;
randomRow2 = tRNG.nextInt(max - min + 1) + min;
randomCol2 = tRNG.nextInt(max - min + 1) + min;
gridArray[randomRow][randomCol] = 'o';
gridArray[randomRow2][randomCol2] = 'X';
for (int i = 0; i < gridArray.length; i++)
{
for (int j = 0; j < gridArray.length; j++)
{
if (gridArray[i][j] != gridArray[randomRow][randomCol] && gridArray[i][j] != gridArray[randomRow2][randomCol2])
{
gridArray[i][j] = '.';
}
System.out.print(gridArray[i][j]);
}
System.out.println("");
}
System.out.println("Enter a movement choice W A S or D");
do{
Scanner keyboard = new Scanner(System.in);
moveSelect = keyboard.nextLine();
if ( moveSelect.equals("w"))
{
gridArray[randomRow][randomCol] = gridArray[randomRow+1][randomCol];
gridArray[randomRow][randomCol] = ' ';
validInput = true;
}
else if ( moveSelect.equals("a"))
{
gridArray[randomRow][randomCol] = gridArray[randomRow][randomCol-1];
gridArray[randomRow][randomCol] = ' ';
validInput = true;
}
else if ( moveSelect.equals("s"))
{
gridArray[randomRow][randomCol] = gridArray[randomRow-1][randomCol];
gridArray[randomRow][randomCol] = ' ';
validInput = true;
}
else if (moveSelect.equals("d"))
{
gridArray[randomRow][randomCol] = gridArray[randomRow][randomCol+1];
gridArray[randomRow][randomCol] = ' ';
validInput = true;
}
else
{
System.out.println("Invalid Entry. Try again.");
validInput = false;
}
} while (validInput == false);
}
}