Skip to content

Instantly share code, notes, and snippets.

@emonti
Created September 28, 2012 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emonti/3797663 to your computer and use it in GitHub Desktop.
Save emonti/3797663 to your computer and use it in GitHub Desktop.
2d grid example for malic
#include <stdio.h>
#define ROWS 10
#define COLUMNS 7
// This is a 2-dimensional array.
// It makes accessing the values of a
// bitmap easy by using x/y references.
int grid[ROWS][COLUMNS] = {
{0,0,0,0,0,0,0}, // 7 columns across
{0,1,1,0,1,1,0},
{0,1,1,0,1,1,0},
{0,0,0,0,0,0,0},
{0,0,0,1,0,0,0},
{0,0,0,1,0,0,0},
{0,0,0,0,0,0,0},
{0,1,1,1,1,1,0},
{0,0,1,1,1,0,0},
{0,0,0,0,0,0,0},
// 10 columns down
};
int main() {
int x,y;
for(x=0; x < ROWS; x++) {
for(y=0; y < COLUMNS; y++) {
// to access a value in the bitmap we just reference it with our
// x/y coordinate.
int value = grid[x][y];
if (value == 1) {
putc('*', stdout); // output a single *
} else {
putc(' ', stdout); // or an empty space
}
}
putc('\n', stdout); // print end of line (and begin at next one)
}
}
/*
here's what the output looks like
$ ./2dgrid
** **
** **
*
*
*****
***
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment