views:

77

answers:

2

How would you write this code?

This particular question is about a maze game that has an arraylist of occupants which are Explorers (you), Monsters (touching will kill you), and Treasures. The game uses blocks of square objects in which these occupants reside in. The particular thing I want to do is file reading which can export the current configuration of the maze or import them as a txt file.

The specs: First read in the rows and cols of the Maze to create a Square[][] of the appropriate size. Then construct and read in all the Squares/Occupants.

For Squares, the Maze will first determine that the line starts with "Square". It will then read in the row and col of the Square and use that information to construct a Square object. Finally it will pass the rest of the Scanner to the Square's toObject method so it can initialize itself.

For all other Occupants, the Maze will determine what kind of Occupant it is and construct the appropriate object using the constructor that only takes a Maze. It will not read the row or the col from the Scanner, but simply pass the Scanner on to the toObject method of the newly created object.

This is code that I have so far which could be wrong:

    public void readMazeFromFile(String fileName) throws IOException, FileNotFoundException, MazeReadException
   {
        Scanner fileSc = new Scanner(new File(fileName));
        String line = fileSc.nextLine(); //whats on the line, will be overwritten
        Scanner lineSc = new Scanner(line);
        String temp;
        lineSc.useDelimiter(",");
        int lineNum = 1; //every time you scan a line out, do lineNum++
        int r1, r2, r3, r4, c1, c2, c3, c4;

        rows = fileSc.nextInt();
        cols = fileSc.nextInt();
        Square hi = new Square(rows, cols);
        line = fileSc.nextLine();
        while ( line != null)
        {
            line = lineSc.nextLine();
            lineSc = new Scanner(line);

            if( lineSc.equals("Square"))
            {
                r1 = lineSc.nextInt();
                c1 = lineSc.nextInt(); 
                hi.toObject(lineSc);
            }
            if (lineSc.equals("Explorer"))
            {

                explorer.toObject(lineSc);
            }
            if (lineSc.equals("Treasure"))
            {
                Treasure.toObject(lineSc);
            }


            lineNum++;
        }

Here is sample output:

5,5
Square,0,0,true,false,false,true,true,true
Square,0,1,true,false,true,false,true,true
Square,0,2,true,false,true,false,false,false
Square,0,3,true,false,false,false,false,false
Square,0,4,true,true,false,false,false,false
Square,1,0,false,false,true,true,true,true
Square,1,1,true,false,true,false,false,false
Square,1,2,true,true,false,false,false,false
Square,1,3,false,true,false,true,false,false
Square,1,4,false,true,false,true,false,false
Square,2,0,true,false,false,true,false,false
Square,2,1,true,false,true,false,false,false
Square,2,2,false,true,false,false,false,false
Square,2,3,false,true,false,true,false,false
Square,2,4,false,true,false,true,false,false
Square,3,0,false,true,false,true,false,false
Square,3,1,true,false,false,true,false,false
Square,3,2,false,true,false,false,false,false
Square,3,3,false,true,true,true,false,false
Square,3,4,false,true,false,true,false,false
Square,4,0,false,true,true,true,false,false
Square,4,1,false,true,true,true,false,false
Square,4,2,false,false,true,true,false,false
Square,4,3,true,false,true,false,false,false
Square,4,4,false,true,true,false,false,false
Explorer,0,0,Scary Name
Treasure,4,4,true
Treasure,2,2,false
Monster,4,4
Monster,3,3

What would you write for this section?

A: 

Use a FileReader on the file and then attach a StreamTokenizer to that, far more efficient :)

http://java.sun.com/j2se/1.4.2/docs/api/java/io/StreamTokenizer.html

http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileReader.html

Chris Dennett
+1  A: 

This is a skeleton that you can start with. You really don't need anything more than a Scanner here; it can do everything that you'd need to scan the input and do the conversion, etc. You want to use its next(), nextInt(), nextBoolean() and nextLine() methods.

    Scanner in = new Scanner(new File(filename));
    in.useDelimiter("\\s+|,");

    int rows = in.nextInt();
    int cols = in.nextInt();
    // construct the array

    while (in.hasNext()) {
        String type = in.next();
        int r = in.nextInt();
        int c = in.nextInt();
        // read more depending on type
    }
polygenelubricants
Thanks much for your help, I've updated my code based on it, but still not done. Have any other suggestions?
Kevin Duke
Well, we can engage in a long discussion on the OOP aspects, e.g. what classes/interfaces to declare, what extends/implements what, what design patterns to use, etc. That's outside the realm of "Java: FileRead question", though.
polygenelubricants