Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Given an ascii text file (best viewed in notepad) with characters:
HTML
+-+-+
| |
+-+-+
| | |
+-+-+

Where each
HTML
+-+
| |
+-+

is a cell and the '|' and '-' are walls.

I would like read each character and, depending on whether it is a '-', '|' or 'space' character (i.e., horizontal, vertical or missing wall), update a 2d char array accordingly. (In the case of a 'space', I must also differentiate between the space in the cell and the space which indicates a missing wall...)

So for the above example, the program would create the following array:
HTML
Cell no.  Walls(NSWE)
----------------------
0              1111
1              1110
2              1111
3              1111

I have gotten this far... (comment continued after program)
C++
#include <fstream.h>

int main()
{
char mazeFile[80];
char MAP[4][4];

cout << "Maze file: ";
cin >> mazeFile;

ifstream fin(mazeFile); // open for reading

char ch;
while (fin.get(ch)){

// distinguish between characters and update array

}

fin.close();

return 0;
}

...Is there a way of getting, say, the nth character and then character n+2, pointing to characters in the file?

If no such method is available what would be the least clumsy way of achieving my goal.

Thanks in advance.
Posted
Updated 20-Oct-12 3:30am
v2
Comments
Richard MacCutchan 20-Oct-12 10:03am    
Read the entire line into a character array and you can then test all the characters in place.
E.B.E 20-Oct-12 10:11am    
Thanks for the suggestion.
That would seem sensible for the .txt file and 4x4 array of the above example.
In reality, I need to apply the principle to a much larger file and array (1122 chars and 256x4 array). Is this still a sensible solution?
Basically, an entire line would be 16*2+1=33 (excluding EOL)
Richard MacCutchan 20-Oct-12 10:51am    
Well it should still work for any length string and number of lines. But I wonder if there is a simpler way to specify the sizes of the arrays, or does this data come from somewhere else?
E.B.E 20-Oct-12 11:23am    
Here is a little background:

I am simulating an algorithm that solves "micromouse" mazes (http://en.wikipedia.org/wiki/Micromouse).

I have finally completed the algorithm and it seems to work well for the one 16x16=256 maze I manually represented in a 256x4 array (actually 256x8, but 4 bits for NSWE) as part of the program.

However, I would like to test many other mazes and entering each maze manually into an array is not feasible. The mazes I would like to test are already publicly available in the above format (or similar).

The text mazes files I need to enter and analyze are 16 cells by 16 cells.

Example: cells 240 to 255 (the first row) would be represented as follows in the text file:

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| | | | | | | | | |
+ +-+ +-+ +-+-+ +-+-+-+-+-+-+-+

(some walls intentionally left out, best viewed in notepad...)

This works out to be 2*16+1=33 chars per line...

Your suggestion of processing a line at a time seems feasible. I would then use

char mazeLine[33];

However, I am a bit concerned that I would end up with some clumsy code.
...Just trying to work out if there is a 'tidy way' before embarking on this part. I am not very familiar with fstream.

If the algorithm works for other mazes, then I need to figure out a way of converting to ASM. Another story for another day.
Richard MacCutchan 20-Oct-12 11:59am    
This has not much to do with fstream; that is just a helper function to read files. The issue is more that you need to process each string of characters to decide which walls exist around each element of the array. So you read the first line and then for each - character you set the relevant cell's N flag. Read the next line and (a bit more complex) you need to check the vertical bars and spaces to decide which cells should have the East and West walls. Read the next line and check for South walls of the previous array and North walls of the next. It is probably easier to visualise if you draw a sample set of text lines and work out how to use the content of each character to set the relevant flags.

1 solution

C++
+-+-+
| |
+-+-+
| | |
+-+-+

Reading each of these lines in turn gives you a string, which can also be addressed as an array of characters. After reading the first line the character at buffer[0] is the top left corner, the character at buffer[1] is the North wall of the first square, and so on. Reading line two the character at buffer[0] is the West wall of the first square, the one at buffer[2] is the East, and also the West wall of the second square. Reading line 3 you are processing the South walls of the first block and the North walls of the next, and so on.

You don't need anything more than getline() to get and process each line of text.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900