简单的2D数组Java游戏

简单的2D数组Java游戏

本文介绍了简单的2D数组Java游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用Java编写一个简单的游戏,该游戏使用2d数组创建点阵网格,以供用户继续前进.我已经做到了,现在我要询问用户他们的运动选择,但是,在那之后,我想重新打印网格,将其旧空间留为空白,并在其中输入"o"他们搬到的空间.我不确定如何重新打印网格,因为当我第一次打印网格时,我使用了if语句来告诉循环中不放置点的位置.我将在下面发布代码,非常感谢您的提示!

I'm trying to write a simple game in Java that creates a grid of dots using a 2d array for the user to move on. I've done that and I've gotten to the point where I'm asking the user for their movement selection, however, after that happens I want to reprint the grid with their old space empty, and with the 'o' in the space they moved to. I'm unsure how to go about reprinting the grid though, as when I first printed it I used an if statement to tell the loop where to not put dots. I'll post the code below, tips are greatly appreciated!

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);
}

}

推荐答案

我认为大多数问题来自对数组的普遍困惑.例如.当您按下w时,代码块中的行将运行:

I think most of the problems come from general confusion about arrays. E.g. the line from the code block run when you press w:

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

将该数组的char值设置为randomRow randomCol(在本例中为"x"),将其数组的char值设置为在randomRow + 1处其下一行的randomCol.然后是下面的代码行:

sets the char value of the array at randomRow, randomCol (in this case an 'x') to the char value of the of the array space one row below it at randomRow+1, randomCol. And then the line of code after that:

gridArray[randomRow][randomCol] = ' ';

为相同的空间分配一个新值,使上面的代码毫无价值!最后,从不修改randomRow和randomCol. (同样,对于x以更高的行值移动的方式,您也有些困惑)

assigns a new value to that same space making the code above worthless! And in the end, randomRow and randomCol are never modified. (also you had some confusion about which way the x would move with a higher row value)

在此代码块中您真正需要做的就是:

all you actually need to do in this code block is:

randomRow -= 1;

此后,您可以简单地使用之前的打印代码重新打印整个网格.尽管此代码还有其他问题,使其远非最佳.还有一个类似的问题,因为for循环中的if语句有效,但后来又导致问题.只需重写它以解决所有错误,就可以得到:

after this, you can simply reprint the whole grid with the printing code from before. Although there are other issues with this code that make it far from optimal. Also there are a similar problem in that if statement in the for loop that worked but led to problems later down the road. Simply rewriting it to fix all the things wrong, you get this:

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("");
}

这篇关于简单的2D数组Java游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 02:06