简体   繁体   English

简单的二维数组Java游戏

[英]Simple 2d array java game

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. 我正在尝试用Java编写一个简单的游戏,该游戏使用2d数组为用户创建点网格。 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. 我已经做完了,到了要问用户选择运动的地步,但是,在那之后,我想重新打印网格,将其旧空间为空,并且将“ o”他们搬到的空间。 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. 但是我不确定如何重新打印网格,因为当我第一次打印网格时,我使用了if语句来告诉循环中不放置点的位置。 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);
}

}

I think most of the problems come from general confusion about arrays. 我认为大多数问题来自对数组的普遍困惑。 Eg the line from the code block run when you press w: 例如,当您按w时,代码块中的行将运行:

gridArray[randomRow][randomCol] = gridArray[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. 将数组的char值设置为randomRow randomCol(在本例中为“ x”)设置为在randomRow + 1处其下方一行的数组空间的char值,randomCol。 And then the line of code after that: 然后是下面的代码行:

gridArray[randomRow][randomCol] = ' ';

assigns a new value to that same space making the code above worthless! 为相同的空间分配一个新值,使上面的代码毫无价值! And in the end, randomRow and randomCol are never modified. 最后,永远不会修改randomRow和randomCol。 (also you had some confusion about which way the x would move with a higher row value) (您也对x以较高的行值移动的方式有些困惑)

all you actually need to do in this code block is: 您在此代码块中实际需要做的就是:

randomRow -= 1;

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. 还有一个类似的问题,因为for循环中的if语句有效,但后来又导致问题。 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("");  
}

Make a method to print the grid if validInput==true 如果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("");  
    }
}

I modified your gridArray[randomRow][randomCol] from =' ' to ='.' 我将您的gridArray [randomRow] [randomCol]从=''修改为='。 because you want it to be a dot right? 因为您希望它是一个点对吗? (If you actually want it to be a space, just leave it like it already is) For validation, change each of the directions to things like: (如果您实际上希望它是一个空间,只需保留它已经存在的状态即可)。为了进行验证,请将每个方向更改为以下内容:

if ( moveSelect.equals("w")&&randomRow!=0)
        {
            gridArray[randomRow][randomCol] = gridArray[randomRow-1][randomCol];
            gridArray[randomRow][randomCol] = '.';
            validInput = true;      
        }

At the end of the do, 在操作结束时,

if(validInput) printGrid(gridArray);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM