Programming for kids: Developing a chess program in BASIC – Part 2

Interested in chess programming? You are interested in philosophy too! What is thinking? Can AI be conscious? What does it mean to do something? Can the AI understand that it does play chess? Explore the AI related articles in Harmonia Philosophica and understand why you are already a philosopher!

Programming for kids – How to program a chess program in BASIC

Other Related articles

In the previous lesson we managed to create our program in BASIC (QB64) and write the first lines of code to ask for input from the user for his move.

Now we will perform the next and one of the most important steps in a chess program: Checking the legality and validity of a move!

Remember that we asked from the user his move by the command…

INPUT "Enter your move: ", Move$

With this command the computer asks for input by the user and then stores what the user entered (after he presses Enter) into the Move$ variable. As we said in the previous lesson, the $ sign in a variable name denotes that the variable stores a string (text) value and not a number.

Now that we have the move into a single text variable, we should first of all “break it down” into four specific elements:

  • Starting column
  • Starting rank
  • Finishing column
  • Finishing rank

In that way we will have the starting and finishing coordinates of the user’s move, which we will use for the checking of the legality of the move later on.

How do we “break up” the text? By using the MID$ built-in function of BASIC which returns a specific character from a text. So if the user entered the move “g2g4” (which is Grob opening by the way, one of my favorites), then the command…

MID$(Move$, 2, 1)

will return the character “2”, since it will start from character 2 of the Move$ variable and will get 1 character.

Without further delay, here is the code which breaks up the move the user entered into the four numbers we are looking for.

INPUT "Enter your move: ", Move$
startingColumnText$ = MID$(Move$, 1, 1)
startingRankText$ = MID$(Move$, 2, 1)
finishingColumnText$ = MID$(Move$, 3, 1)
finishingRankText$ = MID$(Move$, 4, 1)
SELECT CASE startingRankText$
CASE "1"
startingRank = 1
CASE "2"
startingRank = 2
CASE "3"
startingRank = 3
CASE "4"
startingRank = 4
CASE "5"
startingRank = 5
CASE "6"
startingRank = 6
CASE "7"
startingRank = 7
CASE "8"
startingRank = 8
END SELECT
SELECT CASE finishingRankText$
CASE "1"
finishingRank = 1
CASE "2"
finishingRank = 2
CASE "3"
finishingRank = 3
CASE "4"
finishingRank = 4
CASE "5"
finishingRank = 5
CASE "6"
finishingRank = 6
CASE "7"
finishingRank = 7
CASE "8"
finishingRank = 8
END SELECT
SELECT CASE startingColumnText$
CASE "A", "a"
startingColumn = 1
CASE "B", "b"
startingColumn = 2
CASE "C", "c"
startingColumn = 3
CASE "D", "d"
startingColumn = 4
CASE "E", "e"
startingColumn = 5
CASE "F", "f"
startingColumn = 6
CASE "G", "g"
startingColumn = 7
CASE "H", "h"
startingColumn = 8
END SELECT
SELECT CASE finishingColumnText$
CASE "A", "a"
finishingColumn = 1
CASE "B", "b"
finishingColumn = 2
CASE "C", "c"
finishingColumn = 3
CASE "D", "d"
finishingColumn = 4
CASE "E", "e"
finishingColumn = 5
CASE "F", "f"
finishingColumn = 6
CASE "G", "g"
finishingColumn = 7
CASE "H", "h"
finishingColumn = 8
END SELECT

The above code breaks up the Move$ into the above-mentioned four numbers and you would have noticed that the break up has two steps which we didn’t quite mention before: First we break up the text and then we ‘translate’ the four text values into numbers. This is done with the SELECT CASE commands, which essentially select different numeric value for each possible value of the columns and ranks. And yes, the text “8” needs to be translated to the number 8. As far as the computer is concerned they are completely different monsters altogether.

So now we have what we need. The starting column and rank and the finishing ones. How do we check the legality and validity of the move? Well, there is no “magic” way. We just have to write code to do that.

For clarity purposes we will gather all the code performing the check of the legality in one function (= set of code) which we will call ElegxosNomimotitas (= Check of Legality, in Greek). We will define the call parameters of the function (i.e. what parameters we need to pass over the function for it to do its job) and then write the code inside it.

What would be the input parameters?

The starting and end column and ranks of course. And we will also send the Moving Piece in a separate variable because this is a crucial part of the function. Different pieces move in a different way, right?

The function declaration would be something like…

DECLARE SUB ElegxosNomimotitas(ENSkakiera() AS STRING, startColumn AS INTEGER, startRank AS INTEGER, finishColumn AS INTEGER, finishRank AS INTEGER, MovingPieceEN AS STRING)

The ENSkakiera (=Elegxos Nomimotitas Skakiera = Check Legality Chessboard in Greek) is the array of the chessboard (= Skakiera in Greek) we will need to pass. The function will check the legality of a move not in general, but in the context of a specific chessboard of course. Then beyond the chessboard we pass over the start and end columns and ranks and the Moving piece as mentioned above.

READ ALSO:  Programming for kids: Developing a chess program in BASIC – Part 4: Advanced concepts

Now we are in the function which checks the legality of the move. We will show how to do this for one piece, the rook. Then the logic is similar (not exactly, but you will get the meaning) for the other pieces.

Without further delay, here is the code…

IF (MovingPieceEN$ = "wrook" OR MovingPieceEN$ = "brook") THEN

    IF debugMode = 1 THEN PRINT "Nomimotita = " + STR$(NomimotitaEN)

    'Check correctness of move (Rook only moves in lines)

    IF ((startColumn <> finishColumn) AND (startRank <> finishRank)) THEN NomimotitaEN = 0

    IF debugMode = 1 AND NomimotitaEN = 0 THEN PRINT "Checkpoint ROOK-0"

    'Check if the Rook moves beyond the limits of the chessboard

    IF ((finishColumn < 1) OR (finishRank < 1)) THEN NomimotitaEN = 0
    IF ((finishColumn > 8) OR (finishRank > 8)) THEN NomimotitaEN = 0


    'Check if another piece is between the current and the target square

    'Horizontal movement

    IF (startColumn > finishColumn) AND (startRank = finishRank) THEN
        FOR J = startColumn TO finishColumn STEP -1
            IF (J <> startColumn) AND (J <> finishColumn) AND ENSkakiera(J, startRank) <> "" THEN NomimotitaEN = 0
        NEXT J
    END IF

    IF (startColumn < finishColumn) AND (startRank = finishRank) THEN
        FOR J = startColumn TO finishColumn
            IF (J <> startColumn) AND (J <> finishColumn) AND ENSkakiera(J, startRank) <> "" THEN NomimotitaEN = 0
        NEXT J
    END IF

    'Vertical movement

    IF (startColumn = finishColumn) AND (startRank > finishRank) THEN
        FOR J = startRank TO finishRank STEP -1
            IF (J <> startRank) AND (J <> finishRank) AND ENSkakiera(startColumn, J) <> "" THEN NomimotitaEN = 0
        NEXT J
    END IF

    IF (startColumn = finishColumn) AND (startRank < finishRank) THEN
        FOR J = startRank TO finishRank
            IF (J <> startRank) AND (J <> finishRank) AND ENSkakiera(startColumn, J) <> "" THEN NomimotitaEN = 0
        NEXT J
    END IF

    'If the start square is the same as the destination...
    IF startColumn = finishColumn AND startRank = finishRank THEN NomimotitaEN = 0

    'Check if a piece of the same colour is at the destination square
    IF MID$(ENSkakiera$(finishColumn, finishRank), 1, 1) = MID$(ENSkakiera$(startColumn, startRank), 1, 1) THEN NomimotitaEN = 0

    IF debugMode = 1 AND NomimotitaEN = 0 THEN PRINT "Checkpoint ROOK-5": 'INPUT a$

END IF

This code checks for many things:

  • If the rook moves in rows or columns
  • If the rook moved beyond the limits of the chessboard
  • If the rook is blocked by another piece before he reaches the destination square
  • If there is a piece of the same colour at the destination square.

If any of the above validations fail, then the Nomimotita (legality in Greek) variable is set to false (0). If not, the Nomimotita is 1 (= true). Note that we use an integer variable to indicate the Nomimotita (which takes values 0 or 1) instead of a text variable (which would take the values “True” or “False”) to use less memory. But we could anyway use a text variable with the same result.

Note: The “IF debugMode = 1” lines of code are used to print to the screen messages for debugging.

It may look complicated, but at the end it is not. The code is simple and self explanatory, with comments. Take for example the first validation: A rook must move in columns or ranks. How is this translated in the code?

    IF ((startRank = finishRank) AND (startColumn <> finishColumn)) THEN NomimotitaEN = 0
IF ((startColumn = finishColumn) AND (startRank <> finishRank)) THEN NomimotitaEN = 0
IF ((startColumn <> finishColumn) AND (startRank <> finishRank)) THEN NomimotitaEN = 0

Read the code at your own pace.

At the end it is just… English. 🙂

After making sure that the move entered is valid, then all we have to do is… make it and present it to the screen.

'If move is legal, then do the move and present it in the chessbooard
IF Nomimotita = 1 THEN

    IF debugMode = 1 THEN
        PRINT "Now we will redraw the chessboard"
    END IF

    'Do the move
    chessboard$(finishingColumn, finishingRank) = chessboard$(startingColumn, startingRank)
    chessboard$(startingColumn, startingRank) = ""
    CLS
    CALL drawBoard

END IF

Go on and check the program below, which contains all the other move validity checks for all the other pieces. You will find it pretty easy to read and understand. It contains comments within the code to support you in your lesson. 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! Test it on your own and find out potential illegal moves performed by the computer. For a complete and fully functional open source chess program (also excellent for educational purposes) make sure to check Huo Chess in C# (can be found in Harmonia Philosophica here).
  • The starting position is a test position to check potential moves. Update the starting position SUB accordingly to set up the proper initial position!
  • The program supports thinking in only one move depth.
  • Many functions are still not supported: Castling, en passant, check, checkmate.

Until the next lesson, keep coding!

Next lesson: How to make the computer think! Find it here!

Comments (

)

Discover more from Harmonia Philosophica

Subscribe now to keep reading and get access to the full archive.

Continue reading

Verified by ExactMetrics