How to separate a string from a file that contains a sequence of chess rounds?

Asked

Viewed 109 times

-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)?

  • yes! 1.E4(white piece movement) E6(black piece movement) 2.Nf3(mov. white) D5(mov. black) etc

  • 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.

  • 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!

1 answer

0


Good night.

Well, here’s one of the ways to do it, there are other ways...

In the example below, the reading is done character by character and all the file data will be printed on the screen. In this example, I show you one round per line; to identify your separation, I write one ' n' every two characters of read spaces.

For your need, just write the letters on a pointer using dynamic allocation instead of printing them on the screen. If you want to separate the commands by move instead of round, just remove the if on line 20.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE* jogo;

    printf("Mostrando movimentos...\n");

    jogo = fopen("jogo.txt", "r");

    char c = fgetc(jogo);
    int counter = 0;

    do
    {
        if (c == ' ')
        {
            counter++;
            if (counter % 2 == 0) // Quebra linha a cada 2 espaços em branco
                printf("\n");
        }

        printf("%c", c);
        c = fgetc(jogo);
    } while (c != EOF);


    fclose(jogo);
    return 0;
}

About dynamic allocation, if necessary, you can find more information here: https://www.ime.usp.br/~pf/algorithms/classes/aloca.html

  • 1

    Yes! I managed to do it another way, but I’m not able to put the code here in the comment (beginner). Thank you so much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.