about xboard

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
Look
Posts: 364
Joined: Thu Jun 05, 2014 2:14 pm
Location: Iran
Full name: Mehdi Amini

about xboard

Post by Look »

Hi,

Given the recent increased progress on chess GUIs , Some have already recent files and specially replay training like that of ChessBase software. Or they can implement them. For the time being I may spend time on soft-wares other than Xboard.

Yet it has some flaws that need to be corrected too. The following code is an attempt to fix one of them. Code has to be executed in every Xboard project folder. Also when the wrong pattern happens in multiple lines , they have to be corrected manually. Like search and replace.

Code: Select all

// This code corrects a P((args)) to (args) , single line in XBoard project

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <stdnoreturn.h>

#include <unistd.h>
#include <limits.h>

#include <pthread.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <glib/gdir.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

#define STR_SIZE 256 
#define MY_FN "my_temp.tmp"
bool extension(gchar *fn, const char c);
bool ccode(gchar *fn);
void copy_file(const char* const from , char * to);
void restyle(const char * const line_str , char *rsl);
noreturn void err_exit(char * s);

int main(void)
{
    FILE * my_tempfile;

    gchar fn[128]={'\0'};
    gchar *  fn_ptr=&fn[0]; 

    char line_str[STR_SIZE]={'\0'};
    FILE *sf=NULL;

    gchar cwd[256]={'\0'};
    gchar * cwd_ptr;

    GDir *dir_ptr;

    cwd_ptr=strcpy(&cwd[0],g_get_current_dir() );

    if( cwd_ptr==NULL)
        perror("Bad dir.\n");
    g_printf("cwd is %s \n\n",cwd);

    dir_ptr=g_dir_open(cwd,0,NULL);
    if(dir_ptr==NULL)
        perror("dir err.\n");

    while( (fn_ptr=g_dir_read_name(dir_ptr)) !=NULL) // read all files
    {
        g_print(">fn is %s\n",fn_ptr);

        if(ccode(fn_ptr))
        {
            my_tempfile=fopen(MY_FN,"wt");

            printf(">>ccode is %s\n",fn_ptr);
            sf=fopen(fn_ptr,"rt");
            int l=0;

            while(fgets(line_str,STR_SIZE-1,sf) !=NULL) // read all lines in sf
            {
                l++;

                char  rsl[STR_SIZE];
                restyle(line_str, rsl);

                fprintf(my_tempfile, "%s",rsl);

            }

            fclose(sf);
            fclose(my_tempfile);
            copy_file(MY_FN,fn_ptr);
            g_print(">>>file %s was updated.\n\n",fn_ptr);
        }
    }

    return EXIT_SUCCESS;
}

bool ccode(gchar *fn)
{
    bool r=false;
    size_t sl=strlen(fn);
    if(strstr(fn,".c")!=NULL && isalpha(fn[0]) && extension(fn,'c'))
        return true;
    if(strstr(fn,".h")!=NULL && isalpha(fn[0]) && extension(fn,'h'))
        return true;

    return r;
}

bool extension(gchar *fn, const char c)
{
    size_t sl=strlen(fn);
    assert(sl>=3);

    if(fn[sl]=='\0' && fn[sl-1]==c && fn[sl-2]=='.')
        return true;

    return false;
}


void copy_file(const char* const from , char *to)
{
    FILE *f1;
    FILE *f2;
    int c; // consider EOF

    assert(from != to);
    assert(from != NULL);
    assert(to != NULL);

    f1 = fopen(from, "rt");
    if(f1==NULL)
        err_exit("opening f1");

    f2 = fopen(to, "wt");

    if(f2==NULL)
        err_exit("opening f2");

    rewind(f1);
    rewind(f2);

    c = fgetc(f1);
    if(c==EOF)
        err_exit("first char in f1");
    while(c != EOF) 
    {
        fputc(c, f2);
        c = fgetc(f1);
    }

    fclose(f1);
    fclose(f2);

    return;
}

void restyle(const char * const line_str , char *rsl)
{
    char * old_open=strstr(line_str,"P((");
    char * old_close= strstr(line_str,"))"); 

    strncpy(&rsl[0], &line_str[0], STR_SIZE-1);

    size_t sl=strlen(line_str);
    assert(sl<STR_SIZE);

    if(old_open!=NULL && old_close!=NULL ) // line needs updating
    {

        assert(old_open<old_close);
        assert(old_open>line_str);
        assert(old_close>line_str);
        assert(old_open<line_str+sl);
        assert(old_close<line_str+sl);

        int n;

        old_open=strstr(line_str,"P((");
        assert(old_open!=NULL);

        char rslt1[STR_SIZE]={'\0'};
        strncpy(&rslt1[0], &line_str[0], STR_SIZE-1);
        for(n=0; line_str[n]!='\0'&& line_str[n+1]!='\0'&&n<512; n++)
            if(line_str+n >= old_open) 
            {
                rsl[n]=rslt1[n+2]; 
            }

        old_close= strstr(rsl,"))");
        assert(old_close!=NULL);
        char rslt[STR_SIZE]={'\0'};
        strncpy(&rslt[0], &rsl[0], STR_SIZE-1);

        for(n=0; rsl[n]!='\0' && n<512; n++)
            if(rsl+n >= old_close)
            {
                rsl[n]=rslt[n+1];
            }

    }

    return ;
}

noreturn void err_exit(char * s)
{
    perror(s);
    exit(EXIT_FAILURE);
}
Farewell.
User avatar
hgm
Posts: 27788
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: about xboard

Post by hgm »

This 'correction' seems a completely pointless cosmetic change to the source. I admit that the XBoard code is awful, and can probably be cleaned up in more than a hundred different ways, and the P()-macro matter would be pretty much at the bottom of the list. To make the source code presentable will pretty much require a complete rewrite, so any patches on the old source are a waste of time.

I am more interested in what you mean by 'recent files' and 'replay training'. XBoard has a 'Training Mode', but I never used it, and am not sure what it doesn.
User avatar
Look
Posts: 364
Joined: Thu Jun 05, 2014 2:14 pm
Location: Iran
Full name: Mehdi Amini

Re: about xboard

Post by Look »

[...]
hgm wrote: Mon Jul 12, 2021 8:36 pm I am more interested in what you mean by 'recent files' and 'replay training'. XBoard has a 'Training Mode', but I never used it, and am not sure what it doesn.
When user wants to open a file , in 'recent menu' , only files previously opened by XBoard should be shown. Right now a variety of files opened by may programs are shown.

As a training method , User can follow the played games of his/her role model. One loads a game , no moves are visible , use may skip opening moves as he/she wants (by pressing a key). After that for each position user tries to guess the played move of role model. Two possibilities are: User correctly guesses the move or incorrectly. Suppose PGN of the game is in clipboard. A correct guess does not change PGN. Incorrect one creates variation in PGN with a single move. After the game is ended , The whole PGN is shown to user. Now it is time for analysis. User analysis all moves with the help of a chess engine. Understanding when and how he/she misplayed the position. Trying to learn avoiding similar mistakes in future. For my case , I figured out that in every game I performed replay training , I blundered 2 to 3 times per game. Just do not expect a fix coming soon for this.
Farewell.
User avatar
hgm
Posts: 27788
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: about xboard

Post by hgm »

Look wrote: Tue Jul 13, 2021 9:37 amWhen user wants to open a file , in 'recent menu' , only files previously opened by XBoard should be shown. Right now a variety of files opened by may programs are shown.
You mean something similar to the 'recent engines' that show up in the Engine menu, but then for game files in the File menu?

Is that really useful? I suppose people do not often use the menu to load games in the fist place, but would just double-click the PGN file to launch XBoard with it.
As a training method , User can follow the played games of his/her role model. One loads a game , no moves are visible , use may skip opening moves as he/she wants (by pressing a key). After that for each position user tries to guess the played move of role model. Two possibilities are: User correctly guesses the move or incorrectly. Suppose PGN of the game is in clipboard. A correct guess does not change PGN.
I think so far this is exactly what XBoard Training mode does.
Incorrect one creates variation in PGN with a single move.
XBoard might not do this, but it would be trivial to implement. I think now it only throws up a popup to inform the user he entered the wrong move, and takes that move back.
After the game is ended , The whole PGN is shown to user.
I suppose the game would slowly become visible anyway as the user enters more moves (in the Move History window and Comment window). There doesn't seem any reason to withold the moves he has already guessed correctly from him.
Now it is time for analysis. User analysis all moves with the help of a chess engine.
I would prefer it to be the user's choice what he want to do next, rather than to force some automatic behavior on him. It could be that he doesn't want to analyze the entire game. Or not at all. It doesn't seem very taxing for a user that he has to select Analyze Game from the Mode menu.
Understanding when and how he/she misplayed the position. Trying to learn avoiding similar mistakes in future. For my case , I figured out that in every game I performed replay training , I blundered 2 to 3 times per game. Just do not expect a fix coming soon for this.
This might affect what we understand by 'analyzing all moves'. The current Analyze Game would only analyze the main line of a game, and ignore any included variations. Here you probably would want to also analyze the positions after the erroneous moves.

And how should the results of this analysis be presented? Should it again be entered as annotation to the game (in the form of score/depth comments)?
User avatar
Look
Posts: 364
Joined: Thu Jun 05, 2014 2:14 pm
Location: Iran
Full name: Mehdi Amini

Re: about xboard

Post by Look »

hgm wrote: Tue Jul 13, 2021 10:33 am
Look wrote: Tue Jul 13, 2021 9:37 amWhen user wants to open a file , in 'recent menu' , only files previously opened by XBoard should be shown. Right now a variety of files opened by may programs are shown.
You mean something similar to the 'recent engines' that show up in the Engine menu, but then for game files in the File menu?

Is that really useful? I suppose people do not often use the menu to load games in the fist place, but would just double-click the PGN file to launch XBoard with it.
After file menu , open , recents on the left.
As a training method , User can follow the played games of his/her role model. One loads a game , no moves are visible , use may skip opening moves as he/she wants (by pressing a key). After that for each position user tries to guess the played move of role model. Two possibilities are: User correctly guesses the move or incorrectly. Suppose PGN of the game is in clipboard. A correct guess does not change PGN.
I think so far this is exactly what XBoard Training mode does.
OK.
Incorrect one creates variation in PGN with a single move.
XBoard might not do this, but it would be trivial to implement. I think now it only throws up a popup to inform the user he entered the wrong move, and takes that move back.
Throwing a popup is wrong. Also that is a 'different move' not a 'wrong move'. Maybe the user move is actually a good one , even if not played in the game. After taking back , the game move is supposed to be played. User may guess only one move.
After the game is ended , The whole PGN is shown to user.
I suppose the game would slowly become visible anyway as the user enters more moves (in the Move History window and Comment window). There doesn't seem any reason to withold the moves he has already guessed correctly from him.
IMHO this might be optional as per user demand.
Now it is time for analysis. User analysis all moves with the help of a chess engine.
I would prefer it to be the user's choice what he want to do next, rather than to force some automatic behavior on him. It could be that he doesn't want to analyze the entire game. Or not at all. It doesn't seem very taxing for a user that he has to select Analyze Game from the Mode menu.
An analysis sometimes called 'infinite analysis'. Engine would think , user may traverse the game and variations. In order to understand what has happened during the game and training.
Understanding when and how he/she misplayed the position. Trying to learn avoiding similar mistakes in future. For my case , I figured out that in every game I performed replay training , I blundered 2 to 3 times per game. Just do not expect a fix coming soon for this.
This might affect what we understand by 'analyzing all moves'. The current Analyze Game would only analyze the main line of a game, and ignore any included variations. Here you probably would want to also analyze the positions after the erroneous moves.

And how should the results of this analysis be presented? Should it again be entered as annotation to the game (in the form of score/depth comments)?
I am not sure about this. There are several possibilities here user may choose from them.
Farewell.
User avatar
hgm
Posts: 27788
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: about xboard

Post by hgm »

Look wrote: Tue Jul 13, 2021 11:35 amAfter file menu , open , recents on the left.
OK, I see. You want it in the file-browse dialog.

This dialog is a GTK standard dialog, but I suppose the call to the library routine that pops it up contains an argument where you can pass files to be displayed in the left section. I vaguely recall I used something like that to make sure XBoard's 'themes' folder (in /usr/share/) appears on the left when you browse for other piece types.

So it would just be a matter of keeping a small list of filenames that have been recently opened, and passing that to the file-browse popup call. I suppose you want this to be remembered from one XBoard session to the next, though. So the list would have to be remembered as an option -recentGameFiles in the user settings file. (Just like -recentEngines already is.)
Throwing a popup is wrong. Also that is a 'different move' not a 'wrong move'. Maybe the user move is actually a good one , even if not played in the game. After taking back , the game move is supposed to be played. User may guess only one move.
Well, I don't know exactly what xboard does in Training Mode; as I mentioned, I have never used it.
IMHO this might be optional as per user demand.
Well, it is optional: the Move History window can be opened and closed at will.
An analysis sometimes called 'infinite analysis'. Engine would think , user may traverse the game and variations. In order to understand what has happened during the game and training.
Well, in XBoard that would be the Analysis item in the Mode menu. Point is that after reaching the game end, the user can choose whether he rather has infinite analysis or automatic stepping through the game, and from which position he would want to start that.