How would I make a grid array using a *.txt file?

edited October 2017 in Questions about Code

So if I did code like this. I would want to use a .txt file instead of declaring worldGrid.

The txt file would be

1,1,1,1,1,1,1,1,1,1

1,0,0,0,0,0,0,0,0,0 ect.

How would I need to lay things out to do this?

int GRID_TILE_WIDTH=48;
int [][] worldGrid={
  {1,1,1,1,1,1,1,1,1,1},
  {1,0,0,0,0,0,0,0,0,1},
  {1,0,1,1,1,1,0,1,0,1},
  {1,0,1,0,1,0,0,1,0,1},
  {1,0,0,0,1,0,0,1,0,1},
  {1,1,1,0,1,0,1,1,1,1},
  {1,0,0,0,0,0,0,0,0,1},
  {1,0,1,1,0,1,0,1,0,1},
  {1,0,0,1,0,1,0,1,0,1},
  {1,1,1,1,1,1,1,1,1,1}
};
int pCol=8,pRow=1;

void setup()
{
    size(GRID_TILE_WIDTH*10,GRID_TILE_WIDTH*10);
}

void draw()
{
   worldDrawGrid();
}
void worldDrawGrid()
{
  for(int row=0;row<worldGrid.length; row++)
  {
    for(int col=0;col<worldGrid[row].length; col++)
    {
      if(worldGrid[row][col]==1)
      {
       fill(0);
       rect(col*GRID_TILE_WIDTH,row*GRID_TILE_WIDTH,GRID_TILE_WIDTH,GRID_TILE_WIDTH);
      }else
      {
       fill(255);
       rect(col*GRID_TILE_WIDTH,row*GRID_TILE_WIDTH,GRID_TILE_WIDTH,GRID_TILE_WIDTH);
      }
    }
  }
}
Tagged:

Answers

  • loadStrings, split.

    or Table.

    all in the reference.

  • koogs, our answers tend to get shorter with the time...

    ;-)

  • sometimes people just need to know what methods exist and the reference examples are enough to get them rolling. if not they'll ask follow up questions. this one, for instance, has done all the hard work of the display code, just wants the data from a file rather than an array.

    and the Common Questions links are there to be used - i am fed up of answering the same 'how can i display something for n seconds' / 'click on a button' / 'ball doesn't bounce' type questions four times a week...

  • I fully agree.

  • I understand that, and I'm sorry the problem I run into is that I use worldgrid to load the text file is that it says it can't load a int [][] as a String []. I also don't to have it read as the row and col.

  • given your file is actually a csv (comma separated values) file perhaps Table is a better bet? you can tell it that each column is an int and it'll read it as an int...

    or, my preference, is to use a png where each block is a single pixel...

    your grid array doesn't need to be an int[][], a 2d array of anything will do as long as you can differentiate between the blocks and non blocks.

  • edited October 2017

    you wrote:

    the problem I run into is that I use worldgrid to load the text file is that it says it can't load a int [][] as a String [].

    can you show your code?

    This is usually done by for looping over the lines and for each line use split() and for loop over the result of that (each comma separated value inside the line) to fill int [][] from String [].

  • int i = (int)"1"; // returns an int from a string
    
  • edited November 2017

    Here is a simple example of a function, tableToArray, which loads a csv into a Table of ints and copies the Table into an array.

    The sketch also writes a 2D printout of the (identical) contents of the Table and the array.

    This can be made much more concise -- but it demonstrates the concepts.

    /**
     * TableToArray
     * Takes a Table of ints and loads them into an array. Prints the results.
     * 2017-10-29 - Processing 3.3.6
     * forum.processing.org/two/discussion/24790/how-would-i-make-a-grid-array-using-a-txt-file#latest
     **/
    Table table;
    int[][] array;
    
    void setup() {  
      table = loadTable("table.csv", "csv");
      array = tableToIntArray(table);
    
      println();
      printTable(table);
    
      println();
      printArray2D(array);
    }
    
    int[][] tableToIntArray(Table table){
      int[][] results;
      int maxRows = table.getRowCount();
      int maxCols = 0;
      for (TableRow row : table.rows()) {
        maxCols = maxCols > row.getColumnCount() ? maxCols : row.getColumnCount();
      }
      results = new int[maxRows][maxCols];
    
      TableRow row;
      for (int r = 0; r < table.getRowCount(); r++) {
        row = table.getRow(r);
        for(int c=0; c < row.getColumnCount(); c++){
          results[r][c] = row.getInt(c);
        }
      }
      return results;
    }
    
    void printTable (Table table){
      TableRow row;
      for (int r = 0; r < table.getRowCount(); r++) {
        row = table.getRow(r);
        for(int c=0; c < row.getColumnCount(); c++){
          print(row.getInt(c));
        }
        println();
      }
    }
    
    void printArray2D (int[][] array){
      for(int[] r : array){
        for(int c : r){
          print(c);
        }
        println();
      }
    }
    

    Output (twice):

    1000000001
    1011110101
    1010100101
    1000100101
    1110101111
    1000000001
    1011010101
    1001010101
    1111111111
    
Sign In or Register to comment.