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

Huo Chess (console edition)

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!


[Previous episode]


In the previous episode we set the foundations for starting a chess program. In summary we…

  • Discussed the pre-requisites for the tutorial
  • Created the chessboard
  • Got the first input from the user

Now it is time to start the design of the chess thinking algorithm.

Step 0: Design the main steps on paper

Before making any serious programming attempt, one has to leave the computer and get a piece of paper. There, he can design the algorithm of the program. A good design is invaluable for a good program. There is no need to start writing code without having that design first!

Chess program algorithm outline

One can find above a rough outline of the major steps involved in a chess program algorithm.

Note that this algorithm is an over-simplification, but it will do for now. For example, the algorithm above does not take into account the fact that the thinking of the computer must be conducted in various depths and not only in the initial position. We will elaborate on that in next episodes; for now let us just try to analyze all possible moves in a position and choose the one that leads to the best score.

[Next episode: Scanning the chessboard for moves]

[Previous episode]

Learning Greek for Beginners series


The “Learning Greek for Beginners” series is a series of articles that aim at giving you fast-pace lessons on how to speak Greek. They were originally posted in Google Knol in a contest for articles for ‘Dummies’.

List of Lessons

The list of lessons available can be found below

Please do not hesitate to contact us for any comment and/ or suggestion.

The adventure of creating… an adventure! Episode 1: Setting up the images [Quick Basic/ QB64 Tutorial]

First Adventure (in QB64)


Programming is fun!

What does that have to do with a philosophy portal?

Well… I do not know!

But then again…

What do we know about life or death?


The goal of this series of tutorials is to show how to create a small (or big?) adventure game with Quick Basic, so as to disseminate programming knowledge. Even if you do not have prior programming knowledge you will be able to follow the series with some additional effort to clear up some programming details we will encounter along the way.

The source code of the program will always be provided at all steps of the tutorial. Reuse of the code is allowed as long as the source is mentioned properly.

Tools needed

To follow the tutorial you just need the Quick Basic in your computer. Just download the latest QB64 from the relative site here for free.


Before doing any programming one must design what he wants to do. This applies to small as well as big programs.

So what do we want to do?

To create an adventure game!

This game will utilize simple images that we will show to the users as they progress along the game. All the typical adventure game commands will be allowed: Look at, Walk to, Pick up, Use, Talk to, Give, Push, Pull.

The game scenario is something I have not yet thought in its entirety. However I know that the game will start from a room where you are and where you will need to stay alive for at least one day before the rescue team comes and saves you.

The beginning

From where to start from?

Well, from the start (welcome) screen!

To do that we use the PRINT command to print the game title on the screen.

The game will be called…

First Adventure!

Since it is our first adventure.

LOCATE 10, 32: PRINT "First Adventure!"
LOCATE 11, 30: PRINT "Greece, Athens, 2022"
LOCATE 13, 25: PRINT "Seek the moon under the sun…"

The LOCATE commands puts the cursor in a specific line and then we print what we want.

First Adventure – Start image

Setting up the images

Each game takes place in a… place.

To illustrate things we need graphics.

We will use basic graphics in our game, i.e. images we will load into the screen to show things. The first thing is to get the images, right? For the purposes of our tutorial we will use images that are 580×420 pixels in dimension. These images will be shown in a window that is 800×600 in dimensions.

Why these dimensions?

First, for no reason. Just because.

Secondly, I like to have a small window for the program to show small images. You guessed it, the program will not utilize the full screen and will not resize based on the resolution of your screen. Let’s start with the basics and build a bike and we will go to building a spaceship after that…

Images are loaded with the _LOADIMAGE command as it is shown below.

IF GameResolution = 800 THEN
    imgBgrd& = _LOADIMAGE("FA_image_Background_800.png", 32) 'Background image
ELSEIF GameResolution = 1280 THEN
    imgBgrd& = _LOADIMAGE("FA_image_Background_1280.png", 32) 'Background image

You also guessed it right: We have introduced a variable that indicates the resolution of the game. So when I told you that the game will not take into account the resolution of your computer, I lied a bit. Just a bit. We introduce a variable to indicate the resolution used and we will embed it in the code and… we will see how to use it the future.

For now, let’s set the resolution at 800×600…

CONST GameResolution = 800 'Set for resolution 800x600

We repeat the same loading of images for all images of the game. For now, we only have three images: the background image (that is loaded in the beginning and… stays there for ever) and two more images that we put on the background when he game starts.

First Adventure – Background image

The background

The image I have created for background is a black colour image with a white line. This line will be the border between the upper and the lower part of the screen.

  • In the upper part we will show images that are related to the gameplay, e.g. the photo of a room you are in.
  • In the lower part of the screen we will show to the user the possible commands allowed for the game, his inventory of objects (if and when we add one) and any messages.

Simple enough. Yet, nice and effective.

How are those images put in the screen though?

Well, with the PUTIMAGE command…

IF GameResolution = 800 THEN
    _PUTIMAGE (100, 20)-(680, 440), imgStart&, 0
ELSEIF GameResolution = 1280 THEN
    _PUTIMAGE (80, 20)-(660, 500), imgStart&, 0

With the PUTIMAGE command you can set where to place your image upper-left corner (100,20) and where the lower-right corner will be (680,440), which image to load (imgStart&) and where to show it (0 denotes the screen). In that way we can put any of the images we have loaded wherever we want.

First Adventure – Start image

Show the initial messages

When the game starts and the background image is loaded, we print the initial messages to the user. We use the LOCATE command and print the messages in the lower part of the screen, as mentioned above…

'Print text to begin the adventure...
LOCATE 33, 1: PRINT "You are in a room... Alone..."
LOCATE 34, 1: PRINT "There is a snow storm raging outside..."

'Show the trees!
_PUTIMAGE (100, 20)-(680, 440), imgTrees1&, 0

LOCATE 33, 1: PRINT "You must survive for one day..."
LOCATE 34, 1: PRINT "Until the rescue party comes to save the day!"

And as you see in the code, we can load new images whenever we want to show new things. In that case we have loaded a trees picture. After all, we are in a snow storm…

Important Notes

  • Guess what the SLEEP command does. Try to remove it and see what happens.
  • Experiment with the coordinates in the PUTIMAGE command to see how that command can be used. (seek some additional examples commented out in the source code provided)

An epilogue and a promise…

We have just set up the scenery for our game. We have the initial screens and we have understood how images are loaded. We will build on this to expand the game and take it to the next level…

APENDIX I – The source code

Please see below for the full source code of the game.

'Declare variables
DIM imgBgrd&
DIM imgStart&
CONST GameResolution = 800 'Set for resolution 800x600
'const GameResolution = 1280 'Set for resolution 1280x720

LOCATE 10, 32: PRINT "First Adventure!"
LOCATE 11, 30: PRINT "Greece, Athens, 2022"
LOCATE 13, 25: PRINT "Seek the moon under the sun..."

'Load the background from an image in a file
'Load different pictures depending on the resolution we want
'(because the image will be used to set the screen)
'imgBgrd& = _LOADIMAGE("FA_image_Background_800.png", 32) 'Background image

IF GameResolution = 800 THEN
    imgBgrd& = _LOADIMAGE("FA_image_Background_800.png", 32) 'Background image
ELSEIF GameResolution = 1280 THEN
    imgBgrd& = _LOADIMAGE("FA_image_Background_1280.png", 32) 'Background image

'imgStart& = _LOADIMAGE("FA_image_Start_800.png", 32)

'Load images of the game from files
'Images for resolution 800x600 are 580x420
'Images for resolution 1280x720 are 640x480
'Image Start
IF GameResolution = 800 THEN
    imgStart& = _LOADIMAGE("FA_image_Start_800.png", 32)
ELSEIF GameResolution = 1280 THEN
    imgStart& = _LOADIMAGE("FA_image_Start_1280.png", 32)
'Image trees
imgTrees1& = _LOADIMAGE("FA_trees1.png", 32)

'Set the image as destination
'_DEST img1&

'Set the screen mode based on the background image (800x600 png image)
'SCREEN _NEWIMAGE(800, 600, 32)
SCREEN imgBgrd&

'Set background colour to gray
'CLS , _RGB32(127, 127, 127)

'Present the first image of the game on the black screen
'(i.e. put the image2 on top of image1, which is the background loaded initially)

'_PUTIMAGE (10, 10)-(650, 490), imgStart&, 0
'_PUTIMAGE (200, 200)-(400, 400), img2&, 0, (0, 0)-(400, 400)

IF GameResolution = 800 THEN
    _PUTIMAGE (100, 20)-(680, 440), imgStart&, 0
ELSEIF GameResolution = 1280 THEN
    _PUTIMAGE (80, 20)-(660, 500), imgStart&, 0

'Set text colour
'COLOR 7, 5

'Print text to begin the adventure...
LOCATE 33, 1: PRINT "You are in a room... Alone..."
LOCATE 34, 1: PRINT "There is a snow storm raging outside..."

'Show the trees!
_PUTIMAGE (100, 20)-(680, 440), imgTrees1&, 0

LOCATE 33, 1: PRINT "You must survive for one day..."
LOCATE 34, 1: PRINT "Until the rescue party comes to save the day!"

Copy this code in your QB64 IDE and try it out.

In order to use it you must also have the images the program loads in the same folder as the QB64.exe program.

APPENDIX II – The images

Please fine here the images used by the program.

APPENDIX III – Useful resources

One can find many things in the Internet. Information about programming is one of these things. Seek help in the relative QB64 web site or in relevant programming sites to understand how various Quick Basic commands work.

Below I list some resources related to the commands we used.

Feel free to seek knowledge from where you can!

Chess Program thought process analysis article

The goal of this paper is to analyze in a simple way the thought process of a chess program, so as to facilitate understanding among chess programming enthusiasts and to bolster potential improvements in that process.

Huo Chess thinking algorithm (simple)

A new article “Chess Program thought process analysis (Simple) – The Huo Chess example” has been published.

There the thinking process of a chess program is presented in high level in order to help chess programming enthusiasts gain an initial understanding of the general way a chess program works.

One can find the relative article here at Academia.

A new article with more advanced subjects of chess programming will follow.

One can also download the file from below.

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

Learn how to program a chess application from scratch. Ideal for beginners!

Huo Chess and chess programming tutorials

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!


Other Related articles

[Next episode]


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…


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.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;
    //if (BoardColour == true)
        BoardColour = false;
        return ConsoleColor.Gray;

public static void SetPieceColour(string Piece)
    if (Piece.CompareTo("") == 0)
        Console.ForegroundColor = ConsoleColor.DarkGray;
        if (Piece.Substring(0, 5).CompareTo("White") == 0)
            Console.ForegroundColor = ConsoleColor.White;
            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
  // 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] + "]" 
  //    );
    // Print one rank at a time
  for (int i = 7; i > -1; i--)
      "|" + drawChessBoard[0][i] +
      "|" + drawChessBoard[1][i] +
      "|" + drawChessBoard[2][i] +
      "|" + drawChessBoard[3][i] +
      "|" + drawChessBoard[4][i] +
      "|" + drawChessBoard[5][i] +
      "|" + drawChessBoard[6][i] +
      "|" + drawChessBoard[7][i] + "|" 

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


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 =;


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

// 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

'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

'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

    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

    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

    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

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]

Exit mobile version