我正在尝试用Java编写一个简单的游戏,使用2D阵列创建一个点网格,供用户继续使用。我已经做到了这一点,我已经达到了我要求用户选择移动的地步,然而,在那之后,我想重新打印网格,其旧空间为空,并且“o”在他们搬到的空间。我不确定如何重新打印网格,因为当我第一次打印它时,我使用了一个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);
}
}
答案 0 :(得分:1)
我认为大多数问题都来自对数组的一般混淆。例如。当您按下w:
时,代码块中的行会运行gridArray[randomRow][randomCol] = gridArray[randomRow+1][randomCol];
将randomRow,randomCol(在本例中为'x')的数组的char值设置为randomRow + 1,randomCol下面一行下方数组空间的char值。然后是代码行:
gridArray[randomRow][randomCol] = ' ';
为同一个空间分配一个新值,使上面的代码变得毫无价值!最后,randomRow和randomCol永远不会被修改。 (也有一些关于x将以更高的行值移动的方式的混淆)
您在此代码块中实际需要做的就是:
randomRow -= 1;
之后,您可以使用之前的打印代码重新打印整个网格。虽然此代码还存在其他问题,但这些问题远非最佳。还有一个类似的问题,即for循环中的if语句有效,但后来导致了问题。简单地重写它来解决所有错误的问题,你得到这个:
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 (!((i == randomRow && j == randomCol) || (i == randomRow2 && j == 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"))
{
randomRow -= 1;
validInput = true;
}
else if ( moveSelect.equals("a"))
{
randomCol -= 1;
validInput = true;
}
else if ( moveSelect.equals("s"))
{
randomRow += 1;
validInput = true;
}
else if (moveSelect.equals("d"))
{
randomCol -= 1;
validInput = true;
}
else
{
System.out.println("Invalid Entry. Try again.");
validInput = false;
}
} while (validInput == false);
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 (!((i == randomRow && j == randomCol) || (i == randomRow2 && j == randomCol2)))
{
gridArray[i][j] = '.';
}
System.out.print(gridArray[i][j]);
}
System.out.println("");
}
答案 1 :(得分:0)
如果validInput == true
,请创建一个打印网格的方法public static void printGrid(char[][]gridArray){
for (int i = 0; i < gridArray.length; i++)
{
for (int j = 0; j < gridArray.length; j++)
{
System.out.print(gridArray[i][j]);
}
System.out.println("");
}
}
我将gridArray [randomRow] [randomCol]从=''修改为='。'因为你想让它成为一个点吗? (如果你真的想让它成为一个空间,那么就像它已经存在一样) 要进行验证,请将每个方向更改为:
if ( moveSelect.equals("w")&&randomRow!=0)
{
gridArray[randomRow][randomCol] = gridArray[randomRow-1][randomCol];
gridArray[randomRow][randomCol] = '.';
validInput = true;
}
在do结束时,
if(validInput) printGrid(gridArray);