1

I'm working on a school project and the objective is to take a text file that contains a maze of "#", " ", "E", and "S" characters and find your way through this maze. In the assignment it is asking for me to take the file and put each row and column into a 2D char array.

Here is an example of what the file could look like("S" is where I start and "E" is where I end) :

##########
# S ######
## ####E##
## #### ##
#       ##
##########

I thought about using a couple while loops one for finding the height of the maze and adding it to it's "y" component. Then taking each increment of "x" and putting it into the array for it's char value and reading the second y to find both "#" to find the width. However I so far have not implemented it correctly. Do you have any thoughts on how to go about this problem? So far I haven't been able to find it online. Also just to reiterate I need the char value so "#", " ", or "E" and "S" in the proper x and y coordinate in my 2D char array.

I would also provide my code of what I have but that would be considered cheating and as a student I would like to learn the content too. So just thoughts and maybe some examples would be nice.

  • 2
    Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it. – Roman Pustylnikov Oct 19 '15 at 05:46
  • @RomanPustylnikov Does the summary in the second paragraph count as what I have done because that is what I have tried to do and I haven't achieved what I need to. I just feel like posting my code would be cheating. –  Oct 19 '15 at 05:49
  • "Do you have any thoughts on how to go about this problem?" - Yes; specifically if you implemented something but it does not give you the result you expected it would be much more productive if you posted what you did and also clearly stated where it does not produce the desired effect. On SO, be as specific as possible, not as *vague* as possible – Durandal Oct 19 '15 at 06:08

3 Answers3

0

Personally what I would do is to divide this into a board, and have a board object. On the board object have number of square objects. Think of it as a chess board.

  |  |
__|__|__
  |  |
__|__|__
  |  |
  |  |

Each square object would have a X, Y values to say it's location and then the actual value of the square (3, #, ... etc)

public class board {

    ArrayList a<Square>;
}

public class Square {

    int x;
    int y;
    String value;
}

Then you can read your text file and build your board.

Achintha Gunasekara
  • 1,165
  • 1
  • 15
  • 29
0

You can read the lines into the list of strings, and then once you know the y dimension of array, you can create 2d array and populate it with:

Char[] array = line.toCharArray();

Roman Pustylnikov
  • 1,937
  • 1
  • 10
  • 18
  • I tried using a .toCharArray from this post [link](http://stackoverflow.com/questions/10006165/converting-string-to-character-array-in-java) and it wasn't working. Is this the same idea that you had? –  Oct 19 '15 at 06:14
  • That's about how far I am willing to go without some code to be shown. Please read the FAQ and provide us the MCVE. – Roman Pustylnikov Oct 19 '15 at 06:18
0

Read file contents into a string. Use x,y -> index transformation for your coordinates. Access the characters of this string as if the characters where in 2D array. See a solution to the problem "Using an array of Strings to create a 2D (map) array in Java" .

Community
  • 1
  • 1