diff options
-rw-r--r-- | voerbak/argparse.c | 2 | ||||
-rw-r--r-- | voerbak/board.c | 20 | ||||
-rw-r--r-- | voerbak/board.h | 13 | ||||
-rw-r--r-- | voerbak/diewertje.c | 25 | ||||
-rw-r--r-- | voerbak/input | 1 | ||||
-rw-r--r-- | voerbak/readme.md | 8 | ||||
-rw-r--r-- | voerbak/voerbak.c | 18 | ||||
-rw-r--r-- | voerbak/win.c | 7 |
8 files changed, 73 insertions, 21 deletions
diff --git a/voerbak/argparse.c b/voerbak/argparse.c index c6dde5a..2e2b347 100644 --- a/voerbak/argparse.c +++ b/voerbak/argparse.c @@ -5,7 +5,7 @@ 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 doc[] = "Connect 4 engine"; static char args_doc[] = "arguments"; static struct argp_option options[] = { { "width", 'w', "WIDTH", 0, "Field width (columns)"}, diff --git a/voerbak/board.c b/voerbak/board.c index 8008db4..5bc4bd1 100644 --- a/voerbak/board.c +++ b/voerbak/board.c @@ -12,6 +12,12 @@ Board* createBoard(int width, int height) { return gameBoard; } +Board* createCopy(Board* original) { + Board *copy = createBoard(original->width, original->height); + copy->board = original->board; + return copy; +} + void printBoard(Board *b) { for (int i = 0; i < b->length; i++) printf("%d", b->board[i]); @@ -25,17 +31,19 @@ bool boardFull(Board *b) { return true; } -bool dropFisje(Board *b, int column, int disc) { +int 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 + /* bool won = checkWin(b, pos); */ + return pos; } } - printf("e:full\n"); - fflush(stdout); - return false; // unsuccessful drop on board full + if (verbosity >= 0) { + printf("e:full\n"); + fflush(stdout); + } + return -1; } diff --git a/voerbak/board.h b/voerbak/board.h index 9b4c2f6..47f39b2 100644 --- a/voerbak/board.h +++ b/voerbak/board.h @@ -27,6 +27,15 @@ typedef struct { Board* createBoard(int, int); /** + * @brief Create a copy of board + * + * @param board Original board + * + * @return Pointer to new board + */ +Board* createCopy(Board*); + +/** * @brief Print the board array */ void printBoard(Board*); @@ -41,7 +50,7 @@ bool boardFull(Board*); /** * @brief Drop a disc into the board * - * @return `true` if drop was successful, `false` if column full + * @return New disc position or -1 if disc wasn't dropped */ -bool dropFisje(Board*, int, int); +int dropFisje(Board*, int, int); diff --git a/voerbak/diewertje.c b/voerbak/diewertje.c index 6290b5c..a0016fa 100644 --- a/voerbak/diewertje.c +++ b/voerbak/diewertje.c @@ -1,9 +1,34 @@ #include <stdio.h> +#include <memory.h> #include "board.h" +#include "win.h" #include "voerbak.h" int diewertje(Board *b) { + int pre_verbosity = verbosity; verbosity >= 2 && printf("Got a diewertje request, from a board that's %dx%d\n", b->width, b->height); + + int outcome[b->width]; + + verbosity = -1; + Board *copy = createCopy(b); + for (int c = 0; c < b->width; c++) { + int drop = dropFisje(copy, c, 1); + + printBoard(copy); + + outcome[c] = drop == -1 ? 0 : checkWin(copy, drop); + if(drop != -1) copy->board[drop] = 0; // remove disc from copy + } + verbosity = pre_verbosity; + + printf("["); + for(int i = 0; i < b->width; i++) { + printf(i + 1 == b->width ? "%d" : "%d, ", outcome[i]); + } + printf("]\n"); + return 2; } + diff --git a/voerbak/input b/voerbak/input index 1243fe0..8c03bef 100644 --- a/voerbak/input +++ b/voerbak/input @@ -1,4 +1,3 @@ -7 6 4 3 3 diff --git a/voerbak/readme.md b/voerbak/readme.md index 9a93189..1197ce3 100644 --- a/voerbak/readme.md +++ b/voerbak/readme.md @@ -12,6 +12,8 @@ Voerbak uses a 1-dimensional array for storing the playfield, and it's printed a 7|8|9|10|11|12|13 0|1|2|3|4|5|6 +Voerbak is used in this project using api/game/voerbak_connector.py + ## Building ```sh @@ -54,7 +56,7 @@ w|win|int-int = board indices where 4 was connected ```sh $ ./voerbak --help Usage: voerbak [OPTION...] arguments -Connect 4 game solver +Connect 4 engine -c, --solver=NAME Solver used for computing moves (unset is two humans playing) @@ -72,7 +74,3 @@ for any corresponding short options. Report bugs to https://github.com/lonkaars/po-4-op-een-rij/. ``` - - - - diff --git a/voerbak/voerbak.c b/voerbak/voerbak.c index 0b564c0..a46b92f 100644 --- a/voerbak/voerbak.c +++ b/voerbak/voerbak.c @@ -36,11 +36,21 @@ int main(int argc, char* argv[]) { if (move == 0) break; if (move < 1 || move > gameBoard->width) continue; - bool dropSuccess = dropFisje(gameBoard, move - 1, player_1 + 1); + int drop = dropFisje(gameBoard, move - 1, player_1 + 1); - player_1 = player_1 ^ dropSuccess; // only flip turns on successful drop - !cpu_2 && printf("m:%s\n", player_1 ? "true" : "false"); - fflush(stdout); + if (drop == -1) { + printBoard(gameBoard); + continue; + } + + player_1 = !player_1; // only flip turns on successful drop + + bool won = checkWin(gameBoard, drop); + + if (verbosity >= 0 ) { + cpu_2 == false && printf("m:%s\n", player_1 ? "true" : "false"); + fflush(stdout); + } if (boardFull(gameBoard)) { printf("d:full\n"); diff --git a/voerbak/win.c b/voerbak/win.c index 021601a..80ab0ba 100644 --- a/voerbak/win.c +++ b/voerbak/win.c @@ -44,8 +44,11 @@ bool checkWin(Board *b, int pos) { int start_pos = pos + directions[i+0] * values[i+0]; int end_pos = pos + directions[i+4] * values[i+4]; - printf("w:%d-%d\n", start_pos, end_pos); - fflush(stdout); + + if (verbosity >= 0) { + printf("w:%d-%d\n", start_pos, end_pos); + fflush(stdout); + } } } |