0

I am working on a flood-fill recursion assignment where I have to read an ASCII art text file and fill it in. The assignment can be found here: https://faculty.utrgv.edu/robert.schweller/CS2380/homework/hw10.pdf

Recursion() //construcor
{
    column = -1;
    row = -1;
    grid = new char*[size of art row];
    for(int i = 0; i < size of art; i++)
    {
        board[row] = new char[size of art column]
    }
}

I'm not sure if determining the size of the array should be in the constructor or not. I need to know the size of the array in order to know where the user wants to fill the art file in. Also, here is all of the code for a better context. https://pastebin.com/TSYH26Ci

Josh Garza
  • 63
  • 1
  • 7
  • I'd suggest you start by reading documentation related to reading files: http://en.cppreference.com/w/cpp/io/basic_ifstream/basic_ifstream (there's an example at the bottom). Edit this question when you have a specific issue. – Stephen Newell Mar 26 '18 at 23:41
  • The size of the art is the number of lines by the largest number of characters in a row. You can know those numbers until you read the file. I would suggest reading the file into a `vector` of `string`. The `size` of the `vector` is the row count. The max `length` of the `string`s is the column count. You can then convert the `string`s into an array of `char` if that's how you want to proceed. – Matt Mar 27 '18 at 01:03

1 Answers1

1

I would handle your file as binary. I do not know which OS,API you are using so I will answer just in generic way...

  1. get file size siz

    usually seeking to 0 bytes from end of file will give you the file size.

  2. allocate 1D array dat to store your entire file

    dat = new BYTE[siz];
    
  3. load your file into memory (1D array)

    do not forget to use binary access as some ASCII arts could use control codes (ASCII below 32) which could be corrupted by text file access.

  4. scan for end of line

    so scan your array from 0 and stop when you find ASCII codes 13 or 10. Its position will give you the x resolution of your ASCII art

    int xs;
    for (xs=0;xs<siz;xs++)
     if ((dat[xs]==10)||(dat[xs]==13))
      break;
    

    now xs should be holding your x resolution.

  5. compute y resolution ys

    the safest way is to count the number of end of lines (13 or 10). In such case you can even store the line start addresses to some pointer array BYTE **pixel=new (BYTE*)[ys]; which will enable you simple 2D access pixel[y][x]. If your ASCII art is aligned and have constant size per each line than you can compute ys from size..

    ys = siz/(xs+eol_size)
    

    where eol_size is 1 or 2 depending on the used line ending: ((10),(13),(13,10) or (10,13)) so:

    eol_size=1;
    if (xs<siz)
     if ((dat[xs+1]==10)||(dat[xs+1]==13))
      eol_size=2;
    

As we do not have access to any input file we can only guess... If you need to generate one see:

Here example of binary file access in VCL (bullets #1,#2,#3):

Spektre
  • 49,595
  • 11
  • 110
  • 380