summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-09-05 06:59:04 +0300
committereug-vs <eugene@eug-vs.xyz>2022-09-05 06:59:04 +0300
commitfd548b28ce1f883d2b8ef1a12d2b4a28555724c2 (patch)
treef34a6d7c9b3d610d5e5ff94f2a7c58d0ae9143d5
parent12117d2ae40ca1d0db503bf0cb5e0e99ac986762 (diff)
downloadc-chess-fd548b28ce1f883d2b8ef1a12d2b4a28555724c2.tar.gz
feat: allow loading game from text file
-rw-r--r--src/main.c27
1 files changed, 18 insertions, 9 deletions
diff --git a/src/main.c b/src/main.c
index 3468405..78fd66b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -174,14 +174,14 @@ void index_to_notation(int index, char notation[2]) {
notation[1] = (index >> 4) + '1';
}
-Move input_move() {
- Move move;
+Move input_move(FILE* stream) {
+ Move move = { 0, 0, 0 };
char file;
int rank;
char file_destination;
int rank_destination;
- scanf(" %c%i %c%i", &file, &rank, &file_destination, &rank_destination);
+ fscanf(stream, " %c%i%c%i", &file, &rank, &file_destination, &rank_destination);
move.origin = notation_to_index(file, rank);
move.destination = notation_to_index(file_destination, rank_destination);
@@ -630,7 +630,8 @@ Move minimax_search(int* board, int color, int depth, int alpha, int beta, int*
}
int main() {
- FILE* record;
+ FILE* record = fopen(FILENAME, "r");
+ int is_reading = 1;
int board[128];
parse_FEN(DEFAULT_FEN, board);
@@ -656,9 +657,15 @@ int main() {
printf("##### Ply %i #####\n", ply);
printf("Current evaluation: %.2f\n", (double)evaluate_position(board, 0, WHITE) / 10);
- if (color == PLAYER) {
+ if (is_reading) {
+ move = input_move(record);
+ if (move.origin == move.destination) { // EOF
+ fclose(record);
+ is_reading = 0;
+ }
+ } else if (color == PLAYER) {
printf("Enter a move for %s:\n", COLORS[color]);
- move = input_move();
+ move = input_move(stdin);
index_to_notation(move.origin, move_in_notation);
index_to_notation(move.destination, move_in_notation + 2);
} else {
@@ -687,9 +694,11 @@ int main() {
make_move(move, board);
print_board(board);
- record = fopen("record.txt", "a");
- fprintf(record, "%s ", move_in_notation);
- fclose(record);
+ if (!is_reading) {
+ record = fopen("record.txt", "a");
+ fprintf(record, "%s ", move_in_notation);
+ fclose(record);
+ }
ply++;
}