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

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

Programming is fun.

Want to learn? Why not start now?

And what better way there is than to develop an advanced program that makes the computer “think” how to play chess?! Forget about those silly beginners’ example programs which print “Hello world”. Who in his right mind ever spends time to write a program which prints “Hello world” on the screen?

BASIC is fun.

A very easy language for kids to start learning programming. So we will use it here. (Not in the mood for BASIC? Search for the Java and C# relevant tutorials in Harmonia Philosophica!)

And in one day, you will have learnt the basic of how to program a chess program in BASIC! The actual implementation could take a bit longer but in general in some days you will have the knowledge of how the program works, thus being in a position to improve it or write your own!

The BASIC programming language is one of the simplest ones. Especially designed for simplicity, it uses simple commands to perform operations. The commands are easy to learn and use, since they are utilizing English language words which are easily recognizable. Want to print something on the screen? Use the command PRINT! Want to determine what will happen if something else happens? Use the command IF! See what I mean?

IF YOU_UNDERSTAND = "Yes" THEN PRINT "Yes I understand!"

Did you understand the above command? Great!

The only thing that might trouble you if you are new to programming is the notion of variables. A variable is an element which holds values. This element is used in various places of the program. The values could be of different types – the ones we will use are integers (1, 4, 10…) and strings/ characters (e.g. “white pawn”).

Variables could be stored also in arrays. An array is a collection of variables in one or more dimensions. It is also known as a “table”. And yes, you guessed right: we will use an array (table) to store the chessboard. Could you guess the dimensions of that table? Spot on again.

DIM SHARED chessboard$(8, 8)

The command DIM defines a variable. We do all the variable declarations in the beginning of the program. The above command declares an 8×8 array which holds the… you guessed it right: The chessboard!

By the way, we are using QBasic64, a version of Quick Basic that is available for free and pretty popular as well. Check https://www.qb64.org/portal/ to get it. The site also contains excellent tutorials and training material. You will have it as a reference throughout the whole process, so make sure you create a bookmark out of it.

After you download QBasic64 from the site, just click on the QB64.exe file and execute it. You will be presented with the IDE (programming interface) of QBasic where you will write your program. I do not need to explain the “Create new”, “Save” or “Save as” functions…

Just start writing the commands and then press “Save as”. Executing the program is easy as well. Just select Run > Start.

As said above, what is the first thing to do? (Besides the “Create new program” part and saving it in a folder of your choosing with a name of your choosing as well)

Some of the first things to do we already mentioned.

First, declare the variables. BASIC is really simple in the sense that it is not so strict in requiring you to declare all the variables before using them, in contrast to other high level languages like C#. However this is also a downside of the language. Leaving the programmer with some slack makes the programmer careless.

So we will declare all the variables we use…

DEFINT A-Z
OPTION BASE 1 'Make tables dimensions start from 1
DIM SHARED chessboard$(8, 8)
COMMON SHARED startingRank, startingColumn
COMMON SHARED finishingRank, finishingColumn
COMMON SHARED startingRankText$, startingColumnText$
COMMON SHARED finishingRankText$, finishingColumnText$
COMMON SHARED Move$
COMMON SHARED MovingPiece$
COMMON SHARED debugMode

There you go. (Read the QB64 Wiki for what DEFINT A-Z and OPTION BASE 1 commands do – Remember, learning entails the process of… learning)

What is next?

What else?

Ask the user for input!

And what else to ask than his first move! For simplicity purposes we will present the main command which tells the user to put his move. The drawing of the board on the screen will be done by the drawBoard() function. A function is an independent set of code that you can call to perform an action. In our case we call the drawBoard function (with the command… CALL DRAWBOARD – I told you it was simple). For the time being forget how the function works. (Even though reading through it and trying to understand how it works would be a lesson on its own…)

So how would you ask for the user input?

Simple by the command… INPUT!

There you go:

INPUT "Enter your move: ", Move$

This command tells the computer to wait for the user to enter something and press Enter. When this happens, the text entered by the user is saved in the Move$ variable. The dollar sign ($) indicates that the variable is a string (text) and not a number.

Let’s stay there.

For now you have learnt how to…

  • Create a new program.
  • Declare the variables you will use (including the chessboard).
  • Ask from the user to enter his first move.

You have also been acquainted with the notions of variables and functions and with some basic BASIC (get the joke?) commands, like IF… THEN or INPUT and PRINT. Don’t worry if you don’t get everything yet. You will as you program more and more every day.

Next lessons will include the next logical steps:

  • Validate that the user move is valid and legal.
  • Redraw the chessboard with the move of the user.
  • Make the computer think of an answer.

You would be surprised how the last part (the thinking of the computer) is a rather simple one. In essence the computer thinks of all possible moves, validates them (in the same way we will validate the move entered by the user) and then for the valid ones it will calculate a score of the position. The move with the best score will be the move of the computer.

Until next time…

Happy coding!

Next lesson: How to check the legality of the move entered! Check here.

Ultra-fast programming tutorials: How to develop a C# adventure game in one day (without prior programming experience)

Other Related articles

Introduction

Is programming a game difficult?

Don’t be fooled by the title. Yes it is!

However learning the basic principles of programming and making a very simple game is… simple! And the goal of this article is to show how one can learn programming while having fun in just one day.

Yes, you read that right. One day. Do not expect (of course) to be experts. This goes without saying. But you will learn how to create your first program, which will actually do something. This is how I started programming myself. I had bought (my parents actually) a Commodore 16. And there were no games with it. Just a big manual. In which there were the source code of some games I had to type myself into the computer in order to play them! At first I did not know what I typed. But I typed it anyway. And the game was fun.

Next day, I started learning programming.

Because you see, in order to learn something you must LIKE it first! There is absolutely no point in learning programming just by reading big books with incoherent programming exercises which will at the end result in nothing more than code snippets (parts of code) which do nothing important or useful whatsoever. This is how today’s kids learn to hate programming.

The game we will develop is very small and easy: Just an old-times ultra mini text-adventure. Meaning a game with no graphics, which asks for the users to input text commands (e.g. “Open door”) in order to play it.

Sounds fun?

Well, it is!

And most importantly, it is scalable! You can – at your own pace – make it better, more complex, or even add graphics (there will be a lesson for that too). And make it your own super game that will conquer the world! Or at least your friends and parents. 🙂

No more talk. Let us begin!

Setting up the tools

Download Visual Studio from Microsoft. There is a free edition to download. Install it. Figure out how to do that on your own, it is not difficult really – just like installing any other program, just select for now the default configuration suggested by the program.

What you will need is C# to be installed – because there are other languages also supported by Visual Studio (C++, Visual Basic, F# et cetera).

Create the project

Open Visual studio and create a new C# Console Application project.

When first creating the program, Visual Studio will create a lot of code automatically. Your project will look something like the schema below.

This code is the shell of your program. Do not worry about what it does for now. What you need to remember is that the program has a Main class, inside which you should put your code (see the schema above).

Game scenario

What is the scenario of your adventure?

Let us keep it simple: You are a person who have lost his consciousness and is suddenly found inside a locked room. You want to get out. As simple as that. Your goal is to search the room for the key and unlock the outer door!

What will be the solution of the problem?

Again, let us keep it as simple as possible: You search the room. You see a box. You open the box. There is a key inside it. You take the key. You use the key on the door. The door opens. You get out.

It may sound simple – and it is – but this is the basis of a simple game. Remember! You will have time to improve it later on! The point here is to learn the very basics of programming.

The code

Open the newly created project. Now you need to add the code to actually make your game. We will do this by adding the minimum lines of code to make the game. As said above, you will then be able to add additional code and improve the game. What we will show here is the basic principles.

The game has no graphics, so all we need is a way to get user input through text and then present to the user again the results of his action via text.

For a short description of the basic elements of a program (variables, functions etc) check the Huo chess tutorial here.

In short, what we will need is the following programming elements:

Variables: These are elements of different types which hold values. A variable can be an integer variable (which may hold values like 1, 4, 10), a String variable (which may hold values like “key”, “door” etc) or a variable of many other types. We will use the following variables:

  • A variable to hold the command the user has entered. We will call this user_command.
  • A variable to know whether the user has the key or not in his pocket. We will call the variable user_pocket.
  • A variable to determine whether the game must end. We will call this exit_game.

Input/ Output commands: These are the commands we will use to get input from the user (the commands he will issue to do things in the game) and to inform him of the result of his actions with a text message on the screen. There are three major commands we will use for this:

  • Console.Write: This command writes something on the screen, while leaving the cursor at the same line.
  • Console.WriteLine: This command writes a line on the screen and then presses Enter to position the cursor in the next line.
  • Console.ReadLine: This command reads what the user has entered. We will use this command to get the user input and store it in a variable we will call ‘user_command’. Intuitive isn’t it?

All of the above functions are existing functions in C# to process input and output in a program. The fact that they all start with ‘Console.’ simply means that these functions are part of a class called ‘Console’ – look at the class as a large collection of functions. And yes, this is a very simplistic and – to be honest – technically wrong way of putting it. But it will keep your brain from exploding while giving you some time to read in Google what Console is in C#. (remember to check again the basic tutorial for Developing a chess program here, where I have a short description of what a class and a function is)

Decision commands: The program needs to decide what to do when the user does some things. So for example, when the user enters the command “Open door” it needs to be able to decide whether or not the door will open (if the user has the key it will open, if not it will not). We will use the ‘if’ command to perform this kind of operations. The way this command works is very intuitive. You will see in the code how this happens.

The commands the game will accept are the following:

  • Look around
  • Open box
  • Get key
  • Open door
  • Q will exit the game

Loop commands: We need to make the game constantly ask for user input. So after each command the user enters, the program must process it, inform the user of the result of his action and then go back and again ask for input. We will use a do… while look to perform that. With that, every line of code that is within brackets between the ‘do’ and the ‘while’ commands is constantly executed until the condition in the ‘while’ command is met. You will understand when you see the code.

TIP: Google is your friend. Don’t be shy to use it. If you are not sure how a command works just write it into Google search and find out yourself! Not sure how the ‘if’ command works? Google “C# if command” and there you go! (the C# is always needed in the search if you search for C# – but this is rather a tautology and not an advice)

Writing the code

Are you ready to actually write the code?

Well, don’t worry. I have done that for you. Look at code below and write down the program in your own project’s Main method yourself. Go to the static void Main(string[] args) method and write down the below code inside the brackets.

// Declare the variables
String user_command;
String user_pocket = "";
bool exit_game = false;

// Main program loop
do
{
// Ask for user input
Console.Write("What do you want to do? (press Q to exit) ");
user_command = Console.ReadLine();

// Process user input
if ((user_command == "Open door") && (user_pocket == "") )
Console.WriteLine("Door is locked");
else if (user_command == "Look around")
Console.WriteLine("You see a box on a table");
else if (user_command == "Open box")
Console.WriteLine("There is a key inside");
else if (user_command == "Get key")
{   
     Console.WriteLine("You got the key");
     user_pocket = "key";
}
else if ( (user_command == "Open door") && (user_pocket == "key") )
{
     Console.WriteLine("You opened the door! Bravo!");
     user_pocket = "key";
     exit_game = true;
}
else if (user_command == "Q")
     exit_game = true;
else
     Console.WriteLine("Nothing happens");

Console.WriteLine("");
} while (exit_game == false);

There you go!

A game that you wrote!

Your code should look like that.

Important Notes

  • All C# commands end with a semicolon, as you may have noticed. With some exceptions as may have also noticed.
  • The comments are entered after double slash ( // ). They are usually very useful not only for others but mainly for the programmer himself!

Download the code

Ideally you should write the code on your own, so that you practice. However for reference purposes, you can also download the example below.

The file contains the solution as it was developed for this tutorial.

Executing the program

In order to build your code, select Build > Build solution (or Rebuild solution , after you have built it for the first time).

In order to run your program, select Debug > Start without debugging.

The result should look something like this…

That is, if you are smart enough to find the key…

Epilogue

Like when I copied the code from my first Commodore computer manual, you may not know exactly what you write and why. But with what you have read above, you will certainly understand the meaning of the commands and what they do!

You now have a game that you wrote. And potentially a gate to the world of programming. At the end, it is not up to me to teach you anything. It is up to you to learn. As Buddhism says, when the student wants, the teacher appears. I am not your teacher. But I may have instilled in you the desire to search for one…

Happy coding!

APPENDIX – Jurassic Park Adventure Game

When I was a kid, I developed an adventure called “Jussaric Park” based on the known book of Michael Crichton. It was (and still is) a simple yet fun to play text-based adventure writen in QBasic.

You can download the code from below and use your favorite QBasic IDE to view, edit and execute it. The QB64 is a good choice for that.

The user must set the speed when starting the game
Yeap, it is mine!
Graphics are nice… Wait till you hear the crickets…
It’s all Greek to you.
The main screen of the game where you can select the commands
Walk command: Press ‘w’ and simply write where you want to go!
Using the JUPASE (JUrassic PArk Software Emulator)
1 / 6
You made it! You got out of Jurrasic Park!

You can find the code below. Simply copy-paste it into a QBasic editor and execute. The IOURASIO file is the main game. The JUPASE program is called when you reach the Visitors’ Center.

As already mentioned, it is writen in QBasic and it is a playable and enjoyable (at least for those not addicted to ultra-fast 3D graphics) text-based game. It has comments inside the code to help you understand what happens and how it works, so essentially it is an extension of the tutorial you just read.

Note: You will have to update the path in the JUPASE call. (See? Even when having fun, you can alway learn something!)

Have fun!

And keep coding!

Constant app updates: The futility of modern philosophy in practice…

You have encountered the problem: your mobile phone applications are constantly updated and this results in their size constantly growing.

Dozens of megabytes of updates swarm in your mobile phone every day to make your applications better, safer, more usable, more fun.

And no one has ever questioned the obvious: Couldn’t this be done via making the applications SMALLER?

We are so accustomed to the philosophy ‘Bigger the better’ that we cannot even think of such a possibility. For us it is obvious that any improvement results in a bigger application.

And yet, there are ways to improve an application while making it smaller. Removing obsolete parts and making source code more efficient are things which were in the mind of programmers some years ago.

But not anymore.

Why? The reason is simple!

Phones are getting more and more memory anyway, so why burden yourself with such worries? Just add things to the code without caring about performance or optimization of the code. The bigger phones will handle it anyway! And the users will be do happy to have applications with sizes in the hundreds of megabytes! Because they can?!

Once upon a time there were developers who cared about making efficient code. And these developers managed to write chess applications to run in just… 1 KB of memory!

Now we have notepad apps in the dozens of MB. And they are still getting bigger and bigger by the day.

Because stupid users generate the need for stupid developers.

Somehow we should not worry about anything of the above. Because we just get what we deserve.

One day though we will see that 1 KB chess program (search for MicroChess for this, also look for HUO CHESS, which I am constantly developing as the smaller open source code chess program and making it SMALLER in every version while increasing it’s capabilities). And we will be astonished. And…

Well, nothing.

We will just keep on playing with our mobile phone…

Huo Chess (C# micro chess)

Huo Chess DV – GUI 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!

Latest versions

  • In .NET Framework: Huo Chess 0.9922
  • In .NET: Huo Chess DV [2021-11-10]

Huo Chess News

  • 2021-11-10: Ported Huo Chess in Visual Studio 2022 (C# .NET 6.0) to create the latest Huo Chess DV version. The equivalent version in .NET Framework is the v0.9922 version.
  • 2021-04-07: Re-optimization of CountScore (evaluation of position) function for endgames regarding the pawns evaluation.
  • 2021-04-03: Optimized the CountScore (evaluation of position) function for endgames. Huo Chess v0.9921 beat bot at Chess.com with 1100 ELO rating.
  • 2021-03-20: Added the castling capability also for the computer (up to now, the program only recognize and allowed the castling move for the human opponent).
  • 2021-03-14: Thinking in level 4 and 5 is conducted only if the moves entail the capturing of a piece. Removed redundant code. Improved CountScore function with positional elements for the ending of the game. Introduced the elements of mate and stalemate in the thinking process.
  • 2021-01-06: Fixed initialization of nodes 3 and 4.
  • 2020-09-03: Minor updates.
  • 2020-08-10: Updated the countScore function that evaluates positions to include also qualitative criteria for the position.
  • 2020-08-03: Minor updates to the v0.992 Developers Edition. The trimming rules have been updated to enable deeper thinking.
  • 2019-02-21: The v0.992 has received initial operational capability. The algorithm now works for thinking depth of 5 half-moves, with thinking tree trimming applied.
  • 2019-02-12: Huo Chess version 0.991 published. Now the algorithm performs as planned. Thinking depth limited to 3 half-moves.

Huo Chess Overview

The Huo Chess is one of the smallest open source chess programs in C# (the latest .NET console version is only 159 KB is size, even though it utilizes the latest .NET Platform; while the latest version in .NET Framework is only 44 KB in size). It has heavily commented source code so that it can be used for teaching purposes on how to develop a chess program (or a program in general). It can be easily reused in other applications, e.g. to use it as a chess engine for a robot playing chess (see here).

Feel free to re-use it as you see fit (including changing it or selling it), as long as the source is cited properly. The goal is to spread the knowledge on the basics of chess programming, so sharing is important. Feel free also to contribute to the project, by contacting Harmonia Philosophica with ideas or code that can be used.

Huo Chess sites

You can download the latest files via the relative sites:

Alternatively you can download it below.

Important Notes: The latest version of Huo Chess that is still in development is available only in this site and in GitHub repository. The CodeProject and the MSDN sites still have the v0.992 version.

Huo Chess-based tutorials

Over the years I have written several tutorials on how to develop a chess program based on the Huo Chess.

Tutorials on how to develop a chess program include:

LATEST VERSIONS

DV Version highlights

The latest Huo Chess DV (Delta Version) editions are built in the latest Visual Studio 2022 and use the latest C# .NET 6.0 platform. Remember: along with having the goal of being the smallest chess program for educational purposes, the goal is also to constantly follow all developments in the programming landscape so as to stay relevant!

However being modern and flexible has a price. The new platform had a significant impact on the size of the Huo Chess application: Even after all optimizations, the console edition is now 159 KB (from 44 KB that it used to be) and the GUI edition is now 239 KB (from 96 KB that it used to be). Hopefully the application trimming in the new .NET platform will be improved in the future so these numbers can do down.

Read the Develop a (chess) program in Visual Studio 2022 (C# NET 6.0) article to see how the Huo Chess program was ported into Visual Studio 2022 and .NET 6.0, along with some details on the publishing and trimming settings of the application.

Version 0.9922 highlights

The version 0.9922 is the latest .NET Framework version, i.e. the equivalent to the latest DV Version in .NET. This version has only minor updates regarding logs when compared to v0.9921.

OLDER VERSIONS

Version 0.9921 highlights

The new version of Huo Chess (v0.9921) is even smaller in size than all the previous ones and yet much more capable. This was made possible by optimizing code and removing redundant code as well. The official edition with Windows Forms user interface is 77 KB in size (as opposed to 78 KB of the previous version), the Console application with a chessboard User Interface (UI) edition is 45 KB (as opposed to the 46 KB of version v0.992) and the Console application with text-based chessboard UI edition is 44 KB in size (whereas the previous version was 46 KB in size).

The new version has an updated evaluation function (CountScore) for the endgame, it has improved code for the castling move and also has an optimized deep thinking mechanism.

Huo Chess v0.9921 editions

  • Huo Chess 0.9921 with GUI: This version is the main version with a Windows Forms chessboard User Interface.
  • Huo Chess 0.9921 cs: Console application (text only).
  • Huo Chess 0.9921 cs with graphics: Console application, with minimum text-graphics to depict the chessboard.
  • Huo Chess 0.9921 Developers version: Full developers version with Windows Forms chessboard and logs to show the thinking mechanism. (Attention: Remember to delete the logs when needed, since their size can increase significantly)

Version 0.992 highlights

The v0.992 version of Huo Chess is small in size (as all the previous ones) and much more capable. The official edition with Windows Forms user interface is 78 KB in size, the Console application with a chessboard User Interface (UI) edition is 46 KB and the Console application with text-based chessboard UI edition is 46 KB in size.

Issues with the MiniMax thinking algorithm are fixed and now the computer makes logical moves in every circumstance (at least for the depth of 5 half-moves in which it thinks). Capability of thinking at depth of more half-moves will be added in the future. The program also features an Opening book capability (must have positions in the “Opening Book” folder to use it).

Additional to the above, a Developers Edition has been created, which includes analytic logs of the thinking mechanism for debugging (for teaching purposes). The logs can be easily activated or deactivated. This edition also takes a small step towards showing the variant which the computer thinks to the user interface.

The Opening Book editor is distributed separately. Along with the editor, a library of some sample opening book positions is also included in the distribution files.

Huo Chess editions

There are various Huo Chess editions, each one with slightly different User Interface, plus one for developing purposes.

For the latest Huo Chess DV (Delta Version) there are currently two editions:

  • Huo Chess DV console: The console edition supporting opening functionality and up to 5-ply thinking. Currently using C# .NET 6.0 and standing at 159 KB.
  • Huo Chess DV: The GUI edition supporting opening functionality and up to 5-ply thinking. It also contains logs that can be activated to analyze and debug the thinking algorithm. Currently using C# .NET 6.0 and standing at 239 KB.

Both editions have heavily commented code to allow for using the open source chess engine for educational purposes.

Future versions candidate improvements

There is already a list of potential improvements for the next version of Huo Chess, that can be found below:

  • Add castling capability for the computer to also be able to castle. Also castling should be embedded in the CountScore function.
  • Reduce size by improving ‘peripheral’ functions (like CheckForBlackMate, ElegxosNomimotitas et cetera).
  • Increase efficiency of MiniMax algorithm, by taking into account the average score of each branch. (code already in place, but commented out)
  • Add neural network capability: This will be achieved at a very primitive level by having key points of the MiniMax algorithm (the inequalities at each node level analysis in ComputerMove) being dependent of values stored in external configuration files and by updating those files every time a game is played (increase ‘score’ if the game is a win, decrease if it resulted in defeat). This is still in preliminary stage, but I will try to have a relative Developer edition ready in the next version (without compromising the size).
  • Improve CountScore function, to take into account the position of the chessboard, whether you move the same piece twice in the opening et cetera.

Huo Chess DV distribution files (latest .NET version)

The Huo Chess DV version is the version developed in Visual Studio 2022 and utilizing C# .NET 6.0.

These versions are expected to be updated frequently as the .NET Platform is also updated. Needless to say that these editions render the v0.9921 version the last version utilizing the .NET Framework.

Huo Chess v0.9922 distribution files (latest .NET Framework version)

The version 0.9922 of Huo Chess is the latest version available in .NET Framework. It is the equivalent of the DV version but in the .NET Framework.

Huo Chess v0.9921 distribution files

The version 0.9921 of Huo Chess is currently in development. Take a look at the progress of the project exclusively here at Harmonia Philosophica or directly in the GitHub repository. The code is updated frequently.

HUO CHESS 0.9921 with GUI – Developers edition 2021-04-03 (GitHub)

Note that this is a draft version which is still under development. Also please remember to delete the logs after some moves, or disable them completely if you want to play a full game. The size of the logs of the Developers edition can increase significantly after only some moves.

Di mi se mai fu fatta alcuna cosa
(Tell me if anything was ever done to completion)

~ Leonardo da Vinci

Huo Chess v0.992 distribution files

The version 0.992 of Huo Chess is in development. Take a look at the progress of the project exclusively here at Harmonia Philosophica! The code is updated frequently.

All the files of the v0.992 can be found below. Make sure you download the latest one. The previous versions are kept for historical reference purposes. Also at the end feel free to download the latest v0.9921 version that is still in development

This version is the officially stable version that can also be found in the GitHub repository and the official sites (like CodeProject) which host the Huo Chess code.

Huo Chess v0.991 distribution files

Distribution files for the Huo Chess v0.991 version include the following:

  • Huo Chess 0.992 with GUI
  • Huo Chess 0.992 cs
  • Huo Chess 0.992 cs with graphics
  • Huo Chess 0.992 Developers version
  • Opening Book editor
  • Opening Book

Feel free to contact me for anything regarding the code or the project in general. Remember that the goal is not only to create the smallest chess program but also to create a good basis for educational purposes.

Huo Chess Opening Editor

From the beginning of the Huo Chess project (well, almost from the beginning), an opening functionality was developed. The opening book of Huo Chess is edited with the help of a dedicated tool: the Huo Chess Opening Editor. Simply run the tool, play the moves by selecting “Opening mode” and all the moves will be stored as suggested moves for the computer in its opening book.

There is a C++ (cpp) and a C# (cs) version of the tool, which can be both downloaded below.

The tools generate txt files which hold the positions of the opening book, along with the suggested moves for each position. Note that there must exist a ‘Huo Chess Opening Book‘ folder in the same folder as the application for the latter to store the opening book files.

Make sure you keep checking back in this page for more updates.

Huo Chess Java edition

The Java edition of Huo Chess is also developed in parallel with the C# versions. Currently, only the core engine with text interface with the user is developed in Java. Anyone willing to provide a GUI is more than welcome to do so. (I will try to do that as soon as I have time)

This code can be the basis for an Android Huo Chess application. I am currently working with Visual Studio and Xamarin framework to build it.

Huo Chess QBasic edition

The Huo Chess has been written in QBasic as well. Yes, you read it right! The old friend from the past, BASIC, is still alive and kicking!

You can find the code below. Along with the games’ source code, an opening editor is also distributed. Both work in the same way the C# edition works.

Feel free to write to me for any comments or suggestions.

Instead of epilogue…

The ultimate goal is to go back to where I begun programming and fulfill my child dream: to create a chess program for Commodore 128. The porting of the current Huo Chess version to QBasic is currently in progress and then the road to porting to Commodore BASIC v7.0 is open…

And remember: Huo Chess was not made to be completed. But exactly the opposite: It is here to remind us all the constant struggle to perfection. A struggle so vain as any other human struggle and yet, a struggle so familiar and lovely.

Huo Chess will constantly be upgraded…

By me… By you… By everyone…

And year by year it will constantly be better and smaller…

Until there is nothing left…

Because at the end, the best chess game I will ever play will be that game I played as a child, with my father, trying to learn how to play…

APPENDIX – Huo Chess games

This appendix contains various games played by Huo Chess over the years.

Game 1: Huo Chess v0.9921 vs. Jimmy bot (600 ELO rated) at Chess.com

Huo Chess v0.992 vs. Jimmy Bot @ Chess.com [2021-03-21]

[Site “Chess.com”]
[Date “2021.03.21”]
[Round “?”]
[White “Huo_Chess”]
[Black “Jimmy”]
[Result “1-0”]
[TimeControl “-“]

1. g4 g5 2. Nc3 d5 3. Bh3 Nc6 4. Nf3 h6 5. d4 Qd6 6. Be3 b6 7. Bd2 Nb4 8. Nb5 Qd7 9. Nxa7 Rxa7 10. Bxb4 Nf6 11. Ba3 Ba6 12. c3 Bc4 13. Ne5 Qb5 14. Bg2 Ra4 15. e3 Qa5 16. b4 Ba6 17. bxa5 Rxa5 18. Bb2 Bg7 19. Qf3 Bb7 20. c4 h5 21. gxh5 Ra7 cxd5 Ra6 23. e4 Bh6 24. Bc1 Ra4 25. Be3 O-O 26. Kd1 Ba6 27. Nc6 Kh7 28. Nxe7 Ra5 29. Qxf6 Bd3 30. Nf5 Kg8 31. Nxh6+ Kh7 32. Bxg5 Raa8 33. Nxf7 Rxf7 34. Qxf7+ Kh8 35. Bf6# 1-0 

The game resulted in Huo Chess easily beating the bot within 35 moves.

Game 2: Huo Chess v0.9921 vs. Jimmy bot (600 ELO rated) at Chess.com

Huo Chess v0.992 vs. Jimmy Bot @ Chess.com [2021-03-22]

[Event “vs Computer”]
[Site “Chess.com”]
[Date “2021.03.22”]
[Round “?”]
[White “Huo_Chess”]
[Black “Jimmy”]
[Result “1-0”]
[TimeControl “-“]

1. g4 g5 2. Nc3 e5 3. e4 Ne7 4. Nf3 f6 5. Bb5 Ng8 6. Ba4 Nh6 7. d4 a6 8. dxe5 c6 9. exf6 Kf7 10. Nxg5+ Kg6 11. Ne6 Rg8 12. Nxd8 d6 13. Bxh6 Kxh6 14. Nf7+ Kg6 15. Nxd6 b5 16. Bb3 Bxd6 17. Bxg8 Bc5 18. Qf3 h6 19. Ne2 Bb6 20. Nc1 Bd8 21. c4 Bxf6 22. cxb5 cxb5 23. e5 Bxg4 24. Qxa8 Bxe5 25. Qe4+ Kf6 26. Qxg4 Ke7 27. Qe6+ Kd8 28. Qxe5 Nd7 29. Qd6 h5 30. Be6 Kc8 31. Qxd7+ Kb8 32. Qe8+ Kc7 33. Qxh5 Kb7 34. Bd5+ Ka7 35. Nb3 Kb8 36. Rd1 Kc7 37. Qg6 a5 38. Nxa5 b4 39. Qg4 Kb6 40. Qxb4+ Ka6 41. Nb3 Ka7 42. Qb7# 1-0

Game 3: Huo Chess v0.9921 vs Sven bot @ Chess.com (1100 ELO rated)

[Event “vs Computer”]
[Site “Chess.com”]
[Date “2021.03.26”]
[Round “?”]
[White “Huo_Chess”]
[Black “Sven”]
[Result “1/2-1/2”]
[TimeControl “-“]

Huo Chess v0.9921 vs Sven (1100) [2021-03-26]
1. g4 g6 2. Nc3 d5 3. Bh3 c6 4. d4 Nf6 5. Be3 h6 6. Bd2 Na6 7. Nf3 Nb8 8. Bf4 Nxg4 9. Rg1 h5 10. e4 Be6 11. exd5 cxd5 12. Ng5 Bh6 13. Nxe6 fxe6 14. Bxb8 Rxb8 15. Rh1 Bg7 16. Rb1 Qb6 17. Qd3 Rh6 18. Qd2 Ra8 19. Na4 Qd6 20. Nc5 b5 21. Nb7 Qa6 22. Nc5 Qd6 23. Nb7 Qa6 24. Nc5 Qxa2 25. Rd1 Qc4 26. b3 Qxd4 27. Qxd4 Bxd4 28. Rxd4 e5 29. Rxd5 b4 30. c4 Rh8 31. Ke2 Rh6 32. Kf1 Rh7 33. Ke1 Rf7 34. Nd3 Rg7 35. Nxb4 Kf7 36. Nc6 Kg8 37. Nxe5 Nh6 38. c5 g5 39. Nf3 Kh7 40. Nxg5+ Kg6 41. Ne6 Kf7 42. Nxg7 h4 43. Nh5 Ng8 44. Rd4 Rb8 45. b4 a5 46. bxa5 Rb1+ 47. Rd1 Rb7 48. c6 Rb8 49. Rd4 Rb2 50. Rxh4 e5 51. c7 Rc2 52. c8=Q Rxc8 53. Bxc8 Ke7 54. Ba6 Kd7 55. Re4 Ke6 56. f4 Kf5 57. Rxe5+ Kg6 58. Bd3+ Kf7 59. Bc4+ Kf8 60. Rf5+ Ke7 61. Bxg8 Kd7 62. Rd5+ Kc8 63. Rd1 Kb8 64. Ra1 Kc8 65. f5 Kc7 66. Rd1 Kc8 67. Rd2 Kb7 68. Rd8 Kc7 69. Rd1 Kc8 70. Ra1 Kc7 71. f6 Kd8 72. f7 Ke7 73. Rd1 Ke6 74. Rb1 Ke7 75. Ra1 Kf8 76. Nf4 Kg7 77. Nd3 Kf8 78. Nb2 Ke7 79. Rd1 Ke6 80. Na4 Ke7 81. Nb2 Ke6 82. Ra1 Ke7 83. Na4 Kf8 84. Nb6 Ke7 85. Rd1 Kf8 86. Na4 Ke7 87. Nb2 1/2-1/2

Game 4: Huo Chess v0.9921 vs Sven bot @ Chess.com (1100 ELO rated)

[Event “vs Computer”]
[Site “Chess.com”]
[Date “2021.04.02”]
[Round “?”]
[White “Huo_Chess”]
[Black “Sven”]
[Result “1-0”]
[TimeControl “-“]

Huo Chess v0.9921 vs Sven (1100) [2021-04-02]
1. g4 c6 2. Nc3 h6 3. d4 g6 4. Be3 Qc7 5. Qd2 Bg7 6. Bf4 Qd8 7. e4 Kf8 8. Bc4 Ke8 9. Nge2 Qb6 10. Na4 Qd8 11. Nc5 b6 12. Nb3 d6 13. Rg1 Kf8 14. h4 Qe8 15. Nbc1 Bd7 16. Nd3 a5 17. c3 h5 18. g5 a4 19. Qc1 b5 20. Bxb5 Ra5 21. Bc4 Bg4 22. b4 Ra8 23. Ng3 Qd8 24. Nf1 Bd7 25. Qa3 Bg4 26. Nb2 Rh7 27. Nxa4 Na6 28. Bxa6 Qc7 29. Nb2 Bh3 30. Nd2 Be6 31. Qa4 Ke8 32. Nd1 Qb6 33. b5 Bf8 34. Rh1 Bc8 35. Nc4 Qc7 36. b6 Rxa6 37. Nxd6+ exd6 38. Qxa6 Be7 39. Qa8 Qd7 40. Qb8 Kf8 41. c4 Qd8 42. b7 Bxb7 43. Qxb7 Ke8 44. Qxc6+ Kf8 45. Qa6 Qd7 46. Qa8+ Bd8 47. Qa3 Be7 48. Qa6 Qd8 49. Nb2 Qc7 50. Qa3 Qb6 51. Qa8+ Kg7 52. Rb1 Qxd4 53. Ke2 Qb6 54. Qd5 Rh8 55. Na4 Qc7 56. Ra1 Qc8 57. Qd4+ Kh7 58. Bxd6 Qe6 59. Bb8 Qc6 60. Be5 Ba3 61. Bxh8 Bf8 62. Nb2 Qc7 63. Nd1 Qe7 64. Nc3 Qc7 65. Nb5 Qb7 66. Na7 Qa8 67. Be5 Bb4 68. Rab1 Ba3 69. Rb8 Qxe4+ 70. Qxe4 Bc5 71. Nc6 Ba3 72. Rb7 Nh6 73. Qd3 Ng4 74. Qxa3 Nxf2 75. Rxf7+ Kg8 76. Qf8# 1-0 

Game 5: Huo Chess v0.9921 vs. Sven bot @ Chess.com (1100 ELO rating)

[Event “vs Computer”]
[Site “Chess.com”]
[Date “2021.04.07”]
[Round “?”]
[White “Huo_Chess”]
[Black “Sven”]
[Result “1-0”]
[TimeControl “-“]

1. g4 d5 2. Bg2 Be6 3. Nc3 c5 4. e3 Qd6 5. Qf3 Nf6 6. Bh3 b6 7. Nge2 a6 8. d3 Qe5 9. Nf4 g6 10. d4 cxd4 11. Na4 Nbd7 12. Rb1 Rg8 13. Bd2 b5 14. Nd3 Qe4 15. Qxe4 dxe4 16. Nac5 Nxc5 17. Nxc5 Bxa2 18. Ra1 dxe3 19. Bxe3 Nd7 20. Rxa2 Rd8 21. Rxa6 Nb8 22. Ra5 Nc6 23. Rxb5 Ne5 24. Nxe4 f6 25. Bb6 Ra8 26. Ra5 Rb8 27. Ra6 Bh6 28. Bc7 Rb7 29. Ra8+ Kf7 30. Rxg8 Rxc7 31. Rh8 Rxc2 32. Rxh7+ Bg7 33. Rf1 Rc6 34. Nc3 Rb6 35. Na4 Re6 36. g5 Rc6 37. Rg1 Nf3+ 38. Kf1 f5 39. Rg2 Kg8 40. Rg3 Nd4 41. Rh4 Rc2 42. b4 Be5 43. Re3 Rc1+ 44. Kg2 Nc6 45. Rh6 Bg7 46. Rxg6 Kh8 47. b5 f4 48. Re4 Nd4 49. b6 e5 50. Nb2 Kh7 51. Rd6 Nb3 52. Nc4 Ra1 53. Bf5+ Kg8 54. Nxe5 Nc5 55. Rd8+ Bf8 56. Rxf4 Kg7 57. Ng6 Ra4 58. Rf3 Be7 59. Nxe7 Rh4 60. Rd5 Ne4 61. Rd4 Nxg5 62. Rxh4 Kf7 63. Re3 Kf8 64. Re5 Kf7 65. b7 Nf3 66. Kxf3 Kf8 67. b8=Q+ Kg7 68. Qa8 Kf6 69. Qh8+ Kf7 70. Rh7# 1-0
Huo Chess v0.9921 vs Sven (1100) [2021-04-07]

More games to be uploaded soon with stronger opponents!

How to code a chess program in one day. (C# and Java examples)

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!

Introduction

Coding is easy.

Today there are a lot of people learning how to do it.

Why not you?

And you know what is the most difficult step in learning how to code?

TO START LEARNING.

You know there is an old Buddhist saying claiming that “When the student wants, the teacher appears”. So the question is not whether there are tutorials out there to teach you. There are a lot. Here or elsewhere, you can easily find articles showing how to code.

The question is: Are you willing enough?

(And yes, Harmonia Philosophica is mainly a philosophy portal, so feel free to read about philosophy while trying to debug your first chess program)

For this ultra quick training I will use a Java edition of my Huo Chess program. This edition lacks basic functionalities like deep searching, but it can however play chess according to the rules and choose the best move at the current position based on which move earns the most material.

Places to download Huo Chess (source code and executable)

License: You can reuse the code you see here in any way you want, as long as you mention the author and the place where you read it first!

RELATED TUTORIALS:How to program a chess program for Beginners” series will give you a more detailed description of how to create your chess program! Check also the Programming for kids: Developing a chess program in BASIC and the Programming a chess application in QBasic (QB64) tutorials for developing a chess program in BASIC programming language. Check out also the “How to develop an adventure game ultra-fast tutorial” tutorial! You could also find interesting the HUO Writer: A simple AI writer with basic Neural Network capabilities (QB64) article. The How to develop a chess program from scratch (for total beginners) series will help you develop a chess program even though you are a total beginner! Check also our affiliate chess-programming portal!

千里之行,始於足下

(A journey of a thousand miles begins with a single step)

~ Lao Tzu, Tao Te Ching

Step 1: Get a programming interface

This tutorial will show you how to code a demo chess program in one day, even without any prior knowledge of programming. All you need is a programming interface to write and compile (i.e. make an executable program out of your code) the code that you will write. These are also known as Programming IDEs (Integrated Development Interfaces). This could be the Microsoft Visual Studio (the Express edition of which can be downloaded for free) for C# or any Java compiling studio available out there (like NetBeans for example, which again can be downloaded for free).

The tutorial will not cover the details on how to use these programming interfaces. But this is not something hard to learn anyway. Just go to the web (or the page of the tool itself) and you will gain an understanding on how to write a small console program (this is what we are going to do here) in less than an hour. When I refer to a console program I mean a program which does not have graphics but only displays text. We will start with building such a program since writing code for graphics will surely need an additional effort to learn how to do it. And it will be easy to later on add graphics to your program, once you have the AI logic of the chess engine up and ready, don’t you think?

Open the IDE and select to create a new project…
Select to create a simple application…
Your program is created. All you have to do is add the code…

Tools to use for coding your program

  • Microsoft Visual Studio (free edition)
  • Java NetBeans

After you download the IDE, install it in your computer and go to the option New Project. Select to create a simple C# console application (in Visual Studio) or a simple Java program (in NetBeans). Click Create and there you are. You have the shell in which you will add your chess program code!

Step 2: Design the chess program structure

Let’s go into the chess program now.

But wait!

Don’t we need to learn the programming language first?

The answer is simple: NO!

You will learn them as you go.

This is how I (and many others) learned programming in the first place. I opened my Commodore computer and started typing my first program by copying the code from the manual. After I typed it in and spent time to get it right (even copying can be difficult, especially if you are a rookie) I typed RUN and I had my first game! Forget the long chapters of books trying to teach you – after reading 100 pages – how to just print “Hello world” on a screen. Programming is for people who like experimenting and are not afraid to simply start trying things on their machine.

So just go ahead!

You will read here about the basic structure needed for the program, and then you will see the commands required to code that structure into a C# or Java program. The commands are fairly simple and you will understand what they do as you write the program. Do you really need a programming language manual to understand what an “if” command does anyway?

The main structure of the program is depicted in the following schema.

Fairly simple isn’t it?

The story goes like this…

  1. You get input from the user for his move. [EnterMove function]
  2. You check the legality and the validity of the move entered [ElegxosNomimotitas and ElegxosOrthotitas functions, which also utilize the CheckForWhiteCheck and CheckForBlackCheck functions to check if there is a check in the position]
  3. If the move is legal and valid, then do the move. [call drawPosition function to redraw the chessboard; note that the Java edition of the program only prints the chessboard in text]
    • Exercise for you: Add graphics to the application. (use the C# edition ot get some insight on how you could do that)
  4. Call the function which makes the computer think. [ComputerMove function]
  5. Scan the chessboard to find all the pieces of the computer. In the sample code provided for Java, this means that it scans for all the black pieces since the program is made only for the computer to play with black.
    • Exercise for you: Add the ability for the program to play with white as well)
  6. For each piece of the computer, check all possible moves to all possible squares of the chessboard.
  7. If you find a valid move (by invoking the ElegxosNomimotitas and ElegxosOrthotitas functions) then do the move and count the score of the position. [CountScore function: This is one of the most central functions of the program; changing it makes the computer play differently according to the values you assign to pieces or to elements of the chessboard like empty ranks, concentration of the pieces at the center et cetera]
  8. If the score of the move is better than the so far better score, then store this position as the best one. (Note: The program stores the first position analyzed as ‘best’ anyway, so that there is a starting point for the best score)
    • Exercise for you: Change the values in the CountScore function to improve the playing abilities of the computer)
  9. Do the best move found and redraw the chessboard.
  10. Wait for next user input. [call EnterMove function again]

The Java edition of the program does not think in more depth. It just finds the move which earns the more material and makes it. This is not chess isn’t it? Well actually it is. The basis for a good chess program. All you have to do is improve it.

Look at the C# version of “Huo Chess” (search the web for it, there are many repositories where I have uploaded the code, including MSDN Library, CodePlex and Codeproject) to gain a small insight on how the chess program can search into more depth.

Exercise for you: Make the Java edition of the program to think in more depths, by adding more “ComputerMove”-like functions to analyze the next potential moves for the human and the computer. Be careful how you pass over the chessboard between these functions. (yes, Google will be your best friend)

Related resources for C# Huo Chess edition

Step 3 : Write the code

This last section contains the source code of the chess program in Java.

As mentioned above, look for “C# Huo Chess” in the web (See the Recourse listed above) to see the full source code of that Huo Chess edition. This code is also heavily commented to make it easy to read and re-use, while the names of the functions are the same as in the Java edition of the program.

Look also at the “How to program a chess program for Beginners” series here in Harmonia Philosophica, to get some more details on the Huo Chess program. (the tools used in those articles are a bit outdated, but yet the logic is still the same)

Paste that code into the main java file in your NetBeans project and compile it.

Execute your program and enjoy your new chess game.

Epilogue

So there it is. Depending on your copy-paste and reading comprehension skills, you now have a working chess program on your hands and some knowledge on how it could be improved. Everything is up to you now.

Happy coding!

And don’t forget: Keep experimenting!

The worst thing that could happen?

To just ruin your computer…

Exit mobile version
%%footer%%