• Post Reply Bookmark Topic Watch Topic
  • New Topic

solve a maze using recursive methods  RSS feed

 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have an assignment to create and solve a maze using recursive methods.

I was given this grid class
but i wrote some of the code

[

here is a sample text file

9 9
1 3 4 5 6 7 8 9
1 7 9
1 3 4 5 6 7 9
1 3 5 9
1 3 5 6 7 9
1 5 9
1 2 3 5 7 9
1 7 9
1 2 3 4 5 6 7 9
5 4`

the grid class is supposed to read the file and create a grid for the maze the first line of the file is the number of rows and columns in the grid
each subsequent line represents a row and each number represents a column that is blocked. In the constructor i have a for loop for the rows and a while loop for the columns but the indexs of grid[][] are rows and columns i cannnot figure out how to insert each row and column into the grid array to create the grid. also how do i declare the empty space if they don't exist in the text file?
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
What does 5 4` mean in the last line?
What is the algorithm you are using? Write it down, and then refine it so it is all in words of one syllable which even I can understand. It will read something like this:-
When I get to a place where the path forks, try the left branch first and see if I can get through all the way to the far end. If I can't get through, come back to the last fork where I went left and then try to go right and see if it works that way.
Then you have to work out how you go through the grid and whether you have a blocked or clear path ahead of you. Do those numbers simply mean you can't go left to right, or do they block you from going vertically too?
Please always post correctly‑indented code, because badly indented code can hide errors from you. I shall try to get your code indented better, but can't be sure I shall get it right.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what your code would have looked like had you indented it:-I omitted line 61 because it appears to have an unmatched } presumably supposed to match the class name that you omitted.
All sorts of little things which make your code easier to misinterpret but probably don't have a serious functional effect.
Don't mix indentation types as you are doing in lines 35 to 43. Be consistent about blank lines: look at line 41, which ought not to be there. You do need to be obsessive about indentation and names and that sort of thing because tiny indentation errors can cause you to misread your own code. Another error is omitting {} in lines 23‑24. Only use \n if somebody has told you to use the LF character. Use System.lineSeparator() instead. Be consistent about spacing; in line 23 it shou‍ld read column = 0. You will get better execution speed if you use a StringBuilder and its append method instead of the + operator on Strings. You don't need + ""

Another little thing is the while loop in lines 9‑12. Go through that very carefully with a pencil and paper and work out what that actually does. I think it does something completely different from what you think it does.
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
5,4 is the starting point of the maze
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the grid is supposed to read file and input each line as a row in the array and each int as a column that is blocked until the final line i have been struggling with this  for a while and i can't figure it out. this is one class out of four total for the code
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and what about the loop I pointed out earlier? Lines 9‑12. What does that do?
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is supposed to read the file like i explained earlier but when i run the program it does not do that it just reads the second line (because i have the first line defined already) and prints the rest as null
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right after you declare your grid array, initialize each element with SquareState.OPEN.

Then for each of the 9 next lines, open a scanner for that line, and for each nextInt set the relevant SquareState[x][y] to SquareState.BLOCKED.
Finally, read the last line, and mark the accompanying SquareState[x][y] with SquareState.SELECTED.

Now you have a correct grid array, and you can start looking for the exit with a recursive solution,
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i set all the elements of the array to open it prints out the word open because that is a squarestate object but i actually need the symbol ("") or space. Same with blocked i would need the symbol
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is simply a matter of changing your toString method. In stead of

use
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or alternatively, add a toString method to your enum.
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:
Then for each of the 9 next lines, open a scanner for that line, and for each nextInt set the relevant SquareState[x][y] to SquareState.BLOCKED.
Finally, read the last line, and mark the accompanying SquareState[x][y] with SquareState.SELECTED.


when i try to to access each line it only changes the second line and sets the rest to null. i am trying to use a for loop to do this

 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is my current code for the loop

 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in java 8, you can use String[] lines = Files.readAllLines(file);
Then read lines[0] to get the size of the maze,
then:

Something like this.
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:in java 8, you can use String[] lines = Files.readAllLines(file);
.

is there any other way to do this because i don't think im supposed to use an import that is not given
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, plenty.

For instance, open a Scanner to read the file irtself, and then use:

while (sanner.hasNextLine()... String line = scanner.readline(); and then open a scanner for this line, et cetera.

Just out od f curiousity: what imports are allowed?
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
these are the imports allowed
import java.util.Arrays;
import java.util.Scanner;
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see. Well, my last reply just fitts the bill.
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is no readline method for scanner
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, alip of the pen. nextLine
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry for all the questions but i am new at java


Piet Souris wrote:Of course, plenty.

For instance, open a Scanner to read the file irtself, and then use:

while (sanner.hasNextLine()... String line = scanner.readline(); and then open a scanner for this line, et cetera.



how exactly would you OPEN a scanner for the currentline
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If s is a string holding the current line, then you do

It makes me wonder, though, what has been taught so far in your course, assuming you are doing some course? What have you learned to read an input file like the one you are struggling with?
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is my current code to read the file

when it runs it only reads the first line and leaves the rest as opened can someone tell me what i am doing wrong?
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a careful look at your first while loop. It reads all of the data in your file, without changing i. What if you left that loop out all together? Do it and show us the result.
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps I was a little ubclear... I meant something liee this:

Compare that to the code I hgave earlier. In the loop I read a line, open a Scanner for that line and read in all the vlocked cells. That is all there is to it.
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is my current code for the grid



here is the output

 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also it prints the grid twice.
im sorry for the late reply but technically the assignment was due on wednesday november 15 but since half the class did not finish he said to submit it when we finished. this thread here is only about one class in a program consisting of four classes. i have only worked on two classes and basically left the other empty because i had to finsih the two i was working on in order to do the other two
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that looks allright, albeit that in line 10 we read another line from the input, whcih is double, since we do that in lie 6 s well. So, drop line 10, and instead close the Scanner  'scan'.

To be sure, I just wrote a quick & dirty example of reading a maze file, but actually it is the same code as you have. I just do not have a dedicated Maze-class.

Out of curiousity: did all your co students had trouble with reading the data file? If so (I asked i before): what were you youngsters being taught so far to read files like these?

Here is my q&d code:
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think most people where having trouble with the recursion portion of the project which i haven't even started yet.
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was though next to nothing about reading files into a program. every time i ever used a scanner it was to read input from a user never to read a file
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oke. Well, success with the resursion part (recursion is always fun)!
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you it finally works but it prints the grid twice
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!

Can you show me the relevant code? So far I only see the part where you read in the data.
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the entire class

 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure? When I change the symbol of a space to '-' (to make it better visible) and I run your program with the same file as in my previous example code (the 4 x 6 maze) I get this output:

###---
-#+-##
#-##-#
--###-

and that is what you should get.
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i get the correct 9x9 grid but it prints entire grid twice
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i run my code this is the output


 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i can send you the client/test class if you want (but it was provided by my professor so i can't change it]
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With your given 9 x 9 data, I ran your code again and it gives me this:

#-#######
#-----#-#
#-#####-#
#-#-#---#
#-#+###-#
#---#---#
###-#-#-#
#-----#-#
#######-#

Only one maze!

I used this main method (since you had not provided one):


So it must be your professors'code printing it twice!
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well thank you anyways i might post the maze class if i run into any difficulty
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!