diff options
Diffstat (limited to 'voerbak')
-rw-r--r-- | voerbak/argparse.c | 54 | ||||
-rw-r--r-- | voerbak/argparse.h | 9 | ||||
-rw-r--r-- | voerbak/board.c | 41 | ||||
-rw-r--r-- | voerbak/board.h | 47 | ||||
-rw-r--r-- | voerbak/messages.c | 8 | ||||
-rw-r--r-- | voerbak/messages.h | 12 | ||||
-rw-r--r-- | voerbak/voerbak.c | 56 | ||||
-rw-r--r-- | voerbak/voerbak.h | 36 | ||||
-rw-r--r-- | voerbak/win.c | 1 | ||||
-rw-r--r-- | voerbak/win.h | 1 |
10 files changed, 191 insertions, 74 deletions
diff --git a/voerbak/argparse.c b/voerbak/argparse.c new file mode 100644 index 0000000..c6dde5a --- /dev/null +++ b/voerbak/argparse.c @@ -0,0 +1,54 @@ +#include <argp.h> +#include <stdlib.h> + +#include "argparse.h" + +const char *argp_program_version = "2.1.3"; +const char *argp_program_bug_address = "https://github.com/lonkaars/po-4-op-een-rij/"; +static char doc[] = "Connect 4 game solver"; +static char args_doc[] = "arguments"; +static struct argp_option options[] = { + { "width", 'w', "WIDTH", 0, "Field width (columns)"}, + { "height", 'h', "HEIGHT", 0, "Field height (rows)"}, + { "solver", 'c', "NAME", 0, "Solver used for computing moves (unset is two humans playing)"}, + { "verbosity", 'v', "LEVEL", 0, "Verbosity, 0 = none (default), 1 = info, 2 = debug"}, + { 0 } +}; + +static error_t parse_opt (int key, char *arg, struct argp_state *state) { + struct arguments *arguments = state->input; + switch (key) { + case 'w': { + arguments->width = atoi(arg); + break; + } + case 'h': { + arguments->height = atoi(arg); + break; + } + case 'c': { + arguments->solver = arg; + break; + } + case 'v': { + arguments->verbosity = atoi(arg); + break; + } + } + return 0; +} + +static struct argp argp = { options, parse_opt, args_doc, doc }; + +struct arguments argparse(int argc, char* argv[]) { + struct arguments arguments; + + arguments.height = 6; + arguments.width = 7; + arguments.solver = ""; + arguments.verbosity = 0; + + argp_parse(&argp, argc, argv, 0, 0, &arguments); + + return arguments; +} diff --git a/voerbak/argparse.h b/voerbak/argparse.h new file mode 100644 index 0000000..243e81f --- /dev/null +++ b/voerbak/argparse.h @@ -0,0 +1,9 @@ +/** @brief Used for storing arguments */ +struct arguments { + int width, height; + char* solver; + int verbosity; +}; + +/** @brief Parse arguments */ +struct arguments argparse(int argc, char* argv[]); diff --git a/voerbak/board.c b/voerbak/board.c new file mode 100644 index 0000000..8008db4 --- /dev/null +++ b/voerbak/board.c @@ -0,0 +1,41 @@ +#include <memory.h> + +#include "board.h" +#include "win.h" + +Board* createBoard(int width, int height) { + Board *gameBoard = malloc(sizeof(Board)); + gameBoard->board = malloc(sizeof(int) * (width * height - 1)); + gameBoard->width = width; + gameBoard->height = height; + gameBoard->length = width * height; + return gameBoard; +} + +void printBoard(Board *b) { + for (int i = 0; i < b->length; i++) + printf("%d", b->board[i]); + printf("\n"); + fflush(stdout); +} + +bool boardFull(Board *b) { + for (int i = 0; i < b->length; i++) + if (b->board[i] == 0) return false; + return true; +} + +bool dropFisje(Board *b, int column, int disc) { + for (int row = 0; row < b->height; row++) { + int pos = column + row * b->width; + if (b->board[pos] == 0) { + b->board[pos] = disc; + bool won = checkWin(b, pos); + return true; // success + } + } + printf("e:full\n"); + fflush(stdout); + return false; // unsuccessful drop on board full +} + diff --git a/voerbak/board.h b/voerbak/board.h new file mode 100644 index 0000000..9b4c2f6 --- /dev/null +++ b/voerbak/board.h @@ -0,0 +1,47 @@ +#pragma once +#include <stdbool.h> + +/** + * @brief Structure to store a board + * + * @param width Board width + * @param height Board height + * @param length Board state array length (width * height) + * @param board Board state array + */ +typedef struct { + int width; + int height; + int length; + int* board; +} Board; + +/** + * @brief Create new board + * + * @param width Board width + * @param height Board height + * + * @return Empty board + */ +Board* createBoard(int, int); + +/** + * @brief Print the board array + */ +void printBoard(Board*); + +/** + * @brief Check if the board is full (draw) + * + * @return `true` board is full + */ +bool boardFull(Board*); + +/** + * @brief Drop a disc into the board + * + * @return `true` if drop was successful, `false` if column full + */ +bool dropFisje(Board*, int, int); + diff --git a/voerbak/messages.c b/voerbak/messages.c new file mode 100644 index 0000000..abb387a --- /dev/null +++ b/voerbak/messages.c @@ -0,0 +1,8 @@ +#include <stdio.h> +#include <stdbool.h> + +#include "messages.h" + +void parseMessage(char* message, int verbosity) { + verbosity > 2 && printf("Got message: \"%s\"\n", message); +} diff --git a/voerbak/messages.h b/voerbak/messages.h new file mode 100644 index 0000000..dd7b325 --- /dev/null +++ b/voerbak/messages.h @@ -0,0 +1,12 @@ +/** + * @brief Parse special message + * + * 0: embedded + * 1: print help messages (human mode) + * 2: debug mode + * + * @param message + * @param verbosity + */ +void parseMessage(char*, int); + diff --git a/voerbak/voerbak.c b/voerbak/voerbak.c index 1ac74b6..d1982fa 100644 --- a/voerbak/voerbak.c +++ b/voerbak/voerbak.c @@ -1,51 +1,31 @@ -#include <stdio.h> -#include <stdlib.h> -#include <memory.h> #include <stdbool.h> +#include <memory.h> #include "voerbak.h" #include "win.h" +#include "board.h" +#include "messages.h" +#include "argparse.h" -void printBoard(Board *b) { - for (int i = 0; i < b->length; i++) - printf("%d", b->board[i]); - printf("\n"); - fflush(stdout); -} +#define EMPTY "" -bool boardFull(Board *b) { - for (int i = 0; i < b->length; i++) - if (b->board[i] == 0) return false; - return true; -} +int main(int argc, char* argv[]) { + struct arguments arguments = argparse(argc, argv); -bool dropFisje(Board *b, int column, int disc) { - for (int row = 0; row < b->height; row++) { - int pos = column + row * b->width; - if (b->board[pos] == 0) { - b->board[pos] = disc; - bool won = checkWin(b, pos); - return true; // success - } - } - printf("e:full\n"); - fflush(stdout); - return false; // unsuccessful drop on board full -} - -int main() { - int width, height; - scanf("%d %d", &width, &height); - - Board *gameBoard = malloc(sizeof(Board)); - gameBoard->board = malloc(sizeof(int) * (width * height - 1)); - gameBoard->width = width; - gameBoard->height = height; - gameBoard->length = width * height; + Board *gameBoard = createBoard(arguments.width, arguments.height); bool player_1 = true; int move = 0; - while (scanf("%d", &move) == 1) { + char* message = malloc(1); // this is weird and i don't understand it but it prevents a segmentation fault or something + strcpy(message, EMPTY); + while (scanf("%d", &move) == 1 || scanf("%s", message) == 1) { + if (strlen(message) != 0) { + parseMessage(message, arguments.verbosity); + + strcpy(message, EMPTY); // clear message + continue; + } + if (move == 0) break; if (move < 1 || move > gameBoard->width) continue; diff --git a/voerbak/voerbak.h b/voerbak/voerbak.h index 0ef6cf9..d44f382 100644 --- a/voerbak/voerbak.h +++ b/voerbak/voerbak.h @@ -1,42 +1,6 @@ #pragma once #include <stdio.h> #include <stdlib.h> -#include <memory.h> -#include <stdbool.h> - -/** - * @brief Structure to store a board - * - * @param width Board width - * @param height Board height - * @param length Board state array length (width * height) - * @param board Board state array - */ -typedef struct { - int width; - int height; - int length; - int* board; -} Board; - -/** - * @brief Print the board array - */ -void printBoard(Board*); - -/** - * @brief Check if the board is full (draw) - * - * @return `true` board is full - */ -bool boardFull(Board*); - -/** - * @brief Drop a disc into the board - * - * @return `true` if drop was successful, `false` if column full - */ -bool dropFisje(Board*, int, int); /** * @brief Main function diff --git a/voerbak/win.c b/voerbak/win.c index c995060..021601a 100644 --- a/voerbak/win.c +++ b/voerbak/win.c @@ -2,6 +2,7 @@ #include <stdbool.h> #include "voerbak.h" +#include "board.h" int winCheckRecursive(Board *b, int pos, int direction, int d_index, int currentLength) { int newPos = pos + direction; diff --git a/voerbak/win.h b/voerbak/win.h index 41122a5..ec816b0 100644 --- a/voerbak/win.h +++ b/voerbak/win.h @@ -2,6 +2,7 @@ #include <stdbool.h> #include "voerbak.h" +#include "board.h" /** * @brief Get length of longest streak with same color from `pos` in direction `direction` |