aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/tak.c780
-rw-r--r--src/ctaklm.c436
-rw-r--r--src/pptdb.c192
3 files changed, 702 insertions, 706 deletions
diff --git a/include/tak.c b/include/tak.c
index 787e46a..5551e5b 100644
--- a/include/tak.c
+++ b/include/tak.c
@@ -28,34 +28,34 @@ uint8_t white_count, black_count, ply;
void
reset_state(const uint8_t new_board_size) {
- if (new_board_size == 6) {
- board_size = 6;
- white_count = 128 | 30;
- black_count = 128 | 30;
- } else {
- board_size = 5;
- white_count = 128 | 21;
- black_count = 128 | 21;
- }
+ if (new_board_size == 6) {
+ board_size = 6;
+ white_count = 128 | 30;
+ black_count = 128 | 30;
+ } else {
+ board_size = 5;
+ white_count = 128 | 21;
+ black_count = 128 | 21;
+ }
- ply = 0;
- won = 0xFF; // i may live to regret this hack
- current_colour = C_BLACK;
+ ply = 0;
+ won = 0xFF; // i may live to regret this hack
+ current_colour = C_BLACK;
- for (uint8_t k = 0; k < NUM_SQUARES; k++ ) {
- celldat[k] = 0;
- }
+ for (uint8_t k = 0; k < NUM_SQUARES; k++ ) {
+ celldat[k] = 0;
+ }
}
void
next_ply(void) {
- ply++;
- if (ply == 2) {
- current_colour = C_WHITE;
- } else {
- if (current_colour == C_BLACK) current_colour = C_WHITE;
- else current_colour = C_BLACK;
- }
+ ply++;
+ if (ply == 2) {
+ current_colour = C_WHITE;
+ } else {
+ if (current_colour == C_BLACK) current_colour = C_WHITE;
+ else current_colour = C_BLACK;
+ }
}
// ===================================================================
@@ -64,47 +64,47 @@ next_ply(void) {
enum E_RESULT
try_place(const int8_t location, const enum COLOUR colour,
- const enum STONE_VARIANT stone)
+ const enum STONE_VARIANT stone)
{
- // Game is over?
- if (won < 0xFF) return GAME_END;
- // Can't place on an occupied square
- if (COUNT_AT(location)) {
- return ACT_ILLEGAL;
- } else {
- switch (stone) {
- case STONE_STANDING:
- if (ply < 2) return ACT_ILLEGAL;
- // behold the magic GCC comment which defeates
- // -Wimplicit-fallthrough:
- // fall through
- case STONE_FLAT: {
- if (colour == C_BLACK) {
- if (black_count & 127) black_count--;
- else return ACT_ILLEGAL;
- } else {
- if (white_count & 127) white_count--;
- else return ACT_ILLEGAL;
- }
- break;
- }
- case STONE_CAPSTONE: {
- if (ply < 2) return ACT_ILLEGAL;
- if (colour == C_BLACK) {
- if (black_count & 128) black_count &= 127;
- else return ACT_ILLEGAL;
- } else {
- if (white_count & 128) white_count &= 127;
- else return ACT_ILLEGAL;
- }
- break;
- }
- }
+ // Game is over?
+ if (won < 0xFF) return GAME_END;
+ // Can't place on an occupied square
+ if (COUNT_AT(location)) {
+ return ACT_ILLEGAL;
+ } else {
+ switch (stone) {
+ case STONE_STANDING:
+ if (ply < 2) return ACT_ILLEGAL;
+ // behold the magic GCC comment which defeates
+ // -Wimplicit-fallthrough:
+ // fall through
+ case STONE_FLAT: {
+ if (colour == C_BLACK) {
+ if (black_count & 127) black_count--;
+ else return ACT_ILLEGAL;
+ } else {
+ if (white_count & 127) white_count--;
+ else return ACT_ILLEGAL;
+ }
+ break;
+ }
+ case STONE_CAPSTONE: {
+ if (ply < 2) return ACT_ILLEGAL;
+ if (colour == C_BLACK) {
+ if (black_count & 128) black_count &= 127;
+ else return ACT_ILLEGAL;
+ } else {
+ if (white_count & 128) white_count &= 127;
+ else return ACT_ILLEGAL;
+ }
+ break;
+ }
+ }
- colours[location] = colour;
- celldat[location] = NUM_INC | stone;
- return ACT_OK;
- }
+ colours[location] = colour;
+ celldat[location] = NUM_INC | stone;
+ return ACT_OK;
+ }
}
// ===================================================================
@@ -113,92 +113,92 @@ try_place(const int8_t location, const enum COLOUR colour,
static void
push_stones(const int8_t location, const uint8_t count,
- const uint8_t new_colours,
- const enum STONE_VARIANT top_stone) {
- colours[location] = (colours[location] << count) | new_colours;
- celldat[location] = top_stone
- | ((celldat[location] + ((count << NUM_SHIFT))) & NUM_MASK);
+ const uint8_t new_colours,
+ const enum STONE_VARIANT top_stone) {
+ colours[location] = (colours[location] << count) | new_colours;
+ celldat[location] = top_stone
+ | ((celldat[location] + ((count << NUM_SHIFT))) & NUM_MASK);
}
enum E_RESULT
try_move(const int8_t location, const enum MOVE_DIRECTION direction,
- const uint8_t steps, const uint8_t drops[5]) {
- // Game is over?
- if (won < 0xFF) return GAME_END;
- // Can't do this
- if (steps == 0 || steps > 5) return ACT_ILLEGAL;
- // Is the desired direction and count on the board?
- int8_t delta = 0;
- switch (direction) {
- case M_UP: {
- delta = +board_size;
- if (location + delta * steps > NUM_SQUARES) return ACT_ILLEGAL;
- break;
- };
- case M_DOWN: {
- delta = -board_size;
- if (location + delta * steps < 0) return ACT_ILLEGAL;
- break;
- };
- case M_RIGHT: {
- delta = +1;
- if (location + delta * steps > NUM_SQUARES) return ACT_ILLEGAL;
- break;
- };
- case M_LEFT: {
- delta = -1;
- if (location + delta * steps < 0) return ACT_ILLEGAL;
- break;
- };
- };
+ const uint8_t steps, const uint8_t drops[5]) {
+ // Game is over?
+ if (won < 0xFF) return GAME_END;
+ // Can't do this
+ if (steps == 0 || steps > 5) return ACT_ILLEGAL;
+ // Is the desired direction and count on the board?
+ int8_t delta = 0;
+ switch (direction) {
+ case M_UP: {
+ delta = +board_size;
+ if (location + delta * steps > NUM_SQUARES) return ACT_ILLEGAL;
+ break;
+ };
+ case M_DOWN: {
+ delta = -board_size;
+ if (location + delta * steps < 0) return ACT_ILLEGAL;
+ break;
+ };
+ case M_RIGHT: {
+ delta = +1;
+ if (location + delta * steps > NUM_SQUARES) return ACT_ILLEGAL;
+ break;
+ };
+ case M_LEFT: {
+ delta = -1;
+ if (location + delta * steps < 0) return ACT_ILLEGAL;
+ break;
+ };
+ };
- // For every square in the direction
- uint8_t total = 0;
- for (uint8_t k = 0; k < steps; k++) {
- // Can't drop 0 anywhere because we're past the first square
- if (drops[k] == 0)
- return ACT_ILLEGAL;
- // Can't drop more than BOARD_SIZE stones in a square
- if (drops[k] > board_size)
- return ACT_ILLEGAL;
- // Check for overflows
- if (COUNT_AT(location+(k+1)*delta) + drops[k] > 0x0F)
- return ACT_OVERFLOW;
- // Check for capstone
- if (STONE_AT(location+(k+1)*delta) == STONE_CAPSTONE)
- return ACT_ILLEGAL;
- // Check for wall
- if ( (STONE_AT(location+(k+1)*delta) == STONE_STANDING)
- // If not last drop, or not dropping just one, or not a cap
- && ( (k+1 < steps)
- || (drops[k] != 1)
- || (STONE_AT(location) != STONE_CAPSTONE) )
- )
- return ACT_ILLEGAL;
- total += drops[k];
- }
+ // For every square in the direction
+ uint8_t total = 0;
+ for (uint8_t k = 0; k < steps; k++) {
+ // Can't drop 0 anywhere because we're past the first square
+ if (drops[k] == 0)
+ return ACT_ILLEGAL;
+ // Can't drop more than BOARD_SIZE stones in a square
+ if (drops[k] > board_size)
+ return ACT_ILLEGAL;
+ // Check for overflows
+ if (COUNT_AT(location+(k+1)*delta) + drops[k] > 0x0F)
+ return ACT_OVERFLOW;
+ // Check for capstone
+ if (STONE_AT(location+(k+1)*delta) == STONE_CAPSTONE)
+ return ACT_ILLEGAL;
+ // Check for wall
+ if ( (STONE_AT(location+(k+1)*delta) == STONE_STANDING)
+ // If not last drop, or not dropping just one, or not a cap
+ && ( (k+1 < steps)
+ || (drops[k] != 1)
+ || (STONE_AT(location) != STONE_CAPSTONE) )
+ )
+ return ACT_ILLEGAL;
+ total += drops[k];
+ }
- // Can't ask to move 0, more than board_size, or stones available
- if ( (total == 0) || (total > board_size)
- || (total > COUNT_AT(location)) )
- return ACT_ILLEGAL;
+ // Can't ask to move 0, more than board_size, or stones available
+ if ( (total == 0) || (total > board_size)
+ || (total > COUNT_AT(location)) )
+ return ACT_ILLEGAL;
- // Nothing illegal, do it. First we add the stones to the
- // destination squares
- uint8_t j = total;
- for (uint8_t k = 0; k < steps; k++) {
- j -= drops[k];
- push_stones(location+(k+1)*delta,
- drops[k],
- (colours[location] >> j) & (0xFFFF >> (0x10 - drops[k])),
- (k == steps - 1) ? STONE_AT(location) : STONE_FLAT);
- }
- // Then we drop them from the source
- colours[location] >>= total;
- const uint8_t dec_count = celldat[location] - (total << NUM_SHIFT);
- celldat[location] = dec_count & NUM_MASK;
+ // Nothing illegal, do it. First we add the stones to the
+ // destination squares
+ uint8_t j = total;
+ for (uint8_t k = 0; k < steps; k++) {
+ j -= drops[k];
+ push_stones(location+(k+1)*delta,
+ drops[k],
+ (colours[location] >> j) & (0xFFFF >> (0x10 - drops[k])),
+ (k == steps - 1) ? STONE_AT(location) : STONE_FLAT);
+ }
+ // Then we drop them from the source
+ colours[location] >>= total;
+ const uint8_t dec_count = celldat[location] - (total << NUM_SHIFT);
+ celldat[location] = dec_count & NUM_MASK;
- return ACT_OK;
+ return ACT_OK;
}
// ===================================================================
@@ -210,136 +210,136 @@ try_move(const int8_t location, const enum MOVE_DIRECTION direction,
// direction = 0 --> left-to-right, direction = 1 --> bottom-to-top
static uint8_t
dfs_road(uint8_t dfs_stack[NUM_SQUARES], uint8_t dfs_pntr,
- const enum COLOUR colour, const uint8_t direction) {
- while (dfs_pntr > 0) {
- const uint8_t cur = dfs_stack[--dfs_pntr];
- // Made it to the other side?
- if ( (direction == 1 && cur >= board_size * (board_size - 1))
- || (direction == 0 && cur % board_size + 1 == board_size) )
- return 1;
+ const enum COLOUR colour, const uint8_t direction) {
+ while (dfs_pntr > 0) {
+ const uint8_t cur = dfs_stack[--dfs_pntr];
+ // Made it to the other side?
+ if ( (direction == 1 && cur >= board_size * (board_size - 1))
+ || (direction == 0 && cur % board_size + 1 == board_size) )
+ return 1;
- // Check the four neighbours of this cell, provided they exist,
- // are inhabited, and are of the appropriate colour
+ // Check the four neighbours of this cell, provided they exist,
+ // are inhabited, and are of the appropriate colour
- // Direction: > (same row)
- if ( ((cur % board_size) + 1 < board_size)
- && (COUNT_AT(cur + 1)) // wont ever be out of bounds
- && ((colours[cur+1] & 1) == colour)
- && ((celldat[cur+1] & DFS_MASK) == 0) ) {
- dfs_stack[dfs_pntr++] = cur + 1;
- celldat[cur+1] |= DFS_MASK;
- }
+ // Direction: > (same row)
+ if ( ((cur % board_size) + 1 < board_size)
+ && (COUNT_AT(cur + 1)) // wont ever be out of bounds
+ && ((colours[cur+1] & 1) == colour)
+ && ((celldat[cur+1] & DFS_MASK) == 0) ) {
+ dfs_stack[dfs_pntr++] = cur + 1;
+ celldat[cur+1] |= DFS_MASK;
+ }
- // Direction: < (same row)
- if ( (cur % board_size > 0)
- && (COUNT_AT(cur - 1)) // wont ever be out of bounds
- && ((colours[cur-1] & 1) == colour)
- && ((celldat[cur-1] & DFS_MASK) == 0) ) {
- dfs_stack[dfs_pntr++] = cur - 1;
- celldat[cur-1] |= DFS_MASK;
- }
+ // Direction: < (same row)
+ if ( (cur % board_size > 0)
+ && (COUNT_AT(cur - 1)) // wont ever be out of bounds
+ && ((colours[cur-1] & 1) == colour)
+ && ((celldat[cur-1] & DFS_MASK) == 0) ) {
+ dfs_stack[dfs_pntr++] = cur - 1;
+ celldat[cur-1] |= DFS_MASK;
+ }
- // Direction: +
- if ( (cur + board_size < NUM_SQUARES)
- && (COUNT_AT(cur + board_size))
- && ((colours[cur+board_size] & 1) == colour)
- && ((celldat[cur+board_size] & DFS_MASK) == 0) ) {
- dfs_stack[dfs_pntr++] = cur + board_size;
- celldat[cur+board_size] |= DFS_MASK;
- }
+ // Direction: +
+ if ( (cur + board_size < NUM_SQUARES)
+ && (COUNT_AT(cur + board_size))
+ && ((colours[cur+board_size] & 1) == colour)
+ && ((celldat[cur+board_size] & DFS_MASK) == 0) ) {
+ dfs_stack[dfs_pntr++] = cur + board_size;
+ celldat[cur+board_size] |= DFS_MASK;
+ }
- // Direction: -
- if ( (cur >= board_size)
- && (COUNT_AT(cur - board_size))
- && ((colours[cur-board_size] & 1) == colour)
- && ((celldat[cur-board_size] & DFS_MASK) == 0) ) {
- dfs_stack[dfs_pntr++] = cur - board_size;
- celldat[cur-board_size] |= DFS_MASK;
- }
- }
- return 0;
+ // Direction: -
+ if ( (cur >= board_size)
+ && (COUNT_AT(cur - board_size))
+ && ((colours[cur-board_size] & 1) == colour)
+ && ((celldat[cur-board_size] & DFS_MASK) == 0) ) {
+ dfs_stack[dfs_pntr++] = cur - board_size;
+ celldat[cur-board_size] |= DFS_MASK;
+ }
+ }
+ return 0;
}
static enum WIN_TYPE
check_road_colour(const enum COLOUR colour) {
- uint8_t dfs_stack[NUM_SQUARES];
- uint8_t dfs_pntr;
+ uint8_t dfs_stack[NUM_SQUARES];
+ uint8_t dfs_pntr;
- // Prime the depth-first-search stack with all boundary cells of
- // colour COLOUR.
+ // Prime the depth-first-search stack with all boundary cells of
+ // colour COLOUR.
- const enum WIN_TYPE winner =
- (colour == C_BLACK) ? WIN_ROAD_BLACK : WIN_ROAD_WHITE;
+ const enum WIN_TYPE winner =
+ (colour == C_BLACK) ? WIN_ROAD_BLACK : WIN_ROAD_WHITE;
- // We're using the two left-over bits in data_t to track whether
- // we've seen it. Reset those before anything.
+ // We're using the two left-over bits in data_t to track whether
+ // we've seen it. Reset those before anything.
- for (uint8_t k=0; k<NUM_SQUARES; k++)
- celldat[k] &= NOT_DFS_MASK;
+ for (uint8_t k=0; k<NUM_SQUARES; k++)
+ celldat[k] &= NOT_DFS_MASK;
- // First left-to-right
- dfs_pntr = 0;
- for (uint8_t y=0; y<board_size; y++) {
- if ( COUNT_AT(THE_COORDS(0, y)) &&
- (colours[THE_COORDS(0, y)] & 1) == colour) {
- dfs_stack[dfs_pntr++] = THE_COORDS(0, y);
- celldat[THE_COORDS(0, y)] |= DFS_MASK;
- }
- }
- if (dfs_road(dfs_stack, dfs_pntr, colour, 0)) return winner;
+ // First left-to-right
+ dfs_pntr = 0;
+ for (uint8_t y=0; y<board_size; y++) {
+ if ( COUNT_AT(THE_COORDS(0, y)) &&
+ (colours[THE_COORDS(0, y)] & 1) == colour) {
+ dfs_stack[dfs_pntr++] = THE_COORDS(0, y);
+ celldat[THE_COORDS(0, y)] |= DFS_MASK;
+ }
+ }
+ if (dfs_road(dfs_stack, dfs_pntr, colour, 0)) return winner;
- // Clear visited squares
- for (uint8_t k=0; k<NUM_SQUARES; k++)
- celldat[k] &= NOT_DFS_MASK;
+ // Clear visited squares
+ for (uint8_t k=0; k<NUM_SQUARES; k++)
+ celldat[k] &= NOT_DFS_MASK;
- // Then bottom-to-top
- dfs_pntr = 0;
- for (uint8_t x=0; x<board_size; x++) {
- if ( COUNT_AT(THE_COORDS(x, 0)) &&
- (colours[THE_COORDS(x, 0)] & 1) == colour) {
- dfs_stack[dfs_pntr++] = THE_COORDS(x, 0);
- celldat[THE_COORDS(x, 0)] |= DFS_MASK;
- }
- }
- if (dfs_road(dfs_stack, dfs_pntr, colour, 1)) return winner;
+ // Then bottom-to-top
+ dfs_pntr = 0;
+ for (uint8_t x=0; x<board_size; x++) {
+ if ( COUNT_AT(THE_COORDS(x, 0)) &&
+ (colours[THE_COORDS(x, 0)] & 1) == colour) {
+ dfs_stack[dfs_pntr++] = THE_COORDS(x, 0);
+ celldat[THE_COORDS(x, 0)] |= DFS_MASK;
+ }
+ }
+ if (dfs_road(dfs_stack, dfs_pntr, colour, 1)) return winner;
- return 0xFF;
+ return 0xFF;
}
enum WIN_TYPE
check_win(void) {
- int8_t total = 0, board_full = 1;
- for (uint8_t k = 0; k < NUM_SQUARES; k++) {
- if (COUNT_AT(k) == 0) {
- board_full = 0;
- } else if (STONE_AT(k) == STONE_FLAT) {
- total += ((colours[k] & 1) == C_BLACK) ? +1 : -1 ;
- }
- }
+ int8_t total = 0, board_full = 1;
+ for (uint8_t k = 0; k < NUM_SQUARES; k++) {
+ if (COUNT_AT(k) == 0) {
+ board_full = 0;
+ } else if (STONE_AT(k) == STONE_FLAT) {
+ total += ((colours[k] & 1) == C_BLACK) ? +1 : -1 ;
+ }
+ }
- // Do we do a flat count?
- if (black_count == 0 || white_count == 0 || board_full) {
- // Decide based on count
- if (total > 0) return WIN_FLAT_BLACK;
- else if (total < 0) return WIN_FLAT_WHITE;
- else return WIN_DRAW;
- }
+ // Do we do a flat count?
+ if (black_count == 0 || white_count == 0 || board_full) {
+ // Decide based on count
+ if (total > 0) return WIN_FLAT_BLACK;
+ else if (total < 0) return WIN_FLAT_WHITE;
+ else return WIN_DRAW;
+ }
- // Road?
- enum WIN_TYPE rb, rw;
- rb = check_road_colour(C_BLACK);
- rw = check_road_colour(C_WHITE);
+ // Road?
+ enum WIN_TYPE rb, rw;
+ rb = check_road_colour(C_BLACK);
+ rw = check_road_colour(C_WHITE);
- if (rb == WIN_ROAD_BLACK) {
- if (rw == WIN_ROAD_WHITE) {
- return WIN_DRAGON;
- } else {
- return WIN_ROAD_BLACK;
- }
- } else {
- return rw;
- }
+ if (rb == WIN_ROAD_BLACK) {
+ if (rw == WIN_ROAD_WHITE) {
+ return WIN_DRAGON;
+ } else {
+ return WIN_ROAD_BLACK;
+ }
+ } else {
+ return rw;
+ }
}
// ===================================================================
// PTN place parser
@@ -350,32 +350,32 @@ check_win(void) {
enum E_RESULT
parse_place(const uint8_t board_size, char *ptn,
- uint8_t *out_location, enum STONE_VARIANT *out_stone) {
+ uint8_t *out_location, enum STONE_VARIANT *out_stone) {
- if (board_size < 5 || board_size > 6) return PTN_INVALID;
+ if (board_size < 5 || board_size > 6) return PTN_INVALID;
- ASSERT_NONEMPTY;
+ ASSERT_NONEMPTY;
- *out_stone = STONE_FLAT;
- switch (*ptn) {
- case 'C' : { ptn++; *out_stone = STONE_CAPSTONE; break; };
- case 'S' : { ptn++; *out_stone = STONE_STANDING; break; };
- case 'F' : { ptn++; break; };
- }
+ *out_stone = STONE_FLAT;
+ switch (*ptn) {
+ case 'C' : { ptn++; *out_stone = STONE_CAPSTONE; break; };
+ case 'S' : { ptn++; *out_stone = STONE_STANDING; break; };
+ case 'F' : { ptn++; break; };
+ }
- ASSERT_MORE;
+ ASSERT_MORE;
- if ( (*ptn < 'a') || (*ptn > '`' + board_size) ) return PTN_INVALID;
- *out_location = *ptn - 'a';
+ if ( (*ptn < 'a') || (*ptn > '`' + board_size) ) return PTN_INVALID;
+ *out_location = *ptn - 'a';
- ptn++; ASSERT_MORE;
+ ptn++; ASSERT_MORE;
- if ( (*ptn < '1') || (*ptn > board_size + '0') ) return PTN_INVALID;
- *out_location += board_size * (*ptn - '1');
+ if ( (*ptn < '1') || (*ptn > board_size + '0') ) return PTN_INVALID;
+ *out_location += board_size * (*ptn - '1');
- if (*(++ptn) > 0) return PTN_INVALID;
+ if (*(++ptn) > 0) return PTN_INVALID;
- return PTN_VALID;
+ return PTN_VALID;
}
// ===================================================================
@@ -384,74 +384,74 @@ parse_place(const uint8_t board_size, char *ptn,
enum E_RESULT
parse_move(const uint8_t board_size, char *ptn,
- uint8_t *out_location, enum MOVE_DIRECTION *out_direction,
- uint8_t *out_steps, uint8_t out_drops[5]) {
+ uint8_t *out_location, enum MOVE_DIRECTION *out_direction,
+ uint8_t *out_steps, uint8_t out_drops[5]) {
- if (board_size < 5 || board_size > 6) return PTN_INVALID;
+ if (board_size < 5 || board_size > 6) return PTN_INVALID;
- ASSERT_NONEMPTY;
+ ASSERT_NONEMPTY;
- uint8_t picked_up = 1;
+ uint8_t picked_up = 1;
- // Optionally indicate how many stones picked up
- if ( (*ptn >= '1') && (*ptn <= '0' + board_size)) {
- picked_up = *ptn - '0';
- ptn++; ASSERT_MORE;
- }
+ // Optionally indicate how many stones picked up
+ if ( (*ptn >= '1') && (*ptn <= '0' + board_size)) {
+ picked_up = *ptn - '0';
+ ptn++; ASSERT_MORE;
+ }
- // column must be on the board
- if ( (*ptn < 'a') || (*ptn > '`' + board_size) ) return PTN_INVALID;
- *out_location = *ptn - 'a';
+ // column must be on the board
+ if ( (*ptn < 'a') || (*ptn > '`' + board_size) ) return PTN_INVALID;
+ *out_location = *ptn - 'a';
- ptn++; ASSERT_MORE;
+ ptn++; ASSERT_MORE;
- // row must be on the board
- if ( (*ptn < '1') || (*ptn > board_size + '0') ) return PTN_INVALID;
- *out_location += board_size * (*ptn - '1');
+ // row must be on the board
+ if ( (*ptn < '1') || (*ptn > board_size + '0') ) return PTN_INVALID;
+ *out_location += board_size * (*ptn - '1');
- ptn++; ASSERT_MORE;
+ ptn++; ASSERT_MORE;
- // valid direction
- switch (*ptn) {
- case '+': { *out_direction = M_UP; break; }
- case '-': { *out_direction = M_DOWN; break; }
- case '<': { *out_direction = M_LEFT; break; }
- case '>': { *out_direction = M_RIGHT; break; }
- default: return PTN_INVALID;
- }
+ // valid direction
+ switch (*ptn) {
+ case '+': { *out_direction = M_UP; break; }
+ case '-': { *out_direction = M_DOWN; break; }
+ case '<': { *out_direction = M_LEFT; break; }
+ case '>': { *out_direction = M_RIGHT; break; }
+ default: return PTN_INVALID;
+ }
- // Handle the case 'n<column><row><direction>' as
- // 'n<column><row><direction>n' for convenience, if n is omitted
- // assume n = 1
- ptn++;
- if (*ptn == 0) {
- *out_steps = picked_up;
- out_drops[0] = picked_up;
- return PTN_VALID;
- }
+ // Handle the case 'n<column><row><direction>' as
+ // 'n<column><row><direction>n' for convenience, if n is omitted
+ // assume n = 1
+ ptn++;
+ if (*ptn == 0) {
+ *out_steps = picked_up;
+ out_drops[0] = picked_up;
+ return PTN_VALID;
+ }
- // Parse the drops in each subsequent square
- *out_steps = 0;
- uint8_t total = 0;
- while (*ptn) {
- // can't drop more than the carry limit, or less than 1
- if ( (*ptn < '1') || (*ptn > '0' + board_size) )
- return PTN_INVALID;
+ // Parse the drops in each subsequent square
+ *out_steps = 0;
+ uint8_t total = 0;
+ while (*ptn) {
+ // can't drop more than the carry limit, or less than 1
+ if ( (*ptn < '1') || (*ptn > '0' + board_size) )
+ return PTN_INVALID;
- // can't move more than the size of the board in any direction
- if ( (*out_steps + 1 >= board_size) && *ptn)
- return PTN_INVALID;
+ // can't move more than the size of the board in any direction
+ if ( (*out_steps + 1 >= board_size) && *ptn)
+ return PTN_INVALID;
- out_drops[*out_steps] = *ptn - '0';
- total += out_drops[*out_steps];
- *out_steps += 1;
- ptn++;
- }
+ out_drops[*out_steps] = *ptn - '0';
+ total += out_drops[*out_steps];
+ *out_steps += 1;
+ ptn++;
+ }
- // Mismatch between number of stones picked up and total dropped
- if ( total != picked_up ) return PTN_INVALID;
+ // Mismatch between number of stones picked up and total dropped
+ if ( total != picked_up ) return PTN_INVALID;
- return PTN_VALID;
+ return PTN_VALID;
}
// ===================================================================
@@ -460,15 +460,15 @@ parse_move(const uint8_t board_size, char *ptn,
void
generate_place(const uint8_t board_size, const uint8_t in_location,
- const enum STONE_VARIANT in_stone, char out_ptn[4]) {
- switch (in_stone) {
- case STONE_FLAT: { break; }
- case STONE_STANDING: { *out_ptn = 'S'; out_ptn++; break; }
- case STONE_CAPSTONE: { *out_ptn = 'C'; out_ptn++; break; }
- }
- *out_ptn = 'a' + (in_location % board_size); out_ptn++;
- *out_ptn = '1' + (in_location / board_size); out_ptn++;
- *out_ptn = 0;
+ const enum STONE_VARIANT in_stone, char out_ptn[4]) {
+ switch (in_stone) {
+ case STONE_FLAT: { break; }
+ case STONE_STANDING: { *out_ptn = 'S'; out_ptn++; break; }
+ case STONE_CAPSTONE: { *out_ptn = 'C'; out_ptn++; break; }
+ }
+ *out_ptn = 'a' + (in_location % board_size); out_ptn++;
+ *out_ptn = '1' + (in_location / board_size); out_ptn++;
+ *out_ptn = 0;
}
// ===================================================================
@@ -477,31 +477,31 @@ generate_place(const uint8_t board_size, const uint8_t in_location,
void
generate_move(const uint8_t board_size, const uint8_t in_location,
- const enum MOVE_DIRECTION in_direction,
- const uint8_t in_steps, const uint8_t in_drops[5],
- char out_ptn[10]) {
- uint8_t total = 0;
- for (uint8_t k = 0; k<in_steps; k++) total+=in_drops[k];
- if (total > 1) {
- *out_ptn = '0' + total; out_ptn++;
- }
+ const enum MOVE_DIRECTION in_direction,
+ const uint8_t in_steps, const uint8_t in_drops[5],
+ char out_ptn[10]) {
+ uint8_t total = 0;
+ for (uint8_t k = 0; k<in_steps; k++) total+=in_drops[k];
+ if (total > 1) {
+ *out_ptn = '0' + total; out_ptn++;
+ }
- *out_ptn = 'a' + (in_location % board_size); out_ptn++;
- *out_ptn = '1' + (in_location / board_size); out_ptn++;
+ *out_ptn = 'a' + (in_location % board_size); out_ptn++;
+ *out_ptn = '1' + (in_location / board_size); out_ptn++;
- switch (in_direction) {
- case M_UP: { *out_ptn = '+'; break; }
- case M_DOWN: { *out_ptn = '-'; break; }
- case M_LEFT: { *out_ptn = '<'; break; }
- case M_RIGHT: { *out_ptn = '>'; break; }
- }; out_ptn++;
+ switch (in_direction) {
+ case M_UP: { *out_ptn = '+'; break; }
+ case M_DOWN: { *out_ptn = '-'; break; }
+ case M_LEFT: { *out_ptn = '<'; break; }
+ case M_RIGHT: { *out_ptn = '>'; break; }
+ }; out_ptn++;
- for (uint8_t k = 0; (total > 1) && (k < in_steps); k++) {
- *out_ptn = '0' + in_drops[k]; out_ptn++;
- }
+ for (uint8_t k = 0; (total > 1) && (k < in_steps); k++) {
+ *out_ptn = '0' + in_drops[k]; out_ptn++;
+ }
- *out_ptn = 0;
+ *out_ptn = 0;
}
// ===================================================================
@@ -510,58 +510,58 @@ generate_move(const uint8_t board_size, const uint8_t in_location,
static uint8_t
is_not_placement(char *ptn) {
- if (ptn == 0) return 0;
- for (;;ptn++) {
- switch (*ptn) {
- case '+':
- case '-':
- case '>':
- case '<': return 1;
- case 0: return 0;
- }
- }
+ if (ptn == 0) return 0;
+ for (;;ptn++) {
+ switch (*ptn) {
+ case '+':
+ case '-':
+ case '>':
+ case '<': return 1;
+ case 0: return 0;
+ }
+ }
}
enum E_RESULT
do_ptn(char *ptn) {
- // Game over?
- if (won < 0xFF) return GAME_END;
+ // Game over?
+ if (won < 0xFF) return GAME_END;
- enum E_RESULT res;
- uint8_t location;
+ enum E_RESULT res;
+ uint8_t location;
- // Placing or moving?
- if (is_not_placement(ptn)) {
- uint8_t steps, drops[5];
- enum MOVE_DIRECTION direction;
- // Parse it as a move
- res = parse_move(board_size, ptn, &location,
- &direction, &steps, drops);
- // If valid PTN, try to do it
- if (res == PTN_VALID) {
- if (ply < 2) return ACT_ILLEGAL;
- res = try_move(location, direction, steps, drops);
- }
- } else {
- // It was not a move
- enum STONE_VARIANT stone;
- // Was it a valid placement?
- res = parse_place(board_size, ptn, &location, &stone);
- // If so, try it
- if (res == PTN_VALID)
- res = try_place(location, current_colour, stone);
- }
- // A valid ply occured
- if (res == ACT_OK) {
- // Don't bother checking that the game was won early on, could be
- // more conservative here :)
- if (ply >= board_size) {
- won = check_win();
- if (won < 0xFF) return GAME_END;
- }
- // Only step if the game isn't over yet
- next_ply();
- }
+ // Placing or moving?
+ if (is_not_placement(ptn)) {
+ uint8_t steps, drops[5];
+ enum MOVE_DIRECTION direction;
+ // Parse it as a move
+ res = parse_move(board_size, ptn, &location,
+ &direction, &steps, drops);
+ // If valid PTN, try to do it
+ if (res == PTN_VALID) {
+ if (ply < 2) return ACT_ILLEGAL;
+ res = try_move(location, direction, steps, drops);
+ }
+ } else {
+ // It was not a move
+ enum STONE_VARIANT stone;
+ // Was it a valid placement?
+ res = parse_place(board_size, ptn, &location, &stone);
+ // If so, try it
+ if (res == PTN_VALID)
+ res = try_place(location, current_colour, stone);
+ }
+ // A valid ply occured
+ if (res == ACT_OK) {
+ // Don't bother checking that the game was won early on, could be
+ // more conservative here :)
+ if (ply >= board_size) {
+ won = check_win();
+ if (won < 0xFF) return GAME_END;
+ }
+ // Only step if the game isn't over yet
+ next_ply();
+ }
- return res;
+ return res;
}
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;
}
diff --git a/src/pptdb.c b/src/pptdb.c
index e335d8e..3812ee4 100644
--- a/src/pptdb.c
+++ b/src/pptdb.c
@@ -9,123 +9,119 @@ 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';
+ 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; }
+ 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;
- 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;
- 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] != ',');
- uint8_t steps = 0;
- do {
- idx+=2;
- drops[steps++] = pt[idx] - '0';
- } while (idx+2<read && pt[idx+1] != ',');
+ r = try_move(THE_COORDS(s_col, s_row), dir, steps, drops);
- /* char buf[100]; */
- /* generate_move(board_size, THE_COORDS(s_col,s_row), dir, steps, drops, buf); */
- /* puts(buf); */
+ if (r != ACT_OK) return r;
- 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;
+ // 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);
+ (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;
+ 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;
+ for (int k = 0; k < 16; k++) heights[k] = 0;
- FILE *playtak;
- ssize_t read;
- size_t len = 0;
- char *line = NULL;
+ 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);
+ 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++;
- }
+ 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 {
+ road_wins++;
+ road_turns += ply/2+1;
+ }
+ }
+ games++;
+ }
- fclose(playtak);
- if (line) free(line);
+ fclose(playtak);
+ if (line) free(line);
- printf("Read %d games\n",games);
- printf("Illegals: %d\nOverflows: %d\nRoad wins: %d\nFlat wins: %d\n\
+ 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]);
- }
+ 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);
+ exit(EXIT_SUCCESS);
}