-2
I need to read a file that will be encoded as the example:
1.e4 e6 2.Nf3 d5 3.Nc3 Nf6 4.e5 Nfd7 5.d4 b6 6.Bd3 Ba6 7.O-O Be7 8.Ne2 Bxd3 9.Qxd3 c5 10.c4 Nc6 11.cxd5 Nb4 12.Qe4 Nxd5 13.Qg4 Kf8 14.Nf4 Nxf4 15.Bxf4 h5 16.Qg3 cxd4 17.Rfd1 Rc8 18.Rxd4 Rc7 19.Rad1 h4 20.Qg4 h3 21.Ng5 Bxg5 22.Bxg5 Qe8 23.R4d3 f6 24.exf6 gxf6 25.Bf4 f5 26.Rxh3 Rxh3 27.Bd6+ Kf7 28.Qxh3 Rc8 29.Qh7+ Kf6 30.Bg3 Rc4 31.Bh4+ 1-0
This is a sequence of chess rounds with the proper movements made by the white and black pieces, respectively, the rounds being separated by the numbers "1.... 2.... 3.... etc". In case, I need to read the file, store the movement performed, validate, and, if correct, go to the next move, otherwise I close my program. Obs the whole round is written in a single line.
My question is how do I build the algorithm to read each movement at a time by storing it in a character array. The code is being written in C.
Solution found:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define TAM 10
int main(int argc, char *argv[]) {
char branca[TAM], preta[TAM];
int junk;
FILE *f = fopen(argv[1], "r");
memset(branca, 0x0, TAM);
memset(preta, 0x0, TAM);
while(fscanf(f, "%d.%s %s", &junk, branca, preta) && !feof(f)){
printf("BRANCA: %s\n", branca);
printf("PRETA: %s\n", preta);
printf("---------------\n");
}
fclose(f);
return 0;
}
Will the round number always be followed by the '.' (dot) character? Each sequence of characters that identifies a movement will be followed by the ' ' (space)?
– anonimo
yes! 1.E4(white piece movement) E6(black piece movement) 2.Nf3(mov. white) D5(mov. black) etc
– Daniele Cavalcante
Then try with:
fscanf(, arquivo, "%d.%s %s", &rodada[i], mov_branca[i], mov_preta[i]);
until it detects EOF. Now I have no idea what "validate movement" is. Maybe you don’t need the round vector, the i+1 value itself will provide the round.– anonimo
ah, that! as I only need one vector at a time, I will use what you said this way: while(fscanf(f, "%d.%s %s", &junk, white, black) && & !feof(f)){... About the validator, it is a part of the job that asks to validate if each move of the file is correct, ie if certain piece performed the move correctly. If the movement read is wrong according to the rules of each piece, the program ends. If the movement is correct, I move to the next. So I just need one movement at a time. Thank you so much for your help!
– Daniele Cavalcante