javaarrayspacman

PacMan Grid 2D Array JAVA


I'm supposed to make a simple type text PacMan game in Java and for the board I have to use a 2D array to make the grid. Here are the exact directions: At program startup, constructs and displays a 2-dimensional grid using standard array(s) (no collection classes allowed) with the size dynamically specified by the user (X and Y sizes can be different). Places the PacMan in the upper-left corner of the grid facing left All grid cells should have the empty cell character of ‘.’

This is my code so far, but I keep getting errors and I'm not sure how to fix it:

public class myPacMan {
public static void main(String[] args){
    Scanner input = new Scanner (System.in);
    System.out.print("Choose an x value:");
    int x = input.nextInt();
    System.out.print("Choose a y value:");
    int y = input.nextInt();
    int grid [][] = new int [x][y];
    int i, j = 0;
    for(i=0; i<x; i++);
    for(j=0; j<y; j++);
    System.out.print(grid[x][y] + ".");
}

}


Solution

  • Two things. First, remove the semicolons after your for loops. Second, your print statement should use i and j, instead of x and y. X and Y will always be one more than your array, so you'll get an index out of bounds exception.