Created
September 28, 2012 02:52
-
-
Save emonti/3797685 to your computer and use it in GitHub Desktop.
1d grid example for malic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main() | |
{ | |
// Notice, there are no brackets around the rows this time. | |
// This is a 1-dimensional array. Even though it looks 2d in | |
// the code, it's one long list to the computer. | |
// | |
// Using a 1-dimensional array, we can still treat the data | |
// inside of it as a grid in our code, though. | |
int flat_grid[] = { | |
0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 14 rows | |
0,0,0,0,1,1,1,1,1,1,1,1,1,0, | |
0,0,0,0,1,0,0,1,1,1,0,0,1,0, | |
0,0,0,0,1,0,1,1,1,1,0,1,1,0, | |
0,0,0,0,1,1,1,1,1,1,1,1,1,0, | |
0,0,0,0,1,1,1,1,0,1,1,1,1,0, | |
0,0,0,0,1,1,1,1,1,0,1,1,1,0, | |
0,0,0,0,1,1,1,1,0,0,1,1,1,0, | |
0,0,0,0,1,1,1,1,1,1,1,1,1,0, | |
0,0,0,0,1,1,1,1,0,0,1,1,1,0, | |
0,0,0,0,1,1,1,1,0,0,1,1,1,0, | |
0,0,0,0,0,1,1,1,1,1,1,1,0,0, | |
0,0,0,0,0,0,0,0,1,0,0,0,0,0, | |
0,0,0,1,1,0,0,0,1,0,0,0,1,1, | |
0,0,0,0,1,1,1,1,1,1,1,1,1,0, | |
0,0,0,0,0,0,0,1,1,1,0,0,0,0, | |
0,0,0,0,0,0,0,0,1,0,0,0,0,0, | |
// to 17 columns | |
}; | |
// lets set up some variables we'll need... | |
int columns, x, // no. of "columns", and our index into them is "x" | |
rows, y, // no. of "rows", and our index into them is "y" | |
position, // a variable to hold our "position" in the grid | |
value; // a variable to hold the "value" at the "position" | |
rows = 14; | |
columns = 17; | |
for(x=0; x < columns; x++) { | |
for(y=0; y < rows; y++) { | |
// We don't have a convenient 2-d array this time, but thats ok! | |
// We just need to calculate the position with a little "math"... | |
position = (x * rows) + y; | |
//... and get the value at that position! | |
value = flat_grid[position]; | |
// the rest looks pretty much the same as before... | |
if (value == 1) { | |
putc('@', stdout); | |
} else { | |
putc(' ', stdout); | |
} | |
} | |
putc('\n', stdout); // end of line | |
} | |
} | |
/* | |
here's what it looks like when i run it: | |
$ ./1dgrid | |
@@@@@@@@@ | |
@ @@@ @ | |
@ @@@@ @@ | |
@@@@@@@@@ | |
@@@@ @@@@ | |
@@@@@ @@@ | |
@@@@ @@@ | |
@@@@@@@@@ | |
@@@@ @@@ | |
@@@@ @@@ | |
@@@@@@@ | |
@ | |
@@ @ @@ | |
@@@@@@@@@ | |
@@@ | |
@ | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
boo, it looks better on a black+white background :)