summaryrefslogtreecommitdiff
path: root/gameOfLife.c
blob: e0169938c733315d89ffbe60411b0558829c76ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include <unistd.h>
#include <string.h>

#define RANDOM_SEED time(NULL)

typedef enum CellState { DEAD, ALIVE } CellState;

void printUsage()
{
  printf("usage: gameOfLife [-x <SIZE>] [-y <SIZE>] [-d <FLOAT>] [-s <SECONDS>]\n"
         "  -d <FLOAT> sets density of life cells at game start\n"
         "  -s <SECONDS> time to sleep between rounds\n"
         );
}

void initBoard(CellState* board, unsigned int x, unsigned int y, float density)
{
  float roll;
  
  srand(RANDOM_SEED);
  for (int i = 0; i < x * y; i++)
    {
      roll = (float)rand()/(float)RAND_MAX;
      if (roll <= density)
        board[i] = ALIVE;
      else
        board[i] = DEAD;
    }
}

int boardEq(CellState* a, CellState* b, unsigned int x, unsigned int y)
{
  for (int i = 0; i < x * y; i++)
    {
      if (a[i] != b[i])
        return 0;
    }

  return 1;
}

void evolve(CellState* board, CellState* newBoard, unsigned int width, unsigned int height)
{
  int liveNeighbours;
  CellState* focus;
  CellState* newFocus;
  
  for (int y = 0; y < height; y++)
    for (int x = 0; x < width; x++)
      {
        focus = &(board[y * width + x]);
        newFocus = &(newBoard[y * width + x]);
        liveNeighbours = 0;

        for (int n = (y <= 0) ? 0 : -1; n <= ((y + 1) >= height ? 0 : 1); n++)
          for (int m = (x <= 0) ? 0 : -1; m <= ((x + 1) >= width ? 0 : 1); m++)
            {
              // fprintf(stderr, "(%d, %d) + (%d, %d)\n", x, y, m, n); // DEBUG
              if (board[(y + n) * width + (x + m)] == ALIVE && !(n == 0 && m == 0))
                liveNeighbours++;
            }

        if (liveNeighbours == 3)
          *newFocus = ALIVE;
        else if (*focus == ALIVE && liveNeighbours == 2)
          *newFocus = ALIVE;
        else
          *newFocus = DEAD;
      }
}

void printBoard(CellState* board, unsigned int width, unsigned int height)
{
  void drawHoriz() {
    printf("+");
    for (int x = 0; x < width; x++)
      printf("-");
    printf("+\n");
  }

  drawHoriz();
  
  for (int y = 0; y < height; y++)
    {
      printf("|");
      for (int x = 0; x < width; x++)
        if (board[y * width + x] == ALIVE)
          {
            printf("X");
          }
        else
          {
            printf(" ");
          }
      printf("|");
      printf("\n");
    }

  drawHoriz();
}

void main(int argc, char* argv[])
{
  unsigned int x = 16;
  unsigned int y = 16;
  float d = 0;
  float s = 1;

  CellState* board;
  CellState* newBoard;

  int c;
  
  while ((c = getopt(argc, argv, "x:y:d:s:h?")) != -1)
    {
      switch (c)
        {
        case 'x':
          sscanf(optarg, "%d", &x);
          break;
        case 'y':
          sscanf(optarg, "%d", &y);
          break;
        case 'd':
          sscanf(optarg, "%f", &d);
          break;
        case 's':
          sscanf(optarg, "%f", &s);
          break;
        case 'h':
        case '?':
          printUsage();
          exit(EXIT_SUCCESS);
        default:
          printUsage();
          exit(2);
        }
    }

  if (d < 0 || d > 1)
    {
      fprintf(stderr, "Densitiy needs to be in [0;1]\n");
      exit(2);
    }

  if (s <= 0)
    {
      fprintf(stderr, "Sleep interval needs to be greater 0\n");
      exit(2);
    }

  board = malloc(sizeof(CellState) * x * y);
  newBoard = malloc(sizeof(CellState) * x * y);

  memset(board, DEAD, sizeof(CellState) * x * y);
  initBoard(newBoard, x, y, d);

  while (! boardEq(board, newBoard, x, y))
    {
      printBoard(newBoard, x, y);

      usleep((int)(s * 1e6));

      memcpy(board, newBoard, sizeof(CellState) * x * y);
      evolve(board, newBoard, x, y);
    }
  
  free(board);
  exit(EXIT_SUCCESS);
}