diff options
Diffstat (limited to 'src/ct1986.c')
| -rw-r--r-- | src/ct1986.c | 53 |
1 files changed, 28 insertions, 25 deletions
diff --git a/src/ct1986.c b/src/ct1986.c index 44e64e7..91d70b0 100644 --- a/src/ct1986.c +++ b/src/ct1986.c @@ -4,7 +4,8 @@ #include <tak.h> #include <minimax_cnn1986.h> -#include <LCDHD44780.h> + +FILE *lcd = NULL; static char *gamelog = 0; static int human, search_depth; @@ -37,8 +38,9 @@ static void end_game(char *line, char *win) { append_to_gamelog(line, 0); append_to_gamelog(win, 1); - write_string("Game over: "); - write_string(win); + fputs("Game over: ",lcd); + fputs(win,lcd); + fflush(lcd); } static int @@ -47,10 +49,10 @@ handle_turn(char *line) { uint8_t new_win = (won == 0xFF); switch (do_ptn(line)) { // Errors - case PTN_INVALID: { write_string("Invalid PTN."); return -1; break; } - case ACT_ILLEGAL: { write_string("Illegal action."); return -1; break; } + case PTN_INVALID: { fputs("Invalid PTN.",lcd); return -1; break; } + case ACT_ILLEGAL: { fputs("Illegal action.",lcd); return -1; break; } case ACT_OVERFLOW: { - write_string("Move would cause internal overflow, select another."); + fputs("Move would cause internal overflow, select another."); return EXIT_FAILURE; break; } @@ -66,7 +68,7 @@ handle_turn(char *line) { case WIN_ROAD_WHITE: { end_game(line,"R-0"); break; } } } - write_string("Game over, enter `new' to play again."); + fputs("Game over, enter `new' to play again."); if (! new_win) return EXIT_FAILURE; break; } @@ -84,9 +86,10 @@ handle_turn(char *line) { static void new_game(uint8_t size) { reset_state(size); - write_string("New 5x5 game! ct1986 at search depth "); - write_to_lcd(search_depth + '0', LCD_DATA); - write_to_lcd('.', LCD_DATA); + fputs("New 5x5 game! ct1986 at search depth "); + fputc(search_depth + '0', lcd); + fputc('.', lcd); + fflush(lcd); if (gamelog) gamelog = realloc(gamelog, sizeof(char)); else gamelog = malloc(sizeof(char)); gamelog[0] = 0; @@ -95,24 +98,24 @@ new_game(uint8_t size) { static void dot(const uint8_t depth) { if (depth == 0) { - write_to_lcd('#', LCD_DATA); + fputc('#', lcd); } } static int ct1986_turn(const uint8_t search_depth) { // Prepare progress bar - write_string("Computing "); + fputs("Computing "); // Run the minimax float minimax = ct1986_generate(search_depth); // Failed to find a move? if ((minimax <= -infty && (ply & 1) == 1) ||(minimax >= infty && (ply & 1) == 0)) { - write_string("Opponent concedes!"); + fputs("Opponent concedes!"); return EXIT_FAILURE; } else { - write_string("Result: "); - write_string(ct1986_ptn); + fputs("Result: "); + fputs(ct1986_ptn); handle_turn(ct1986_ptn); return EXIT_SUCCESS; } @@ -121,10 +124,10 @@ ct1986_turn(const uint8_t search_depth) { static int input_is_not_turn(const char *line) { if (!strcmp(line,"help")) { - write_string("Valid commands: depth [0-9], help, new, \ + fputs("Valid commands: depth [0-9], help, new, \ play (b|w), self-play, <PTN>."); } else if (!strcmp(line,"log")) { - write_string(gamelog); + fputs(gamelog); } else if (!strcmp(line,"new")) { new_game(5); } else if (!strcmp(line,"self-play")) { @@ -132,10 +135,11 @@ play (b|w), self-play, <PTN>."); } else if (!strncmp(line,"depth",5)) { if (strnlen(line,7) == 7 && line[6] >= '0' && line[6] <= '9') { search_depth = line[6] - '0'; - write_string("New search depth: "); - write_to_lcd(line[6], LCD_DATA); + fputs("New search depth: ",lcd); + fputc(line[6], lcd); + fflush(lcd); } else { - write_string("Usage: depth [0-9]."); + fputs("Usage: depth [0-9].",lcd); } } else if (!strncmp(line,"play",4)) { if (strnlen(line,7) == 6 @@ -145,7 +149,7 @@ play (b|w), self-play, <PTN>."); human = 1 - (line[5] & 1); new_game(5); } else { - write_string("Usage: play (b|w).\n"); + fputs("Usage: play (b|w).\n", lcd); } } else { return EXIT_FAILURE; @@ -158,9 +162,8 @@ main(int argc, char **argv) { (void)(argc); (void)(argv); - puts("Init."); - init_lcd(); - puts("Starting up."); + lcd = fopen("/dev/lcd","w"); + if (lcd == NULL) return EXIT_FAILURE; // Set up output function for ct1986 ct1986_display_progress = ˙ @@ -174,7 +177,7 @@ main(int argc, char **argv) { size_t alloc_size; for (int playing = 1; playing;) { - write_string("Welcome to ct1986!"); + fputs("Welcome to ct1986!\n",lcd); fflush(lcd); fflush(stdout); while ((human == 0) && (read = getline(&line, &alloc_size, stdin)) != -1) { if (read) { |
