Skip to content

Instantly share code, notes, and snippets.

@hunterbridges
Created January 27, 2013 18:41
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 hunterbridges/4649688 to your computer and use it in GitHub Desktop.
Save hunterbridges/4649688 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct Cell {
int by;
} Cell;
int main(int argc, char *argv[]) {
int row = 5;
int num_cells = row * row;
Cell cells[num_cells];
// Generate the game map
int i = 0;
unsigned int seed = (unsigned int)time(NULL);
srandom(seed);
for (i = 0; i < num_cells; i++) {
int by = random() % 2;
Cell this = {.by = by};
cells[i] = this;
printf("[%d] ", this.by);
if ((i + 1) % 5 == 0) printf("\n");
}
printf("\n");
// Check for defended cells
for (i = 0; i < num_cells; i++) {
Cell this = cells[i];
char match = '\0';
match |= (i - row < 0 || cells[i - row].by == this.by) << 3;
match |= (i % row == row - 1 || cells[i + 1].by == this.by) << 2;
match |= (i + row > num_cells - 1 || cells[i + row].by == this.by) << 1;
match |= (i % row == 0 || cells[i - 1].by == this.by);
char display = match == 15 ? 'D' : ' ';
printf("[%c] ", display);
if ((i + 1) % 5 == 0) printf("\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment