initial commit

AuthorKonata <konata@posteo.jp>
Date
Commit4eaccd4dc25a9c60dbfe4373f3a7317a41d2f0e8
3 files changed, 95 insertions(+)
▾AMakefile
@@ -0,0 +1,3 @@
all:
gcc -Og -g simple-go.c \
-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 @@
#include "simple-go.h"
go_board* create_board(size_t size)
{
go_board* board = malloc(sizeof(*board));
board->field_array = malloc(size*size*sizeof(*board->field_array));
for(size_t i = 0; i < size; i++)
{
board->field_array[i] = EMPTY;
}
board->size = size;
return board;
}
void print_board(go_board* board)
{
for(size_t y = 0; y < board->size; y++)
{
for(size_t x = 0; x < board->size; x++)
{
putchar(get_board_at(board,y,x));
}
putchar('\n');
}
}
char get_board_at(go_board* board, size_t y, size_t x)
{
if(y >= 0 && x >= 0 && y < board->size && x < board->size)
return board->field_array[y*board->size+x];
else
return INVALID_FIELD;
}
void set_board_at(go_board* board, size_t y, size_t x, char item)
{
if(y >= 0 && x >= 0 && y < board->size && x < board->size)
board->field_array[y*board->size+x] = item;
}
void find_group(go_board* board, go_board* overlay, size_t y, size_t x)
{
set_board_at(overlay, y-1, x, GROUP);
char field = get_board_at(board,y,x);
if(get_board_at(board,y-1,x) == field && get_board_at(overlay,y-1,x) == EMPTY)
find_group(board, overlay, y-1, x);
if(get_board_at(board,y,x-1) == field && get_board_at(overlay,y,x-1) == EMPTY)
find_group(board, overlay, y, x-1);
if(get_board_at(board,y+1,x) == field && get_board_at(overlay,y+1,x) == EMPTY)
find_group(board, overlay, y+1, x);
if(get_board_at(board,y,x+1) == field && get_board_at(overlay,y,x+1) == EMPTY)
find_group(board, overlay, y, x+1);
}
▾Asimple-go.h
@@ -0,0 +1,32 @@
#ifndef SIMPLE_GO_H
#define SIMPLE_GO_H
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#define BLACK 'b'
#define WHITE 'w'
#define EMPTY ' '
#define GROUP '#'
#define INVALID_FIELD '\0'
typedef struct go_board
{
char* field_array;
size_t size;
} go_board;
typedef struct game_state
{
struct go_board* board;
bool black_turn;
} game_state;
go_board* create_board(size_t size);
char get_board_at(go_board* board, size_t y, size_t x);
void set_board_at(go_board* board, size_t y, size_t x, char item);
#endif //SIMPLE_GO_H