Pages

Saturday, April 19, 2014

C or Ruby

Now that I've more time, I am debating between Ruby and C/C++.

I would like to write the chess engine in C/C++ to improve my  C skills, but I just cannot let go of the highly expressive Ruby.  Ruby is also much slower compared to C.

So I've decided to start with C++ and see how intolerable it is to me.  The first step of writing a chess engine is to program a board representation. A board representation is the foundation of the chess engine. It is used in keeping track of the chessboard state,  evaluation of board positions and almost every part of the engine.

There are different types of board representation, which can be categorized into 2 main approaches. Namely the Square Centric, Piece Centric and Hybrid Approach.  The descriptions of these approaches can be found in the link here.

Long story short, I've picked the square centric, bitboard representation.  A bitboard represents the board with a 64-bit word. Using 0s and 1s to denote whether a pieces/ potential move is there. This representation can take advantage of the 64-bit hardware and speed up the operations.

Take the following example:
queen attacks    &  opponent pieces  =  attacked pieces
. . . . . . . .     1 . . 1 1 . . 1     . . . . . . . .
. . . 1 . . 1 .     1 . 1 1 1 1 1 .     . . . 1 . . 1 .
. 1 . 1 . 1 . .     . 1 . . . . . 1     . 1 . . . . . .
. . 1 1 1 . . .     . . . . . . . .     . . . . . . . .
1 1 1 * 1 1 1 .  &  . . . * . . 1 .  =  . . . * . . 1 .
. . 1 1 1 . . .     . . . . . . . .     . . . . . . . .
. . . 1 . 1 . .     . . . . . . . .     . . . . . . . .
. . . 1 . . . .     . . . . . . . .     . . . . . . . .
Number of attacked pieces can be calculated through bitwise and of two boards! On top of that, this operation only takes 1 CPU cycle to complete! I can imagine the chess engine will perform calculations just like this thousand times over and over again in search. No wonder bitboard is a popular representation among chess engines.

Friday, December 6, 2013

Programming Checkmate, how it begins...

On a late winter night, I have decided to watch some YouTube videos on chess for no reason.  After watching a few hours of chess video, I have decided to pick up chess as my new hobby.   I've tried to be good at many kinds of chess game before, such as Go and Chinese chess, but I've never mastered any of them.

One of my former colleagues suggested me to take a look at chess.com and play some chess over there.   Chess.com has a lot of resources and tutorial to help beginners like to me to master this game.  I really liked their tutorials and exercises and I would spend at least half an hour a day working on their tactics problems.

I thought I would become reasonably well at chess after a few days of practice,  but I was wrong.  I get pwned by computer chess engines and other players all the time.  As a programmer, the idea of writing a program to play chess and solve all the tactics problems I was working came to my mind.  After a some research, I found out that programming a chess engine isn't that hard. 

I've decided to start writing a chess engine, so I can practice programming, be a better chess player and learn some AI.  This blog shall document this project and my other side projects.

Some milestones for my chess engine:
1) Write an engine that can play chess.  Doesn't have to be smart, but just need to be able to generate some moves that don't violate the rules.

2) Improve the engine so that it can beat me in chess.

3) ??? => Profit?