简体   繁体   中英

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

}

I think most of the problems come from general confusion about arrays. Eg the line from the code block run when you press 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. 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. (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;

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

Make a method to print the grid if 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 ='.' 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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