0

I am having a problem in this program....can anybody help me create a BufferedReader out of this maze problem...then i have to create a text document entitled with maze.txt...

well here is my program

import java.io.*;
import java.util.*;

public class Amazing{
public static void main(String[]args){
Scanner console=new Scanner(System.in);
FileWriter fw;
BufferedWriter bw;
int r,c;

try{
fw=new FileWriter("maze.txt",true);
bw=new BufferedWriter(fw);
String str[][]={{"","1","2","3","4","5","6","7","8","9","10"},
                {"1","{}","{}","{}","{}","{}","{}","{}","{}","{}","{}"},
                {"2","<>","{}","<>","<>","<>","<>","<>","<>","<>","<>"},
                {"3","<>","<>","{}","<>","<>","<>","<>","<>","<>","<>"},
                {"4","<>","<>","<>","{}","<>","<>","<>","<>","<>","<>"},
                {"5","<>","<>","{}","<>","<>","<>","<>","<>","<>","<>"},
                {"6","<>","<>","{}","{}","{}","<>","<>","<>","<>","<>"},
                {"7","<>","<>","<>","<>","{}","<>","<>","<>","<>","<>"},
                {"8","<>","<>","{}","{}","{}","<>","<>","<>","<>","<>"},
                {"9","<>","<>","{}","{}","{}","{}","{}","{}","<>","<>"},
               {"10","<>","<>","<>","<>","<>","<>","<>","[]","<>","<>"}};
bw.write("\n{}=Open, <>=Close, []=Exit");
for(int i=10;i>=0;i--){
   bw.write("\n");
   if(i==0){
    bw.write("\n");
    }
    for(int j=0;j<=10;j++){
     bw.write(str[i][j]);
     }
}
bw.write("\n\n"+" ");

do{
System.out.print("\n\n    Enter the position:");
   r=console.nextInt();
   c=console.nextInt();
   }while(c==0||r==0);

   System.out.println("\n\n   Sample Run:");
   System.out.print("\n\n\n"+"    Positions"+"      Remarks");
   System.out.print("\n    "+r+"    "+c+"      Start");
   bw.write("\n\n   Sample Run:");
   bw.write("\n\n\n"+"    Positions"+"      Remarks");
   bw.write("\n     "+r+"    "+c+"    Start");

   while(str[r][c]!="[]"){
   if(c!=1){
      if(str[r+1][c-1].equals("{}")){
      r=r+1;
      c=c-1;
      System.out.print("\n    "+r+"    "+c+"     Open");
      bw.write("\n    "+r+"    "+c+"     Open");
      }
      else{
       System.out.print("\n    "+(r+1)+"    "+(c-1)+"     Backtrack");
       bw.write("\n    "+(r+1)+"    "+(c-1)+"     Backtrack");
       }


      if(str[r][c-1].equals("{}") && r==1){
      c=c-1;
      System.out.print("\n    "+r+"    "+c+"     Open");
      bw.write("\n    "+r+"    "+c+"     Open");
      }
      else{
       System.out.print("\n    "+r+"    "+(c+1)+"     Backtrack");
       bw.write("\n    "+r+"    "+(c+1)+"     Backtrack");
       }
       }


       if(str[r+1][c].equals("{}")){
      r=r+1;
      System.out.println("\n    "+r+"    "+c+"     Open");
      bw.write("\n    "+r+"    "+c+"     Open");
      }
      else{
       System.out.print("\n    "+(r+1)+"    "+c+"     Backtrack");
       bw.write("\n    "+(r+1)+"    "+c+"     Backtrack");
       }

       if(c!=10){
       if(str[r+1][c+1].equals("{}")){
       r=r+1;
       c=c+1;
       System.out.println("\n    "+r+"    "+c+"     Open");
       bw.write("\n    "+r+"    "+c+"     Open");
       }
       else{
       System.out.println("\n    "+(r+1)+"    "+(c+1)+"     Backtrack");
       bw.write("\n    "+(r+1)+"    "+(c+1)+"     Backtrack");
       }


      if(str[r][c+1].equals("{}")&& r!=1){
      c=c+1;
      System.out.println("\n    "+r+"    "+c+"     Open");
      bw.write("\n    "+r+"    "+c+"     Open");
      }
      else{
       System.out.println("\n    "+r+"    "+(c-1)+"     Backtrack");
       bw.write("\n    "+r+"    "+(c-1)+"     Backtrack");
       }
       }
      if(str[r+1][c].equals("[]")){
      r=r+1;
      System.out.println("\n    "+r+"    "+c+"     Exit");
      bw.write("\n    "+r+"    "+c+"     Exit");
      }
   }

   System.out.println("\n\n\n  Congratulations!!!");
   bw.write("\n\n\n  Congratulations!!!");

   bw.close();
   }
   catch(IOException e){
   System.out.println(e);
   }

   }
   }
2
Contributors
4
Replies
7
Views
8 Years
Discussion Span
Last Post by nope3d
0

I am having a problem in this program....can anybody help me create a BufferedReader out of this maze problem...

Yes.

then i have to create a text document entitled with maze.txt...

Okay.

If you want better answers, you're going to have to ask more detailed questions, particularly the second one, which isn't even a question. Write a concise but detailed paragraph explaining what you need and what you are stuck on. We could guess and probably be correct, but don't make us guess. Tell us.

0

...thanks for your concern...sorry for I ded not write the whole question correctly...

well here is the question....

Write a program that would allow the user to go out in the maze.The maze is found in maze.txt. if position is "{}" it is open, if "<>" it is backtrack, if "[]" it is exit. Then accept an initial position using command line then lead him the way to go out from the maze.

ex.

maze.txt
10 <><><><><><><>[]<><>
9 <><>{}{}{}{}{}{}<><>
8 <><>{}{}{}<><><><><>
7 <><><><>{}<><><><><>
6 <><>{}{}{}<><><><><>
5 <><>{}<><><><><><><>
4 <><><>{}<><><><><><>
3 <><>{}<><><><><><><>
2 <>{}<><><><><><><><>
1 {}{}{}{}{}{}{}{}{}{}
1 2 3 4 5 6 7 8 9 10


Sample Run:

Enter the position: 1 1

Postion Remarks
1 1 open
2 2 open
3 3 open
4 4 open
5 5 backtrack
4 4 open
5 3 open
.
.
.
.
.
10 8 exit

Congratulations!!!

.... since as you can see I have created a filewriter but then my instructor tell me that it is wrong since she want a bufferedreader to read the maze.txt and would execute the sample run......then if you think that my program is wrong then your free to change it if you can....thanks a lot...

0

Find out whether you are supposed to have the numbers 1 - 10 in the text file. I tend to doubt it, but I don't know.

Google "java how to read file" and pick a tutorial that you like. Here's a little sample code that can serve as a skeleton. You'll need to replace line 20 and instead have it parse line into a String[] array and stick it into str, but the example shows how to read lines from a file. There's not much to it. Set up a BufferedReader in line 7, read a line in line 18. The tough part is parsing that line and sticking it where you want it.

public static String[][] ReadFile (String filename)
    {
        String str[][] = new String[11][11];
        BufferedReader br = null;
        try
        {
            br = new BufferedReader(new FileReader(filename));
        }
        catch (FileNotFoundException ex)
        {
            System.out.println ("File not found");
            System.exit (1);
        }

        String line;
        try
        {
            while ((line = br.readLine()) != null)
            {
                System.out.println (line);
            }
        }
        catch (IOException ex)
        {
            System.out.println ("IO Exception");
            System.exit (1);
        }

        return str;
    }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.