Question

So I'm working on an assignment for my game design class to build a pacman clone, it'll be throughout the semester.

Currently I've got a text file that is the pacman maze

see below :

WWWWWWWWWWWWWWWWWWWWWWWWWWWW
W............WW............W
W.WWWW.WWWWW.WW.WWWWW.WWWW.W
W*WWWW.WWWWW.WW.WWWWW.WWWW*W
W.WWWW.WWWWW.WW.WWWWW.WWWW.W
W..........................W
W.WWWW.WW.WWWWWWWW.WW.WWWW.W
W.WWWW.WW.WWWWWWWW.WW.WWWW.W
W......WW....WW....WW......W
WWWWWW.WWWWW.WW.WWWWW.WWWWWW
WWWWWW.WWWWW.WW.WWWWW.WWWWWW
WWWWWW.WW..........WW.WWWWWW
WWWWWW.WW.WWWWWWWW.WW.WWWWWW
WWWWWW.WW.WWWWWWWW.WW.WWWWWW
..........WWWWWWWW..........
WWWWWW.WW.WWWWWWWW.WW.WWWWWW
WWWWWW.WW.WWWWWWWW.WW.WWWWWW
WWWWWW.WW..........WW.WWWWWW
WWWWWW.WW.WWWWWWWW.WW.WWWWWW
WWWWWW.WW.WWWWWWWW.WW.WWWWWW
W............WW............W
W.WWWW.WWWWW.WW.WWWWW.WWWW.W
W*WWWW.WWWWW.WW.WWWWW.WWWW*W
W...WW................WW...W
WWW.WW.WW.WWWWWWWW.WW.WW.WWW
WWW.WW.WW.WWWWWWWW.WW.WW.WWW
W......WW....WW....WW......W
W.WWWWWWWWWW.WW.WWWWWWWWWW.W
W.WWWWWWWWWW.WW.WWWWWWWWWW.W
W..........................W
WWWWWWWWWWWWWWWWWWWWWWWWWWWW 

the idea is that this is read in, line by line by a reader from the java io package and then used to populate a 2d array, I think then I can use a loop to specify where to print images using the paint class with the data in the array.

My problem currently is the paint method, it doesnt seem to be working at all but I cant find what's wrong with it at the moment. Can anyone point me in the right direction?

(My codes formatting has been messed up a tad by indentation required here, I'm also new to the java IO package, first time I've seen exception handling!)

Thanks in advance for any help!

//imports
import java.awt.*;
import java.io.*;
import javax.swing.*;


public class Maze extends JFrame
{

//V.Decleration
private static final Dimension WindowSize = new Dimension (600,600);
static char[][] Amaze = new char[28][31];


//default constructor
public Maze()
{
this.setTitle("Pacman");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
int x = screensize.width/2 - WindowSize.width/2;
int y = screensize.height/2 - WindowSize.height/2;
setBounds(x, y, WindowSize.width, WindowSize.height);
setVisible(true);
}

public void paint (Graphics g)
{

String line = null;

try
  { 
     BufferedReader reader = new BufferedReader(new FileReader("G:\\Game       Design\\Pacman\\src\\Maze.txt"));


  for (int i=0; i<=31; i++)
 {   
do
 {
    try
    {

    line=reader.readLine();
     for (int y=0; y<=28; y++)
     {

      Amaze[y][i]=line.charAt(y); 
     }

   }catch (IOException e) { }
 }

while (line!= null);
    try
      {
       reader.close();
      } catch (IOException e) { }
    }
 } catch (FileNotFoundException e) {}

 }

//main
public static void main (String [] args)
{
 Maze maze = new Maze();

 for (int i=0;i<=28;i++)
 System.out.print(Amaze[i][31]);
}

}
Was it helpful?

Solution

Three suggestions:

  1. It may be less misleading to separate your functions to do just one job at a time. So there should be a function to read the file and then another to paint the results.
  2. Put more System.out.println() statements in your code when you're trying to debug. That's a good way to check whether each part did what you intended. For example, printing the line variable after reading it in would at least let you know whether you were reading the file correctly.
  3. Always print out your exceptions. They will tell you what went wrong and where.

That said, this code will load and print your maze:

import java.io.*;

public class Read2DArray {
    private final int WIDTH = 28;
    private final int HEIGHT = 31;

    private char[][] maze = new char[WIDTH][HEIGHT];

    public static void main(String[] args) {
        Read2DArray array = new Read2DArray();
        array.loadFile("maze.txt");
        array.printArray();
    }

    public void loadFile(String fname) {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(fname));

            String line;
            int col = 0, row = 0;
            while((line = reader.readLine()) != null && row < HEIGHT) {
                for(col = 0; col < line.length() && col < WIDTH; col++) {
                    maze[col][row] = line.charAt(col);
                }
                row++;
            }
            reader.close();
        } catch(IOException e) {
            e.printStackTrace();
        }
    }

    public void printArray() {
        for(int row = 0; row < HEIGHT; row++) {
            for(int col = 0; col < WIDTH; col++) {
                System.out.print(maze[col][row]);
            }
            System.out.println();
        }
    }
}

OTHER TIPS

You are creating an array that is too small for the loop. Namely: new char[28][31]; will only allow for a maximum index of 27 and 30. Your for loops are:

for (int i=0; i<=31; i++)
for (int y=0; y<=28; y++)

Use i<31 and y<28, or increase your array to be [29][32]. Either one of these should solve your current problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top