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)
- Huo Chess Main Page
- Huo Chess @ Codeplex
- Huo Chess @ Codeproject (includes analysis of the code)
- Huo Chess @ MSDN Code Gallery
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 dummies” 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.
(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?
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.
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…
- You get input from the user for his move. [EnterMove function]
- 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]
- 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)
- Call the function which makes the computer think. [ComputerMove function]
- 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)
- For each piece of the computer, check all possible moves to all possible squares of the chessboard.
- 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]
- 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)
- Do the best move found and redraw the chessboard.
- 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 dummies” 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.
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.
And don’t forget: Keep experimenting!
The worst thing that could happen?
To just ruin your computer…