aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <l.leblansch@gmail.com>2021-02-24 09:50:58 +0100
committerlonkaars <l.leblansch@gmail.com>2021-02-24 09:50:58 +0100
commit2fe4352955042ef5415f6830d8c9d05d39159b7e (patch)
treeced3fd5d601e96afac6e433e2b2f33b8345d4706
parent9b712de308e150cc70cf7571c9debfff17978fd2 (diff)
added diewertje cpu solver
-rw-r--r--voerbak/diewertje.c8
-rw-r--r--voerbak/diewertje.h11
-rw-r--r--voerbak/solvers.c12
-rw-r--r--voerbak/solvers.h3
-rw-r--r--voerbak/voerbak.c11
-rw-r--r--voerbak/voerbak.h4
6 files changed, 45 insertions, 4 deletions
diff --git a/voerbak/diewertje.c b/voerbak/diewertje.c
new file mode 100644
index 0000000..93a8596
--- /dev/null
+++ b/voerbak/diewertje.c
@@ -0,0 +1,8 @@
+#include <stdio.h>
+
+#include "board.h"
+
+int diewertje(Board *b) {
+ printf("Got a diewertje request, from a board that's %dx%d\n", b->width, b->height);
+ return 2;
+}
diff --git a/voerbak/diewertje.h b/voerbak/diewertje.h
new file mode 100644
index 0000000..617a6f4
--- /dev/null
+++ b/voerbak/diewertje.h
@@ -0,0 +1,11 @@
+#include "board.h"
+
+/**
+ * @brief Diewertje game solver
+ *
+ * @param Board* game board to decide next move for
+ *
+ * @return int Column to place disc in
+ */
+int diewertje(Board*);
+
diff --git a/voerbak/solvers.c b/voerbak/solvers.c
new file mode 100644
index 0000000..e3aab12
--- /dev/null
+++ b/voerbak/solvers.c
@@ -0,0 +1,12 @@
+#include <string.h>
+#include <stdio.h>
+
+#include "board.h"
+
+#include "diewertje.h"
+
+int cpuMove(Board* b, char* solver) {
+ if (strcmp(solver, "diewertje") == 0) return diewertje(b);
+
+ return 0;
+}
diff --git a/voerbak/solvers.h b/voerbak/solvers.h
new file mode 100644
index 0000000..295edf8
--- /dev/null
+++ b/voerbak/solvers.h
@@ -0,0 +1,3 @@
+#include "board.h"
+
+int cpuMove(Board*, char*);
diff --git a/voerbak/voerbak.c b/voerbak/voerbak.c
index d1982fa..2782163 100644
--- a/voerbak/voerbak.c
+++ b/voerbak/voerbak.c
@@ -6,6 +6,7 @@
#include "board.h"
#include "messages.h"
#include "argparse.h"
+#include "solvers.h"
#define EMPTY ""
@@ -14,6 +15,7 @@ int main(int argc, char* argv[]) {
Board *gameBoard = createBoard(arguments.width, arguments.height);
+ bool cpu_2 = strlen(arguments.solver) > 0;
bool player_1 = true;
int move = 0;
char* message = malloc(1); // this is weird and i don't understand it but it prevents a segmentation fault or something
@@ -26,13 +28,15 @@ int main(int argc, char* argv[]) {
continue;
}
+ move:
+
if (move == 0) break;
if (move < 1 || move > gameBoard->width) continue;
bool dropSuccess = dropFisje(gameBoard, move - 1, player_1 + 1);
player_1 = player_1 ^ dropSuccess; // only flip turns on successful drop
- printf("m:%s\n", player_1 ? "true" : "false");
+ !cpu_2 && printf("m:%s\n", player_1 ? "true" : "false");
fflush(stdout);
if (boardFull(gameBoard)) {
@@ -41,6 +45,11 @@ int main(int argc, char* argv[]) {
}
printBoard(gameBoard);
+
+ if (cpu_2 && !player_1) {
+ move = cpuMove(gameBoard, arguments.solver);
+ goto move;
+ }
}
return 0;
diff --git a/voerbak/voerbak.h b/voerbak/voerbak.h
index d44f382..a828d02 100644
--- a/voerbak/voerbak.h
+++ b/voerbak/voerbak.h
@@ -2,7 +2,5 @@
#include <stdio.h>
#include <stdlib.h>
-/**
- * @brief Main function
- */
+/** @brief Main function */
int main();