summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ctaklm.c69
-rw-r--r--src/pptdb.c65
2 files changed, 104 insertions, 30 deletions
diff --git a/src/ctaklm.c b/src/ctaklm.c
index 9d369f8..3d4310c 100644
--- a/src/ctaklm.c
+++ b/src/ctaklm.c
@@ -190,16 +190,17 @@ end_game(char *line, char *win) {
putchar('\n');
}
-static void
+static int
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 PTN_INVALID: { puts("Invalid PTN."); return -1; break; }
+ case ACT_ILLEGAL: { puts("Illegal action."); return -1; break; }
case ACT_OVERFLOW: {
puts("Move would cause internal overflow, select another.");
+ return -1;
break;
}
// Game has ended
@@ -216,6 +217,7 @@ handle_turn(char *line) {
}
}
puts("Game over, enter `new' to play again.");
+ if (! new_win) return -1;
break;
}
// Valid, append to game log
@@ -227,6 +229,8 @@ handle_turn(char *line) {
break;
}
}
+
+ return 0;
}
static void
@@ -238,6 +242,59 @@ new_game(uint8_t size) {
gamelog[0] = 0;
}
+static int
+load_ptn(const char* fn) {
+ FILE *fh = NULL;
+
+ fh = fopen(fn, "r");
+ if (fh == NULL) return EXIT_FAILURE;
+
+ int space1, space2;
+ enum E_RESULT r;
+
+ ssize_t read;
+ size_t alloc_size;
+ char *line = NULL;
+ while ((read = getline(&line, &alloc_size, fh)) != -1) {
+ if (read && line[0] <= '9' && line[0] >= '0') {
+ // Trim trailing \n
+ line[read-1] = 0;
+ // Find first separator
+ space1 = 0;
+ while (space1 < read && line[space1++] != ' ');
+ // If still on line
+ if (space1 < read) {
+ // Find next separator or end of line, either way mark the split
+ space2 = space1;
+ while (space2 < read && line[space2] != ' ') space2++;
+ line[space2] = 0;
+ // Try the first piece we found
+ r = handle_turn(line+space1);
+ if (r) {
+ printf("Error on: %s\n", line+space1);
+ break;
+ }
+ // If there's a second piece, try it
+ if (space2 + 1 < read) {
+ r = handle_turn(line+space2+1);
+ if (r) {
+ printf("Error on: %s", line+space2+1);
+ break;
+ }
+ } else {
+ break;
+ }
+ }
+ }
+ }
+
+ if (line) free(line);
+ fclose(fh);
+
+
+ return EXIT_SUCCESS;
+}
+
int
main(int argc, char **argv) {
(void)(argc);
@@ -255,6 +312,12 @@ main(int argc, char **argv) {
print_info();
} else if (!strncmp(line,"log",3)) {
puts(gamelog);
+ } else if (!strncmp(line,"load",4)) {
+ if (strnlen(line,6) >= 6) {
+ load_ptn(line+5);
+ } else {
+ puts("Usage: load <file.ptn>.");
+ }
} else if (!strncmp(line,"auto",4)) {
if (!strncmp(line,"auto board",10)) {
auto_board = ~auto_board;
diff --git a/src/pptdb.c b/src/pptdb.c
index 92155b5..7179ab0 100644
--- a/src/pptdb.c
+++ b/src/pptdb.c
@@ -17,11 +17,11 @@ write_input(void) {
/* fprintf(training_fh,"%d,",current_colour == C_BLACK); */
// Two numbers for flats remaining
- /* fprintf(training_fh,"%.6f,%.6f,", */
- /* (float)(white_count & 127)/max_flats, */
- /* (float)(black_count & 127)/max_flats); */
- // Two data points for each square
- /* float t; uint16_t mask; */
+ /*
+ * fprintf(training_fh,"%.6f,%.6f,",
+ * (float)(white_count & 127)/max_flats,
+ * (float)(black_count & 127)/max_flats);
+ */
float val;
uint16_t mask = 1;
@@ -48,21 +48,22 @@ write_input(void) {
mask <<= 1;
}
+
/*
- float t;
- for (int k = 0; k < board_size * board_size; k++) {
- // stacks encoded as balanced ternary
- const int h = COUNT_AT(k);
- t = 0;
- mask = 1<<h;
- for (int j = 1; j < h; j++) {
- t += (colours[k] & mask) ? +1 : -1;
- t /= 3.0;
- mask >>=1;
- }
- fprintf(training_fh,"%.8f,",2*t);
- }
- */
+ * float t;
+ * for (int k = 0; k < board_size * board_size; k++) {
+ * // stacks encoded as balanced ternary
+ * const int h = COUNT_AT(k);
+ * t = 0;
+ * mask = 1<<h;
+ * for (int j = 1; j < h; j++) {
+ * t += (colours[k] & mask) ? +1 : -1;
+ * t /= 3.0;
+ * mask >>=1;
+ * }
+ * fprintf(training_fh,"%.8f,",2*t);
+ * }
+ */
}
@@ -141,7 +142,8 @@ main(int argc, char **argv) {
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;
+ uint32_t road_wins=0, flat_wins=0, road_turns=0, flat_turns=0,
+ white_wins = 0, black_wins = 0;
for (int k = 0; k < 16; k++) heights[k] = 0;
@@ -171,10 +173,10 @@ main(int argc, char **argv) {
}
/*
- for (int k = 0; k < size*size; k++) {
- fprintf(training_fh,"\"BT %d\",",k);
- }
- */
+ * for (int k = 0; k < size*size; k++) {
+ * fprintf(training_fh,"\"BT %d\",",k);
+ * }
+ */
fputs("\"White win\",\"Black win\"\n",training_fh);
} else generate=0;
@@ -206,6 +208,10 @@ main(int argc, char **argv) {
road_wins++;
road_turns += ply/2+1;
}
+ if (win == WIN_FLAT_BLACK || win == WIN_ROAD_BLACK)
+ black_wins++;
+ else if (win == WIN_FLAT_WHITE || win == WIN_ROAD_WHITE)
+ white_wins++;
}
}
games++;
@@ -219,9 +225,14 @@ main(int argc, char **argv) {
printf("Read %d games\n",games);
if (generate==0) {
- 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,
+ printf("Illegals: %d\nOverflows: %d\n\
+Black wins: %.3f%%\n\
+Road wins: %d\nFlat wins: %d\n\
+Average turns to road win: %.3f\n\
+Average turns to flat win: %f\n",
+ illegal, overflow,
+ (double)black_wins / (double)(black_wins+white_wins) * 100,
+ road_wins, flat_wins,
(double)(road_turns)/(double)(road_wins),
(double)(flat_turns)/(double)(flat_wins));
for (int k = 2; k < 16; k++) {