How to make a good site.
How to get a good Alexa rank.
How to make meatballs.
How to buy a used car.
How to make money.
How to mine Bitcoins.
How to make babies.
How to kill babies. (a.k.a. “abortion”)
How to watch solar eclipse.
How to lose belly fat.
How to earn the lottery.
How to find free… you know what.
How to find true love.
How to start a new business.
How to promote your site.
How to train your dragon.
People engulfed in the everlasting Sisyphian effort of ‘doing something’. Not because they want to; most of them anyway despise doing what they do and that is why they so eagerly search for ways to do it. But because they are afraid of the alternative: Doing nothing. People wanting to do things only because doing nothing will let them alone with their most dreaded enemy: their self.
It is easy to always do something. You are occupied so much that you always keep the darkness away. You drift away from the abyss of your own consciousness by limiting your thoughts to the shallowness of existence. You don’t have to worry about the important thing except about what you are… doing.
That the dark secret of our modern times: We are all filling the basin of Danaides – judging those who don’t – but we never care to see whether the jar has any holes. And no matter how long we keep doing it and how little results we see, we keep on doing it mesmerized by the great story taught to us since we learnt how to walk: You must do something…
Let go. Try to just breath.
Look at the butterflies.
Stare at the raging ocean.
Try not to search for how to do something.
But try to remember how to do nothing.
You will discover that this is the hardest thing you ever did…
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)
(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)
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)
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.