0

我正在尝试用 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);
}

}
4

2 回答 2

1

我认为大多数问题来自对数组的普遍混淆。例如,当您按下 w 时,代码块中的行会运行:

gridArray[randomRow][randomCol] = gridArray[randomRow+1][randomCol];

将 randomRow, randomCol 处的数组的 char 值(在本例中为“x”)设置为其下一行 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("");  
}
于 2013-03-11T02:39:57.640 回答
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;      
        }

在做的最后,

if(validInput) printGrid(gridArray);
于 2013-03-11T02:09:32.920 回答