Chess game

User projects written in or related to FreeBASIC.
frisian
Posts: 249
Joined: Oct 08, 2009 17:25

Re: Chess game

Post by frisian »

FB_Numpty wrote:It's not as simple as the original problem with lower/upper case characters and it doesn't seem to be a problem with movegen function as latest version is perfect on perft for the following position as far as i tested it (to d = 7)
8/PPP4k/8/8/8/8/4Kppp/8 w - - 0 1
I'm wondering if it is related to WQ/BQ values - in all 3 games by the time of the promotion to a new queen, the original Q had been taken. You're more familiar with this aspect of the coding having made changes, I wonder does it suggest anything obvious to you?
This type of error can be hard to find but the program is simple that make live a little easier.
First thing what comes to mind is that the error look's identical to some error's I have found. When the program can not process the info that it receives or it sends info that the other side can not process this can lead to a forfeit on time. I tested the move generator on some positions to depth 10 in order to see if the moves generated where correct in positions with castling, so it is very unlikely that the move generator is causing this. The part that should keep track of the number of queen's is not working correctly but that has no influence on the game play. I have seen a game where 2 pawn's where promoted to queen (same side). I have not altered that part of the code.

In order to get an idea what could caused this we need to find out whats going on inside the program.
I assume that you used the version that I posted on 23 April.

(I assume that the program end in a normal way by receiving the command "quit" if not or in case of a crash the code needs to changed/extended)
To find out what info is send and received by the program we need to put extra code in the program.
At the top of the program place
Open "error"+Str(Int(Timer))+".txt" For Append As #99
this will open a file named error????.txt where ???? is the integer part of the timer.
(timer will reset at midnight)

Just under Function SendCommand(sWBCmd As String) As Integer
Print #99,"$$";swbcmd;"$$"
The text send will be printed between "$$" making spaces easier to spot

In Function GetCommand As String between the line GetCommand = sTemp and the line End function insert
Print #99,"##";stemp;"##"
This will show the text that the program receives.

In Sub CommandLoop just after the line ElseIf sWBCmd = "quit" Then add
savegame : Close #99

In the version of 23 April there is still the modified version of SaveGame (that I used, forgot to restore it).
Modified version of SaveGame.

Code: Select all

Sub SaveGame

  '****************************************
  ' Writes game details to 'SaveNOmega.DAT'
  ' routine saves:
  '   - board position
  '   - move list
  '****************************************
  Dim As Integer x, piece
 ' Dim As UInteger y, ff = FreeFile
 Dim As UInteger y, ff = 99
  /'
  'OPEN "O", 1, "SaveNomega.DAT"

  Open "SaveNomega.DAT" For Output As #ff

  Print #ff, cmpclr                              ' ### computer is ???
  Print #ff, side                                ' ### witch side is to play
  Print #ff, bookhit                             ' ### out of book ???
  Print #ff, w_cas(1)                            ' ### is castling allowed
  Print #ff, w_cas(2)
  Print #ff, b_cas(1)
  Print #ff, b_cas(2)
  Print #ff, mat_left
  Print #ff, endgame

  For X = 90 To 20 Step -10
    For Y = X+1 To X+8
      Print #ff, B(Y)
    Next Y
  Next X
  If moveno = 0 Then moveno = 1                  '###
  Print #ff, MoveNo

  For y = 1 To MoveNo
    Print #ff, move_hist(y, 1), move_hist(y, 2), move_hist(y, 3), move_hist(y, 4)
  Next y
'/
 
  Print #ff,
  Print #ff, "//////////////////////////////////////////"
  Print #ff,

  For X = 90 To 20 Step -10
    For Y = X+1 To X+8
      piece = B(y)
      If piece < 0 Then piece += 20
      Print #ff, "   "; b_str(piece);            ' u_str(Y) ;
    Next Y
    Print #ff, "   ***  ";(X \ 10) - 1
    Print #ff, "   -----------------------------"
  Next X

  Print #ff, "   *********************************** "
  Print #ff, "   *********************************** "

  Print #ff, "   a   b   c   d   e   f   g   h   "

  Print #ff,
  Print #ff, "Move   White    Black"

  For y = 1 To MoveNo
    Print #ff, Using " ###"; y;
    Print #ff, "   "; move_2_str(move_hist(y, 1)); "-"; move_2_str(move_hist(y, 2));
    If move_hist(y, 3) = 0 Then Exit For
    Print #ff, "    "; move_2_str(move_hist(y, 3)); "-"; move_2_str(move_hist(y, 4))
  Next y

  Close #ff

End Sub
Start playing and when there is problem look in the error file to see if you can spot something. Could be that the letter that tells into what piece the pawn is promoted is in uppercase. Keep an eye on the files produced, for every game there is a file, when the games are short you could end up with a lot of files.

It's a quick way to see what goes on.

P.S.
I have updated the version, it now has the original SaveGame routine.
FB_Numpty
Posts: 33
Joined: Jan 28, 2014 19:22

Re: Chess game

Post by FB_Numpty »


First thing what comes to mind is that the error look's identical to some error's I have found. When the program can not process the info that it receives or it sends info that the other side can not process this can lead to a forfeit on time. I tested the move generator on some positions to depth 10 in order to see if the moves generated where correct in positions with castling, so it is very unlikely that the move generator is causing this. The part that should keep track of the number of queen's is not working correctly but that has no influence on the game play. I have seen a game where 2 pawn's where promoted to queen (same side). I have not altered that part of the code.
Yes I was using the 23/4/14 version. I'm pretty sure you're right and it isn't a move gen problem - this looks robust and that perft position should have identified any issues with promotion. Yes, I couldn't recall WQ/BQ variables causing this problem - the only time I saw a problem like this in the original Numpty was in the Chessmaster GUI which if I remember correctly had a bug in the winboard promotion moce representation (I fixed this eventually). I remember that handling pawn promotion was a difficult bug ridden exercise which took a long time to sort to work correctly - multiple promotions are handled ok also. It is odd because I didn't have any problems with pawn promotion when I played manually against the engine, nor in several hundred engine v engine games, only when playing on ICS (where 3 games were lost due to this out of only 6 games played altogether).

Your added debugging feature is a very good addition, I will give it a go and replay the game I posted, thanks for the suggestion.

On a separate issue, I'm conscious version control is a potential issue - I integrated the winboard draw feature into 23/4/14 version. This was one of the newer winboard features and the code needed some changes - winboard requires the engine to offer a draw rather than claim a draw in certain conditions (Numpty was doing the latter) This removes the problem where the engine will occasionally hang when it thinks there is a three fold repetition (ie it has declared the game over as drawn when it isn't), and similarly for 50 move rule. The draw feature also enables Numpty to accept a draw offer (I've set this to a pre-defined contempt value - ie if best_score < -100 .....) I will post the code changes tomorrow.
FB_Numpty
Posts: 33
Joined: Jan 28, 2014 19:22

Re: Chess game

Post by FB_Numpty »

Re my earlier post, here are code changes/additions for implementing the draw function:

In Sub CommandLoop:

Code: Select all

sTemp = "feature myname=" + Chr$(34) + "NoMega-14/04/23" + Chr$(34) + " sigint=0 sigterm=0 reuse=0 analyze=0 setboard=1 draw=1 name=0" 'added draw=1 28414
and:

Code: Select all

ELSEIF sWBCmd = "draw" THEN                   'added draw code 28/4/14
        if best_score <= -100 then
            SendCommand("offer draw")
            ending
        end if
        continue Do

In Assessboard function:

Code: Select all

 If fifty_move = 100 And WinboardMode = 1 Then SendCommand("offer draw")  'added 28414 (can't end game with result)
 
 If fifty_move > 100 And WinboardMode = 1 Then          ' changed to > 100 28414 (def draw if >100)
    SendCommand( "1/2-1/2 {Draw by 50 move rule}")
    Return TRUE
  ElseIf fifty_move = 100 And WinboardMode = 0 Then
    display
    Print "Draw by 50 move rule!"
    Return TRUE                              'GoTo asb7
  End If
and:

Code: Select all

  If rep_pos = 3 then                       'added draw code 28414
     if WinboardMode = 1 then
      SendCommand("offer draw")
      end if
 end if

  
  If rep_pos = 4 Then                       'changed from 3 to 4 (28414)
    game_end = TRUE
    If WinboardMode = 1 Then
      SendCommand( "1/2-1/2 {Draw by repetition}")
    Else
      display
      Print "Draw by 3 fold repetition!!!!"
    End If
  End If

On a different topic, notwithstanding the ongoing development, given there has been a significant improvement since the last offical engine release (Numpty DD), would you have any objection if I make a release with the most recent code base, of course with proper accreditation (I am getting asked from a few dedicated Numpty followers!). As with all release versions, I always make a clear record of what changes have been made. The last version was named DD in rememberance of renowned chess programmer Don Dailey who died shortly before the engine was released. In terms of naming, I thought one naming option for the next release might be Numpty_frisian (or of course your real name if you wish!)
FB_Numpty
Posts: 33
Joined: Jan 28, 2014 19:22

Re: Chess game

Post by FB_Numpty »

I've been playing around with the latest Numpty version and played a lot of test games against self and range of opponents. I've found a few bits and bobs:

I've fixed the problem with the crashes on piece promotion - think this is due to different handling of promoted piece in different gui (remember now it was a problem with Chessmaster specifically). All works well across a range of different gui both online and off line with the following change:

Code: Select all

 ' ...in winboard mode
  If WinboardMode = 1 And B(MoveTo) = 1 And MoveTo > 90 Then
    If pr_str = "q" or pr_str = "Q" Then B(MoveTo) = 6
    If pr_str = "r" or pr_str = "R" Then B(MoveTo) = 4
    If pr_str = "b" or pr_str = "B" Then B(MoveTo) = 3
    If pr_str = "n" or pr_str = "N" Then B(MoveTo) = 2
  End If

  If WinboardMode = 1 And B(MoveTo) = -1 And MoveTo < 29 Then
    If pr_str = "q" or pr_str = "Q" Then B(MoveTo) = -6
    If pr_str = "r" or pr_str = "R" Then B(MoveTo) = -4
    If pr_str = "b" or pr_str = "B" Then B(MoveTo) = -3
    If pr_str = "n" or pr_str = "N" Then B(MoveTo) = -2
End If


Also, I've reverted the 'easy move' code to the original, as in the current version it doesn't work as no easy move's are being made. Given the speed increase, I also took the liberty of changing the condition to:

Code: Select all

If depth = 5 And root_sort(1) - root_sort(2) > 250 And Move_list(depth + 1, 1, 1) = B_M_From_easy_move And Move_list(depth + 1, 1, 2) = B_M_To_easy_move Then it_depth = max_depth
(ie changing depth = 5 from 4). Sometimes in the original version d=4 was not sufficient to protect against back rank mates and with the speed increase, depth = 5 is still quick enough for an easy move!

Also, strangely for such a mature project, a bug i've not seen in playing many thousands of games with various versions - I found the following game in one of the test tournaments:
[Event "40_10_test"]
[Site "MyPC-PC"]
[Date "2014.05.12"]
[Round "11"]
[White "Numpty_DD"]
[Black "Numpty10514_d5"]
[Result "1-0"]
[BlackElo "1575"]
[ECO "B30"]
[Opening "Sicilian"]
[Time "22:11:30"]
[Variation "2...Nc6 3.Nc3 Nf6"]
[WhiteElo "1500"]
[TimeControl "40/600:40/600:40/600"]
[Termination "adjudication"]
[PlyCount "22"]
[WhiteType "program"]
[BlackType "program"]

1. e4 c5 2. Nf3 Nc6 3. Nc3 Nf6 4. Bb5 Qc7 5. Bxc6 dxc6 6. h3 e5 7. d3 h6 8.
b3 Be6 9. Nxe5 {(f3e5) +10.42/6 20} Bd6 {(f8d6) -10.90/6 16} 10. Nf3
{(e5f3) +10.49/6 11} Nd7 {(f6d7) -10.85/6 15} 11. O-O {(e1g1) +10.54/6 18}
O-O {(e8g8) -10.80/6 13 Arena Adjudication} 1-0
The corresponding game with switched colours finished exactly the same indicating the bug is in the original version. Looking at the evals - at first guess wonder if the black queen 'disappears' somehow during force mode (first 8 moves are from a GM games database), can't think this sort of error could be in the eval function, still will be interesting to find out!
frisian
Posts: 249
Joined: Oct 08, 2009 17:25

Re: Chess game

Post by frisian »

FB_Numpty

I have not been very active with FB or the forum the past few month, I hope to catch up with things.
Would like to hear from you if still working on your chess program.
Luis Babboni
Posts: 375
Joined: Mar 15, 2015 12:41

Re: Chess game

Post by Luis Babboni »

Hi,

I´m trying to do my own moves generator.

How to debug it?

I read about programs called "perft" in C++ to do it.
But how you did it in FB?
For the moment all that I could do is see if the number of moves from some position till some depth are correct or not comparing it with literature, but if not.... is so hard to find where is the bug.

Thanks.
Post Reply