ref: 294dd4480614df64edf438b966061584a5826ada
parent: 5b635b6c6c015ed8abff09cda8b051178984a986
author: zamfofex <zamfofex@twdb.moe>
date: Tue Oct 22 06:56:23 EDT 2024
make style changes
--- a/chess.c
+++ b/chess.c
@@ -33,11 +33,9 @@
int i;
unsigned char to;
- while (*deltas)
- {+ while (*deltas) {to = from;
- for (i = 0 ; i < count ; i++)
- {+ for (i = 0 ; i < count ; i++) {to += *deltas * n;
if (chess->board[to] == moonfish_outside) break;
if (chess->board[to] / 16 == chess->board[from] / 16) break;
@@ -48,8 +46,6 @@
}
}
-int moonfish_moves(struct moonfish_chess *chess, struct moonfish_move *moves, unsigned char from);
-
int moonfish_validate(struct moonfish_chess *chess)
{int x, y;
@@ -56,13 +52,13 @@
struct moonfish_move moves[32];
int i, count;
- for (y = 0 ; y < 8 ; y++)
- for (x = 0 ; x < 8 ; x++)
- {- count = moonfish_moves(chess, moves, (x + 1) + (y + 2) * 10);
- for (i = 0 ; i < count ; i++)
- if (chess->board[moves[i].to] % 16 == moonfish_king)
- return 0;
+ for (y = 0 ; y < 8 ; y++) {+ for (x = 0 ; x < 8 ; x++) {+ count = moonfish_moves(chess, moves, (x + 1) + (y + 2) * 10);
+ for (i = 0 ; i < count ; i++) {+ if (chess->board[moves[i].to] % 16 == moonfish_king) return 0;
+ }
+ }
}
return 1;
@@ -103,10 +99,9 @@
if (!chess->ooo[1 - chess->white]) return;
to = from - 3;
- while (to != from)
- if (chess->board[to++] != moonfish_empty)
- return;
-
+ while (to != from) {+ if (chess->board[to++] != moonfish_empty) return;
+ }
if (moonfish_check(chess)) return;
if (moonfish_attacked(chess, from, from - 1)) return;
if (moonfish_attacked(chess, from, from - 2)) return;
@@ -124,10 +119,9 @@
if (!chess->oo[1 - chess->white]) return;
to = from + 2;
- while (to != from)
- if (chess->board[to--] != moonfish_empty)
- return;
-
+ while (to != from) {+ if (chess->board[to--] != moonfish_empty) return;
+ }
if (moonfish_check(chess)) return;
if (moonfish_attacked(chess, from, from + 1)) return;
if (moonfish_attacked(chess, from, from + 2)) return;
@@ -143,8 +137,7 @@
unsigned char color;
color = chess->board[from] & 0xF0;
- if ((color == 0x10 && from < 80) || (color == 0x20 && from > 40))
- {+ if ((color == 0x10 && from < 80) || (color == 0x20 && from > 40)) {moonfish_force_move(chess, moves, from, to);
return;
}
@@ -159,8 +152,8 @@
{int dy;
- if (to == chess->passing)
- {+ if (to == chess->passing) {+
dy = chess->white ? 10 : -10;
moonfish_force_move(chess, moves, from, to);
@@ -168,10 +161,9 @@
return;
}
- if (chess->board[to] / 16 != chess->board[from] / 16)
- if (chess->board[to] != moonfish_empty)
- if (chess->board[to] != moonfish_outside)
+ if (chess->board[to] / 16 != chess->board[from] / 16 && chess->board[to] != moonfish_empty && chess->board[to] != moonfish_outside) {moonfish_pawn_moves(chess, moves, from, to);
+ }
}
static void moonfish_move_pawn(struct moonfish_chess *chess, struct moonfish_move **moves, unsigned char from)
@@ -180,13 +172,11 @@
dy = chess->white ? 10 : -10;
- if (chess->board[from + dy] == moonfish_empty)
- {+ if (chess->board[from + dy] == moonfish_empty) {+
moonfish_pawn_moves(chess, moves, from, from + dy);
- if (chess->white ? from < 40 : from > 80)
- if (chess->board[from + dy * 2] == moonfish_empty)
- {+ if ((chess->white ? from < 40 : from > 80) && chess->board[from + dy * 2] == moonfish_empty) {moonfish_force_move(chess, moves, from, from + dy * 2);
(*moves)[-1].chess.passing = from + dy;
}
@@ -216,22 +206,21 @@
moves0 = moves;
piece = chess->board[from];
- if (chess->white ? piece / 16 == 1 : piece / 16 == 2)
- {+ if (chess->white ? piece / 16 == 1 : piece / 16 == 2) {+
moonfish_deltas(chess, &moves, from, deltas[piece % 16 - 1], steps[piece % 16 - 1], 1);
moonfish_deltas(chess, &moves, from, deltas[piece % 16 - 1], steps[piece % 16 - 1], -1);
if (piece % 16 == moonfish_pawn) moonfish_move_pawn(chess, &moves, from);
- if (piece % 16 == moonfish_king)
- {+ if (piece % 16 == moonfish_king) {+
moonfish_castle_high(chess, &moves, from);
moonfish_castle_low(chess, &moves, from);
count = moves - moves0;
- for (i = 0 ; i < count ; i++)
- {+ for (i = 0 ; i < count ; i++) {moves0[i].chess.oo[1 - chess->white] = 0;
moves0[i].chess.ooo[1 - chess->white] = 0;
}
@@ -253,19 +242,18 @@
chess->ooo[1] = 1;
chess->passing = 0;
- for (y = 0 ; y < 12 ; y++)
- for (x = 0 ; x < 10 ; x++)
- chess->board[x + y * 10] = moonfish_outside;
+ for (y = 0 ; y < 12 ; y++) {+ for (x = 0 ; x < 10 ; x++) {+ chess->board[x + y * 10] = moonfish_outside;
+ }
+ }
- for (x = 0 ; x < 8 ; x++)
- {+ for (x = 0 ; x < 8 ; x++) {chess->board[x + 21] = pieces[x] | 0x10;
chess->board[x + 91] = pieces[x] | 0x20;
chess->board[x + 31] = moonfish_white_pawn;
chess->board[x + 81] = moonfish_black_pawn;
-
- for (y = 4 ; y < 8 ; y++)
- chess->board[(x + 1) + y * 10] = moonfish_empty;
+ for (y = 4 ; y < 8 ; y++) chess->board[(x + 1) + y * 10] = moonfish_empty;
}
}
@@ -288,27 +276,25 @@
x1 = name[2] - 'a';
y1 = name[3] - '1';
- type = chess->board[(x0 + 1) + (y0 + 2) * 10] % 16;
- if (type == moonfish_king && x0 == 4)
- {+ type = moonfish_empty;
+ if (name[4] == 0) type = chess->board[(x0 + 1) + (y0 + 2) * 10] % 16;
+ if (name[4] == 'q') type = moonfish_queen;
+ if (name[4] == 'r') type = moonfish_rook;
+ if (name[4] == 'b') type = moonfish_bishop;
+ if (name[4] == 'n') type = moonfish_knight;
+ if (type == moonfish_empty) return 1;
+
+ if (type == moonfish_king && x0 == 4) {if (x1 == 0) x1 = 2;
if (x1 == 7) x1 = 6;
}
- if (name[4] == 0) { }- else if (name[4] == 'q') type = moonfish_queen;
- else if (name[4] == 'r') type = moonfish_rook;
- else if (name[4] == 'b') type = moonfish_bishop;
- else if (name[4] == 'n') type = moonfish_knight;
- else return 1;
-
from = (x0 + 1) + (y0 + 2) * 10;
to = (x1 + 1) + (y1 + 2) * 10;
count = moonfish_moves(chess, moves, from);
- for (i = 0 ; i < count ; i++)
- {+ for (i = 0 ; i < count ; i++) {if (moves[i].to != to) continue;
if (moves[i].chess.board[to] % 16 != type) continue;
*move = moves[i];
@@ -339,8 +325,7 @@
piece = move->chess.board[move->to];
- if (piece != chess->board[move->from])
- {+ if (piece != chess->board[move->from]) {if (piece % 16 == moonfish_queen) name[4] = 'q';
if (piece % 16 == moonfish_rook) name[4] = 'r';
if (piece % 16 == moonfish_bishop) name[4] = 'b';
@@ -356,10 +341,8 @@
count = moonfish_moves(chess, moves, from);
- for (i = 0 ; i < count ; i++)
- {- if (moves[i].to == to && moonfish_validate(&moves[i].chess))
- {+ for (i = 0 ; i < count ; i++) {+ if (moves[i].to == to && moonfish_validate(&moves[i].chess)) {*found = moves[i];
return 0;
}
@@ -374,10 +357,11 @@
unsigned char type, color;
char ch;
- for (y = 0 ; y < 8 ; y++)
- for (x = 0 ; x < 8 ; x++)
- chess->board[(x + 1) + (y + 2) * 10] = moonfish_empty;
-
+ for (y = 0 ; y < 8 ; y++) {+ for (x = 0 ; x < 8 ; x++) {+ chess->board[(x + 1) + (y + 2) * 10] = moonfish_empty;
+ }
+ }
x = 0;
y = 0;
@@ -388,32 +372,29 @@
chess->ooo[1] = 0;
chess->passing = 0;
- for (;;)
- {+ for (;;) {+
ch = *fen++;
if (ch == 0) return 0;
if (ch == ' ') break;
- if (ch == '/')
- {+ if (ch == '/') {x = 0;
y++;
continue;
}
- if (ch >= '0' && ch <= '9')
- {+ if (ch >= '0' && ch <= '9') {x += ch - '0';
continue;
}
- /* note: assumes ASCII */
- if (ch >= 'A' && ch <= 'Z')
- ch -= 'A' - 'a',
+ color = 0x20;
+ if (ch >= 'A' && ch <= 'Z') {+ ch -= 'A' - 'a';
color = 0x10;
- else
- color = 0x20;
+ }
type = 0;
if (ch == 'p') type = 1;
@@ -431,8 +412,8 @@
if (*fen++ == 'b') chess->white = 0;
if (*fen++ != ' ') return 0;
- for (;;)
- {+ for (;;) {+
ch = *fen++;
if (ch == 0) return 0;
@@ -463,13 +444,13 @@
int x, y;
int i, count;
- for (y = 0 ; y < 8 ; y++)
- for (x = 0 ; x < 8 ; x++)
- {- count = moonfish_moves(chess, moves, (x + 1) + (y + 2) * 10);
- for (i = 0 ; i < count ; i++)
- if (moonfish_validate(&moves[i].chess))
- return 0;
+ for (y = 0 ; y < 8 ; y++) {+ for (x = 0 ; x < 8 ; x++) {+ count = moonfish_moves(chess, moves, (x + 1) + (y + 2) * 10);
+ for (i = 0 ; i < count ; i++) {+ if (moonfish_validate(&moves[i].chess)) return 0;
+ }
+ }
}
return 1;
@@ -503,27 +484,28 @@
found = 0;
- for (y = yi0 ; y < yi1 ; y++)
- for (x = xi0 ; x < xi1 ; x++)
- {- count = moonfish_moves(chess, moves, (x + 1) + (y + 2) * 10);
- for (i = 0 ; i < count ; i++)
- {- if (chess->board[moves[i].from] % 16 != type) continue;
- if (captured && chess->board[moves[i].to] == moonfish_empty) continue;
- if (promotion && promotion != moves[i].chess.board[moves[i].to] % 16) continue;
- if (moves[i].to % 10 != x1) continue;
- if (moves[i].to / 10 - 1 != y1) continue;
+ for (y = yi0 ; y < yi1 ; y++) {+
+ for (x = xi0 ; x < xi1 ; x++) {- if (!moonfish_validate(&moves[i].chess)) continue;
- if (check && !moonfish_check(chess)) continue;
- if (check == 2 && !moonfish_checkmate(chess)) continue;
- if (found) return 1;
- found = 1;
- *move = moves[i];
+ count = moonfish_moves(chess, moves, (x + 1) + (y + 2) * 10);
+ for (i = 0 ; i < count ; i++) {+
+ if (chess->board[moves[i].from] % 16 != type) continue;
+ if (captured && chess->board[moves[i].to] == moonfish_empty) continue;
+ if (promotion && promotion != moves[i].chess.board[moves[i].to] % 16) continue;
+ if (moves[i].to % 10 != x1) continue;
+ if (moves[i].to / 10 - 1 != y1) continue;
+
+ if (!moonfish_validate(&moves[i].chess)) continue;
+ if (check && !moonfish_check(chess)) continue;
+ if (check == 2 && !moonfish_checkmate(chess)) continue;
+ if (found) return 1;
+ found = 1;
+ *move = moves[i];
+ }
}
}
-
if (!found) return 1;
return 0;
}
@@ -545,8 +527,7 @@
name = name_array;
/* reverse the string (because it is easier to parse) */
- for (i = length / 2 ; i < length ; i++)
- {+ for (i = length / 2 ; i < length ; i++) {ch = name[i];
name[i] = name[length - i - 1];
name[length - i - 1] = ch;
@@ -553,30 +534,42 @@
}
check = 0;
- if (*name == '+') check = 2;
- else if (*name == '+') check = 1;
+ if (*name == '+') check = 1;
+ if (*name == '#') check = 2;
if (check) name++;
count = 0;
- for (name0 = name ; *name0 != 0 ; name0++)
- {+ for (name0 = name ; *name0 != 0 ; name0++) {if (*name0 == '-') continue;
if (*name0 == '_') continue;
- if (*name0 == '0') { count++; continue; }- if (*name0 == 'O') { count++; continue; }- if (*name0 == 'o') { count++; continue; }+ if (*name0 == '0') {+ count++;
+ continue;
+ }
+ if (*name0 == 'O') {+ count++;
+ continue;
+ }
+ if (*name0 == 'o') {+ count++;
+ continue;
+ }
count = 0;
break;
}
- if (count > 0)
- {+ if (count > 0) {+
if (chess->white) y1 = 1;
else y1 = 8;
- if (count == 2) x1 = 7;
- else if (count == 3) x1 = 3;
- else return 1;
+ if (count == 2) {+ x1 = 7;
+ }
+ else {+ if (count == 3) x1 = 3;
+ else return 1;
+ }
return moonfish_match_move(chess, move, moonfish_king, 0, 5, y1, x1, y1, check, 0);
}
@@ -587,30 +580,34 @@
y1 = 0;
check = 0;
- switch (*name++)
- {+ switch (*name++) {default:
promotion = 0;
name--;
break;
- case 'K': case 'k':
+ case 'K':
+ case 'k':
promotion = moonfish_king;
break;
- case 'Q': case 'q':
+ case 'Q':
+ case 'q':
promotion = moonfish_queen;
break;
- case 'R': case 'r':
+ case 'R':
+ case 'r':
promotion = moonfish_rook;
break;
- case 'B': case 'b':
+ case 'B':
+ case 'b':
promotion = moonfish_bishop;
break;
- case 'N': case 'n':
+ case 'N':
+ case 'n':
promotion = moonfish_knight;
break;
}
- if (promotion != 0 && *name == '=') name++;
+ if (promotion != moonfish_empty && *name == '=') name++;
capture = 0;
if (*name >= '1' && *name <= '8') y1 = *name++ - '0';
@@ -622,8 +619,7 @@
if (x1 == 0) return 1;
if (y1 == 0) return 1;
- switch (*name++)
- {+ switch (*name++) {default:
type = moonfish_pawn;
if (x0 && y0) type = chess->board[x0 + (y0 + 1) * 10] % 16;
@@ -630,19 +626,23 @@
if (type == 0x0F) return 1;
name--;
break;
- case 'K': case 'k':
+ case 'K':
+ case 'k':
type = moonfish_king;
break;
- case 'Q': case 'q':
+ case 'Q':
+ case 'q':
type = moonfish_queen;
break;
- case 'R': case 'r':
+ case 'R':
+ case 'r':
type = moonfish_rook;
break;
- case 'B': /* no lowercase here */
+ case 'B':
type = moonfish_bishop;
break;
- case 'N': case 'n':
+ case 'N':
+ case 'n':
type = moonfish_knight;
break;
}
@@ -658,26 +658,24 @@
unsigned char piece;
int count;
- for (y = 7 ; y >= 0 ; y--)
- {+ for (y = 7 ; y >= 0 ; y--) {+
count = 0;
- for (x = 0 ; x < 8 ; x++)
- {+
+ for (x = 0 ; x < 8 ; x++) {+
piece = chess->board[(x + 1) + (y + 2) * 10];
- if (piece == moonfish_empty)
- {+ if (piece == moonfish_empty) {count++;
continue;
}
- if (count != 0)
- {+ if (count != 0) {*fen++ = '0' + count;
count = 0;
}
- switch (piece % 16)
- {+ switch (piece % 16) {default:
return;
case moonfish_pawn:
@@ -723,8 +721,7 @@
if (fen[-1] == ' ') *fen++ = '-';
*fen++ = ' ';
- if (chess->passing)
- {+ if (chess->passing) {*fen++ = 'a' + chess->passing % 10 - 1;
*fen++ = '1' + chess->passing / 10 - 2;
}
@@ -750,24 +747,20 @@
to_x = move->to % 10 - 1;
to_y = move->to / 10 - 2;
- if (chess->board[move->from] % 16 == moonfish_king && from_x == 4)
- {- if (to_x == 2)
- {+ if (chess->board[move->from] % 16 == moonfish_king && from_x == 4) {+ if (to_x == 2) {strcpy(name, "O-O-O");
return;
}
- if (to_x == 6)
- {+ if (to_x == 6) {strcpy(name, "O-O");
return;
}
}
- if (chess->board[move->from] % 16 == moonfish_pawn)
- {- if (from_x != to_x)
- {+ if (chess->board[move->from] % 16 == moonfish_pawn) {+
+ if (from_x != to_x) {*name++ = from_x + 'a';
*name++ = 'x';
}
@@ -775,8 +768,7 @@
*name++ = to_x + 'a';
*name++ = to_y + '1';
- if (move->chess.board[move->to] % 16 != moonfish_pawn)
- {+ if (move->chess.board[move->to] % 16 != moonfish_pawn) {*name++ = '=';
*name++ = names[move->chess.board[move->to] % 16 - 2];
}
@@ -790,42 +782,38 @@
rank_ambiguity = 0;
ambiguity = 0;
- for (y = 0 ; y < 8 ; y++)
- for (x = 0 ; x < 8 ; x++)
- {- if ((x + 1) + (y + 2) * 10 == move->from) continue;
- count = moonfish_moves(chess, moves, (x + 1) + (y + 2) * 10);
+ for (y = 0 ; y < 8 ; y++) {- for (i = 0 ; i < count ; i++)
- {- if (moves[i].to != move->to) continue;
- if (chess->board[moves[i].from] != chess->board[move->from]) continue;
+ for (x = 0 ; x < 8 ; x++) {- ambiguity = 1;
- if (moves[i].from % 10 - 1 == from_x) file_ambiguity = 1;
- if (moves[i].from / 10 - 2 == from_y) rank_ambiguity = 1;
+ if ((x + 1) + (y + 2) * 10 == move->from) continue;
+ count = moonfish_moves(chess, moves, (x + 1) + (y + 2) * 10);
+
+ for (i = 0 ; i < count ; i++) {+
+ if (moves[i].to != move->to) continue;
+ if (chess->board[moves[i].from] != chess->board[move->from]) continue;
+
+ ambiguity = 1;
+ if (moves[i].from % 10 - 1 == from_x) file_ambiguity = 1;
+ if (moves[i].from / 10 - 2 == from_y) rank_ambiguity = 1;
+ }
}
}
-
*name++ = names[(chess->board[move->from] & 0xF) - 2];
- if (ambiguity)
- {- if (file_ambiguity)
- {- if (rank_ambiguity)
- *name++ = from_x + 'a';
+ if (ambiguity) {+ if (file_ambiguity) {+ if (rank_ambiguity) *name++ = from_x + 'a';
*name++ = from_y + '1';
}
- else
- {+ else {*name++ = from_x + 'a';
}
}
- if (chess->board[move->to] != moonfish_empty)
- *name++ = 'x';
-
+ if (chess->board[move->to] != moonfish_empty) *name++ = 'x';
+
*name++ = to_x + 'a';
*name++ = to_y + '1';
--- a/main.c
+++ b/main.c
@@ -12,7 +12,7 @@
{static char line[2048];
- char *arg;
+ char *arg, *arg0;
struct moonfish_move move;
char name[6];
long int our_time, their_time, *xtime;
@@ -22,8 +22,7 @@
char *end;
long int time;
- if (argc > 1)
- {+ if (argc > 1) {fprintf(stderr, "usage: %s\n", argv[0]);
return 1;
}
@@ -30,17 +29,16 @@
moonfish_chess(&chess);
- for (;;)
- {+ for (;;) {+
+ fflush(stdout);
+
errno = 0;
- if (fgets(line, sizeof line, stdin) == NULL)
- {- if (errno)
- {+ if (fgets(line, sizeof line, stdin) == NULL) {+ if (errno) {perror(argv[0]);
return 1;
}
-
break;
}
@@ -47,34 +45,31 @@
arg = strtok(line, "\r\n\t ");
if (arg == NULL) continue;
- if (!strcmp(arg, "go"))
- {+ if (!strcmp(arg, "go")) {+
our_time = -1;
their_time = -1;
depth = -1;
time = -1;
- for (;;)
- {- arg = strtok(NULL, "\r\n\t ");
- if (arg == NULL) break;
+ for (;;) {- if (!strcmp(arg, "wtime") || !strcmp(arg, "btime"))
- {- if (chess.white)
- {- if (!strcmp(arg, "wtime")) xtime = &our_time;
+ arg0 = strtok(NULL, "\r\n\t ");
+ if (arg0 == NULL) break;
+
+ if (!strcmp(arg0, "wtime") || !strcmp(arg0, "btime")) {+
+ if (chess.white) {+ if (!strcmp(arg0, "wtime")) xtime = &our_time;
else xtime = &their_time;
}
- else
- {- if (!strcmp(arg, "wtime")) xtime = &their_time;
+ else {+ if (!strcmp(arg0, "wtime")) xtime = &their_time;
else xtime = &our_time;
}
arg = strtok(NULL, "\r\n\t ");
- if (arg == NULL)
- {+ if (arg == NULL) {fprintf(stderr, "%s: malformed 'go' command\n", argv[0]);
return 1;
}
@@ -81,18 +76,17 @@
errno = 0;
*xtime = strtol(arg, &end, 10);
- if (errno != 0 || *end != 0 || *xtime < 0)
- {+ if (errno || *end != 0 || *xtime < 0) {fprintf(stderr, "%s: malformed time in 'go' command\n", argv[0]);
return 1;
}
}
- else if (!strcmp(arg, "depth"))
- {+
+ if (!strcmp(arg0, "depth")) {+
arg = strtok(NULL, "\r\n\t ");
- if (arg == NULL)
- {+ if (arg == NULL) {fprintf(stderr, "%s: malformed 'go depth' command\n", argv[0]);
return 1;
}
@@ -99,23 +93,21 @@
errno = 0;
depth = strtol(arg, &end, 10);
- if (errno != 0)
- {+ if (errno) {perror(argv[0]);
return 1;
}
- if (*end != 0 || depth < 0 || depth > 1000)
- {+ if (*end != 0 || depth < 0 || depth > 1000) {fprintf(stderr, "%s: malformed depth in 'go' command\n", argv[0]);
return 1;
}
}
- else if (!strcmp(arg, "movetime"))
- {+
+ if (!strcmp(arg0, "movetime")) {+
arg = strtok(NULL, "\r\n\t ");
- if (arg == NULL)
- {+ if (arg == NULL) {fprintf(stderr, "%s: malformed 'go movetime' command\n", argv[0]);
return 1;
}
@@ -122,32 +114,27 @@
errno = 0;
time = strtol(arg, &end, 10);
- if (errno != 0)
- {+ if (errno) {perror(argv[0]);
return 1;
}
- if (*end != 0 || time < 0)
- {+ if (*end != 0 || time < 0) {fprintf(stderr, "%s: malformed move time in 'go' command\n", argv[0]);
return 1;
}
}
- else
- {- fprintf(stderr, "%s: warning: unexpected '%s' in 'go' command\n", argv[0], arg);
- }
}
if (our_time < 0) our_time = 0;
if (their_time < 0) their_time = 0;
- if (depth >= 0)
+ if (depth >= 0) {score = moonfish_best_move_depth(&chess, &move, depth);
- else if (time >= 0)
- score = moonfish_best_move_time(&chess, &move, time);
- else
- score = moonfish_best_move_clock(&chess, &move, our_time, their_time);
+ }
+ else {+ if (time >= 0) score = moonfish_best_move_time(&chess, &move, time);
+ else score = moonfish_best_move_clock(&chess, &move, our_time, their_time);
+ }
if (depth < 0) depth = 1;
printf("info depth %d score cp %ld\n", depth, score);@@ -154,29 +141,23 @@
moonfish_to_uci(&chess, &move, name);
printf("bestmove %s\n", name);+ continue;
}
- else if (!strcmp(arg, "quit"))
- {- break;
- }
- else if (!strcmp(arg, "position"))
- {+
+ if (!strcmp(arg, "quit")) break;
+
+ if (!strcmp(arg, "position")) {+
arg = strtok(NULL, "\r\n\t ");
- if (arg == NULL)
- {+ if (arg == NULL) {fprintf(stderr, "incomplete 'position' command\n");
return 1;
}
- if (!strcmp(arg, "startpos"))
- {- moonfish_chess(&chess);
- }
- else if (!strcmp(arg, "fen"))
- {+ if (!strcmp(arg, "fen")) {+
arg = strtok(NULL, "\r\n");
- if (arg == NULL)
- {+ if (arg == NULL) {fprintf(stderr, "incomplete 'position fen' command\n");
return 1;
}
@@ -183,31 +164,30 @@
moonfish_from_fen(&chess, arg);
arg = strstr(arg, "moves");
- if (arg != NULL)
- {+ if (arg != NULL) {do arg--;
- while (*arg == '\t' || *arg == ' ');
+ while (*arg == '\t' || *arg == ' ') ;
strcpy(line, arg);
strtok(line, "\r\n\t ");
}
- else
- {+ else { strtok("", "\r\n\t ");}
}
- else
- {- fprintf(stderr, "malformed 'position' command\n");
- return 1;
+ else {+ if (!strcmp(arg, "startpos")) {+ moonfish_chess(&chess);
+ }
+ else {+ fprintf(stderr, "malformed 'position' command\n");
+ return 1;
+ }
}
arg = strtok(NULL, "\r\n\t ");
- if (arg != NULL && !strcmp(arg, "moves"))
- {- while ((arg = strtok(NULL, "\r\n\t ")) != NULL)
- {- if (moonfish_from_uci(&chess, &move, arg))
- {+ if (arg != NULL && !strcmp(arg, "moves")) {+ while ((arg = strtok(NULL, "\r\n\t ")) != NULL) {+ if (moonfish_from_uci(&chess, &move, arg)) {fprintf(stderr, "%s: invalid move '%s'\n", argv[0], arg);
return 1;
}
@@ -214,26 +194,24 @@
chess = move.chess;
}
}
+
+ continue;
}
- else if (!strcmp(arg, "uci"))
- {+ if (!strcmp(arg, "uci")) { printf("id name moonfish\n"); printf("id author zamfofex\n"); printf("uciok\n");+ continue;
}
- else if (!strcmp(arg, "isready"))
- {+
+ if (!strcmp(arg, "isready")) { printf("readyok\n");+ continue;
}
- else if (!strcmp(arg, "debug") || !strcmp(arg, "setoption") || !strcmp(arg, "ucinewgame") || !strcmp(arg, "stop"))
- {- }
- else
- {- fprintf(stderr, "%s: unknown command '%s'\n", argv[0], arg);
- }
- fflush(stdout);
+ if (!strcmp(arg, "debug") || !strcmp(arg, "setoption") || !strcmp(arg, "ucinewgame") || !strcmp(arg, "stop")) continue;
+
+ fprintf(stderr, "%s: unknown command '%s'\n", argv[0], arg);
}
return 0;
--- a/moonfish.h
+++ b/moonfish.h
@@ -52,6 +52,8 @@
/* the board is not just an 8 x 8 array because of an optisation that can be performed when generating moves */
+/* ~ ~ ~ ~ ~ */
+
/* white pieces */
#define moonfish_white_pawn 0x11
#define moonfish_white_knight 0x12
@@ -88,8 +90,8 @@
#endif
/* represents a chess position */
-struct moonfish_chess
-{+struct moonfish_chess {+
/* 10 x 12 array board representation */
unsigned char board[120];
@@ -107,8 +109,8 @@
};
/* represents a move that can be made on a given position */
-struct moonfish_move
-{+struct moonfish_move {+
/* the position after the move is played */
struct moonfish_chess chess;
--- a/search.c
+++ b/search.c
@@ -51,8 +51,7 @@
#define moonfish_omega 0x2000
-struct moonfish_thread
-{+struct moonfish_thread {thrd_t thread;
struct moonfish_analysis *analysis;
struct moonfish_move move;
@@ -59,8 +58,7 @@
int score;
};
-struct moonfish_analysis
-{+struct moonfish_analysis {struct moonfish_chess chess;
struct moonfish_thread threads[256];
int score;
@@ -81,8 +79,7 @@
{struct timespec ts;
- if (clock_gettime(CLOCK_MONOTONIC, &ts))
- {+ if (clock_gettime(CLOCK_MONOTONIC, &ts)) {perror(NULL);
exit(1);
}
@@ -104,19 +101,18 @@
score = 0;
- for (y = 0 ; y < 8 ; y++)
- for (x = 0 ; x < 8 ; x++)
- {- from = (x + 1) + (y + 2) * 10;
- type = chess->board[from] % 16;
- color = chess->board[from] / 16 - 1;
-
- if (chess->board[from] == moonfish_empty) continue;
- x1 = x;
- y1 = y;
- if (x1 > 3) x1 = 7 - x1;
- if (color == 1) y1 = 7 - y1;
- score -= moonfish_values[x1 + y1 * 4 + (type - 1) * 32] * (color * 2 - 1);
+ for (y = 0 ; y < 8 ; y++) {+ for (x = 0 ; x < 8 ; x++) {+ from = (x + 1) + (y + 2) * 10;
+ type = chess->board[from] % 16;
+ color = chess->board[from] / 16 - 1;
+ if (chess->board[from] == moonfish_empty) continue;
+ x1 = x;
+ y1 = y;
+ if (x1 > 3) x1 = 7 - x1;
+ if (color == 1) y1 = 7 - y1;
+ score -= moonfish_values[x1 + y1 * 4 + (type - 1) * 32] * (color * 2 - 1);
+ }
}
return score;
@@ -131,8 +127,7 @@
long int t1, c;
struct moonfish_move moves[32];
- if (depth < 0)
- {+ if (depth < 0) {score = moonfish_score(chess);
if (!chess->white) score *= -1;
if (score >= beta) return beta;
@@ -139,31 +134,34 @@
if (score < alpha - 100) return alpha;
if (score > alpha) alpha = score;
}
- else if (thread->analysis->time >= 0 && time < 5)
- {- depth = 0;
+ else {+ if (thread->analysis->time >= 0 && time < 5) {+ depth = 0;
+ }
}
- for (y = 0 ; y < 8 ; y++)
- for (x = 0 ; x < 8 ; x++)
- {- count = moonfish_moves(chess, moves, (x + 1) + (y + 2) * 10);
- for (i = 0 ; i < count ; i++)
- {- if (!moonfish_validate(&moves[i].chess)) continue;
+ for (y = 0 ; y < 8 ; y++) {+
+ for (x = 0 ; x < 8 ; x++) {- if (depth < 0)
- if (chess->board[moves[i].to] == moonfish_empty)
- if (moves[i].chess.board[moves[i].to] == chess->board[moves[i].from])
- continue;
-
- t1 = moonfish_clock();
- c = 2 * time * i / count - t1 + t0;
-
- score = -moonfish_search(thread, &moves[i].chess, -beta, -alpha, depth - 1, t1, time / count + c);
-
- if (score >= beta) return beta;
- if (score > alpha) alpha = score;
+ count = moonfish_moves(chess, moves, (x + 1) + (y + 2) * 10);
+ for (i = 0 ; i < count ; i++) {+
+ if (!moonfish_validate(&moves[i].chess)) continue;
+
+ if (depth < 0) {+ if (chess->board[moves[i].to] == moonfish_empty) {+ if (moves[i].chess.board[moves[i].to] == chess->board[moves[i].from]) continue;
+ }
+ }
+ t1 = moonfish_clock();
+ c = 2 * time * i / count - t1 + t0;
+
+ score = -moonfish_search(thread, &moves[i].chess, -beta, -alpha, depth - 1, t1, time / count + c);
+
+ if (score >= beta) return beta;
+ if (score > alpha) alpha = score;
+ }
}
}
@@ -190,64 +188,64 @@
int x, y;
struct moonfish_move moves[32];
int i, j, count;
+
#ifdef moonfish_no_threads
+
int total;
int invalid_count;
- if (analysis->time >= 0)
- {+ if (analysis->time >= 0) {+
total = 0;
- for (y = 0 ; y < 8 ; y++)
- for (x = 0 ; x < 8 ; x++)
- {- invalid_count = 0;
- count = moonfish_moves(&analysis->chess, moves, (x + 1) + (y + 2) * 10);
- for (i = 0 ; i < count ; i++)
- if (!moonfish_validate(&moves[i].chess))
- invalid_count++;
- total += count - invalid_count;
+ for (y = 0 ; y < 8 ; y++) {+ for (x = 0 ; x < 8 ; x++) {+ invalid_count = 0;
+ count = moonfish_moves(&analysis->chess, moves, (x + 1) + (y + 2) * 10);
+ for (i = 0 ; i < count ; i++) {+ if (!moonfish_validate(&moves[i].chess)) invalid_count++;
+ }
+ total += count - invalid_count;
+ }
}
-
analysis->time /= total;
}
+
#endif
j = 0;
- for (y = 0 ; y < 8 ; y++)
- for (x = 0 ; x < 8 ; x++)
- {- count = moonfish_moves(&analysis->chess, moves, (x + 1) + (y + 2) * 10);
- for (i = 0 ; i < count ; i++)
- {- if (!moonfish_validate(&moves[i].chess)) continue;
+ for (y = 0 ; y < 8 ; y++) {+
+ for (x = 0 ; x < 8 ; x++) {- analysis->threads[j].analysis = analysis;
- analysis->threads[j].move = moves[i];
-
- if (thrd_create(&analysis->threads[j].thread, &moonfish_start_search, analysis->threads + j) != thrd_success)
- {- fprintf(stderr, "error creating thread\n");
- exit(1);
+ count = moonfish_moves(&analysis->chess, moves, (x + 1) + (y + 2) * 10);
+ for (i = 0 ; i < count ; i++) {+
+ if (!moonfish_validate(&moves[i].chess)) continue;
+
+ analysis->threads[j].analysis = analysis;
+ analysis->threads[j].move = moves[i];
+
+ if (thrd_create(&analysis->threads[j].thread, &moonfish_start_search, analysis->threads + j) != thrd_success) {+ fprintf(stderr, "error creating thread\n");
+ exit(1);
+ }
+
+ j++;
}
-
- j++;
}
}
-
analysis->score = -2 * moonfish_omega;
- for (i = 0 ; i < j ; i++)
- {- if (thrd_join(analysis->threads[i].thread, NULL) != thrd_success)
- {+ for (i = 0 ; i < j ; i++) {+
+ if (thrd_join(analysis->threads[i].thread, NULL) != thrd_success) {fprintf(stderr, "error joining thread\n");
exit(1);
}
- if (analysis->threads[i].score > analysis->score)
- {+ if (analysis->threads[i].score > analysis->score) {*best_move = analysis->threads[i].move;
analysis->score = analysis->threads[i].score;
}
--- a/tools/analyse.c
+++ b/tools/analyse.c
@@ -11,8 +11,7 @@
#include "../moonfish.h"
#include "tools.h"
-struct moonfish_ply
-{+struct moonfish_ply {struct moonfish_chess chess;
char name[6];
char best[6];
@@ -26,8 +25,7 @@
struct moonfish_ply *main;
};
-struct moonfish_fancy
-{+struct moonfish_fancy {struct moonfish_ply plies[256];
int i, count;
pthread_mutex_t *mutex;
@@ -47,30 +45,26 @@
{unsigned char piece;
- if (x + 1 == fancy->x && y + 1 == fancy->y)
+ if (x + 1 == fancy->x && y + 1 == fancy->y) { printf("\x1B[48;5;219m");- else if (x % 2 == y % 2)
- printf("\x1B[48;5;111m");- else
- printf("\x1B[48;5;69m");-
+ }
+ else {+ if (x % 2 == y % 2) printf("\x1B[48;5;111m");+ else printf("\x1B[48;5;69m");+ }
y = 7 - y;
piece = fancy->plies[fancy->i].chess.board[(x + 1) + (y + 2) * 10];
- if (piece == moonfish_empty)
- {+ if (piece == moonfish_empty) { printf(" ");return;
}
- if (piece / 16 == 1)
- printf("\x1B[38;5;253m");- else
- printf("\x1B[38;5;240m");+ if (piece / 16 == 1) printf("\x1B[38;5;253m");+ else printf("\x1B[38;5;240m");- switch (piece % 16)
- {+ switch (piece % 16) {case 1:
printf("\xE2\x99\x9F ");return;
@@ -101,8 +95,7 @@
ply = fancy->plies + fancy->i;
- if (ply->checkmate != 0)
- {+ if (ply->checkmate != 0) {score = 1;
white = 0;
black = 0;
@@ -109,13 +102,11 @@
if (ply->checkmate > 0) white = 1;
else black = 1;
}
- else
- {+ else {white = ply->white;
black = ply->black;
score = white + black + ply->draw;
- if (score == 0)
- {+ if (score == 0) {white = 1;
black = 1;
score = 2;
@@ -131,10 +122,8 @@
white /= score;
black /= score;
- if (ply->draw == 0)
- {- while (16 - white - black > 0)
- {+ if (ply->draw == 0) {+ while (16 - white - black > 0) {if (white_mod > black_mod) white++;
else black++;
}
@@ -142,40 +131,33 @@
draw = 16 - white - black;
- while (black > 1)
- {+ while (black > 1) { printf("\x1B[48;5;240m \x1B[0m\x1B[B\x08");black -= 2;
}
- if (black)
- {- if (draw)
- {+ if (black) {+ if (draw) { printf("\x1B[38;5;67m\x1B[48;5;240m\xE2\x96\x84\x1B[0m\x1B[B\x08");draw--;
}
- else
- {+ else { printf("\x1B[38;5;253m\x1B[48;5;240m\xE2\x96\x84\x1B[0m\x1B[B\x08");white--;
}
}
- while (draw > 1)
- {+ while (draw > 1) { printf("\x1B[48;5;67m \x1B[0m\x1B[B\x08");draw -= 2;
}
- if (draw)
- {+ if (draw) { printf("\x1B[38;5;253m\x1B[48;5;67m\xE2\x96\x84\x1B[0m\x1B[B\x08");black--;
}
- while (white > 1)
- {+ while (white > 1) { printf("\x1B[48;5;253m \x1B[0m\x1B[B\x08");white -= 2;
}
@@ -185,54 +167,65 @@
{struct moonfish_ply *ply;
int score;
+ int length;
ply = fancy->plies + i;
+ length = strlen(ply->san);
- if (i >= fancy->count || ply->name[0] == 0)
- {- printf("%10s", "");+ if (i >= fancy->count || ply->name[0] == 0) {+ printf("%12s", "");return;
}
if (i == fancy->i) printf("\x1B[48;5;248m\x1B[38;5;235m");- if (fancy->ephemeral != 0 && ply->ephemeral != 0)
- {+ if (fancy->ephemeral && ply->ephemeral) { printf("\x1B[38;5;240m(%s)", ply->san);- printf("%*s\x1B[0m", 8 - (int) strlen(ply->san), "");+ printf("%*s\x1B[0m", 10 - length, "");return;
}
printf(" %s", ply->san);- if (moonfish_finished(&ply->chess))
- {+ if (moonfish_finished(&ply->chess)) { if (moonfish_checkmate(&ply->chess)) printf("\x1B[38;5;160m# "); else printf("\x1B[38;5;111m(0)");+ printf("%*s\x1B[0m", 8 - length, "");+ return;
}
- else if (ply->checkmate)
- {+
+ if (ply->checkmate) {+
printf("\x1B[38;5;162m#");+ if (ply->checkmate > -10 && ply->checkmate < 10) {+ printf("#%+d", ply->checkmate);+ }
+ else {+ if (ply->checkmate > 0) printf("#+X");+ else printf("#-X");+ }
- if (ply->checkmate > -10 && ply->checkmate < 10)
- printf("%+d", ply->checkmate);- else
- printf("X");+ printf("%*s\x1B[0m", 8 - length, "");+ return;
}
- else if (i > 0)
- {- if (ply[-1].ephemeral != 0) score = ply->score + ply[-1].score;
- else score = ply->score + ply[-1].main->score;
- if (ply[-1].main->checkmate != 0 || score > 200) printf("\x1B[38;5;124m?? ");- else if (score > 100) printf("\x1B[38;5;173m? ");- else printf(" ");+
+ if (i <= 0) {+ printf("%*s", 11 - length, "");+ return;
}
- else
- {- printf(" ");+
+ if (ply[-1].ephemeral) score = ply->score + ply[-1].score;
+ else score = ply->score + ply[-1].main->score;
+
+ if (ply[-1].main->checkmate != 0 || score > 200) {+ printf("\x1B[38;5;124m?? ");}
+ else {+ if (score > 100) printf("\x1B[38;5;173m? ");+ else printf(" ");+ }
- printf("%*s\x1B[0m", 6 - (int) strlen(ply->san), "");+ printf("%*s\x1B[0m", 8 - length, "");}
static void moonfish_scoresheet(struct moonfish_fancy *fancy)
@@ -240,10 +233,9 @@
int i, j;
i = fancy->offset * 2;
- if (fancy->plies[0].chess.white != 0) i++;
+ if (fancy->plies[0].chess.white) i++;
- for (j = 0 ; j < 6 ; j++)
- {+ for (j = 0 ; j < 6 ; j++) { printf("\x1B[23G");moonfish_scoresheet_move(fancy, i);
moonfish_scoresheet_move(fancy, i + 1);
@@ -257,7 +249,7 @@
struct moonfish_ply *ply;
int score;
- if (fancy->idle == 0) printf("\x1B[38;5;103m");+ if (!fancy->idle) printf("\x1B[38;5;103m"); else printf("\x1B[38;5;111m"); printf("(+)\x1B[0m ");@@ -266,19 +258,26 @@
if (!ply->chess.white) score *= -1;
- if (moonfish_checkmate(&ply->chess))
- printf("\x1B[38;5;160m#\x1B[0m");- else if (moonfish_finished(&ply->chess))
- printf("0");- else if (ply->checkmate != 0)
- printf("#%+d", ply->checkmate);- else if (score < 0)
- printf("-%d.%02d", -score / 100, -score % 100);- else
- printf("%d.%02d", score / 100, score % 100);+ if (moonfish_finished(&ply->chess)) {+ if (moonfish_checkmate(&ply->chess)) {+ printf("\x1B[38;5;160m#\x1B[0m");+ }
+ else {+ printf("0");+ }
+ }
+ else {+ if (ply->checkmate != 0) {+ printf("#%+d", ply->checkmate);+ }
+ else {+ if (score < 0) printf("-%d.%02d", -score / 100, -score % 100);+ else printf("%d.%02d", score / 100, score % 100);+ }
+ }
printf(" (depth %d)", ply->depth);- if (fancy->idle == 0) printf(" in %d.%ds of %ds", fancy->taken / 1000, fancy->taken % 1000 / 100, fancy->time / 1000);+ if (!fancy->idle) printf(" in %d.%ds of %ds", fancy->taken / 1000, fancy->taken % 1000 / 100, fancy->time / 1000); else printf(" in %ds", fancy->time / 1000); printf("\x1B[0K");}
@@ -289,8 +288,7 @@
printf("\x1B[%dH", fancy->oy);- for (y = 0 ; y < 8 ; y++)
- {+ for (y = 0 ; y < 8 ; y++) { printf(" ");for (x = 0 ; x < 8 ; x++) moonfish_fancy_square(fancy, x, y);
printf("\x1B[0m\n");@@ -332,9 +330,9 @@
changed = 0;
- for (;;)
- {- if (changed != 0) moonfish_fancy(fancy);
+ for (;;) {+
+ if (changed) moonfish_fancy(fancy);
changed = 0;
pthread_mutex_unlock(fancy->mutex);
@@ -344,15 +342,14 @@
arg = strtok_r(line, "\r\n\t ", &buffer);
if (arg == NULL) continue;
- if (!strcmp(arg, "bestmove"))
- {- if (fancy->stop == 0) fancy->idle = 1;
+ if (!strcmp(arg, "bestmove")) {+ if (!fancy->stop) fancy->idle = 1;
fancy->stop = 0;
changed = 1;
continue;
}
- if (fancy->stop != 0) continue;
+ if (fancy->stop) continue;
if (strcmp(arg, "info")) continue;
ply = fancy->plies[fancy->i];
@@ -360,22 +357,20 @@
pv = 0;
- for (;;)
- {+ for (;;) {+
arg = strtok_r(NULL, "\r\n\t ", &buffer);
if (arg == NULL) break;
- if (!strcmp(arg, "lowerbound") || !strcmp(arg, "upperbound"))
- {+ if (!strcmp(arg, "lowerbound") || !strcmp(arg, "upperbound")) {changed = 0;
break;
}
- if (!strcmp(arg, "depth"))
- {+ if (!strcmp(arg, "depth")) {+
arg = strtok_r(NULL, "\r\n\t ", &buffer);
- if (arg == NULL || moonfish_int(arg, &ply.depth) != 0 || ply.depth < 0)
- {+ if (arg == NULL || moonfish_int(arg, &ply.depth) || ply.depth < 0) {fprintf(stderr, "%s: malformed 'depth' in 'info' command\n", fancy->argv0);
exit(1);
}
@@ -383,11 +378,10 @@
continue;
}
- if (!strcmp(arg, "multipv"))
- {+ if (!strcmp(arg, "multipv")) {+
arg = strtok_r(NULL, "\r\n\t ", &buffer);
- if (arg == NULL || moonfish_int(arg, &pv) != 0 || pv <= 0)
- {+ if (arg == NULL || moonfish_int(arg, &pv) || pv <= 0) {fprintf(stderr, "%s: malformed 'multipv' in 'info' command\n", fancy->argv0);
exit(1);
}
@@ -395,28 +389,24 @@
continue;
}
- if (!strcmp(arg, "pv"))
- {+ if (!strcmp(arg, "pv")) {+
changed = 1;
if (pv < 1) pv = 1;
i = 0;
- while (i < sizeof fancy->pv - 1)
- {+ while (i < sizeof fancy->pv - 1) {arg = strtok_r(NULL, "\r\n\t ", &buffer);
if (arg == NULL) break;
- if (moonfish_from_uci(&ply.chess, &move, arg) != 0)
- {+ if (moonfish_from_uci(&ply.chess, &move, arg)) {fprintf(stderr, "%s: invalid move: %s\n", fancy->argv0, arg);
exit(1);
}
- if (i == 0 && pv == 1)
- {+ if (i == 0 && pv == 1) {strcpy(ply.best, arg);
}
- if (pv > 1)
- {+ if (pv > 1) {ply.chess = move.chess;
i = 1;
continue;
@@ -434,25 +424,22 @@
continue;
}
- if (!strcmp(arg, "wdl"))
- {+ if (!strcmp(arg, "wdl")) {+
changed = 1;
arg = strtok_r(NULL, "\r\n\t ", &buffer);
- if (arg == NULL || moonfish_int(arg, &ply.white) != 0 || ply.white < 0)
- {+ if (arg == NULL || moonfish_int(arg, &ply.white) || ply.white < 0) {fprintf(stderr, "%s: malformed 'wdl' win in 'info' command\n", fancy->argv0);
exit(1);
}
arg = strtok_r(NULL, "\r\n\t ", &buffer);
- if (arg == NULL || moonfish_int(arg, &ply.draw) != 0 || ply.draw < 0)
- {+ if (arg == NULL || moonfish_int(arg, &ply.draw) || ply.draw < 0) {fprintf(stderr, "%s: malformed 'wdl' draw in 'info' command\n", fancy->argv0);
exit(1);
}
arg = strtok_r(NULL, "\r\n\t ", &buffer);
- if (arg == NULL || moonfish_int(arg, &ply.black) != 0 || ply.black < 0)
- {+ if (arg == NULL || moonfish_int(arg, &ply.black) || ply.black < 0) {fprintf(stderr, "%s: malformed 'wdl' loss in 'info' command\n", fancy->argv0);
exit(1);
}
@@ -459,8 +446,7 @@
ply.checkmate = 0;
- if (!ply.chess.white)
- {+ if (!ply.chess.white) {score = ply.white;
ply.white = ply.black;
ply.black = score;
@@ -469,13 +455,12 @@
continue;
}
- if (!strcmp(arg, "time"))
- {+ if (!strcmp(arg, "time")) {+
changed = 1;
arg = strtok_r(NULL, "\r\n\t ", &buffer);
- if (arg == NULL || moonfish_int(arg, &fancy->taken) != 0 || fancy->taken < 0)
- {+ if (arg == NULL || moonfish_int(arg, &fancy->taken) || fancy->taken < 0) {fprintf(stderr, "%s: malformed 'time' in 'info' command\n", fancy->argv0);
exit(1);
}
@@ -489,11 +474,10 @@
arg = strtok_r(NULL, "\r\n\t ", &buffer);
if (arg == NULL) break;
- if (!strcmp(arg, "cp"))
- {+ if (!strcmp(arg, "cp")) {+
arg = strtok_r(NULL, "\r\n\t ", &buffer);
- if (arg == NULL || moonfish_int(arg, &score) != 0)
- {+ if (arg == NULL || moonfish_int(arg, &score)) {fprintf(stderr, "%s: malformed 'cp' in 'info score' command\n", fancy->argv0);
exit(1);
}
@@ -507,8 +491,7 @@
if (score > 0) ply.white += score;
else ply.black -= score;
- if (!ply.chess.white)
- {+ if (!ply.chess.white) {score = ply.white;
ply.white = ply.black;
ply.black = score;
@@ -517,11 +500,10 @@
continue;
}
- if (!strcmp(arg, "mate"))
- {+ if (!strcmp(arg, "mate")) {+
arg = strtok_r(NULL, "\r\n\t ", &buffer);
- if (arg == NULL || moonfish_int(arg, &score) != 0)
- {+ if (arg == NULL || moonfish_int(arg, &score)) {fprintf(stderr, "%s: malformed 'mate' in 'info score' command\n", fancy->argv0);
exit(1);
}
@@ -538,7 +520,7 @@
}
}
- if (changed != 0) fancy->plies[fancy->i] = ply;
+ if (changed) fancy->plies[fancy->i] = ply;
}
}
@@ -582,13 +564,12 @@
if (fancy->fen == NULL) fprintf(fancy->in, "startpos");
else fprintf(fancy->in, "fen %s", fancy->fen);
- if (fancy->i > 0)
- {+ if (fancy->i > 0) {+
fprintf(fancy->in, " moves");
i = 0;
- for (;;)
- {+ for (;;) {i += fancy->plies[i].count + 1;
if (i > fancy->i) break;
fprintf(fancy->in, " %s", fancy->plies[i].name);
@@ -595,8 +576,7 @@
}
i = fancy->plies[fancy->i].main - fancy->plies;
- for (;;)
- {+ for (;;) {i++;
if (i > fancy->i) break;
fprintf(fancy->in, " %s", fancy->plies[i].name);
@@ -627,7 +607,7 @@
{int i;
i = fancy->i + 2;
- if (fancy->plies[0].chess.white != 0) i--;
+ if (fancy->plies[0].chess.white) i--;
if (i < fancy->offset * 2 + 2) fancy->offset = i / 2 - 1;
if (i > fancy->offset * 2 + 12) fancy->offset = i / 2 - 6;
if (fancy->offset < 0) fancy->offset = 0;
@@ -648,18 +628,16 @@
if (move == NULL) name[0] = 0;
else moonfish_to_uci(&fancy->plies[fancy->i].chess, move, name);
- if (ply->ephemeral == 0 && fancy->i + count + 1 < fancy->count && !strcmp(name, next->name))
- {+ if (!ply->ephemeral && fancy->i + count + 1 < fancy->count && !strcmp(name, next->name)) {+
i = fancy->i;
- for (;;)
- {+ for (;;) {fancy->plies[i].count -= count;
- if (fancy->plies[i].ephemeral == 0) break;
+ if (!fancy->plies[i].ephemeral) break;
i--;
}
- for (i = fancy->i + 1 ; i < fancy->count ; i++)
- {+ for (i = fancy->i + 1 ; i < fancy->count ; i++) {fancy->plies[i].main -= count;
}
@@ -676,20 +654,17 @@
if (fancy->count - count + 1 >= (int) (sizeof fancy->plies / sizeof *fancy->plies)) return;
i = fancy->i;
- for (;;)
- {+ for (;;) {fancy->plies[i].count -= count - 1;
if (fancy->plies[i].ephemeral == 0) break;
i--;
}
- for (i = fancy->i + 1 ; i < fancy->count ; i++)
- {+ for (i = fancy->i + 1 ; i < fancy->count ; i++) {fancy->plies[i].main -= count - 1;
}
- if (fancy->i + count < fancy->count - 1 && next[-1].name[0] == 0)
- {+ if (fancy->i + count < fancy->count - 1 && next[-1].name[0] == 0) {ply++;
next++;
}
@@ -715,8 +690,7 @@
ply->chess = move->chess;
- if (i + 1 < fancy->count && ply->chess.white == ply[1].chess.white)
- {+ if (i + 1 < fancy->count && ply->chess.white == ply[1].chess.white) {moonfish_play(fancy, NULL);
fancy->i = i;
}
@@ -760,8 +734,8 @@
if (args[0].value != NULL && args[1].value != NULL) moonfish_usage(args, format, argv[0]);
- for (;;)
- {+ for (;;) {+
value = strchr(*command, '=');
if (value == NULL) break;
@@ -773,8 +747,8 @@
if (command_count <= 0) moonfish_usage(args, format, argv[0]);
}
- if (!strcmp(*command, "--"))
- {+ if (!strcmp(*command, "--")) {+
command_count--;
command++;
@@ -784,8 +758,7 @@
/* initialise data structures */
fancy = malloc(sizeof *fancy);
- if (fancy == NULL)
- {+ if (fancy == NULL) {perror(argv[0]);
return 1;
}
@@ -823,32 +796,27 @@
moonfish_chess(&fancy->plies[0].chess);
- if (args[0].value != NULL)
- {+ if (args[0].value != NULL) {fancy->fen = args[0].value;
- if (moonfish_from_fen(&fancy->plies[0].chess, fancy->fen) != 0)
- {+ if (moonfish_from_fen(&fancy->plies[0].chess, fancy->fen)) {fprintf(stderr, "%s: invalid FEN\n", argv[0]);
}
}
- if (args[1].value != NULL)
- {+ if (args[1].value != NULL) {+
file = fopen(args[1].value, "r");
- if (file == NULL)
- {+ if (file == NULL) {perror(argv[0]);
return 1;
}
- for (;;)
- {- if (moonfish_pgn(file, &fancy->plies[fancy->i].chess, &move, fancy->i == 0 ? 1 : 0) != 0) break;
+ for (;;) {+ if (moonfish_pgn(file, &fancy->plies[fancy->i].chess, &move, fancy->i == 0 ? 1 : 0)) break;
moonfish_play(fancy, &move);
}
- for (i = 0 ; i < fancy->count ; i++)
- {+ for (i = 0 ; i < fancy->count ; i++) {fancy->plies[i].ephemeral = 0;
fancy->plies[i].count = 0;
fancy->plies[i].main = fancy->plies + i;
@@ -857,8 +825,7 @@
fclose(file);
fancy->fen = malloc(128);
- if (fancy->fen == NULL)
- {+ if (fancy->fen == NULL) {perror(argv[0]);
return 1;
}
@@ -869,14 +836,12 @@
/* configure the terminal for displaying the user interface */
- if (tcgetattr(0, &moonfish_termios))
- {+ if (tcgetattr(0, &moonfish_termios)) {perror(argv[0]);
return 1;
}
- if (atexit(&moonfish_exit))
- {+ if (atexit(&moonfish_exit)) {moonfish_exit();
return 1;
}
@@ -885,8 +850,7 @@
sigemptyset(&action.sa_mask);
action.sa_flags = 0;
- if (sigaction(SIGTERM, &action, NULL) || sigaction(SIGINT, &action, NULL) || sigaction(SIGQUIT, &action, NULL))
- {+ if (sigaction(SIGTERM, &action, NULL) || sigaction(SIGINT, &action, NULL) || sigaction(SIGQUIT, &action, NULL)) {perror(argv[0]);
return 1;
}
@@ -894,8 +858,7 @@
termios = moonfish_termios;
termios.c_lflag &= ~(ECHO | ICANON);
- if (tcsetattr(0, TCSANOW, &termios))
- {+ if (tcsetattr(0, TCSANOW, &termios)) {perror(argv[0]);
return 1;
}
@@ -906,8 +869,7 @@
printf("\x1B[6n");fflush(stdout);
- for (;;)
- {+ for (;;) {ch = getchar();
if (ch == EOF) return 1;
if (ch == 0x1B) break;
@@ -925,8 +887,7 @@
fprintf(fancy->in, "uci\n");
moonfish_wait(fancy->out, "uciok");
- for (;;)
- {+ for (;;) {value = strchr(*options, '=');
if (value == NULL) break;
fprintf(fancy->in, "setoption name %.*s value %s\n", (int) (value - *options), *options, value + 1);
@@ -940,8 +901,7 @@
/* start thread to communicate with the bot */
error = pthread_create(&thread, NULL, &moonfish_start, fancy);
- if (error != 0)
- {+ if (error) {fprintf(stderr, "%s: %s\n", fancy->argv0, strerror(error));
return 1;
}
@@ -949,8 +909,8 @@
/* main UI loop */
pthread_mutex_lock(fancy->mutex);
- for (;;)
- {+ for (;;) {+
pthread_mutex_unlock(fancy->mutex);
ch0 = getchar();
pthread_mutex_lock(fancy->mutex);
@@ -966,8 +926,7 @@
if (ch0 == EOF) break;
/* handle up arrow */
- if (ch0 == 'A')
- {+ if (ch0 == 'A') {if (fancy->i == 0) continue;
fancy->i = 0;
moonfish_scroll(fancy);
@@ -976,8 +935,7 @@
}
/* handle down arrow */
- if (ch0 == 'B')
- {+ if (ch0 == 'B') {if (fancy->i == fancy->count - 1) continue;
fancy->i = fancy->count - 1;
moonfish_scroll(fancy);
@@ -986,15 +944,13 @@
}
/* handle right arrow */
- if (ch0 == 'C')
- {+ if (ch0 == 'C') {if (fancy->i >= fancy->count - 1) continue;
- if (fancy->plies[fancy->i].ephemeral == 0 && fancy->ephemeral != 0)
- {+ if (!fancy->plies[fancy->i].ephemeral && fancy->ephemeral) {fancy->i += fancy->plies[fancy->i].count;
}
if (fancy->i < fancy->count - 1) fancy->i++;
- if (fancy->plies[fancy->i].name[0] == 0) fancy->i++;
+ if (!fancy->plies[fancy->i].name[0]) fancy->i++;
moonfish_scroll(fancy);
moonfish_go(fancy);
continue;
@@ -1001,15 +957,12 @@
}
/* handle left arrow */
- if (ch0 == 'D')
- {+ if (ch0 == 'D') {if (fancy->i == 0) continue;
- if (fancy->plies[fancy->i].ephemeral == 0 && fancy->ephemeral != 0)
- {+ if (!fancy->plies[fancy->i].ephemeral && fancy->ephemeral) {fancy->i = fancy->plies[fancy->i - 1].main - fancy->plies;
}
- else
- {+ else {fancy->i--;
}
moonfish_scroll(fancy);
@@ -1036,10 +989,8 @@
y1 = ch - 0x21 - fancy->oy + 2;
/* handle scroll up */
- if (ch0 == 0x60)
- {- if (fancy->offset > 0)
- {+ if (ch0 == 0x60) {+ if (fancy->offset > 0) {fancy->offset--;
moonfish_fancy(fancy);
}
@@ -1047,10 +998,8 @@
}
/* handle scroll down */
- if (ch0 == 0x61)
- {- if (fancy->offset < fancy->count / 2 - 6)
- {+ if (ch0 == 0x61) {+ if (fancy->offset < fancy->count / 2 - 6) {fancy->offset++;
moonfish_fancy(fancy);
}
@@ -1058,20 +1007,17 @@
}
/* "(+)" button clicked */
- if (ch0 == 0x20 && y1 == 1 && x1 >= 21 && x1 <= 23)
- {+ if (ch0 == 0x20 && y1 == 1 && x1 >= 21 && x1 <= 23) {moonfish_bump(fancy);
continue;
}
/* move name clicked (on scoresheet) */
- if (ch0 == 0x20 && y1 >= 2 && y1 <= 7 && x1 >= 21 && x1 <= 40)
- {+ if (ch0 == 0x20 && y1 >= 2 && y1 <= 7 && x1 >= 21 && x1 <= 44) {i = (fancy->offset + y1) * 2 - 4;
if (fancy->plies[0].chess.white) i++;
- if (x1 > 30) i++;
- if (i < fancy->count && fancy->plies[i].name[0] != 0)
- {+ if (x1 > 32) i++;
+ if (i < fancy->count && fancy->plies[i].name[0] != 0) {fancy->i = i;
moonfish_go(fancy);
}
@@ -1079,12 +1025,9 @@
}
/* "best move" button clicked */
- if (ch0 == 0x20 && y1 == 8 && x1 >= 21 && x1 <= 40)
- {- if (fancy->plies[fancy->i].best[0] != 0)
- {- if (moonfish_from_uci(&fancy->plies[fancy->i].chess, &move, fancy->plies[fancy->i].best))
- {+ if (ch0 == 0x20 && y1 == 8 && x1 >= 21 && x1 <= 40) {+ if (fancy->plies[fancy->i].best[0] != 0) {+ if (moonfish_from_uci(&fancy->plies[fancy->i].chess, &move, fancy->plies[fancy->i].best)) {fprintf(stderr, "%s: invalid best move: %s\n", fancy->argv0, fancy->plies[fancy->i].best);
return 1;
}
@@ -1098,8 +1041,7 @@
x1 /= 2;
/* mouse down with no square selected: select the square under the mouse */
- if (ch0 == 0x20 && fancy->x == 0)
- {+ if (ch0 == 0x20 && fancy->x == 0) {fancy->x = x1;
fancy->y = y1;
moonfish_fancy(fancy);
@@ -1110,8 +1052,7 @@
if (fancy->x == 0) continue;
/* handle mouse down: if the clicked square is the selected square, deselect it */
- if (ch0 == 0x20 && x1 == fancy->x && y1 == fancy->y)
- {+ if (ch0 == 0x20 && x1 == fancy->x && y1 == fancy->y) {fancy->x = 0;
moonfish_fancy(fancy);
continue;
@@ -1118,16 +1059,15 @@
}
/* handle mouse up or mouse down: if it forms a valid move, play it on the board */
- if (ch0 == 0x20 || ch0 == 0x23)
- if (moonfish_move_from(&fancy->plies[fancy->i].chess, &move, fancy->x, fancy->y, x1, y1) == 0)
- {- moonfish_play(fancy, &move);
- continue;
+ if (ch0 == 0x20 || ch0 == 0x23) {+ if (!moonfish_move_from(&fancy->plies[fancy->i].chess, &move, fancy->x, fancy->y, x1, y1)) {+ moonfish_play(fancy, &move);
+ continue;
+ }
}
/* handle mouse down: when there isn't a valid move under the mouse, just select the new square */
- if (ch0 == 0x20)
- {+ if (ch0 == 0x20) {fancy->x = x1;
fancy->y = y1;
moonfish_fancy(fancy);
--- a/tools/chat.c
+++ b/tools/chat.c
@@ -17,27 +17,23 @@
while (*line == ' ') line++;
if (*line == 0) return;
- if (*line == '@')
- {+ if (*line == '@') {line++;
while (*line != ' ' && *line != 0) line++;
while (*line == ' ') line++;
}
- if (*line == ':')
- {+ if (*line == ':') {line++;
while (*line != ' ' && *line != 0) line++;
while (*line == ' ') line++;
}
- while (*line != 0)
- {+ while (*line != 0) {+
*command = realloc(*command, sizeof **command * (*count + 1));
- if (*line == ':')
- {+ if (*line == ':') {(*command)[*count] = strdup(line + 1);
- if ((*command)[*count] == NULL)
- {+ if ((*command)[*count] == NULL) {perror(argv0);
exit(1);
}
@@ -49,8 +45,7 @@
while (line[length] != ' ' && line[length] != 0) length++;
(*command)[*count] = strndup(line, length);
line += length;
- if ((*command)[*count] == NULL)
- {+ if ((*command)[*count] == NULL) {perror(argv0);
exit(1);
}
@@ -77,11 +72,10 @@
int count;
int i;
- for (;;)
- {+ for (;;) {+
line = moonfish_read_line(argv0, tls);
- if (line == NULL)
- {+ if (line == NULL) {fprintf(stderr, "%s: IRC connection closed unexpectedly\n", argv0);
exit(1);
}
@@ -91,14 +85,13 @@
if (count == 0) continue;
- if (!strcmp(command[0], "433"))
- {+ if (!strcmp(command[0], "433")) {+
moonfish_free_chat(command, count);
under_count++;
- if (under_count > 16)
- {+ if (under_count > 16) {fprintf(stderr, "%s: Tried too many nicknames\n", argv0);
exit(1);
}
@@ -111,10 +104,9 @@
continue;
}
- if (!strcmp(command[0], "PING"))
- {- if (count < 2)
- {+ if (!strcmp(command[0], "PING")) {+
+ if (count < 2) {moonfish_free_chat(command, count);
moonfish_write_text(argv0, tls, "PONG\r\n");
continue;
@@ -127,24 +119,21 @@
continue;
}
- if (!strcmp(command[0], "PRIVMSG"))
- {- if (count < 3)
- {+ if (!strcmp(command[0], "PRIVMSG")) {+
+ if (count < 3) {fprintf(stderr, "%s: Invalid IRC message\n", argv0);
exit(1);
}
*channel = strdup(command[1]);
- if (*channel == NULL)
- {+ if (*channel == NULL) {perror(argv0);
exit(1);
}
*message = strdup(command[2]);
- if (*message == NULL)
- {+ if (*message == NULL) {perror(argv0);
exit(1);
}
@@ -178,8 +167,7 @@
fprintf(in, "uci\n");
moonfish_wait(out, "uciok");
- for (;;)
- {+ for (;;) {value = strchr(*options, '=');
if (value == NULL) break;
fprintf(in, "setoption name %.*s value %s\n", (int) (value - *options), *options, value + 1);
@@ -195,8 +183,7 @@
/* todo: validate password */
password = getenv("moonfish_chat_password");- if (password != NULL && *password != 0)
- {+ if (password != NULL && *password != 0) {moonfish_write_text(argv0, tls, "PASS ");
moonfish_write_text(argv0, tls, password);
moonfish_write_text(argv0, tls, "\r\n");
@@ -215,8 +202,7 @@
moonfish_write_text(argv0, tls, "\r\n");
names = strdup("");- if (names == NULL)
- {+ if (names == NULL) {perror(argv0);
exit(1);
}
@@ -224,15 +210,15 @@
channel = NULL;
message = NULL;
- for (;;)
- {+ for (;;) {+
if (channel != NULL) free(channel);
if (message != NULL) free(message);
moonfish_read_chat(argv0, tls, username, &channel, &message);
- if (!strcmp(message, "!chess reset"))
- {+ if (!strcmp(message, "!chess reset")) {+
moonfish_chess(&chess);
fprintf(in, "ucinewgame\n");
@@ -243,21 +229,20 @@
continue;
}
- if (moonfish_from_san(&chess, &move, message))
- continue;
+ if (moonfish_from_san(&chess, &move, message)) continue;
moonfish_to_uci(&chess, &move, name);
chess = move.chess;
- if (moonfish_finished(&chess))
- {+ if (moonfish_finished(&chess)) {+
moonfish_to_fen(&chess, fen);
moonfish_chess(&chess);
fprintf(in, "ucinewgame\n");
- for (i = 0 ; fen[i] != 0 ; i++)
+ for (i = 0 ; fen[i] != 0 ; i++) {if (fen[i] == ' ') fen[i] = '_';
-
+ }
moonfish_write_text(argv0, tls, "PRIVMSG ");
moonfish_write_text(argv0, tls, channel);
moonfish_write_text(argv0, tls, " :https://lichess.org/export/fen.gif?fen=");
@@ -272,8 +257,7 @@
}
names = realloc(names, strlen(names) + strlen(name) + 2);
- if (names == NULL)
- {+ if (names == NULL) {perror(argv0);
exit(1);
}
@@ -293,8 +277,7 @@
name0 = moonfish_wait(out, "bestmove");
names = realloc(names, strlen(names) + strlen(name0) + 2);
- if (names == NULL)
- {+ if (names == NULL) {perror(argv0);
exit(1);
}
@@ -302,8 +285,7 @@
strcat(names, " ");
strcat(names, name0);
- if (moonfish_from_uci(&chess, &move, name0))
- {+ if (moonfish_from_uci(&chess, &move, name0)) {fprintf(stderr, "%s: invalid move '%s' by the bot\n", argv0, name0);
exit(1);
}
@@ -312,9 +294,9 @@
chess = move.chess;
moonfish_to_fen(&chess, fen);
- for (i = 0 ; fen[i] != 0 ; i++)
+ for (i = 0 ; fen[i] != 0 ; i++) {if (fen[i] == ' ') fen[i] = '_';
-
+ }
moonfish_write_text(argv0, tls, "PRIVMSG ");
moonfish_write_text(argv0, tls, channel);
moonfish_write_text(argv0, tls, " :");
@@ -327,8 +309,8 @@
moonfish_write_text(argv0, tls, fen);
moonfish_write_text(argv0, tls, "\r\n");
- if (moonfish_finished(&chess))
- {+ if (moonfish_finished(&chess)) {+
moonfish_chess(&chess);
fprintf(in, "ucinewgame\n");
@@ -357,8 +339,7 @@
options = moonfish_args(args, format, argc, argv);
command = options;
- for (;;)
- {+ for (;;) {if (*command == NULL) moonfish_usage(args, format, argv[0]);
if (strchr(*command, '=') == NULL) break;
command++;
--- a/tools/https.c
+++ b/tools/https.c
@@ -14,13 +14,12 @@
data = data0;
- while (length > 0)
- {+ while (length > 0) {+
result = tls_read(tls, data, length);
if (result == 0) return 1;
if (result == TLS_WANT_POLLIN || result == TLS_WANT_POLLOUT) continue;
- if (result == -1)
- {+ if (result == -1) {fprintf(stderr, "%s: %s\n", argv0, tls_error(tls));
exit(1);
}
@@ -39,13 +38,12 @@
data = data0;
- while (length > 0)
- {+ while (length > 0) {+
result = tls_write(tls, data, length);
if (result == 0) return 1;
if (result == TLS_WANT_POLLIN || result == TLS_WANT_POLLOUT) continue;
- if (result == -1)
- {+ if (result == -1) {fprintf(stderr, "%s: %s\n", argv0, tls_error(tls));
exit(1);
}
@@ -70,19 +68,16 @@
line = NULL;
length = 0;
- for (;;)
- {+ for (;;) {+
line = realloc(line, length + 1);
- if (line == NULL)
- {+ if (line == NULL) {perror(argv0);
exit(1);
}
- if (moonfish_read(argv0, tls, line + length, 1))
- {- if (length == 0)
- {+ if (moonfish_read(argv0, tls, line + length, 1)) {+ if (length == 0) {free(line);
return NULL;
}
@@ -89,8 +84,7 @@
line[length] = 0;
return line;
}
- if (line[length] == '\n')
- {+ if (line[length] == '\n') {line[length] = 0;
return line;
}
@@ -105,8 +99,7 @@
moonfish_write_text(argv0, tls, request);
moonfish_write_text(argv0, tls, " HTTP/1.0\r\n");
- if (token != NULL)
- {+ if (token != NULL) {moonfish_write_text(argv0, tls, "Authorization: Bearer ");
moonfish_write_text(argv0, tls, token);
moonfish_write_text(argv0, tls, "\r\n");
@@ -118,8 +111,8 @@
moonfish_write_text(argv0, tls, host);
moonfish_write_text(argv0, tls, "\r\n");
- if (type != NULL)
- {+ if (type != NULL) {+
moonfish_write_text(argv0, tls, "Content-Type: ");
moonfish_write_text(argv0, tls, type);
moonfish_write_text(argv0, tls, "\r\n");
@@ -143,16 +136,13 @@
line = moonfish_read_line(argv0, tls);
- if (strncmp(line, success0, sizeof success0 - 1))
- if (strncmp(line, success1, sizeof success1 - 1))
+ if (strncmp(line, success0, sizeof success0 - 1) && strncmp(line, success1, sizeof success1 - 1)) {return 1;
-
- for (;;)
- {+ }
+ for (;;) {free(line);
line = moonfish_read_line(argv0, tls);
- if (*line == 0)
- {+ if (*line == 0) {free(line);
break;
}
@@ -166,14 +156,12 @@
struct tls *tls;
tls = tls_client();
- if (tls == NULL)
- {+ if (tls == NULL) {fprintf(stderr, "%s: Could not create libtls client\n", argv0);
exit(1);
}
- if (tls_connect(tls, host, port))
- {+ if (tls_connect(tls, host, port)) {fprintf(stderr, "%s: %s\n", argv0, tls_error(tls));
exit(1);
}
@@ -183,8 +171,7 @@
void moonfish_close(char *argv0, struct tls *tls)
{- if (tls_close(tls) != 0)
- {+ if (tls_close(tls)) {fprintf(stderr, "%s: %s\n", argv0, tls_error(tls));
exit(1);
}
--- a/tools/learn.c
+++ b/tools/learn.c
@@ -16,10 +16,9 @@
double score;
errno = 0;
- if (fgets(line, 2048, file) == NULL)
- {- if (errno)
- {+ if (fgets(line, 2048, file) == NULL) {+
+ if (errno) {perror(argv0);
exit(1);
}
@@ -26,8 +25,7 @@
errno = 0;
rewind(file);
- if (errno)
- {+ if (errno) {perror(argv0);
exit(1);
}
@@ -34,8 +32,7 @@
}
arg = strrchr(line, ' ');
- if (arg == NULL)
- {+ if (arg == NULL) {fprintf(stderr, "%s: improper FEN line\n", argv0);
exit(1);
}
@@ -42,8 +39,7 @@
errno = 0;
score = strtod(arg + 1, &end);
- if (errno != 0 || (*end != 0 && *end != '\n') || score > 10000 || score < -10000)
- {+ if (errno || (*end != 0 && *end != '\n') || score > 10000 || score < -10000) {fprintf(stderr, "%s: unexpected score\n", argv0);
exit(1);
}
@@ -63,8 +59,7 @@
score = moonfish_score(&chess);
error = score - score0;
- for (i = 0 ; i < moonfish_count ; i++)
- {+ for (i = 0 ; i < moonfish_count ; i++) {prev = moonfish_values[i];
moonfish_values[i] += 1.0 / 256 / 256 / 8;
moonfish_from_fen(&chess, fen);
@@ -88,14 +83,12 @@
for (i = 0 ; i < moonfish_count ; i++) gradient[i] = 0;
- for (i = 0 ; i < 2048 ; i++)
- {+ for (i = 0 ; i < 2048 ; i++) {score = moonfish_next_line(argv0, line, file);
error += moonfish_gradient(gradient, score, line);
}
- for (i = 0 ; i < moonfish_count ; i++)
- moonfish_values[i] -= gradient[i] / 2048;
+ for (i = 0 ; i < moonfish_count ; i++) moonfish_values[i] -= gradient[i] / 2048;
return error;
}
@@ -109,15 +102,13 @@
double error;
int iteration;
- if (argc != 2)
- {+ if (argc != 2) {if (argc > 0) fprintf(stderr, "usage: %s <file-name>\n", argv[0]);
return 1;
}
file = fopen(argv[1], "r");
- if (file == NULL)
- {+ if (file == NULL) {perror(argv[0]);
return 1;
}
@@ -124,8 +115,7 @@
errno = 0;
rewind(file);
- if (errno)
- {+ if (errno) {perror(argv[0]);
return 1;
}
@@ -132,8 +122,8 @@
iteration = 0;
- for (;;)
- {+ for (;;) {+
iteration++;
if (iteration > 0x1000) return 0;
@@ -140,17 +130,19 @@
error = moonfish_step(argv[0], file, gradient);
printf("\n");- for (i = 0 ; i < moonfish_count ; i++)
- printf("%.0f,", moonfish_values[i]);+ for (i = 0 ; i < moonfish_count ; i++) printf("%.0f,", moonfish_values[i]); printf("\n"); printf("iteration: %d\n", iteration); printf("current error: ");- if (error > 10000 * 1000)
+ if (error > 10000 * 1000) { printf("%.0fM\n", error / 1000 / 1000);- else if (error > 10000)
+ continue;
+ }
+ if (error > 10000) { printf("%.0fK\n", error / 1000);- else
- printf("%.0f\n", error);+ continue;
+ }
+ printf("%.0f\n", error);}
}
--- a/tools/lichess.c
+++ b/tools/lichess.c
@@ -12,8 +12,7 @@
#include "tools.h"
#include "https.h"
-struct moonfish_game
-{+struct moonfish_game {char *argv0;
char *host;
char *port;
@@ -65,12 +64,11 @@
move_count = -1;
variant = 0;
- for (;;)
- {+ for (;;) {+
pthread_mutex_unlock(&moonfish_mutex);
- if (root != NULL)
- {+ if (root != NULL) {cJSON_Delete(root);
root = NULL;
}
@@ -80,8 +78,7 @@
pthread_mutex_lock(&moonfish_mutex);
- if (line[0] == 0)
- {+ if (line[0] == 0) {free(line);
continue;
}
@@ -96,12 +93,12 @@
type = cJSON_GetObjectItem(root, "type");
if (!cJSON_IsString(type)) moonfish_json_error(game->argv0);
- if (!strcmp(type->valuestring, "gameState"))
- {- state = root;
- }
- else if (!strcmp(type->valuestring, "gameFull"))
- {+ state = NULL;
+
+ if (!strcmp(type->valuestring, "gameState")) state = root;
+
+ if (!strcmp(type->valuestring, "gameFull")) {+
state = cJSON_GetObjectItem(root, "state");
if (!cJSON_IsObject(state)) moonfish_json_error(game->argv0);
@@ -111,8 +108,7 @@
if (!cJSON_IsObject(white_player)) moonfish_json_error(game->argv0);
id = cJSON_GetObjectItem(white_player, "id");
- if (id != NULL && !cJSON_IsNull(id))
- {+ if (id != NULL && !cJSON_IsNull(id)) {if (!cJSON_IsString(id)) moonfish_json_error(game->argv0);
if (!strcmp(id->valuestring, game->username)) white = 1;
}
@@ -120,24 +116,19 @@
fen = cJSON_GetObjectItem(root, "initialFen");
if (!cJSON_IsString(fen)) moonfish_json_error(game->argv0);
- if (strcmp(fen->valuestring, "startpos"))
- {+ if (strcmp(fen->valuestring, "startpos")) {strcpy(game->fen, "fen ");
strcat(game->fen, fen->valuestring);
variant = 1;
}
- else
- {+ else {strcpy(game->fen, "startpos");
}
}
- else
- {- continue;
- }
- if (white == -1)
- {+ if (state == NULL) continue;
+
+ if (white == -1) {fprintf(stderr, "%s: 'gameState' event received prior to 'gameFull' event\n", game->argv0);
exit(1);
}
@@ -146,12 +137,11 @@
if (!cJSON_IsString(moves)) moonfish_json_error(game->argv0);
count = 0;
- if (moves->valuestring[0] != 0)
- {+ if (moves->valuestring[0] != 0) {count = 1;
- for (i = 0 ; moves->valuestring[i] != 0 ; i++)
- if (moves->valuestring[i] == ' ')
- count++;
+ for (i = 0 ; moves->valuestring[i] != 0 ; i++) {+ if (moves->valuestring[i] == ' ') count++;
+ }
}
if (count <= move_count) continue;
@@ -174,21 +164,19 @@
moonfish_wait(out, "readyok");
fprintf(in, "position %s", game->fen);
- if (count > 0)
- {+ if (count > 0) {+
fprintf(in, " moves ");
- if (!variant)
- {+ if (!variant) {fprintf(in, "%s", moves->valuestring);
}
- else
- {+ else {+
moonfish_from_fen(&chess, game->fen + 4);
name = strtok(moves->valuestring, " ");
- for (;;)
- {- if (moonfish_from_uci(&chess, &move, name))
- {+ for (;;) {+
+ if (moonfish_from_uci(&chess, &move, name)) {fprintf(stderr, "%s: invalid move '%s' from the bot\n", game->argv0, name);
exit(1);
}
@@ -206,40 +194,34 @@
fprintf(in, "isready\n");
moonfish_wait(out, "readyok");
- if (count > 1)
- {+ if (count > 1) {fprintf(in, "go wtime %d btime %d", wtime->valueint, btime->valueint);
if (winc->valueint > 0) fprintf(in, " winc %d", winc->valueint);
if (binc->valueint > 0) fprintf(in, " binc %d", binc->valueint);
fprintf(in, "\n");
}
- else
- {+ else {fprintf(in, "go movetime 6000\n");
}
name = moonfish_wait(out, "bestmove");
- if (name == NULL)
- {+ if (name == NULL) {fprintf(stderr, "%s: could not find 'bestmove' command\n", game->argv0);
exit(1);
}
- if (variant)
- {- if (chess.board[25] == moonfish_white_king)
- {+ if (variant) {+
+ if (chess.board[25] == moonfish_white_king) {if (!strcmp(name, "e1c1")) name = "e1a1";
if (!strcmp(name, "e1g1")) name = "e1h1";
}
- if (chess.board[25] % 16 == moonfish_black_king)
- {+ if (chess.board[25] % 16 == moonfish_black_king) {if (!strcmp(name, "e8c8")) name = "e8a8";
if (!strcmp(name, "e8g8")) name = "e8h8";
}
- if (moonfish_from_uci(&chess, &move, name))
- {+ if (moonfish_from_uci(&chess, &move, name)) {fprintf(stderr, "%s: invalid move '%s' from the bot\n", game->argv0, name);
exit(1);
}
@@ -246,12 +228,12 @@
}
snprintf(request, sizeof request, "POST /api/bot/game/%s/move/%s", game->id, name);
- if (moonfish_basic_request(game->argv0, game->host, game->port, game->token, request, NULL, NULL))
- {+ if (moonfish_basic_request(game->argv0, game->host, game->port, game->token, request, NULL, NULL)) {fprintf(stderr, "%s: could not make move '%s' in game '%s'\n", game->argv0, name, game->id);
snprintf(request, sizeof request, "POST /api/bot/game/%s/resign", game->id);
- if (moonfish_basic_request(game->argv0, game->host, game->port, game->token, request, NULL, NULL))
+ if (moonfish_basic_request(game->argv0, game->host, game->port, game->token, request, NULL, NULL)) {fprintf(stderr, "%s: could not resign game '%s'\n", game->argv0, game->id);
+ }
break;
}
}
@@ -281,8 +263,7 @@
moonfish_request(game->argv0, tls, game->host, request, game->token, NULL, 0);
- if (moonfish_response(game->argv0, tls))
- {+ if (moonfish_response(game->argv0, tls)) {fprintf(stderr, "%s: could not request game event stream\n", game->argv0);
exit(1);
}
@@ -308,23 +289,20 @@
root = NULL;
- for (;;)
- {- if (root != NULL)
- {+ for (;;) {+
+ if (root != NULL) {cJSON_Delete(root);
root = NULL;
}
line = moonfish_read_line(argv0, tls);
- if (line == NULL)
- {+ if (line == NULL) {fprintf(stderr, "%s: connection with Lichess closed\n", argv0);
exit(1);
}
- if (line[0] == 0)
- {+ if (line[0] == 0) {free(line);
continue;
}
@@ -339,8 +317,8 @@
type = cJSON_GetObjectItem(root, "type");
if (!cJSON_IsString(type)) moonfish_json_error(argv0);
- if (!strcmp(type->valuestring, "gameStart"))
- {+ if (!strcmp(type->valuestring, "gameStart")) {+
challenge = cJSON_GetObjectItem(root, "game");
if (!cJSON_IsObject(challenge)) moonfish_json_error(argv0);
@@ -348,14 +326,12 @@
if (!cJSON_IsString(id)) moonfish_json_error(argv0);
game = malloc(sizeof *game);
- if (game == NULL)
- {+ if (game == NULL) {fprintf(stderr, "%s: could not allocate game\n", argv0);
exit(1);
}
- if (strlen(id->valuestring) > sizeof game->id - 1)
- {+ if (strlen(id->valuestring) > sizeof game->id - 1) {fprintf(stderr, "%s: game ID '%s' too long\n", argv0, id->valuestring);
exit(1);
}
@@ -369,8 +345,7 @@
game->username = username;
error = pthread_create(&thread, NULL, &moonfish_handle_game, game);
- if (error != 0)
- {+ if (error) {fprintf(stderr, "%s: %s\n", argv0, strerror(error));
exit(1);
}
@@ -392,41 +367,34 @@
variant = cJSON_GetObjectItem(variant, "key");
if (!cJSON_IsString(variant)) moonfish_json_error(argv0);
- if (!strcmp(variant->valuestring, "fromPosition"))
- {+ if (!strcmp(variant->valuestring, "fromPosition")) {+
fen = cJSON_GetObjectItem(challenge, "initialFen");
if (!cJSON_IsString(fen)) moonfish_json_error(argv0);
invalid = moonfish_from_fen(&chess, fen->valuestring);
- if (!invalid)
- if (chess.oo[0] || chess.ooo[0])
- if (chess.board[25] != moonfish_white_king)
- invalid = 1;
-
- if (!invalid)
- if (chess.oo[1] || chess.ooo[1])
- if (chess.board[95] != moonfish_black_king)
- invalid = 1;
-
+ if (!invalid && (chess.oo[0] || chess.ooo[0]) && chess.board[25] != moonfish_white_king) invalid = 1;
+ if (!invalid && (chess.oo[1] || chess.ooo[1]) && chess.board[95] != moonfish_black_king) invalid = 1;
if (!invalid && chess.oo[0] && chess.board[28] != moonfish_white_rook) invalid = 1;
if (!invalid && chess.oo[1] && chess.board[98] != moonfish_black_rook) invalid = 1;
if (!invalid && chess.ooo[0] && chess.board[21] != moonfish_white_rook) invalid = 1;
if (!invalid && chess.ooo[1] && chess.board[91] != moonfish_black_rook) invalid = 1;
- if (invalid)
- {+ if (invalid) {snprintf(request, sizeof request, "POST /api/challenge/%s/decline", id->valuestring);
- if (moonfish_basic_request(argv0, host, port, token, request, NULL, "reason=standard"))
+ if (moonfish_basic_request(argv0, host, port, token, request, NULL, "reason=standard")) {fprintf(stderr, "%s: could not decline challenge '%s' (chess 960 FEN)\n", argv0, id->valuestring);
+ }
continue;
}
}
- else if (strcmp(variant->valuestring, "standard"))
- {+
+ if (strcmp(variant->valuestring, "standard")) {snprintf(request, sizeof request, "POST /api/challenge/%s/decline", id->valuestring);
- if (moonfish_basic_request(argv0, host, port, token, request, NULL, "reason=standard"))
+ if (moonfish_basic_request(argv0, host, port, token, request, NULL, "reason=standard")) {fprintf(stderr, "%s: could not decline challenge '%s' (variant)\n", argv0, id->valuestring);
+ }
continue;
}
@@ -433,17 +401,18 @@
speed = cJSON_GetObjectItem(challenge, "speed");
if (!cJSON_IsString(speed)) moonfish_json_error(argv0);
- if (!strcmp(speed->valuestring, "correspondence"))
- {+ if (!strcmp(speed->valuestring, "correspondence")) {snprintf(request, sizeof request, "POST /api/challenge/%s/decline", id->valuestring);
- if (moonfish_basic_request(argv0, host, port, token, request, NULL, "reason=tooSlow"))
+ if (moonfish_basic_request(argv0, host, port, token, request, NULL, "reason=tooSlow")) {fprintf(stderr, "%s: could not decline challenge '%s' (too slow)\n", argv0, id->valuestring);
+ }
continue;
}
snprintf(request, sizeof request, "POST /api/challenge/%s/accept", id->valuestring);
- if (moonfish_basic_request(argv0, host, port, token, request, NULL, NULL))
+ if (moonfish_basic_request(argv0, host, port, token, request, NULL, NULL)) {fprintf(stderr, "%s: could not accept challenge '%s'\n", argv0, id->valuestring);
+ }
}
}
@@ -459,8 +428,7 @@
moonfish_request(argv0, tls, host, "GET /api/account", token, NULL, 0);
- if (moonfish_response(argv0, tls))
- {+ if (moonfish_response(argv0, tls)) {fprintf(stderr, "%s: could not request the Lichess username\n", argv0);
exit(1);
}
@@ -478,8 +446,7 @@
if (!cJSON_IsString(id)) moonfish_json_error(argv0);
username = strdup(id->valuestring);
- if (username == NULL)
- {+ if (username == NULL) {perror(argv0);
exit(1);
}
@@ -511,16 +478,13 @@
if (command_count < 1) moonfish_usage(args, format, argv[0]);
token = getenv("lichess_token");- if (token == NULL || token[0] == 0)
- {+ if (token == NULL || token[0] == 0) {fprintf(stderr, "%s: Lichess token not provided\n", argv[0]);
return 1;
}
- for (i = 0 ; token[i] != 0 ; i++)
- {- if (token[i] <= 0x20 || token[i] >= 0x7F)
- {+ for (i = 0 ; token[i] != 0 ; i++) {+ if (token[i] <= 0x20 || token[i] >= 0x7F) {fprintf(stderr, "%s: invalid token provided for Lichess\n", argv[0]);
return 1;
}
@@ -530,8 +494,7 @@
tls = moonfish_connect(argv[0], args[0].value, args[1].value);
moonfish_request(argv[0], tls, args[0].value, "GET /api/stream/event", token, NULL, 0);
- if (moonfish_response(argv[0], tls))
- {+ if (moonfish_response(argv[0], tls)) {fprintf(stderr, "%s: could not request event stream\n", argv[0]);
return 1;
}
--- a/tools/perft.c
+++ b/tools/perft.c
@@ -19,15 +19,16 @@
perft = 0;
- for (y = 0 ; y < 8 ; y++)
- for (x = 0 ; x < 8 ; x++)
- {- count = moonfish_moves(chess, moves, (x + 1) + (y + 2) * 10);
- for (i = 0 ; i < count ; i++)
- if (moonfish_validate(&moves[i].chess))
- perft += moonfish_perft(&moves[i].chess, depth - 1);
+ for (y = 0 ; y < 8 ; y++) {+ for (x = 0 ; x < 8 ; x++) {+ count = moonfish_moves(chess, moves, (x + 1) + (y + 2) * 10);
+ for (i = 0 ; i < count ; i++) {+ if (moonfish_validate(&moves[i].chess)) {+ perft += moonfish_perft(&moves[i].chess, depth - 1);
+ }
+ }
+ }
}
-
return perft;
}
@@ -44,17 +45,14 @@
long int depth;
struct moonfish_chess chess;
- if (moonfish_args(args, NULL, argc, argv) - argv != argc)
- moonfish_usage(args, NULL, argv[0]);
+ if (moonfish_args(args, NULL, argc, argv) - argv != argc) moonfish_usage(args, NULL, argv[0]);
errno = 0;
depth = strtol(args[1].value, &end, 10);
- if (errno != 0 || *end != 0 || depth < 0 || depth >= 24)
- moonfish_usage(args, NULL, argv[0]);
+ if (errno || *end != 0 || depth < 0 || depth >= 24) moonfish_usage(args, NULL, argv[0]);
moonfish_chess(&chess);
- if (args[0].value != NULL && moonfish_from_fen(&chess, args[0].value))
- moonfish_usage(args, NULL, argv[0]);
+ if (args[0].value != NULL && moonfish_from_fen(&chess, args[0].value)) moonfish_usage(args, NULL, argv[0]);
printf("perft %ld: %ld\n", depth, moonfish_perft(&chess, depth));--- a/tools/pgn.c
+++ b/tools/pgn.c
@@ -11,15 +11,13 @@
{int ch;
- for (;;)
- {+ for (;;) {+
ch = getc(file);
if (ch == EOF) return ch;
- if (ch == '{')- {- for (;;)
- {+ if (ch == '{') {+ for (;;) {ch = getc(file);
if (ch == EOF) return ch;
if (ch == '}') break;
@@ -27,10 +25,8 @@
continue;
}
- if (ch == ';')
- {- for (;;)
- {+ if (ch == ';') {+ for (;;) {ch = getc(file);
if (ch == EOF) return ch;
if (ch == '\r' || ch == '\n') break;
@@ -51,10 +47,8 @@
static int moonfish_pgn_skip(FILE *file, int ch)
{- if (ch == '"')
- {- for (;;)
- {+ if (ch == '"') {+ for (;;) {ch = getc(file);
if (ch == '\\') ch = getc(file);
if (ch == EOF) return 1;
@@ -63,10 +57,8 @@
return 0;
}
- if (ch == '(')- {- for (;;)
- {+ if (ch == '(') {+ for (;;) {ch = moonfish_pgn_token(file);
if (ch == EOF) return 1;
if (ch == ')') break;
@@ -74,15 +66,14 @@
return 0;
}
- for (;;)
- {+ for (;;) {ch = getc(file);
if (ch == EOF) return 1;
- if (moonfish_isalnum(ch) == 0) return 0;
+ if (!moonfish_isalnum(ch)) return 0;
}
}
-int moonfish_pgn(FILE *file, struct moonfish_chess *chess, struct moonfish_move *move, int allow_attr)
+int moonfish_pgn(FILE *file, struct moonfish_chess *chess, struct moonfish_move *move, int allow_tag)
{int i;
char buffer[256];
@@ -90,18 +81,17 @@
ch = moonfish_pgn_token(file);
- for (;;)
- {+ for (;;) {+
if (ch == EOF) return -1;
if (ch == '*') return 1;
- if (moonfish_isalnum(ch) != 0)
- {+ if (moonfish_isalnum(ch)) {+
i = 0;
buffer[i++] = ch;
- for (;;)
- {+ for (;;) {ch = getc(file);
if (moonfish_isalnum(ch) == 0) break;
if (i >= (int) sizeof buffer - 1) break;
@@ -114,34 +104,31 @@
if (!strcmp(buffer, "1-0")) return 1;
if (!strcmp(buffer, "0-1")) return 1;
- if (buffer[0] >= '0' && buffer[0] <= '9')
- {+ if (buffer[0] >= '0' && buffer[0] <= '9') {ch = moonfish_pgn_token(file);
continue;
}
- if (moonfish_from_san(chess, move, buffer) != 0) return -1;
+ if (moonfish_from_san(chess, move, buffer)) return -1;
return 0;
}
- if (ch == '[')
- {- if (allow_attr == 0) return -1;
+ if (ch == '[') {+ if (!allow_tag) return -1;
+
i = 0;
ch = moonfish_pgn_token(file);
if (ch == EOF) return -1;
- if (moonfish_isalnum(ch) == 0) return -1;
+ if (!moonfish_isalnum(ch)) return -1;
buffer[i++] = ch;
- for (;;)
- {+ for (;;) {ch = moonfish_pgn_token(file);
if (ch == EOF) return -1;
- if (moonfish_isalnum(ch) == 0) break;
- if (i >= (int) sizeof buffer - 1)
- {- if (moonfish_pgn_skip(file, ch) != 0) return -1;
+ if (!moonfish_isalnum(ch)) break;
+ if (i >= (int) sizeof buffer - 1) {+ if (moonfish_pgn_skip(file, ch)) return -1;
break;
}
buffer[i++] = ch;
@@ -148,14 +135,13 @@
}
buffer[i] = 0;
- if (strcmp(buffer, "FEN"))
- {- for (;;)
- {+ if (strcmp(buffer, "FEN")) {+
+ for (;;) {ch = moonfish_pgn_token(file);
if (ch == EOF) return -1;
if (ch == ']') break;
- if (moonfish_pgn_skip(file, ch) != 0) return -1;
+ if (moonfish_pgn_skip(file, ch)) return -1;
}
ch = moonfish_pgn_token(file);
@@ -166,8 +152,7 @@
ch = moonfish_pgn_token(file);
if (ch != '"') return -1;
- for (;;)
- {+ for (;;) {ch = getc(file);
if (ch == '"') break;
if (ch == '\\') ch = getc(file);
@@ -177,7 +162,7 @@
}
buffer[i] = 0;
- if (moonfish_from_fen(chess, buffer) != 0) return -1;
+ if (moonfish_from_fen(chess, buffer)) return -1;
ch = moonfish_pgn_token(file);
if (ch != ']') return -1;
--- a/tools/play.c
+++ b/tools/play.c
@@ -13,8 +13,7 @@
#include "../moonfish.h"
#include "tools.h"
-struct moonfish_fancy
-{+struct moonfish_fancy {struct moonfish_chess chess;
int white;
int our_time, their_time;
@@ -31,12 +30,13 @@
{unsigned char piece;
- if (x + 1 == fancy->x && y + 1 == fancy->y)
+ if (x + 1 == fancy->x && y + 1 == fancy->y) { printf("\x1B[48;5;219m");- else if (x % 2 == y % 2)
- printf("\x1B[48;5;111m");- else
- printf("\x1B[48;5;69m");+ }
+ else {+ if (x % 2 == y % 2) printf("\x1B[48;5;111m");+ else printf("\x1B[48;5;69m");+ }
if (fancy->white) y = 7 - y;
else x = 7 - x;
@@ -43,19 +43,15 @@
piece = fancy->chess.board[(x + 1) + (y + 2) * 10];
- if (piece == moonfish_empty)
- {+ if (piece == moonfish_empty) { printf(" ");return;
}
- if (piece >> 4 == 1)
- printf("\x1B[38;5;253m");- else
- printf("\x1B[38;5;240m");+ if (piece >> 4 == 1) printf("\x1B[38;5;253m");+ else printf("\x1B[38;5;240m");- switch (piece & 0xF)
- {+ switch (piece & 0xF) {case 1:
printf("\xE2\x99\x9F ");return;
@@ -81,13 +77,13 @@
{ printf(" \x1B[48;5;248m\x1B[38;5;235m ");- if (time < 60)
+ if (time < 60) { printf("%4ds", time);- else if (time < 3600)
- printf("%2d:%02d", time / 60, time % 60);- else
- printf("%dh %d:%02d", time / 3600, time % 3600 / 60, time % 60);-
+ }
+ else {+ if (time < 3600) printf("%2d:%02d", time / 60, time % 60);+ else printf("%dh %d:%02d", time / 3600, time % 3600 / 60, time % 60);+ }
printf(" \x1B[0m %s \n", name);}
@@ -101,8 +97,7 @@
printf("\x1B[10A");- if (clock_gettime(CLOCK_MONOTONIC, ×pec))
- {+ if (clock_gettime(CLOCK_MONOTONIC, ×pec)) {perror(fancy->argv0);
exit(1);
}
@@ -113,21 +108,17 @@
delta = timespec.tv_sec - fancy->timespec.tv_sec;
if (timespec.tv_nsec - fancy->timespec.tv_nsec > 500000000L) delta++;
- if (fancy->white == fancy->chess.white)
- our_time -= delta;
- else
- their_time -= delta;
+ if (fancy->white == fancy->chess.white) our_time -= delta;
+ else their_time -= delta;
done = 0;
- if (our_time < 0)
- {+ if (our_time < 0) {done = 1;
our_time = 0;
}
- if (their_time < 0)
- {+ if (their_time < 0) {done = 1;
their_time = 0;
}
@@ -134,11 +125,11 @@
moonfish_clock(their_time, fancy->their_name);
- for (y = 0 ; y < 8 ; y++)
- {+ for (y = 0 ; y < 8 ; y++) { printf(" ");- for (x = 0 ; x < 8 ; x++)
+ for (x = 0 ; x < 8 ; x++) {moonfish_fancy_square(fancy, x, y);
+ }
printf("\x1B[0m\n");}
@@ -153,8 +144,8 @@
fancy = data;
- for (;;)
- {+ for (;;) {+
pthread_mutex_lock(fancy->mutex);
moonfish_fancy(fancy);
@@ -185,10 +176,11 @@
static int moonfish_move_from(struct moonfish_chess *chess, struct moonfish_move *move, int x0, int y0, int x1, int y1)
{- if (!chess->white)
- {- x0 = 9 - x0; y0 = 9 - y0;
- x1 = 9 - x1; y1 = 9 - y1;
+ if (!chess->white) {+ x0 = 9 - x0;
+ y0 = 9 - y0;
+ x1 = 9 - x1;
+ y1 = 9 - y1;
}
y0 = 10 - y0;
@@ -204,19 +196,16 @@
int *time;
long int *nano;
- if (clock_gettime(CLOCK_MONOTONIC, ×pec))
- {+ if (clock_gettime(CLOCK_MONOTONIC, ×pec)) {perror(fancy->argv0);
exit(1);
}
- if (fancy->white)
- {+ if (fancy->white) {if (fancy->chess.white) time = &fancy->their_time, nano = &fancy->their_nano;
else time = &fancy->our_time, nano = &fancy->our_nano;
}
- else
- {+ else {if (fancy->chess.white) time = &fancy->our_time, nano = &fancy->our_nano;
else time = &fancy->their_time, nano = &fancy->their_nano;
}
@@ -227,8 +216,7 @@
timespec.tv_sec -= prev_timespec.tv_sec;
timespec.tv_nsec -= prev_timespec.tv_nsec;
- while (timespec.tv_nsec < 0)
- {+ while (timespec.tv_nsec < 0) {timespec.tv_nsec += 1000000000L;
timespec.tv_sec--;
}
@@ -236,8 +224,7 @@
*time -= timespec.tv_sec;
*nano -= timespec.tv_nsec;
- while (*nano < 0)
- {+ while (*nano < 0) {*nano += 1000000000L;
(*time)--;
}
@@ -251,13 +238,11 @@
char *arg;
struct moonfish_move move;
- if (fancy->white == fancy->chess.white)
- {+ if (fancy->white == fancy->chess.white) {white_time = fancy->our_time * 1000 + fancy->our_nano / 1000000;
black_time = fancy->their_time * 1000 + fancy->their_nano / 1000000;
}
- else
- {+ else {white_time = fancy->their_time * 1000 + fancy->their_nano / 1000000;
black_time = fancy->our_time * 1000 + fancy->our_nano / 1000000;
}
@@ -273,8 +258,7 @@
moonfish_wait(out, "readyok");
fprintf(in, "go wtime %d btime %d", white_time, black_time);
- if (fancy->increment > 0)
- fprintf(in, " winc %d binc %d", fancy->increment * 1000, fancy->increment * 1000);
+ if (fancy->increment > 0) fprintf(in, " winc %d binc %d", fancy->increment * 1000, fancy->increment * 1000);
fprintf(in, "\n");
arg = moonfish_wait(out, "bestmove");
@@ -282,8 +266,7 @@
pthread_mutex_lock(fancy->mutex);
- if (moonfish_from_uci(&fancy->chess, &move, arg))
- {+ if (moonfish_from_uci(&fancy->chess, &move, arg)) {fprintf(stderr, "%s: invalid move '%s' from the bot\n", fancy->argv0, arg);
exit(1);
}
@@ -328,14 +311,12 @@
moonfish_spawn(command, &in, &out, NULL);
- if (tcgetattr(0, &moonfish_termios))
- {+ if (tcgetattr(0, &moonfish_termios)) {perror(argv[0]);
return 1;
}
- if (atexit(&moonfish_exit))
- {+ if (atexit(&moonfish_exit)) {moonfish_exit();
return 1;
}
@@ -344,8 +325,7 @@
sigemptyset(&action.sa_mask);
action.sa_flags = 0;
- if (sigaction(SIGTERM, &action, NULL) || sigaction(SIGINT, &action, NULL) || sigaction(SIGQUIT, &action, NULL))
- {+ if (sigaction(SIGTERM, &action, NULL) || sigaction(SIGINT, &action, NULL) || sigaction(SIGQUIT, &action, NULL)) {perror(argv[0]);
return 1;
}
@@ -353,15 +333,13 @@
termios = moonfish_termios;
termios.c_lflag &= ~(ECHO | ICANON);
- if (tcsetattr(0, TCSANOW, &termios))
- {+ if (tcsetattr(0, TCSANOW, &termios)) {perror(argv[0]);
return 1;
}
fancy = malloc(sizeof *fancy);
- if (fancy == NULL)
- {+ if (fancy == NULL) {perror(argv[0]);
return 1;
}
@@ -372,28 +350,18 @@
moonfish_chess(&fancy->chess);
if (args[0].value != NULL) moonfish_from_fen(&fancy->chess, args[0].value);
- if (args[2].value != NULL)
- {- if (!strcmp(args[2].value, "white"))
- {- fancy->white = 1;
- }
- else if (!strcmp(args[2].value, "black"))
- {- fancy->white = 0;
- }
- else
- {+ if (args[2].value != NULL) {+ fancy->white = -1;
+ if (!strcmp(args[2].value, "white")) fancy->white = 1;
+ if (!strcmp(args[2].value, "black")) fancy->white = 0;
+ if (fancy->white == -1) {fprintf(stderr, "%s: unknown color '%s'\n", argv[0], args[2].value);
return 1;
}
}
- else
- {- if (args[0].value == NULL)
- fancy->white = time(NULL) % 2;
- else
- fancy->white = fancy->chess.white;
+ else {+ if (args[0].value == NULL) fancy->white = time(NULL) % 2;
+ else fancy->white = fancy->chess.white;
}
fancy->x = 0;
@@ -405,8 +373,7 @@
fancy->their_name = command[0];
- if (sscanf(args[1].value, "%d+%d", &fancy->our_time, &fancy->increment) != 2)
- {+ if (sscanf(args[1].value, "%d+%d", &fancy->our_time, &fancy->increment) != 2) {fprintf(stderr, "%s: unknown time control '%s'\n", argv[0], args[1].value);
return 1;
}
@@ -419,11 +386,10 @@
fprintf(in, "uci\n");
- for (;;)
- {+ for (;;) {+
arg = moonfish_next(out);
- if (arg == NULL)
- {+ if (arg == NULL) {fprintf(stderr, "%s: UCI error\n", argv[0]);
return 1;
}
@@ -449,8 +415,7 @@
fprintf(in, "isready\n");
moonfish_wait(out, "readyok");
- if (clock_gettime(CLOCK_MONOTONIC, &fancy->timespec))
- {+ if (clock_gettime(CLOCK_MONOTONIC, &fancy->timespec)) {perror(argv[0]);
return 1;
}
@@ -460,8 +425,7 @@
printf("\x1B[6n");fflush(stdout);
- for (;;)
- {+ for (;;) {ch = getchar();
if (ch == EOF) return 1;
if (ch == 0x1B) break;
@@ -471,14 +435,12 @@
oy -= 11;
error = pthread_create(&thread, NULL, &moonfish_start, fancy);
- if (error != 0)
- {+ if (error) {fprintf(stderr, "%s: %s\n", fancy->argv0, strerror(error));
exit(1);
}
- if (fancy->white != fancy->chess.white)
- {+ if (fancy->white != fancy->chess.white) {*name++ = ' ';
moonfish_go(fancy, names + 1, name, in, out);
name += strlen(name);
@@ -490,8 +452,8 @@
printf("\x1B[?1000h");fflush(stdout);
- for (ch0 = 0 ; ch0 != EOF ; ch0 = getchar())
- {+ for (ch0 = 0 ; ch0 != EOF ; ch0 = getchar()) {+
if (fancy->white && !fancy->chess.white) continue;
if (!fancy->white && fancy->chess.white) continue;
@@ -507,8 +469,8 @@
ch0 = getchar();
if (ch0 == EOF) break;
- if (ch0 == 0x20 && fancy->x == 0)
- {+ if (ch0 == 0x20 && fancy->x == 0) {+
pthread_mutex_lock(fancy->mutex);
ch = getchar();
@@ -531,9 +493,8 @@
continue;
}
- if (fancy->x != 0)
- if (ch0 == 0x20 || ch0 == 0x23)
- {+ if (fancy->x != 0 && (ch0 == 0x20 || ch0 == 0x23)) {+
ch = getchar();
if (ch == EOF) break;
x1 = ch - 0x21 - ox;
@@ -550,8 +511,8 @@
if (x1 == 0) continue;
if (y1 == 0) continue;
- if (moonfish_move_from(&fancy->chess, &move, fancy->x, fancy->y, x1, y1) == 0)
- {+ if (!moonfish_move_from(&fancy->chess, &move, fancy->x, fancy->y, x1, y1)) {+
*name++ = ' ';
moonfish_to_uci(&fancy->chess, &move, name);
name += strlen(name);
@@ -581,14 +542,10 @@
printf("\x1B[?1000h");fflush(stdout);
}
- else
- {+ else {pthread_mutex_lock(fancy->mutex);
- if (ch0 == 0x20 && x1 == fancy->x && y1 == fancy->y)
- fancy->x = 0;
- else
- fancy->x = x1,
- fancy->y = y1;
+ if (ch0 == 0x20 && x1 == fancy->x && y1 == fancy->y) fancy->x = 0;
+ else fancy->x = x1, fancy->y = y1;
x1 = 0;
y1 = 0;
moonfish_fancy(fancy);
--- a/tools/tools.h
+++ b/tools/tools.h
@@ -6,8 +6,7 @@
#include <stdio.h>
-struct moonfish_arg
-{+struct moonfish_arg {char *letter;
char *name;
char *format;
@@ -27,6 +26,6 @@
int moonfish_int(char *arg, int *result);
-int moonfish_pgn(FILE *file, struct moonfish_chess *chess, struct moonfish_move *move, int allow_attr);
+int moonfish_pgn(FILE *file, struct moonfish_chess *chess, struct moonfish_move *move, int allow_tag);
#endif
--- a/tools/utils.c
+++ b/tools/utils.c
@@ -15,26 +15,22 @@
int pid;
long int count, i;
- if (pipe(p1) < 0)
- {+ if (pipe(p1) < 0) { perror("pipe");exit(1);
}
- if (pipe(p2) < 0)
- {+ if (pipe(p2) < 0) { perror("pipe");exit(1);
}
pid = fork();
- if (pid < 0)
- {+ if (pid < 0) { perror("fork");exit(1);
}
- if (pid != 0)
- {+ if (pid != 0) {*in_fd = p1[1];
*out_fd = p2[0];
close(p1[0]);
@@ -42,14 +38,12 @@
return;
}
- if (directory != NULL && chdir(directory) != 0)
- {+ if (directory != NULL && chdir(directory)) { perror("chdir");exit(1);
}
- if (dup2(p1[0], 0) != 0 || dup2(p2[1], 1) != 1 || dup2(p2[1], 2) != 2)
- {+ if (dup2(p1[0], 0) != 0 || dup2(p2[1], 1) != 1 || dup2(p2[1], 2) != 2) { perror("dup2");exit(1);
}
@@ -69,29 +63,25 @@
moonfish_fork(argv, &in_fd, &out_fd, directory);
*in = fdopen(in_fd, "w");
- if (*in == NULL)
- {+ if (*in == NULL) { perror("fdopen");exit(1);
}
*out = fdopen(out_fd, "r");
- if (*out == NULL)
- {+ if (*out == NULL) { perror("fdopen");exit(1);
}
errno = 0;
- if (setvbuf(*in, NULL, _IOLBF, 0))
- {+ if (setvbuf(*in, NULL, _IOLBF, 0)) { if (errno != 0) perror("setvbuf");exit(1);
}
errno = 0;
- if (setvbuf(*out, NULL, _IOLBF, 0))
- {+ if (setvbuf(*out, NULL, _IOLBF, 0)) { if (errno != 0) perror("setvbuf");exit(1);
}
@@ -108,8 +98,8 @@
{char *line, *arg, *buffer;
- for (;;)
- {+ for (;;) {+
line = moonfish_next(file);
if (line == NULL) exit(1);
@@ -126,7 +116,7 @@
errno = 0;
long_result = strtol(arg, &end, 10);
- if (errno != 0 || *end != 0) return 1;
+ if (errno || *end != 0) return 1;
if (long_result < INT_MIN) return 1;
if (long_result > INT_MAX) return 1;
*result = long_result;
@@ -143,11 +133,10 @@
col1 = 0;
col2 = 0;
- for (i = 0 ; args[i].letter != NULL || args[i].name != NULL ; i++)
- {+ for (i = 0 ; args[i].letter != NULL || args[i].name != NULL ; i++) {+
n = 0;
- if (args[i].letter != NULL)
- {+ if (args[i].letter != NULL) {n += 1 + strlen(args[i].letter);
if (args[i].format != NULL) n += strlen(args[i].format);
}
@@ -154,8 +143,7 @@
if (n > col1) col1 = n;
n = 0;
- if (args[i].name != NULL)
- {+ if (args[i].name != NULL) {n += 2 + strlen(args[i].name);
if (args[i].format != NULL) n += 1 + strlen(args[i].format);
}
@@ -169,17 +157,15 @@
if (args[0].letter == NULL && args[0].name == NULL) return;
- for (i = 0 ; args[i].letter != NULL || args[i].name != NULL ; i++)
- {+ for (i = 0 ; args[i].letter != NULL || args[i].name != NULL ; i++) {+
fprintf(out, " ");
n = 0;
- if (args[i].letter != NULL)
- {+ if (args[i].letter != NULL) {n += 1 + strlen(args[i].letter);
fprintf(out, "-%s", args[i].letter);
- if (args[i].format != NULL)
- {+ if (args[i].format != NULL) {n += strlen(args[i].format);
fprintf(out, "\x1B[36m%s\x1B[0m", args[i].format);
}
@@ -188,28 +174,24 @@
if (args[i].letter != NULL && args[i].name != NULL) fprintf(out, ", ");
else fprintf(out, " ");
- while (n < col1)
- {+ while (n < col1) {fprintf(out, " ");
n++;
}
n = 0;
- if (args[i].name != NULL)
- {+ if (args[i].name != NULL) {n += 2 + strlen(args[i].name);
fprintf(out, "--%s", args[i].name);
- if (args[i].format != NULL)
- {+ if (args[i].format != NULL) {n += 1 + strlen(args[i].format);
fprintf(out, "=\x1B[36m%s\x1B[0m", args[i].format);
}
}
- if (args[i].description != NULL)
- {- while (n < col2)
- {+ if (args[i].description != NULL) {+
+ while (n < col2) {fprintf(out, " ");
n++;
}
@@ -227,31 +209,28 @@
char *name;
size_t length;
- for (i = 0 ; args[i].letter != NULL || args[i].name != NULL ; i++)
- {+ for (i = 0 ; args[i].letter != NULL || args[i].name != NULL ; i++) {+
name = args[i].letter;
if (name == NULL) continue;
length = strlen(name);
if (strncmp(arg, name, length)) continue;
- if (args[i].format == NULL)
- {+ if (args[i].format == NULL) {arg += length;
- if (arg[0] == 0)
- {+ if (arg[0] == 0) {args[i].value = "";
return 0;
}
continue;
}
- else
- {+ else {+
args[i].value = arg + length;
if (arg[length] == '=') args[i].value = arg + length + 1;
- if (arg[length] == 0)
- {+ if (arg[length] == 0) {if (*argc <= 0) return 1;
(*argc)--;
(*argv)++;
@@ -271,26 +250,22 @@
char *name;
size_t length;
- for (i = 0 ; args[i].letter != NULL || args[i].name != NULL ; i++)
- {+ for (i = 0 ; args[i].letter != NULL || args[i].name != NULL ; i++) {+
name = args[i].name;
if (name == NULL) continue;
length = strlen(name);
if (strncmp(arg, name, length)) continue;
- if (args[i].format == NULL)
- {- if (arg[length] == 0)
- {+ if (args[i].format == NULL) {+ if (arg[length] == 0) {args[i].value = "";
return 0;
}
return 1;
}
- else
- {- if (arg[length] == 0)
- {+ else {+ if (arg[length] == 0) {if (*argc <= 0) return 1;
(*argc)--;
(*argv)++;
@@ -297,8 +272,7 @@
args[i].value = (*argv)[0];
return 0;
}
- if (arg[length] == '=')
- {+ if (arg[length] == '=') {args[i].value = arg + length + 1;
return 0;
}
@@ -315,8 +289,8 @@
if (argc <= 0) return argv;
argv0 = argv[0];
- for (;;)
- {+ for (;;) {+
argc--;
argv++;
if (argc <= 0) return argv;
@@ -326,8 +300,7 @@
if (!strcmp(arg, "-")) return argv;
if (!strcmp(arg, "--") && rest_format != NULL) return argv + 1;
- if (!strcmp(arg, "--help") || !strcmp(arg, "-h") || !strcmp(arg, "-H"))
- {+ if (!strcmp(arg, "--help") || !strcmp(arg, "-h") || !strcmp(arg, "-H")) {moonfish_usage_to(args, rest_format, argv0, stdout);
exit(0);
}
@@ -335,14 +308,13 @@
if (arg[0] != '-') return argv;
arg++;
- if (arg[0] == '-')
- {+ if (arg[0] == '-') {arg++;
- if (moonfish_name_arg(args, arg, &argc, &argv) != 0) moonfish_usage(args, rest_format, argv0);
+ if (moonfish_name_arg(args, arg, &argc, &argv)) moonfish_usage(args, rest_format, argv0);
continue;
}
- if (moonfish_letter_arg(args, arg, &argc, &argv) != 0) moonfish_usage(args, rest_format, argv0);
+ if (moonfish_letter_arg(args, arg, &argc, &argv)) moonfish_usage(args, rest_format, argv0);
}
}
--
⑨