Created
January 27, 2013 18:41
-
-
Save hunterbridges/4649688 to your computer and use it in GitHub Desktop.
This file contains hidden or 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> | |
#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