February 27 2017

This past weekend I once again spent time working on my chess program. As you may recall, if you have been following along, I last discussed using collections as a means of determining the attack path and squares of white to determine the best possible defense against said attack. I discovered two things.

  • The collection can be replaced with a compound case statement
  • As the game wore on, processing time increased due to the growing number of choices in moves.

Lets look at the compound case statement.

I am calling the attack piece AP and the piece being attacked as PA

Also remember that it is blacks move so PA is black and AP is white. Now lets say AP is a Bishop on square A4 and it is attacking Blacks Queen on E8. If Black’s Queen can take the Bishop without reprisal then the software does that with no issue. In this case however that is not the case so we need to know which squares are between A4 and E8

Here is my statement

If Left(AP,1)  < Left(PA,1) .and. Right(AP,1) < Right(PA,1) then

‘We know AP is to the lower right of PA and attacks at a diagonal.

else if Left(AP,1)  > Left(PA,1) .and. Right(AP,1) > Right(PA,1) then

‘We know AP is to the upper right of PA and attacks at a diagonal.

etc..

So all I actually need to solve for is the relationship as position of the attack piece to the piece under attack the the software fills in the blanks building my list of squares black needs to look at. Once that is known the code steps through the list looking for blocks, attacks or protection of the attacked piece.

Now lets look at the growing processing time based on the number of moves and squares to address. As I sat and twiddled my thumbs watching the computer clunk along, I though what if I created a button on the board which when clicked would allow the player to make black’s move and learn it for future games. I know that for example computers are not good with blitz games or sacrifices, whereas human players can spot those opportunities fairly easily. So if I make the move for black, I want black to learn the move so the next time it is encountered, black will make the move quickly rather than looking at all possible options.

The image below is of a finished game using my new routine. I started making the moves for black at move 7 and the computer made the next three moves based on white’s moves rather quickly. The rest of the moves I made and as you can see black won.

TC-Chess Wins

After completing the game, I replayed the game entering the same moves for white and black responded beautifully and quickly giving the same result with black averaging about 3 seconds per move. Needless to say I was very happy.

Here is the code to learn the move.

‘Date Created: 21-Nov-2014 11:28:36 AM
‘Last Updated: 27-Feb-2017 10:41:49 AM
‘Created By  : cdc
‘Updated By  : NLaws
parentform.commit()

t = table.open(“ob_header”)
t.enter_begin()
t.obnbr = 99
t.booktype = “Learning”
t.event = “Game”
t.site = “Desktop”
t.date = Date()
t.round = 1
t.white = “Human”
t.balck = “TakeCharge”
t.resullt = “0”
t.enter_end()
t.close()
rNbr = 99

pm = table.open(“piecemoves”)
pm.fetch_last()
r_Nbr = pm.recno()
pm.close()

a_tbl = table.open(“openmoves”)
append.t_db = “piecemoves”
append.m_key = “”
append.t_key = “”
append.m_filter = “”
append.t_filter = “recno() = Var->r_Nbr”
append.type = “All”
append.m_count = 16
append.m_field1 = “MOVENBR”
append.m_exp1 = “@PIECEMOVES->MOVENBR”
append.m_field2 = “WHITEVAL”
append.m_exp2 = “@PIECEMOVES->WHITEVAL”
append.m_field3 = “BLACKVAL”
append.m_exp3 = “@PIECEMOVES->BLACKVAL”
append.m_field4 = “SCORE”
append.m_exp4 = “@PIECEMOVES->SCORE”
append.m_field5 = “WPIECE”
append.m_exp5 = “left(word(@Piecemoves->Whitemove,1,\”:\”,1),-4)”
append.m_field6 = “BPIECE”
append.m_exp6 = “left(word(@Piecemoves->Blackmove,1,\”:\”,1),-4)”
append.m_field7 = “WMVFRMA”
append.m_exp7 = “left(alltrim(word(@Piecemoves->Whitemove,2,\”:\”,1)),-2)”
append.m_field8 = “BMVFRMA”
append.m_exp8 = “left(alltrim(word(@Piecemoves->Blackmove,2,\”:\”,1)),-2)”
append.m_field9 = “WMVTOA”
append.m_exp9 = “lower(alltrim(word(@Piecemoves->Whitemove,3,\”:\”,1)))”
append.m_field10 = “BMVTOA”
append.m_exp10 = “lower(alltrim(word(@Piecemoves->Blackmove,3,\”:\”,1)))”
append.m_field11 = “Gamemvs1”
append.m_exp11 = “@PIECEMOVES->TC_CMV1”
append.m_field12 = “Gamemvs2”
append.m_exp12 = “@PIECEMOVES->TC_CMV2”
append.m_field13 = “Gamemvs3”
append.m_exp13 = “@PIECEMOVES->TC_CMV3”
append.m_field14 = “Gamemvs4”
append.m_exp14 = “@PIECEMOVES->TC_CMV4”
append.m_field15 = “OB_Nbr”
append.m_exp15 = “Var->RNbr”
append.m_field16 = “CurrentBoard”
append.m_exp16 = “@PIECEMOVES->CURRENTBOARD”
append.t_count = 0
a_tbl.append()
a_tbl.close()

The key here is not to learn each move in the game so far, only the last move based on the current board layout.The table PieceMove records each move in a game as a single record, so I open the table and fetch the last record assigning the record number to a variable r_Nbr. Next I run an append for just that record in the PieceMove table using r_Nbr as the filter. Now when a game is played, the software always checks for the current board piece layout before calculating new moves. When it see’s a matching image, it makes that move rather than computing all possible moves. If you think about it, that is exactly what a human does. The more games they play the more piece layouts they recognize and make their moves accordingly. Mission accomplished!

The next thing I want to do is add a control which will allow the player to set up custom board piece positions. This way they can recreate screens they have seen on the web and practice solutions. Then they can teach the program only the best moves. Now that will be cool.

That’s all for today. If you own a business and need help with your Alpha Software program, contact us. Are rates are reasonable and our work is guaranteed.

Phone:713 417-6831

EMail: NLawson@cdc-takecharge.com