Skip to content

Commit 8f7660f

Browse files
hozlucas28TiagoGiannottiGuidolinares
committed
feature: add countNeighbors function
Co-authored-by: Tiago Giannotti <[email protected]> Co-authored-by: Guidolinares <[email protected]>
1 parent 73b845d commit 8f7660f

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

libs/utilities.c

+28
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,34 @@
88

99
#include "./patterns/main.h"
1010

11+
int countNeighbors(TGame* pGame, int cellRow, int cellCol, int radius) {
12+
int i;
13+
int j;
14+
15+
int startRow = cellRow - radius;
16+
int startCol = cellCol - radius;
17+
18+
int endRow = cellRow + radius + 1;
19+
int endCol = cellCol + radius + 1;
20+
21+
int neighbors = 0;
22+
23+
for (i = startRow; i < endRow; i++) {
24+
if (i > pGame->rows - 1) break;
25+
if (i < 0) continue;
26+
27+
for (j = startCol; j < endCol; j++) {
28+
if (j > pGame->cols - 1) break;
29+
if (j < 0) continue;
30+
31+
if (i == cellRow && j == cellCol) continue;
32+
if (pGame->dashboard[i][j] == ALIVE_CELL) neighbors++;
33+
}
34+
}
35+
36+
return neighbors;
37+
}
38+
1139
void drawPattern(TGame* pGame, char* pattern) {
1240
TPattern SPattern;
1341

libs/utilities.h

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ typedef struct {
2626
int generation; /** Represents the generation number. */
2727
} TGame;
2828

29+
// TODO: Documentation
30+
int countNeighbors(TGame* pGame, int row, int col, int radius);
31+
2932
/**
3033
* @brief Draws a specified pattern on a Conway's Game of Life board.
3134
*

0 commit comments

Comments
 (0)