1

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

}
Eogcloud
  • 1,335
  • 4
  • 19
  • 44
  • 4
    Print all `Exception`s that are caught with an `e.printStackTrace();`. Exceptions are generally not to be ignored. They explain what went wrong, e.g. in your paint method. – The Nail Jan 27 '12 at 21:18
  • 1
    I don't understand why you think your paint method would paint. You're not calling any method that actually paints. You're just reading a file and populating an array. You need to show more than this. – AHungerArtist Jan 27 '12 at 21:18
  • You both seem to have misunderstood my question. I am well aware that this code does not print anything, I'm merely trying to move the data from the text file into a 2d character array currently. Once I manage to do this, I can work on the rest. – Eogcloud Jan 27 '12 at 21:25
  • If you bothered to read the whole question and the code before posting arrogant comments, you would have seen from obvious evidence and some common sense that I'm not painting anything. Hence to total lack of code to do so. Once I hammer out the issue of populating the array correctly I can begin to do so, as I've stated twice now. As you'll see from the code, the problem is indeed within the paint method, and it doesn't work. So what I've said is not wrong or inaccurate. it's called the paint method as it will be painting things in future. – Eogcloud Jan 27 '12 at 21:46
  • 1
    So what doesn't work about the paint method? – thedaian Jan 27 '12 at 21:54
  • I've been looking at it for two hours now, I can't find the problem. -the line reader should pull a string from the file, assign it to the string line -I've a nested loop that controls rows and columns for the 2d array, the inner loop also uses the line.charAt(y) method to loop through whatever string is pulled from the file and assigning those individual characters to the array in the correct position (imagine the text file as a 28x30 matrix of characters) -I'm getting arrayoutofbounds exceptions on this line " Amaze[y][i]=line.charAt(y); " in the loop -main prints the aray to check it – Eogcloud Jan 27 '12 at 22:01
  • I THINK it seems to be some fundamental problem with my array or loop, but again that's vague at best. I dont know where to start finding the problem, I cant find code that's fundamentally "wrong" hence my post here, thanks for the reply! – Eogcloud Jan 27 '12 at 22:02

2 Answers2

2

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.

thedaian
  • 1,313
  • 9
  • 11
  • I must havve pasted the code after messing around with it, They were correct originally but I've been playing with the code a lot and trying some different things, that slipped through before my mistake! I tried the 2d array at [1000][1000] and issue persists unfortuantely :/ Thank you for your reply though! – Eogcloud Jan 27 '12 at 22:32
1

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();
        }
    }
}
Steve Blackwell
  • 5,904
  • 32
  • 49
  • Hey, thanks for the reply, you helped a lot. I didn't want to just copy and paste this as I felt I wouldnt learn anything, so I modified mine a bit to use the way in which you did it above. Separate Functions is a good idea, I was pretty caught up in trying to find the problem though that I didn't even consider that! It works, I'm still not entirely sure why mine wasn't working though! Thanks for your help though Steve! – Eogcloud Jan 27 '12 at 22:51