Programming for kids – How to program a chess program in BASIC
Other Related articles
- Huo Chess Main Page
- How to Develop a Chess program for Beginners Series
- Programming a chess application in QBasic (QB64)

In the previous lesson we managed to create the function which checks the legality of a move. With that we managed to check the move entered by the user and present it to the screen.
What is next?
To make the computer think of an answer!
In essence this is easy! (the difficult part is to make the computer think of a really good move, that we will tackle later on)
What we will need besides the SUB which checks the legality of the move (ElegxosNomimotitas) is a SUB which counts the score of any given position in the chessboard. This will allow us to evaluate all the possible moves, so that the computer can select the best one.
This is done by the code below, which simply scans the chessboard for pieces and adds or subtracts the value of each piece it founds from the total score of the chessboard.
SUB countScore
positionScore = 0 FOR I = 1 TO 8 FOR J = 1 TO 8 IF chessboard$(I, J) = "wpawn" THEN positionScore = positionScore + 1 IF chessboard$(I, J) = "wrook" THEN positionScore = positionScore + 5 IF chessboard$(I, J) = "wknight" THEN positionScore = positionScore + 3 IF chessboard$(I, J) = "wbishop" THEN positionScore = positionScore + 3 IF chessboard$(I, J) = "wqueen" THEN positionScore = positionScore + 9 IF chessboard$(I, J) = "wking" THEN positionScore = positionScore + 100 IF chessboard$(I, J) = "bpawn" THEN positionScore = positionScore - 1 IF chessboard$(I, J) = "brook" THEN positionScore = positionScore - 5 IF chessboard$(I, J) = "bknight" THEN positionScore = positionScore - 3 IF chessboard$(I, J) = "bbishop" THEN positionScore = positionScore - 3 IF chessboard$(I, J) = "bqueen" THEN positionScore = positionScore - 9 IF chessboard$(I, J) = "bking" THEN positionScore = positionScore - 100 NEXT J NEXT I
END SUB
Easy? Pretty much. All that is needed is a nested FOR loop (read in QB64 here how that works). Nothing more.
Now that we have that, let’s move on to the main course.
The ComputerMove SUB, which – you guessed right – makes the computer perform a move!
In essence, the steps needed are pretty simple:
Scan the chessboard.
If you find a piece of the computer, then…
Scan all possible moves of that piece to all squares of the chessboard.
For every possible move, check the legality of that move.
If the move is legal, then make it!
Check the score of the move.
If the score is best than the current best move (in the beginning there obviously no current best move), then this is the current best move!
After you have examined all the possible moves, do the current best move
Simple isn’t it?
How is the scanning of the chessboard and the checking of all possible moves performed? With four nested FOR loops.
Check the code below. It simple scans all the chessboard with the FOR loops of I and J and then, if it finds a piece which belongs to the computer, it scans all possible destination squares with the FOR loops of ii and jj.
How do we determine is the piece we found is one of the computer’s pieces? We compare the first letter of the piece (which would be ‘w’ or ‘b’ for white and black pieces) with the color of the player. If for example the color of the player is ‘w’ (for white) and we encounter a piece ‘brook’, then this is a piece of the computer since it is black – i.e. opposite than the color of the player.
'Scan the chessboard... FOR I = 1 TO 8 FOR J = 1 TO 8 'If you find a piece of the computer... IF ((MID$(chessboard$(I, J), 1, 1) = "w" AND playerColor$ = "b") OR (MID$(chessboard$(I, J), 1, 1) = "b" AND playerColor$ = "w")) THEN 'Scan all possible destination squares... FOR ii = 1 TO 8 FOR jj = 1 TO 8 startingColumn = I startingRank = J finishingColumn = ii finishingRank = jj MovingPiece$ = chessboard$(I, J) ProsorinoKommati$ = chessboard$(ii, jj) 'Check legality of the move entered CALL ElegxosNomimotitas(chessboard$(), 0, startingColumn, startingRank, finishingColumn, finishingRank, MovingPiece$) 'If move is legal, then do the move and present it in the chessbooard IF Nomimotita = 1 THEN 'Do the move chessboard$(finishingColumn, finishingRank) = chessboard$(startingColumn, startingRank) chessboard$(startingColumn, startingRank) = "" 'Count the score of the move CALL countScore 'If the score is better than the existing best score, then this is the best move now (and the best score) IF ((playerColor$ = "b" AND positionScore >= bestPositionScore) OR (playerColor$ = "w" AND positionScore <= bestPositionScore)) THEN bestStartingRank = startingRank bestStartingColumn = startingColumn bestFinishingRank = finishingRank bestFinishingColumn = finishingColumn bestPositionScore = positionScore END IF END IF 'Undo the move chessboard$(startingColumn, startingRank) = MovingPiece$ chessboard$(finishingColumn, finishingRank) = ProsorinoKommati$ NEXT jj NEXT ii END IF NEXT J NEXT I
If the move analyzed is legal (the ElegxosNomimotitas SUB is called to determine that) then the move is performed. The score of the position resulting after that is counted (the CountScore SUB is called for that). If the score is better than the current ‘best score’ (the initial best score is zero of course) then this move is registered as best move.
After the scanning is complete, we simply perform the best move!
'Do the best move found chessboard$(bestFinishingColumn, bestFinishingRank) = chessboard$(bestStartingColumn, bestStartingRank) chessboard$(bestStartingColumn, bestStartingRank) = "" CLS CALL drawBoard
Easy? Yes!
Happy coding!
Read the full program here. Copy and paste the code in your QBasic interpreter/ compiler to see it and compile it.
IMPORTANT NOTES
- The code is draft and errors might exist. Make sure to also check Huo Chess in C# (can be found in Harmonia Philosophica here).
Next lesson: How to improve the game of the computer.