summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ctaklm.c122
-rw-r--r--src/pptdb.c131
2 files changed, 192 insertions, 61 deletions
diff --git a/src/ctaklm.c b/src/ctaklm.c
index 72c0ae2..53e6178 100644
--- a/src/ctaklm.c
+++ b/src/ctaklm.c
@@ -17,7 +17,7 @@ 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) {
+ const uint8_t top, const uint8_t beyond_carry_limit) {
if (beyond_carry_limit) {
fputs(und,stdout);
} else {
@@ -38,7 +38,7 @@ put_stone(const enum STONE_VARIANT stone, const enum COLOUR colour,
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);
@@ -51,16 +51,16 @@ print_cell_line(const uint8_t line,
// 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;
+ // skip these lines
+ idx = 0xFF;
} else {
- // offset back
- idx -= (k+1)*SQUARE_H - stack_size;
+ // 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);
+ idx == 0, idx >= board_size);
} else {
putchar(' ');
}
@@ -81,25 +81,25 @@ print_board_line(const uint8_t line) {
// 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("+");
+ 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);
+ 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(' ');
+ 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');
}
@@ -109,8 +109,8 @@ print_board_line(const uint8_t line) {
// 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);
}
}
@@ -123,10 +123,10 @@ print_square(const uint8_t col, const uint8_t 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);
+ 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 {
@@ -140,17 +140,17 @@ print_square(const uint8_t col, const uint8_t row) {
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)" : "");
+ 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);
+ 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);
+ wht,white_count >> 7,rst,
+ blk,black_count >> 7,rst);
}
char *gamelog = 0;
@@ -167,15 +167,15 @@ append_to_gamelog(const char *line, const uint8_t win_line) {
prepend[1] = 0;
} else if (ply & 1) {
snprintf(prepend, 7, "%s%d. ",
- (ply == 1) ? "" : "\n", ply/2+1);
+ (ply == 1) ? "" : "\n", ply/2+1);
} else {
strcpy(prepend, " ");
}
// Make room for this line
gamelog = realloc(gamelog,
- strlen(gamelog)
- + strlen(prepend)
- + strlen(line) + 1);
+ strlen(gamelog)
+ + strlen(prepend)
+ + strlen(line) + 1);
strcat(gamelog,prepend);
strcat(gamelog,line);
}
@@ -195,7 +195,7 @@ handle_turn(char *line) {
// Track win state
uint8_t new_win = (won == 0xFF);
switch (do_ptn(line)) {
- // Errors
+ // Errors
case PTN_INVALID: { puts("Invalid PTN."); break; }
case ACT_ILLEGAL: { puts("Illegal action."); break; }
case ACT_OVERFLOW: {
@@ -206,14 +206,14 @@ handle_turn(char *line) {
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; }
- }
+ 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;
@@ -247,8 +247,8 @@ main(int argc, char **argv) {
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>.");
+ 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)) {
@@ -257,31 +257,31 @@ main(int argc, char **argv) {
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");
+ 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");
+ auto_info = ~auto_info;
+ printf("Automatic info display %s.\n",
+ (auto_info) ? "Enabled" : "Disabled");
} else {
- puts("Usage: auto (board|info).");
+ 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');
+ && 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);
+ 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);
+ new_game(5);
} else if (strnlen(line,5) >= 5 && line[4] >= '5' && line[4] <= '6') {
- new_game(line[4]-'0');
+ new_game(line[4]-'0');
} else {
- puts("Usage: new [56].");
+ puts("Usage: new [56].");
}
} else {
handle_turn(line);
diff --git a/src/pptdb.c b/src/pptdb.c
new file mode 100644
index 0000000..e335d8e
--- /dev/null
+++ b/src/pptdb.c
@@ -0,0 +1,131 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <tak.h>
+
+uint64_t heights[16];
+
+// Warning: performs _no_ checks on input whatsoever
+static enum E_RESULT
+parse_line(char *pt, const ssize_t read) {
+ ssize_t idx = 0;
+ enum E_RESULT r;
+ for(;;) {
+ if (pt[idx] == 'P') {
+ // P [A-F][1-6] [CF]?,
+ idx+=2;
+ enum STONE_VARIANT stone;
+ const uint8_t col = pt[idx]-'A', row = pt[idx+1]-'1';
+
+ if (idx + 3 < read) {
+ switch (pt[idx+3]) {
+ case 'W': { stone = STONE_STANDING; break; }
+ case 'C': { stone = STONE_CAPSTONE; break; }
+ default: { stone = STONE_FLAT; break; }
+ }
+ } else { stone = STONE_FLAT; }
+
+ /* char buf[100]; */
+ /* generate_place(board_size, THE_COORDS(col,row), stone, buf); */
+ /* puts(buf); */
+
+ r = try_place(THE_COORDS(col,row), current_colour, stone);
+ if (r != ACT_OK) return r;
+ } else if (pt[idx] == 'M') {
+ // M [A-F][1-6] [A-F][1-6]( [1-6])+,
+ idx+=2;
+ uint8_t drops[board_size];
+ const uint8_t s_col =pt[idx]-'A', s_row=pt[idx+1]-'1',
+ d_col=pt[idx+3]-'A', d_row=pt[idx+4]-'1';
+ idx+=4;
+
+ enum MOVE_DIRECTION dir;
+ if (s_col < d_col) dir=M_RIGHT;
+ else if (s_col > d_col) dir=M_LEFT;
+ else if (s_row < d_row) dir=M_UP;
+ else if (s_row > d_row) dir=M_DOWN;
+
+ uint8_t steps = 0;
+ do {
+ idx+=2;
+ drops[steps++] = pt[idx] - '0';
+ } while (idx+2<read && pt[idx+1] != ',');
+
+ /* char buf[100]; */
+ /* generate_move(board_size, THE_COORDS(s_col,s_row), dir, steps, drops, buf); */
+ /* puts(buf); */
+
+ r = try_move(THE_COORDS(s_col, s_row), dir, steps, drops);
+
+ if (r != ACT_OK) return r;
+
+ // Measure height of stacks exceeding 1
+ for (int k = 0; k < board_size * board_size; k++) {
+ if (COUNT_AT(k)>1) heights[COUNT_AT(k)]+=1;
+ }
+ }
+ while (idx<read && pt[idx++]!=',');
+ if (idx>=read) return ACT_OK;
+ next_ply();
+ }
+ return ACT_OK;
+}
+
+
+int
+main(int argc, char **argv) {
+ (void)(argc);
+
+ enum E_RESULT r;
+ enum WIN_TYPE win;
+ uint32_t games = 0, overflow=0, illegal = 0;
+ uint32_t road_wins=0, flat_wins=0, road_turns=0, flat_turns=0;
+
+ for (int k = 0; k < 16; k++) heights[k] = 0;
+
+ FILE *playtak;
+ ssize_t read;
+ size_t len = 0;
+ char *line = NULL;
+
+ const uint8_t size = argv[1][0]-'0';
+ playtak = fopen(argv[2], "r");
+ if (playtak == NULL) exit(EXIT_FAILURE);
+
+ while ((read = getline(&line, &len, playtak)) != -1) {
+ reset_state(size);
+ r = parse_line(line,read);
+ if (r == ACT_ILLEGAL) {
+ illegal++;
+ printf("Culprit: (%ld) %s",read,line);
+ } else if (r == ACT_OVERFLOW) {
+ overflow++;
+ } else {
+ win = check_win();
+ if (win == WIN_FLAT_BLACK || win == WIN_FLAT_WHITE || win == WIN_DRAW) {
+ flat_wins++;
+ flat_turns += ply/2+1;
+ } else if (win == WIN_DRAGON || win == WIN_ROAD_BLACK || win == WIN_ROAD_WHITE){
+ road_wins++;
+ road_turns += ply/2+1;
+ }
+ }
+ games++;
+ }
+
+ fclose(playtak);
+ if (line) free(line);
+
+ printf("Read %d games\n",games);
+ printf("Illegals: %d\nOverflows: %d\nRoad wins: %d\nFlat wins: %d\n\
+Average turns to road win: %.3f\nAverage turns to flat win: %f\n",
+ illegal,overflow, road_wins, flat_wins,
+ (double)(road_turns)/(double)(road_wins),
+ (double)(flat_turns)/(double)(flat_wins));
+ for (int k = 2; k < 16; k++) {
+ printf("Height %2d: %7ld\n",k,heights[k]);
+ }
+
+ exit(EXIT_SUCCESS);
+}