diff options
| author | tslil clingman <tslil@posteo.de> | 2021-01-21 20:46:28 -0500 |
|---|---|---|
| committer | tslil <tslil@posteo.de> | 2026-08-28 19:37:41 +0100 |
| commit | 8461e5ebea6e899eb0643f34ff03d7797d1441bf (patch) | |
| tree | 38518e28192a113dee1ef4821247a0a2ca3c8cc1 /src/ct1986.c | |
| parent | e54f496ce4acab029d95506736539c7f296cbbc8 (diff) | |
Starting work on LCD library
Diffstat (limited to 'src/ct1986.c')
| -rw-r--r-- | src/ct1986.c | 68 |
1 files changed, 26 insertions, 42 deletions
diff --git a/src/ct1986.c b/src/ct1986.c index f697877..273fd98 100644 --- a/src/ct1986.c +++ b/src/ct1986.c @@ -1,23 +1,14 @@ #include <stdlib.h> -#include <stdio.h> #include <string.h> #include <tak.h> #include <negamax_cnn1986.h> +#include <lcdlib.h> -const char* esc = "\x1B[L"; - -FILE *lcd = NULL; static char *gamelog = 0; static int human; static void -write_line(const char *line) { - fprintf(lcd, "%s\n", line); - fflush(lcd); -} - -static void append_to_gamelog(const char *line, const uint8_t win_line) { // I _could_ dynamically compute the size but ... don't let // `perfect' be the enemy of `good' ? @@ -45,8 +36,7 @@ static void end_game(char *line, char *win) { append_to_gamelog(line, 0); append_to_gamelog(win, 1); - fprintf(lcd, "Game over: %s\n", win); - fflush(lcd); + lcd_printf_line(L_SCROLL, "Game over: %s", win); } static int @@ -55,9 +45,9 @@ handle_turn(char *line) { uint8_t new_win = (won == 0xFF); switch (do_ptn(line)) { // Errors - case PTN_INVALID: { write_line("Invalid PTN."); break; } - case ACT_ILLEGAL: { write_line("Illegal ply."); break; } - case ACT_OVERFLOW: { write_line("Overflow."); break; } + case PTN_INVALID: { lcd_put_line(L_SCROLL, "Invalid PTN."); break; } + case ACT_ILLEGAL: { lcd_put_line(L_SCROLL, "Illegal ply."); break; } + case ACT_OVERFLOW: { lcd_put_line(L_SCROLL, "Overflow."); break; } // Game has ended case GAME_END: { // Did it end this turn? @@ -71,7 +61,7 @@ handle_turn(char *line) { } return EXIT_SUCCESS; } else { - write_line("Game over."); + lcd_put_line(L_SCROLL, "Game over."); break; } } @@ -87,7 +77,7 @@ handle_turn(char *line) { static void new_game(uint8_t size) { reset_state(size); - write_line("New 5x5 game."); + lcd_put_line(L_SCROLL, "New 5x5 game."); if (gamelog) gamelog = realloc(gamelog, sizeof(char)); else gamelog = malloc(sizeof(char)); gamelog[0] = 0; @@ -95,34 +85,31 @@ new_game(uint8_t size) { // Set up output function for negamax_cnn1986 static uint8_t perc; + inline void negamax_cnn1986_display_progress(const uint8_t depth) { if (depth == 0) { perc++; - // back four spaces and clear line, followed by perc% - fprintf(lcd, "%sl%sl%sl%sl%sk%3d%%", - esc, esc, esc, esc, esc, - perc*4); - fflush(lcd); + lcd_printf_line(L_OVERWRITE, "Computing: %d%%", perc*4); } } static int negamax_cnn1986_turn() { // Prepare progress bar - fputs("Computing: ", lcd); fflush(lcd); + lcd_put_line(L_SCROLL, "Computing: 0%"); // Run the minimax perc = 0; float minimax = negamax_cnn1986_generate(); - fputc('\n', lcd); fflush(lcd); // Failed to find a move? if (minimax <= -infty) { - fprintf(lcd, "%s concedes!\n", (ply & 1) ? "Black" : "White"); + lcd_printf_line(L_SCROLL, "%s concedes!", + (ply & 1) ? "Black" : "White"); return EXIT_FAILURE; } else { - fprintf(lcd, "%s: %s\n", - (ply & 1) ? "Black" : "White", - negamax_cnn1986_ptn); + lcd_printf_line(L_SCROLL, "%s: %s", + (ply & 1) ? "Black" : "White", + negamax_cnn1986_ptn); handle_turn(negamax_cnn1986_ptn); return EXIT_SUCCESS; } @@ -130,7 +117,7 @@ negamax_cnn1986_turn() { static void do_game_log(void) { - fputs(gamelog, lcd); + lcd_put_line(L_SCROLL, "Not implemented."); } static int @@ -138,14 +125,14 @@ input_is_not_turn(const char *line) { switch (line[0]) { case 'l': { do_game_log(); break; } case 'n': { new_game(5); break; } - case 'd': { + case 's': { negamax_cnn1986_search_depth = line[1] - '0'; - fprintf(lcd, "Search depth: %d\n", negamax_cnn1986_search_depth); - fflush(lcd); + lcd_printf_line(L_SCROLL, "Search depth: %d", + negamax_cnn1986_search_depth); break; } - case 'b': { human = 1; new_game(5); break; } - case 'w': { human = 0; new_game(5); break; } + case 'B': { human = 1; new_game(5); break; } + case 'W': { human = 0; new_game(5); break; } default: return EXIT_FAILURE; } return EXIT_SUCCESS; @@ -156,11 +143,8 @@ main(int argc, char **argv) { (void)(argc); (void)(argv); - lcd = fopen("/dev/lcd", "w"); - if (lcd == NULL) return EXIT_FAILURE; - - fprintf(lcd, "%sB", esc); - fflush(lcd); + if (lcd_begin() != EXIT_SUCCESS) + return EXIT_FAILURE; human = 0; negamax_cnn1986_search_depth = 3; @@ -172,8 +156,8 @@ main(int argc, char **argv) { for (int playing = 1; playing;) { while (human == 0) { - if (ply & 1) write_line("Black: "); - else write_line("White: "); + if (ply & 1) lcd_put_line(L_SCROLL, "Black: "); + else lcd_put_line(L_SCROLL, "White: "); read = getline(&line, &alloc_size, stdin); if (read > 0) { line[read - 1] = 0; @@ -194,6 +178,6 @@ main(int argc, char **argv) { } } - fclose(lcd); + lcd_end(); return EXIT_SUCCESS; } |
