How to develop a chess program from scratch (for total beginners) Ep. 1

Huo Chess and chess programming tutorials

[Next episode]

Overview

This is a tutorial that shows how one can develop a chess program from scratch. Minimum knowledge of programming is nice to have, but even people with no knowledge will be able to follow since the topics are described in a way independent of programming languages.

Details about programming are explained when needed in a simple and intuitive manner. Code examples will be provided at each step of the tutorial in three programming languages: C#, Java and BASIC, so as to cover all the possible needs of the readers.

The tutorial will be updated constantly with new content added every month. The goal is to have a complete tutorial by the end of the year. This is a live page!

Code to use for the tutorial

Huo Chess: The open source chess program for tutorials

Along the tutorial, we will use the code from Huo Chess. The Huo Chess is a micro chess program that was created with education as its main goal from the beginning. It aims to be the smallest chess program out there that can be used for tutorials.

The current version is a mere 48 KB (Java edition, C# console .NET Framework edition). Huo Chess is freely available in C# (.NET Core and .NET Framework), Java and QBasic (QB64) editions.

You can find Huo Chess source code for free at any one of the GitHub repositories:

Download the code and have it readily available when reading this tutorial. (But even if you do not, code examples will be provided inside the tutorial, so there is nothing to worry about!)

Don’t have a tool to use to view code or just want to read the tutorial without one? Do not worry! Just download the text files below which contain the source code of Huo Chess in all of its editions! (of course these files cannot be used to compile the program in C# which needs more files in the project, but the Java and BASIC editions are indeed the full program.

Huo Chess Java code (From GitHub)

Note that the Huo Chess source code is constantly updated, so make sure you come back and download the latest from time to time!

Now that we have the code to use, let’s talk a bit about the programming pre-requisities that will be needed for this tutorial.

Programming pre-requisites

This article can be used by beginners in programming. You can read the code here and the explanations of what it does and you will get a fairly good understanding on what is going on.

However you need to help yourself a little bit. Go on and open a tutorial on how to program a simple program in your favorite language and experiment on your own! The tutorials in chess-programming are based on open source Huo Chess, which is available in three main languages: C#, Java and BASIC (you might also find C++ and Visual Basic editions out there, however these are no longer maintained officially). However this tutorial will not explain everything on how one can code in these languages.

You are expected to have an environment in which you can code. This could be:

  • MS Visual Studio for C#
  • NetBeans for Java
  • QB64 for Quick Basic 64 bit

One can download for free the relevant tools for the above-mentioned languages. Using these tools pretty much involves the following steps: Open tool > Create new project > Name your new project (e.g. MyChess) > Start coding. Of course details differ, but this is where you get in.

Regarding programming, the following concepts are used:

  • Variables: Elements which are used inside a program and which hold values that can be manipulated.
  • Arrays: Multi-dimensional tables of variables. The most commonly used array in a chess program is of course the… chess board!
  • Functions and sub-routines: Sets of code that is independent so it can be invoked from multiple places in your program. An example of a function is the CountScore function which is called numerous times in a chess program to count the score of a given position. When calling a function you may need to give a parameter for it to function properly. In the case of CountScore, the parameter passed to the function is the chessboard. In BASIC we use sub-routines to do the same things we do with functions in C# and Java.
  • Main commands used: The main commands used are the for loops, and the if statements. Along of course with the commands to print output to the screen and to get input from the user. Yes, programming a chess program entails not many commands, but mostly work to design the algorithm.

For the above you need to make some search on your own to see how these notions work. Some previous tutorials in chess-programming mention some explanations about the above. Or you can simply search in the Internet for more. Or, as mentioned above, you can just read the code of Huo Chess and try to understand from there how these notions are used! I strongly recommend the latter two methods combined.

Experimentation is your friend!

Download the Huo Chess projects from GitHub sites (select the latest release from there) and simply open them in the programming tool you use! Then try to first compile the program and see if it works. After that, you can just create a copy of the project for experimentation and play around with the source code! There is no better way to learn than experimenting yourself! The first program I ever wrote was a program I read in the manual of the Commodore 16 I had. I just typed in the commands I saw without even understanding what they meant. And soon, I had my first program ready to go! A few days later, the commands I had typed in started to make more sense.

We are all afraid of what we do not understand, but it is up to us to make use of our time properly and change that.

Step 0: The chessboard

Where does one start to program a new chess program? But from what else than the chessboard!

Various programming languages have different ways to declare an array, but as you can see from the following examples all the ways simply do one thing: Create a new array with the name we choose (intuitively we call it… chessboard), the type we want (the array will be of type String, meaning it will hold characters, e.g. “White Rook” or “WR”) and the dimensions (8×8) we want.

Look at the code that does exactly that…

C# code

public static String[,] SkakieraLog = new String[8, 8];

Java code

public static String[][] Skakiera = new String[8][8];

BASIC code

DIM SHARED chessboard$(8, 8)

Note: ‘Skakiera’ is Greek for chessboard…

There!

Ready to start playing…

Step 1: Fill in the chessboard

How can you play in an empty chessboard? Well, you cannot.

The next step after creating the chessboard is filling it in with pieces. This is easy. All you have to do is allocate the proper values (indicating the pieces, e.g. “White Rook” or “WR” if you use abbreviations) to the elements of the array you just created.

C# code

Skakiera[(0), (0)] = "White Rook";
Skakiera[(0), (1)] = "White Pawn";
Skakiera[(0), (6)] = "Black Pawn";
Skakiera[(0), (7)] = "Black Rook";
Skakiera[(1), (0)] = "White Knight";
Skakiera[(1), (1)] = "White Pawn";
Skakiera[(1), (6)] = "Black Pawn";
Skakiera[(1), (7)] = "Black Knight";
Skakiera[(2), (0)] = "White Bishop";
Skakiera[(2), (1)] = "White Pawn";
Skakiera[(2), (6)] = "Black Pawn";
Skakiera[(2), (7)] = "Black Bishop";
Skakiera[(3), (0)] = "White Queen";
Skakiera[(3), (1)] = "White Pawn";
Skakiera[(3), (6)] = "Black Pawn";
Skakiera[(3), (7)] = "Black Queen";
Skakiera[(4), (0)] = "White King";
Skakiera[(4), (1)] = "White Pawn";
Skakiera[(4), (6)] = "Black Pawn";
Skakiera[(4), (7)] = "Black King";
Skakiera[(5), (0)] = "White Bishop";
Skakiera[(5), (1)] = "White Pawn";
Skakiera[(5), (6)] = "Black Pawn";
Skakiera[(5), (7)] = "Black Bishop";
Skakiera[(6), (0)] = "White Knight";
Skakiera[(6), (1)] = "White Pawn";
Skakiera[(6), (6)] = "Black Pawn";
Skakiera[(6), (7)] = "Black Knight";
Skakiera[(7), (0)] = "White Rook";
Skakiera[(7), (1)] = "White Pawn";
Skakiera[(7), (6)] = "Black Pawn";
Skakiera[(7), (7)] = "Black Rook";

Java code

    // Put the pieces in the chessboard (e.g. WR for White Rook)
    Skakiera[0][0] = "WR";
    Skakiera[0][1] = "WP";
    Skakiera[1][0] = "WN";
    Skakiera[1][1] = "WP";
    Skakiera[2][0] = "WB";
    Skakiera[2][1] = "WP";
    Skakiera[3][0] = "WQ";
    Skakiera[3][1] = "WP";
    Skakiera[4][0] = "WK";
    Skakiera[4][1] = "WP";
    Skakiera[5][0] = "WB";
    Skakiera[5][1] = "WP";
    Skakiera[6][0] = "WN";
    Skakiera[6][1] = "WP";
    Skakiera[7][0] = "WR";
    Skakiera[7][1] = "WP";
    // Put the black pieces as well
    Skakiera[0][7] = "BR";
    Skakiera[0][6] = "BP";
    Skakiera[1][7] = "BN";
    Skakiera[1][6] = "BP";
    Skakiera[2][7] = "BB";
    Skakiera[2][6] = "BP";
    Skakiera[3][7] = "BQ";
    Skakiera[3][6] = "BP";
    Skakiera[4][7] = "BK";
    Skakiera[4][6] = "BP";
    Skakiera[5][7] = "BB";
    Skakiera[5][6] = "BP";
    Skakiera[6][7] = "BN";
    Skakiera[6][6] = "BP";
    Skakiera[7][7] = "BR";
    Skakiera[7][6] = "BP";

BASIC code

    chessboard$(1, 1) = "wrook": chessboard$(1, 2) = "wpawn"
    chessboard$(2, 1) = "wknight": chessboard$(2, 2) = "wpawn"
    chessboard$(3, 1) = "wbishop": chessboard$(3, 2) = "wpawn"
    chessboard$(4, 1) = "wqueen": chessboard$(4, 2) = "wpawn"
    chessboard$(5, 1) = "wking": chessboard$(5, 2) = "wpawn"
    chessboard$(6, 1) = "wbishop": chessboard$(6, 2) = "wpawn"
    chessboard$(7, 1) = "wknight": chessboard$(7, 2) = "wpawn"
    chessboard$(8, 1) = "wrook": chessboard$(8, 2) = "wpawn"

    chessboard$(1, 7) = "bpawn": chessboard$(1, 8) = "brook"
    chessboard$(2, 7) = "bpawn": chessboard$(2, 8) = "bknight"
    chessboard$(3, 7) = "bpawn": chessboard$(3, 8) = "bbishop"
    chessboard$(4, 7) = "bpawn": chessboard$(4, 8) = "bqueen"
    chessboard$(5, 7) = "bpawn": chessboard$(5, 8) = "bking"
    chessboard$(6, 7) = "bpawn": chessboard$(6, 8) = "bbishop"
    chessboard$(7, 7) = "bpawn": chessboard$(7, 8) = "bknight"
    chessboard$(8, 7) = "bpawn": chessboard$(8, 8) = "brook"

Note that in all these editions, the initiation of the starting position is not done by simply adding the above code after the declaration of the chessboard array. The initiation of the chessboard is put inside a function (C#, Java) or a Sur-routine (QBasic) that is called and performs this initiation.

What are functions or subroutines? To put it simply, these are sections of code with a specific name and a well defined functionality that can be re-used over and over again in a program. Check the full source code of the Huo Chess to get a glimpse on how these functions/subs work.

Step 2: Show the chessboard

What good a chess program is if it does not show the chessboard to the user? After we have filled it in this is the next logical step. But how to do that? There are many ways to present a chessboard with graphics and one can find many of these for free with a little Google search. Huo Chess itself has versions with graphics that present the chessboard is a graphical way (e.g. the Huo Chess DV edition).

For now however, we will just focus on presenting the chessboard with a method based on text: We will just “print” the chessboard on the screen. And after every move we will print it again.

The method is easy. We just loop through the chessboard and print every square as either empty or with the piece that is in that square (this is what we do in Java and BASIC editions).

C# code (simple text-based graphics)

public static void Display_board(string[,] DrawSkakiera)
{
    bool BoardColour = true;
    //False = Black True = While
    int RowCounter = 7;
    while (RowCounter > -1)
    {
        for (int ColumnCounter = 0; ColumnCounter <= 7; ColumnCounter++)
        {
            Console.BackgroundColor = SetBoardColour(ref BoardColour);
            SetPieceColour(DrawSkakiera[ColumnCounter, RowCounter]);
            Console.Write(" ");
            Console.Write(get_key(DrawSkakiera[ColumnCounter, RowCounter]));
            Console.Write(" ");
        }
        Console.WriteLine("");
        Console.BackgroundColor = SetBoardColour(ref BoardColour);
        //Does this to switch the colour at the end of the row
        Console.BackgroundColor = ConsoleColor.Black;
        Console.ForegroundColor = ConsoleColor.White;
        RowCounter -= 1;
    }

    // Return to the default letters colour
    Console.ForegroundColor = ConsoleColor.Gray;
}

public static string get_key(string Piece)
{
    if (Piece.CompareTo("") == 0)
        return " ";
    if (Piece.CompareTo("White Pawn") == 0)
        return "o";
    if (Piece.CompareTo("White Rook") == 0)
        return "R";
    if (Piece.CompareTo("White Bishop") == 0)
        return "B";
    if (Piece.CompareTo("White King") == 0)
        return "K";
    if (Piece.CompareTo("White Knight") == 0)
        return "N";
    if (Piece.CompareTo("White Queen") == 0)
        return "Q";
    if (Piece.CompareTo("Black Pawn") == 0)
        return "o";
    if (Piece.CompareTo("Black Rook") == 0)
        return "R";
    if (Piece.CompareTo("Black Bishop") == 0)
        return "B";
    if (Piece.CompareTo("Black King") == 0)
        return "K";
    if (Piece.CompareTo("Black Knight") == 0)
        return "N";
    if (Piece.CompareTo("Black Queen") == 0)
        return "Q";
    return Piece;
}

public static System.ConsoleColor SetBoardColour(ref bool BoardColour)
{
    if (BoardColour == false)
    {
        BoardColour = true;
        return ConsoleColor.DarkGray;
    }
    else
    //if (BoardColour == true)
    {
        BoardColour = false;
        return ConsoleColor.Gray;
    }
}

public static void SetPieceColour(string Piece)
{
    if (Piece.CompareTo("") == 0)
    {
        Console.ForegroundColor = ConsoleColor.DarkGray;
    }
    else
    {
        if (Piece.Substring(0, 5).CompareTo("White") == 0)
        {
            Console.ForegroundColor = ConsoleColor.White;
        }
        else
        {
            Console.ForegroundColor = ConsoleColor.Black;
        }
    }
}

Or it could be even simpler that that: Just print the move the computer plays and forget about the chessboard altogether!

C# code (just print the move!)

            Console.WriteLine(String.Concat("My move: ", HY_Starting_Column_Text, Best_Move_StartingRank.ToString(), " -> ", HY_Finishing_Column_Text, Best_Move_FinishingRank.ToString()));

Java code

  // Function to draw the chessboard position
  // It simply prints the pieces, nothing fancy
  public static void drawPosition() {
      
  // Transform chessboard to the drawChessBoard
  // by adding spaces in the empty squares.
  // This allows the program to draw a decent chessboard.
  for (int i = 0; i < 8; i++)
  {
    for (int j = 0; j < 8; j++)
    {
        if (!Skakiera[i][j].equals(""))
            drawChessBoard[i][j] = Skakiera[i][j];
        else if (Skakiera[i][j].equals(""))
            drawChessBoard[i][j] = "  ";
    }
  }
  // Print an empty line to separate the printed chessboard from the previous text in the screen
  System.out.println("");
  
  // Print one rank at a time
  //for (int i = 7; i > -1; i--)
  //{
  //    System.out.println(
  //    "[" + Skakiera[0][i] + "]" +
  //    "[" + Skakiera[1][i] + "]" +
  //    "[" + Skakiera[2][i] + "]" +
  //    "[" + Skakiera[3][i] + "]" +
  //    "[" + Skakiera[4][i] + "]" +
  //    "[" + Skakiera[5][i] + "]" +
  //    "[" + Skakiera[6][i] + "]" +
  //    "[" + Skakiera[7][i] + "]" 
  //    );
  //}
  
  System.out.println(
      "-------------------------");
      
    // Print one rank at a time
  for (int i = 7; i > -1; i--)
  {
      System.out.println(
      "|" + drawChessBoard[0][i] +
      "|" + drawChessBoard[1][i] +
      "|" + drawChessBoard[2][i] +
      "|" + drawChessBoard[3][i] +
      "|" + drawChessBoard[4][i] +
      "|" + drawChessBoard[5][i] +
      "|" + drawChessBoard[6][i] +
      "|" + drawChessBoard[7][i] + "|" 
      );
      
      System.out.println(
      "-------------------------");
  }
  
  
  }

BASIC code

SUB drawBoard

    sqcolor$ = "" 'Square color
    piece$ = "" 'Piece to be printed
    pfcolor = 0 'Piece front color
    pbcolor = 1 'Piece back color

    MT$ = CHR$(219)
    SQ$ = MT$ + MT$ + MT$

    'CLS 0
    SCREEN 0
    'COLOR 6
    'LINE (0, 0)-(30, 30), 0, BF

    LOCATE 1, 1
    PRINT "HUO Chess (HUOC) by Spiros (h uo) Kakos - Alpha version"

    FOR i = 1 TO 8
        FOR j = 1 TO 8

            IF (i + j) MOD 2 = 0 THEN
                sqcolor$ = "b"
            ELSEIF (i + j) MOD 2 <> 0 THEN
                sqcolor$ = "w"
            END IF

            'Columns are the first number inside the parenthesis and
            'because we start drawing the board from upside-up, we
            'must apply this (9 - i) to draw the board correctly

            'Print the square
            LOCATE 2 + (9 - i), 1 + (j - 1) * 3
            IF sqcolor$ = "w" THEN
                COLOR 7, 0
            ELSEIF sqcolor$ = "b" THEN
                COLOR 0, 7
            END IF
            PRINT SQ$

            'Determine the color of the piece to print
            IF LEFT$(chessboard$(j, i), 1) = "w" AND sqcolor$ = "w" THEN
                pfcolor = 15
                pbcolor = 7
            ELSEIF LEFT$(chessboard$(j, i), 1) = "w" AND sqcolor$ = "b" THEN
                pfcolor = 15
                pbcolor = 0
            ELSEIF LEFT$(chessboard$(j, i), 1) = "b" AND sqcolor$ = "w" THEN
                pfcolor = 5
                pbcolor = 7
            ELSEIF LEFT$(chessboard$(j, i), 1) = "b" AND sqcolor$ = "b" THEN
                pfcolor = 5
                pbcolor = 0
            END IF

            SELECT CASE chessboard$(j, i)

                CASE "wking"
                    piece$ = "K"
                CASE "wqueen"
                    piece$ = "Q"
                CASE "wrook"
                    piece$ = "R"
                CASE "wbishop"
                    piece$ = "B"
                CASE "wknight"
                    piece$ = "N"
                CASE "wpawn"
                    piece$ = "o"
                CASE "bking"
                    piece$ = "K"
                CASE "bqueen"
                    piece$ = "Q"
                CASE "brook"
                    piece$ = "R"
                CASE "bbishop"
                    piece$ = "B"
                CASE "bknight"
                    piece$ = "N"
                CASE "bpawn"
                    piece$ = "o"
                CASE ""
                    piece$ = ""

            END SELECT

            'Print the piece
            LOCATE 2 + (9 - i), 2 + (j - 1) * 3
            COLOR pfcolor, pbcolor
            PRINT piece$

        NEXT j
    NEXT i

    'Restore color of screen and text
    COLOR 7, 0

    'Restore cursor
    LOCATE 13, 1

END SUB

If one reviews the example above, it is evident that this is not the optimum way. Still, it produces the result we want and let us focus on the important things, i.e. the development of the algorithm that we will soon start.

Programming notes: About functions and subroutines

The code to draw the chessboard is put inside sections of code called functions (C#, Java) or subroutines (BASIC). These sections are independent self-contained sections of code that can be invoked (called) from any place in your program and perform the tasks they are programmed to perform. Essentially, the main reason for using functions/ subroutines is to have the code tidy and not to repeat code that is used many times in the program.

> Check Huo Chess source code to see how functions are used.

Step 3: Get input from the user

The next step is to get input from the user. This is of essence, since you want your program to play with another human.

Getting input from the user is relatively easy. All you have to do is use the relative command of the language to use and… get the input.

C# code

Console.Write("\nColor (w/b)? ");
String the_choise_of_user = Console.ReadLine();

//v0.980: Reduce choices (only 'w' and 'b' valid)
if (the_choise_of_user.CompareTo("w") == 0)
{
    m_PlayerColor = "White";
    m_WhoPlays = "Human";
}
else if (the_choise_of_user.CompareTo("b") == 0)
{
    m_PlayerColor = "Black";
    m_WhoPlays = "HY";
}

Console.Write("Enter move (e.g. e2e4): ");
PlayerMove = Console.ReadLine().ToUpper();
m_StartingColumn = PlayerMove.Substring(0, 1);
m_StartingRank = Int32.Parse(PlayerMove.Substring(1, 1));
m_FinishingColumn = PlayerMove.Substring(2, 1);
m_FinishingRank = Int32.Parse(PlayerMove.Substring(3, 1));

if (m_StartingColumn.CompareTo("A") == 0)
    m_StartingColumnNumber = 1;
else if (m_StartingColumn.CompareTo("B") == 0)
    m_StartingColumnNumber = 2;
else if (m_StartingColumn.CompareTo("C") == 0)
    m_StartingColumnNumber = 3;
else if (m_StartingColumn.CompareTo("D") == 0)
    m_StartingColumnNumber = 4;
else if (m_StartingColumn.CompareTo("E") == 0)
    m_StartingColumnNumber = 5;
else if (m_StartingColumn.CompareTo("F") == 0)
    m_StartingColumnNumber = 6;
else if (m_StartingColumn.CompareTo("G") == 0)
    m_StartingColumnNumber = 7;
else if (m_StartingColumn.CompareTo("H") == 0)
    m_StartingColumnNumber = 8;


if (m_FinishingColumn.CompareTo("A") == 0)
    m_FinishingColumnNumber = 1;
else if (m_FinishingColumn.CompareTo("B") == 0)
    m_FinishingColumnNumber = 2;
else if (m_FinishingColumn.CompareTo("C") == 0)
    m_FinishingColumnNumber = 3;
else if (m_FinishingColumn.CompareTo("D") == 0)
    m_FinishingColumnNumber = 4;
else if (m_FinishingColumn.CompareTo("E") == 0)
    m_FinishingColumnNumber = 5;
else if (m_FinishingColumn.CompareTo("F") == 0)
    m_FinishingColumnNumber = 6;
else if (m_FinishingColumn.CompareTo("G") == 0)
    m_FinishingColumnNumber = 7;
else if (m_FinishingColumn.CompareTo("H") == 0)
    m_FinishingColumnNumber = 8;

Java code

System.out.println("Huo Chess v0.991 by Spiros Kakos");
System.out.println("Please choose color (w/b)");
m_PlayerColor = reader.next();

System.out.println("");

System.out.println("Enter move (e.g. c2c4) and press Enter. Enter 'q' to quit.");
// Read the move the user inputs
playerMove = reader.next();

// Break the move entered in starting and finishing columns and ranks
startingColumn = Character.toString( playerMove.charAt(0) );
startingRank = Character.toString( playerMove.charAt(1) );
finishingColumn = Character.toString( playerMove.charAt(2) );
finishingRank = Character.toString( playerMove.charAt(3) );

switch (startingColumn)
{
  case "a":
      startingColumnNum = 1; break;
  case "b":
      startingColumnNum = 2; break;
  case "c":
      startingColumnNum = 3; break;
  case "d":
      startingColumnNum = 4; break;
  case "e":
      startingColumnNum = 5; break;
  case "f":
      startingColumnNum = 6; break;
  case "g":
      startingColumnNum = 7; break;
  case "h":
      startingColumnNum = 8; break;
}

switch (finishingColumn)
{
  case "a":
      finishingColumnNum = 1; break;
  case "b":
      finishingColumnNum = 2; break;
  case "c":
      finishingColumnNum = 3; break;
  case "d":
      finishingColumnNum = 4; break;
  case "e":
      finishingColumnNum = 5; break;
  case "f":
      finishingColumnNum = 6; break;
  case "g":
      finishingColumnNum = 7; break;
  case "h":
      finishingColumnNum = 8; break;
}

BASIC code

SetPlayerColor:
'Set the colour of the player
INPUT "Set your color. Please select 'w' or 'b': ", playerColor$
IF playerColor$ <> "w" AND playerColor$ <> "b" THEN GOTO SetPlayerColor

SetThinkingDepth:
'Set the thinking depth
PRINT "": INPUT "Set thinking depth. Please select 1 or 3 or 5: ", thinkingDepth
IF thinkingDepth <> 1 AND thinkingDepth <> 3 AND thinkingDepth <> 5 THEN GOTO SetThinkingDepth

    LOCATE 13, 1: 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)

    IF humanLogs$ = "y" THEN debugMode = 1 ELSE debugMode = 0

    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

Note that the move entered by the user might need to be ‘translated’ for the program to use it. This is necessary because the move entered will be written in a format like ‘c2c4’ but the program code will more easily handle coordinated which are all in a number format. So the move c2c4 for example, will be translated to 3234, meaning that the user moves from the square with coordinates 32 (column 3, row 2) to the square with coordinates 34 (column 3, row 4). Numbers are much more easily used by a program than letters.

Note also that I have included in the above samples some additional things that I did not mention, but which are also essential to set up the game. For example, before even starting the game the human player must select his or her color of course!

Now we are set.

We have a chessboard ready, the human player has enters his move and the… fun starts! How will the computer think of a response?

The first cycle of the tutorial is finished.

In the next cycle we will start exploring how the computer algorithm will work to analyze the chessboard and find a move to answer the human opponent!

Coming soon: Start designing the chess thinking algorithm…

[Next episode]

2 responses to “How to develop a chess program from scratch (for total beginners) Ep. 1”

  1. […] To that end, we proudly introduce the Chess Programming portal! (starting with a new chess programming tutorial for total beginners at “How to develop a chess program from scratch (for total beginners) Ep. 1“) […]

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: