aboutsummaryrefslogtreecommitdiff
path: root/src/ctaklm.c
diff options
context:
space:
mode:
authortslil clingman <tslil@posteo.de>2021-01-20 22:49:49 -0500
committertslil <tslil@posteo.de>2026-08-28 19:37:41 +0100
commit2ac4a6e1d3b6440de22d95f48454d6282c78726d (patch)
tree2496adbdf994c0bf574fbcc62486d244d9673d8b /src/ctaklm.c
parent1b0338a0546b760e8a7ffa61b38c410a166aae7c (diff)
parent36cecb722fb66d35cdbf69ebcf2aa6f49bfc082b (diff)
Merge branch 'pi'
Diffstat (limited to 'src/ctaklm.c')
-rw-r--r--src/ctaklm.c96
1 files changed, 52 insertions, 44 deletions
diff --git a/src/ctaklm.c b/src/ctaklm.c
index 080da01..24613e9 100644
--- a/src/ctaklm.c
+++ b/src/ctaklm.c
@@ -1,10 +1,9 @@
-#include <stdio.h>
#include <stdlib.h>
+#include <stdio.h>
#include <string.h>
-#include <linenoise.h>
#include <tak.h>
-#include <ct1986.h>
+#include <minimax_cnn1986.h>
static const char *blk = "\033[41m", *wht = "\033[44m";
static const char *und = "\033[4m", *rst = "\033[0m";
@@ -147,12 +146,9 @@ print_info(void) {
(ply & 1) ? "Black" : "White",
rst,
(ply < 2) ? " (counter-play start)" : "");
- printf("Flats remaining: %s%02d%s, %s%02d%s\n",
- wht,white_count & 127,rst,
- blk,black_count & 127,rst);
- printf("Caps remaining: %s%d%s, %s%d%s\n",
- wht,white_count >> 7,rst,
- blk,black_count >> 7,rst);
+ printf("Flats/Caps remaining: %s%02d/%d%s, %s%02d/%d%s\n",
+ wht, white_count & 127, white_count >> 7, rst,
+ blk, black_count & 127, black_count >> 7, rst);
}
static char *gamelog = 0;
@@ -300,8 +296,9 @@ load_ptn(const char* fn) {
static float sum_depth, num_check;
-static void
-dot(const uint8_t depth) {
+// Set up output function for ct1986
+inline void
+ct1986_display_progress(const uint8_t depth) {
sum_depth += depth;
num_check += 1;
if (depth == 0) {
@@ -312,28 +309,31 @@ dot(const uint8_t depth) {
static int
ct1986_turn(const uint8_t search_depth) {
- // Prepare progress bar
- fputs("Computing [", stdout);
- for (int k = 0; k < board_size * board_size; k++) putchar(' ');
- fputs("]\033[26D",stdout);
- fflush(stdout);
- // Run the minimax
- sum_depth = 0; num_check = 0;
- float minimax = ct1986_generate(search_depth);
- puts("]\033[1C");
- // Failed to find a move?
- if ((minimax <= -infty && (ply & 1) == 1)
- ||(minimax >= infty && (ply & 1) == 0)) {
- puts("Opponent concedes!");
- return EXIT_FAILURE;
+ if (won == 0xFF) {
+ // Prepare progress bar
+ fputs("Computing [", stdout);
+ for (int k = 0; k < board_size * board_size; k++) putchar(' ');
+ fputs("]\033[26D",stdout);
+ fflush(stdout);
+ // Run the minimax
+ sum_depth = 0; num_check = 0;
+ float minimax = ct1986_generate(search_depth);
+ fputs("\033[1C ", stdout);
+ // Failed to find a move?
+ if (minimax <= -infty) {
+ puts("Opponent concedes!");
+ return EXIT_FAILURE;
+ } else {
+ printf("Result: %s (%.2f, %.1e, %.2f)\n",
+ ct1986_ptn,
+ minimax*100.0,
+ num_check,
+ sum_depth/num_check);
+ handle_turn(ct1986_ptn);
+ return EXIT_SUCCESS;
+ }
} else {
- printf("Result: %s (%.2f, %.1e, %.2f)\n",
- ct1986_ptn,
- minimax*100.0,
- num_check,
- sum_depth/num_check);
- handle_turn(ct1986_ptn);
- return EXIT_SUCCESS;
+ return EXIT_FAILURE;
}
}
@@ -417,25 +417,33 @@ main(int argc, char **argv) {
(void)(argc);
(void)(argv);
- // Set up output function for ct1986
- ct1986_display_progress = &dot;
-
human = 0;
- search_depth = 3;
+ search_depth = 1;
new_game(5);
- char *line;
+ char *line = NULL;
+ ssize_t read = -1;
+ size_t alloc_size;
+
for (int playing = 1; playing;) {
- while((human == 0) && (line = linenoise("ctaklm> ")) != NULL) {
- if (input_is_not_turn(line)) {
- int r = handle_turn(line);
- if (r == EXIT_SUCCESS && won == 0xFF) {
- human = 1;
+ while (human == 0) {
+ fputs("ctaklm> ", stdout);
+ fflush(stdout);
+ read = getline(&line, &alloc_size, stdin);
+ if (read > 0) {
+ line[read - 1] = 0;
+ if (input_is_not_turn(line)) {
+ int r = handle_turn(line);
+ if (r == EXIT_SUCCESS && won == 0xFF) {
+ human = 1;
+ }
}
+ } else {
+ read = -1;
+ human = 1;
}
- free(line);
}
- if ((human == 0) && line == NULL) {
+ if (read == -1) {
playing = 0;
} else {
ct1986_turn(search_depth);