initial commit

AuthorKonata <konata@posteo.jp>
Date
Commit4eaccd4dc25a9c60dbfe4373f3a7317a41d2f0e8
3 files changed, 95 insertions(+)
AMakefile
@@ -0,0 +1,3 @@
1+all:
2+ gcc -Og -g simple-go.c \
3+ -Wall -Werror -Wpedantic -Wnull-dereference -Wshadow -Wconversion -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wstrict-overflow=5 -Wunreachable-code -Wno-unused-parameter
Asimple-go.c
@@ -0,0 +1,60 @@
1+#include "simple-go.h"
2+
3+go_board* create_board(size_t size)
4+{
5+ go_board* board = malloc(sizeof(*board));
6+ board->field_array = malloc(size*size*sizeof(*board->field_array));
7+ for(size_t i = 0; i < size; i++)
8+ {
9+ board->field_array[i] = EMPTY;
10+ }
11+ board->size = size;
12+
13+ return board;
14+}
15+
16+void print_board(go_board* board)
17+{
18+ for(size_t y = 0; y < board->size; y++)
19+ {
20+ for(size_t x = 0; x < board->size; x++)
21+ {
22+ putchar(get_board_at(board,y,x));
23+ }
24+ putchar('\n');
25+ }
26+}
27+
28+char get_board_at(go_board* board, size_t y, size_t x)
29+{
30+ if(y >= 0 && x >= 0 && y < board->size && x < board->size)
31+ return board->field_array[y*board->size+x];
32+ else
33+ return INVALID_FIELD;
34+}
35+
36+void set_board_at(go_board* board, size_t y, size_t x, char item)
37+{
38+ if(y >= 0 && x >= 0 && y < board->size && x < board->size)
39+ board->field_array[y*board->size+x] = item;
40+}
41+
42+void find_group(go_board* board, go_board* overlay, size_t y, size_t x)
43+{
44+ set_board_at(overlay, y-1, x, GROUP);
45+ char field = get_board_at(board,y,x);
46+
47+ if(get_board_at(board,y-1,x) == field && get_board_at(overlay,y-1,x) == EMPTY)
48+ find_group(board, overlay, y-1, x);
49+
50+ if(get_board_at(board,y,x-1) == field && get_board_at(overlay,y,x-1) == EMPTY)
51+ find_group(board, overlay, y, x-1);
52+
53+ if(get_board_at(board,y+1,x) == field && get_board_at(overlay,y+1,x) == EMPTY)
54+ find_group(board, overlay, y+1, x);
55+
56+ if(get_board_at(board,y,x+1) == field && get_board_at(overlay,y,x+1) == EMPTY)
57+ find_group(board, overlay, y, x+1);
58+}
59+
60+
Asimple-go.h
@@ -0,0 +1,32 @@
1+#ifndef SIMPLE_GO_H
2+#define SIMPLE_GO_H
3+#include <stdlib.h>
4+#include <stdint.h>
5+#include <stdbool.h>
6+#include <stdio.h>
7+
8+#define BLACK 'b'
9+#define WHITE 'w'
10+#define EMPTY ' '
11+#define GROUP '#'
12+#define INVALID_FIELD '\0'
13+
14+typedef struct go_board
15+{
16+ char* field_array;
17+ size_t size;
18+} go_board;
19+
20+typedef struct game_state
21+{
22+ struct go_board* board;
23+ bool black_turn;
24+} game_state;
25+
26+
27+go_board* create_board(size_t size);
28+char get_board_at(go_board* board, size_t y, size_t x);
29+void set_board_at(go_board* board, size_t y, size_t x, char item);
30+
31+
32+#endif //SIMPLE_GO_H