diff options
Diffstat (limited to 'src/ctaklm.c')
| -rw-r--r-- | src/ctaklm.c | 436 |
1 files changed, 218 insertions, 218 deletions
diff --git a/src/ctaklm.c b/src/ctaklm.c index 53e6178..9d369f8 100644 --- a/src/ctaklm.c +++ b/src/ctaklm.c @@ -17,140 +17,140 @@ const char *rev = "\033[7m", *und = "\033[4m", *rst = "\033[0m"; static void put_stone(const enum STONE_VARIANT stone, const enum COLOUR colour, - const uint8_t top, const uint8_t beyond_carry_limit) { - if (beyond_carry_limit) { - fputs(und,stdout); - } else { - if (colour == C_BLACK) fputs(blk, stdout); - else fputs(wht,stdout); - } - if (top) { - switch (stone) { - case STONE_FLAT: putchar(CHAR_FLT); break; - case STONE_STANDING: putchar(CHAR_STN); break; - case STONE_CAPSTONE: putchar(CHAR_CAP); break; - } - } else { - putchar( (colour == C_BLACK) ? 'B' : 'W' ); - } - fputs(rst,stdout); + const uint8_t top, const uint8_t beyond_carry_limit) { + if (beyond_carry_limit) { + fputs(und,stdout); + } else { + if (colour == C_BLACK) fputs(blk, stdout); + else fputs(wht,stdout); + } + if (top) { + switch (stone) { + case STONE_FLAT: putchar(CHAR_FLT); break; + case STONE_STANDING: putchar(CHAR_STN); break; + case STONE_CAPSTONE: putchar(CHAR_CAP); break; + } + } else { + putchar( (colour == C_BLACK) ? 'B' : 'W' ); + } + fputs(rst,stdout); } static void print_cell_line(const uint8_t line, - const uint8_t col, const uint8_t row) { + const uint8_t col, const uint8_t row) { - const uint8_t location = THE_COORDS(col, row), - stack_size = COUNT_AT(location); + const uint8_t location = THE_COORDS(col, row), + stack_size = COUNT_AT(location); - uint8_t idx; - for (uint8_t k = 0; k < SQUARE_W; k++) { - idx = line + k*SQUARE_H; - // Are we in the last column to be displayed? - if ((k+1)*SQUARE_H > stack_size) { - // If so, then if we can't fill it offset the starting line so - // that it fills from the bottom up instead of the top down - if (line < (k+1)*SQUARE_H - stack_size ) { - // skip these lines - idx = 0xFF; - } else { - // offset back - idx -= (k+1)*SQUARE_H - stack_size; - } - } - if (idx < stack_size) { - put_stone(STONE_AT(location), colours[location] & (1 << idx), - idx == 0, idx >= board_size); - } else { - putchar(' '); - } - } + uint8_t idx; + for (uint8_t k = 0; k < SQUARE_W; k++) { + idx = line + k*SQUARE_H; + // Are we in the last column to be displayed? + if ((k+1)*SQUARE_H > stack_size) { + // If so, then if we can't fill it offset the starting line so + // that it fills from the bottom up instead of the top down + if (line < (k+1)*SQUARE_H - stack_size ) { + // skip these lines + idx = 0xFF; + } else { + // offset back + idx -= (k+1)*SQUARE_H - stack_size; + } + } + if (idx < stack_size) { + put_stone(STONE_AT(location), colours[location] & (1 << idx), + idx == 0, idx >= board_size); + } else { + putchar(' '); + } + } } // It takes board_size*(SQUARE_H+1)+1 lines to print the board, they // may be requested in any order and at any time static void print_board_line(const uint8_t line) { - const uint8_t mod = line % (SQUARE_H + 1), - row = board_size - line/(SQUARE_H + 1) - 1; + const uint8_t mod = line % (SQUARE_H + 1), + row = board_size - line/(SQUARE_H + 1) - 1; - // Print leader, either row number if half-way through square or - // padding spaces otherwise - if (mod == (SQUARE_H + 1)/2 ) printf("%d. ",row + 1); - else fputs(" ",stdout); + // Print leader, either row number if half-way through square or + // padding spaces otherwise + if (mod == (SQUARE_H + 1)/2 ) printf("%d. ",row + 1); + else fputs(" ",stdout); - // Top and bottom of squares receive borders - if (mod == 0) { - for (uint8_t x = 0; x < board_size; x++) { - putchar('+'); - for (uint8_t k = 0; k < SQUARE_W; k++) putchar('-'); - } - puts("+"); - } else { - // Interior of board should be filled by borders and pieces - if (line < board_size*(SQUARE_H+1)) { - for (uint8_t x = 0; x < board_size; x++) { - putchar('|'); - print_cell_line(mod - 1 , x, row); - } - puts("|"); - } else { - // Bottom of board has column markers - for (uint8_t x = 0; x < board_size; x++) { - for (uint8_t k = 0; k <= SQUARE_W/2; k++) putchar(' '); - printf("%c.",x+'a'); - for (uint8_t k = 0; k < SQUARE_W-SQUARE_W/2-2; k++) putchar(' '); - } - putchar('\n'); - } - } + // Top and bottom of squares receive borders + if (mod == 0) { + for (uint8_t x = 0; x < board_size; x++) { + putchar('+'); + for (uint8_t k = 0; k < SQUARE_W; k++) putchar('-'); + } + puts("+"); + } else { + // Interior of board should be filled by borders and pieces + if (line < board_size*(SQUARE_H+1)) { + for (uint8_t x = 0; x < board_size; x++) { + putchar('|'); + print_cell_line(mod - 1 , x, row); + } + puts("|"); + } else { + // Bottom of board has column markers + for (uint8_t x = 0; x < board_size; x++) { + for (uint8_t k = 0; k <= SQUARE_W/2; k++) putchar(' '); + printf("%c.",x+'a'); + for (uint8_t k = 0; k < SQUARE_W-SQUARE_W/2-2; k++) putchar(' '); + } + putchar('\n'); + } + } } // Simple wrapper to print the whole board in one go static void print_board(void) { - for (uint8_t k = 0; k<board_size*(SQUARE_H+1)+2; k++) { - print_board_line(k); - } + for (uint8_t k = 0; k<board_size*(SQUARE_H+1)+2; k++) { + print_board_line(k); + } } // Print the contents of a single square static void print_square(const uint8_t col, const uint8_t row) { - if (row < board_size && col < board_size) { - printf("%c%c: ",'a'+col,'1'+row); - const uint8_t stack_size = COUNT_AT(THE_COORDS(col, row)); - if (stack_size > 0) { - uint8_t mask = 1 << (stack_size - 1); - for (uint8_t k = 0; k < stack_size; k++, mask >>= 1) { - put_stone(STONE_AT(THE_COORDS(col, row)), - colours[THE_COORDS(col, row)] & mask, - k+1 == stack_size, - stack_size-k >= board_size); - } - puts("<-- top"); - } else { - puts("(empty)"); - } - } else { - printf("Requested square not on board (%dx%d).\n",board_size,board_size); - } + if (row < board_size && col < board_size) { + printf("%c%c: ",'a'+col,'1'+row); + const uint8_t stack_size = COUNT_AT(THE_COORDS(col, row)); + if (stack_size > 0) { + uint8_t mask = 1 << (stack_size - 1); + for (uint8_t k = 0; k < stack_size; k++, mask >>= 1) { + put_stone(STONE_AT(THE_COORDS(col, row)), + colours[THE_COORDS(col, row)] & mask, + k+1 == stack_size, + stack_size-k >= board_size); + } + puts("<-- top"); + } else { + puts("(empty)"); + } + } else { + printf("Requested square not on board (%dx%d).\n",board_size,board_size); + } } static void print_info(void) { - printf("Turn: %2d, %s%s%s%s\n", - ply/2 + 1, - (ply & 1) ? blk : wht, - (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("Turn: %2d, %s%s%s%s\n", + ply/2 + 1, + (ply & 1) ? blk : wht, + (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); } char *gamelog = 0; @@ -158,137 +158,137 @@ uint8_t auto_board = 0xFF, auto_info = 0xFF; 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' ? - char prepend[8]; + // I _could_ dynamically compute the size but ... don't let + // `perfect' be the enemy of `good' ? + char prepend[8]; - if (win_line) { - prepend[0] = '\n'; - prepend[1] = 0; - } else if (ply & 1) { - snprintf(prepend, 7, "%s%d. ", - (ply == 1) ? "" : "\n", ply/2+1); - } else { - strcpy(prepend, " "); - } - // Make room for this line - gamelog = realloc(gamelog, - strlen(gamelog) - + strlen(prepend) - + strlen(line) + 1); - strcat(gamelog,prepend); - strcat(gamelog,line); + if (win_line) { + prepend[0] = '\n'; + prepend[1] = 0; + } else if (ply & 1) { + snprintf(prepend, 7, "%s%d. ", + (ply == 1) ? "" : "\n", ply/2+1); + } else { + strcpy(prepend, " "); + } + // Make room for this line + gamelog = realloc(gamelog, + strlen(gamelog) + + strlen(prepend) + + strlen(line) + 1); + strcat(gamelog,prepend); + strcat(gamelog,line); } static void end_game(char *line, char *win) { - append_to_gamelog(line, 0); - append_to_gamelog(win, 1); - print_board(); - puts("Game over:"); - puts(gamelog); - putchar('\n'); + append_to_gamelog(line, 0); + append_to_gamelog(win, 1); + print_board(); + puts("Game over:"); + puts(gamelog); + putchar('\n'); } static void handle_turn(char *line) { - // Track win state - uint8_t new_win = (won == 0xFF); - switch (do_ptn(line)) { - // Errors - case PTN_INVALID: { puts("Invalid PTN."); break; } - case ACT_ILLEGAL: { puts("Illegal action."); break; } - case ACT_OVERFLOW: { - puts("Move would cause internal overflow, select another."); - break; - } - // Game has ended - case GAME_END: { - // Did it end this turn? - if (new_win) { - switch (won) { - case WIN_DRAGON: { end_game(line,"R-R"); break; } - case WIN_DRAW: { end_game(line,"1/2-1/2"); break; } - case WIN_FLAT_BLACK: { end_game(line,"0-F"); break; } - case WIN_FLAT_WHITE: { end_game(line,"F-0"); break; } - case WIN_ROAD_BLACK: { end_game(line,"0-R"); break; } - case WIN_ROAD_WHITE: { end_game(line,"R-0"); break; } - } - } - puts("Game over, enter `new' to play again."); - break; - } - // Valid, append to game log - case PTN_VALID: - case ACT_OK: { - append_to_gamelog(line, 0); - if (auto_board) print_board(); - if (auto_info) print_info(); - break; - } - } + // Track win state + uint8_t new_win = (won == 0xFF); + switch (do_ptn(line)) { + // Errors + case PTN_INVALID: { puts("Invalid PTN."); break; } + case ACT_ILLEGAL: { puts("Illegal action."); break; } + case ACT_OVERFLOW: { + puts("Move would cause internal overflow, select another."); + break; + } + // Game has ended + case GAME_END: { + // Did it end this turn? + if (new_win) { + switch (won) { + case WIN_DRAGON: { end_game(line,"R-R"); break; } + case WIN_DRAW: { end_game(line,"1/2-1/2"); break; } + case WIN_FLAT_BLACK: { end_game(line,"0-F"); break; } + case WIN_FLAT_WHITE: { end_game(line,"F-0"); break; } + case WIN_ROAD_BLACK: { end_game(line,"0-R"); break; } + case WIN_ROAD_WHITE: { end_game(line,"R-0"); break; } + } + } + puts("Game over, enter `new' to play again."); + break; + } + // Valid, append to game log + case PTN_VALID: + case ACT_OK: { + append_to_gamelog(line, 0); + if (auto_board) print_board(); + if (auto_info) print_info(); + break; + } + } } static void new_game(uint8_t size) { - reset_state(size); - printf("New %dx%d game!\n",size,size); - if (gamelog) gamelog = realloc(gamelog, sizeof(char)); - else gamelog = malloc(sizeof(char)); - gamelog[0] = 0; + reset_state(size); + printf("New %dx%d game!\n",size,size); + if (gamelog) gamelog = realloc(gamelog, sizeof(char)); + else gamelog = malloc(sizeof(char)); + gamelog[0] = 0; } int main(int argc, char **argv) { - (void)(argc); - (void)(argv); - new_game(5); + (void)(argc); + (void)(argv); + new_game(5); - char *line; - while((line = linenoise("ctaklm> ")) != NULL) { - if (!strncmp(line,"help",5)) { - puts("Valid commands: auto (board|info), board, help, info,\ + char *line; + while((line = linenoise("ctaklm> ")) != NULL) { + if (!strncmp(line,"help",5)) { + puts("Valid commands: auto (board|info), board, help, info,\ log, square, new [56], <PTN>."); - } else if (!strncmp(line,"board",6)) { - print_board(); - } else if (!strncmp(line,"info",5)) { - print_info(); - } else if (!strncmp(line,"log",3)) { - puts(gamelog); - } else if (!strncmp(line,"auto",4)) { - if (!strncmp(line,"auto board",10)) { - auto_board = ~auto_board; - printf("Automatic board display %s.\n", - (auto_board) ? "Enabled" : "Disabled"); - } else if (!strncmp(line,"auto info",9)) { - auto_info = ~auto_info; - printf("Automatic info display %s.\n", - (auto_info) ? "Enabled" : "Disabled"); - } else { - puts("Usage: auto (board|info)."); - } - } else if (!strncmp(line,"square",6)) { - if (strnlen(line, 9) >= 9 - && line[7] >= 'a' && line[7] <= '`'+board_size - && line[8] >= '1' && line[8] <= '0'+board_size) { - print_square(line[7]-'a', line[8]-'1'); - } else { - printf("Usage: square [a-%c][1-%c]\n",'`'+board_size,'0'+board_size); - } - } else if (!strncmp(line,"new",3)) { - if (line[3] == 0) { - new_game(5); - } else if (strnlen(line,5) >= 5 && line[4] >= '5' && line[4] <= '6') { - new_game(line[4]-'0'); - } else { - puts("Usage: new [56]."); - } - } else { - handle_turn(line); - } + } else if (!strncmp(line,"board",6)) { + print_board(); + } else if (!strncmp(line,"info",5)) { + print_info(); + } else if (!strncmp(line,"log",3)) { + puts(gamelog); + } else if (!strncmp(line,"auto",4)) { + if (!strncmp(line,"auto board",10)) { + auto_board = ~auto_board; + printf("Automatic board display %s.\n", + (auto_board) ? "Enabled" : "Disabled"); + } else if (!strncmp(line,"auto info",9)) { + auto_info = ~auto_info; + printf("Automatic info display %s.\n", + (auto_info) ? "Enabled" : "Disabled"); + } else { + puts("Usage: auto (board|info)."); + } + } else if (!strncmp(line,"square",6)) { + if (strnlen(line, 9) >= 9 + && line[7] >= 'a' && line[7] <= '`'+board_size + && line[8] >= '1' && line[8] <= '0'+board_size) { + print_square(line[7]-'a', line[8]-'1'); + } else { + printf("Usage: square [a-%c][1-%c]\n",'`'+board_size,'0'+board_size); + } + } else if (!strncmp(line,"new",3)) { + if (line[3] == 0) { + new_game(5); + } else if (strnlen(line,5) >= 5 && line[4] >= '5' && line[4] <= '6') { + new_game(line[4]-'0'); + } else { + puts("Usage: new [56]."); + } + } else { + handle_turn(line); + } - free(line); - } - return 0; + free(line); + } + return 0; } |
