Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 638371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:43:31+00:00 2026-05-13T20:43:31+00:00

How would you write this code? This particular question is about a maze game

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:43:32+00:00Added an answer on May 13, 2026 at 8:43 pm

    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
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 388k
  • Answers 388k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As of the 3.2 SDK you can access the view… May 15, 2026 at 12:33 am
  • Editorial Team
    Editorial Team added an answer It depends on what you're trying to do. You mention… May 15, 2026 at 12:33 am
  • Editorial Team
    Editorial Team added an answer If you are using Ruby on rails I would suggest… May 15, 2026 at 12:33 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.