Chess programmer. Where do I start

Sort:
Wilbert_78

Well I agree and don't agree with what the guys above here said. Follow the link I've posted earlier. While I agree with Wafflemaster, that you're not going to create an engine right away, I do see a great opportunity for you to start learning code with a chessgame.

How?

Divide it up. Forget the engine for now.
Start with a board. Learn how to get that board on the screen.
After that, implement an overlay with the pieces.
Implement moving pieces without rules (mouse input etc)
Implement the basic chess rules.
Implement castling, en passant and promoting.
Implement moving the pieces within the rules of chess.
Now you have made an analyze board.

Now implement the basic chessprotocols (UCI comes to mind) (See earlier mentioned link).

And when you're done with that, your chessboard can talk to existing engines!

By this time you will have become quite comfortable with the language of your choice. And really, don't care for what people recommond. Python, C, Java or if you wish (and I would not encourage it) qbasic would do.

Personally I would recommend either, python or a C based language, but something like C# (mono) would do just fine. Your engine isn't competing to be the best at this point. It's about learning how to code... once you've learned 1 language it gets a lot easier to pick up another, because you've already learned how a functional program is set up. The language in which you do that is yours to pick. I would even go as far as to say, don't do it in pure C or C++ if this is your first program. You'll spend more time on memory handling than on your actual program. Python would really be a nice choice. It's a bit weird language, but you can actually read it 10 years later and still understand it and it implements great best practice coding ideas.

So basicly, start with the gui, get to know your language of choice and general coding ideas... and then if you still want to, implement your own engine!

darkschyte

you could use .NET with c# or c++, u should make a MVC (model view-controller) split in differents modules (objects) the graphics (2 or 3D) the inference engine, the netcode and the main loop

 

C# is quite easy and it suses delegates (pointer references) and is quite easy to implement EAP (event-based async programming) wich is useful for the netcode :) using callbacks, a chess UI no so hard using WPF or even win forms. 

With C++ u could download de DX SDK for free on microsoft page and it's a bit different to use than other graphics library but the results are way nicer :)

pelly13

I wrote a program in Delphi 3. It's a oop-pascal language. My engine does 10kN/sec on my 2GHz laptop. Compare this with Fritz doing 2M N/sec. I will need ASM (assembly) to speed it up further.

I read a book (1989) by v.d.Herik : Chess for computers. ISBN : 9062332714

Pre_VizsIa

@wafflemaster - yes, a chess engine wuld be a vrey difficult first project.

@Wilbert - Graphical User Interfaces (GUIs) are not that easy to write.

@pelly - assembly language is a royal pain to program in, as you probably know.

Wilbert_78

Timothy, well, that depends on the language. C# / mono would be an excellent starting point for a new programmer into gui programming. I still agree with you, when new to programming, this is not an easy task. But to give him advice to printf hello world is a bit outdated in this day and age as far as I'm concerned. Hence I did the little break up of how a chessgame works.

pelly13

@Timothy: Yes , assembly is horrible. I want to speed-up some routines by using inline-asm. These are the move-generator , evaluation function and quiescene function. Delphi allows you to write small routines ( or inline) in ASM , but still they are very hairy.

wbport

This is how Robert Hyatt of Cray Blitz fame did it (at one time):

http://archive.computerhistory.org/projects/chess/related_materials/text/3-2%20and%203-3.Cray_Channels_Vol-3_No-1.Checkmate_The_Cray-1_Plays_Chess.Hyatt.1980/Cray_Channels_Vol-3_No-1.Checkmate_The_Cray-1_Plays_Chess.Hyatt.1980.062303023.sm.pdf

temetvince

Avoid gui like the plague. A command line program will work just fine. You can always add a gui later.

I'd recommend not even programming to start with. Write your "program" in psuodocode. Just use English to follow the logic. Alan Turing did this and wrote a chess program that couldn't be tested because no computers existed that were fast enough to run it. You can download it for free. If it comes with source code that might be a fun thing to learn from.

As for beginner languages, python for something that will stick with you or processing for something that has basic graphics and can help transition to java. C++ is my tool of choice and what I learned with, but it was taught through a college course. Don't worry about speed. You can always port your code later to a faster language.

pelly13

Pseudo code :

function Analyse(Depth,Alfa,Beta):integer;

if Checkmate then

    Result:=-INFINITY+Depth;

else

if Stalemate then

    Result:=0;

else

if Depth=0 then

   Result:=Evaluate;  { Give the position a value }

else

begin

   Best:=-INFINITY;

   GenMoves(List);

   i:=1;

   while (i<=lengthOf(List)) do

   begin

           Move=List[i];

           Play(Move);

           Move.Value:=-Analyse(Depth-1,-Beta,-Alfa); { Search 1 ply deeper }

          UnPlay(Move);

          if (Move.Value>Best) then

          begin

                 Best:=Move.Value;

                 if (Best>Alfa) then

                 begin

                         Alfa:=Best;

                         if (Best>=Beta) then

                         begin

                                 i:=SizeOfList(List);  { stop analyzing}

                         end;

                 end;

           end;

           Inc(i);       { Next index in List }                  

   end;

   Result:=Best;

end;

ifoody

The engines you know are working all on almost the same program, the only difference is how strong and fast is the engine\computer, and what is its depth of moves - how far can it calculate.

watcha

There used to be a simple, well commented and easy to understand open source Winboard compatible bitboard engine on the net ( called 'wingletx' ) with step to step guide and explanation on how it is built up and how it works ( since then it was unfortunately removed, at least I can't find it now ). It could be compiled with the free version of Microsoft Visual C++. I have compiled it and most of what I know about the working of engines comes from there.

I still have the source and docs if anyone is interested.

Derekjj
Wilbert_78 wrote:

Timothy, well, that depends on the language. C# / mono would be an excellent starting point for a new programmer into gui programming. I still agree with you, when new to programming, this is not an easy task. But to give him advice to printf hello world is a bit outdated in this day and age as far as I'm concerned. Hence I did the little break up of how a chessgame works.

I doubt the OP would know what a printf command is.

Derekjj
pelly13 wrote:

Pseudo code :

function Analyse(Depth,Alfa,Beta):integer;

if Checkmate then

    Result:=-INFINITY+Depth;

else

if Stalemate then

    Result:=0;

else

if Depth=0 then

   Result:=Evaluate;  { Give the position a value }

else

begin

   Best:=-INFINITY;

   GenMoves(List);

   i:=1;

   while (i<=lengthOf(List)) do

   begin

           Move=List[i];

           Play(Move);

           Move.Value:=-Analyse(Depth-1,-Beta,-Alfa); { Search 1 ply deeper }

          UnPlay(Move);

          if (Move.Value>Best) then

          begin

                 Best:=Move.Value;

                 if (Best>Alfa) then

                 begin

                         Alfa:=Best;

                         if (Best>=Beta) then

                         begin

                                 i:=SizeOfList(List);  { stop analyzing}

                         end;

                 end;

           end;

           Inc(i);       { Next index in List }                  

   end;

   Result:=Best;

end;

Just let the OP figure it out on his own, that way he can learn.

watcha


Cavatine

In "The Seven Habits of Highly Effective People", the first habit is "Begin with the end in mind". What is your vision for the outcome of your chess-program-writing endeavor? Envision yourself successful and joyous, and then think about what steps you can take to get you there. https://www.stephencovey.com/7habits/7habits.php

temetvince

Cavatine, while you have very sound advice, I actually took it that the OP had the end in mind when the post was created, but lacked the knowledge to know what steps needed to be taken.

That's why I recommended avoiding guis and using python or processing (probably python), or even better pseudocode. Anything else at this point is probably too advanced. 

The main thing a college course will help with is not just learning a language (which is actually fairly easy), but learning how to program.

Pseudocode can be very simple. Think of it like an actual game of chess.

Set up board

White makes move

Black makes move

Repeat

Game ends

 

Now, you can reorganize and expand your ideas as you learn techniques, of which there are many different ways to do it.

Set up board

    Use 2d array, 8x8 (an example programming technique, no need to worry if you don't understand it. Just shows how even pseudocode can grow as you gain knowledge)

    fill array with pieces

Computer move process

    is game over?

    No? Then am I in check?

    No? Then (insert whatever you think the program should think about)

White move

    Is the human white? 

    No? Okay, do the computer move process part

 

etc etc. Very simple. It doesn't matter what language you use. Some are better than others for speed, etc. But the process is definitely doable without actually coding. If you just jump in and start coding, you'll learn a ton about coding, but I doubt you'll learn a lot about making a chess program. The experience of learning coding will get in the way of your goals, at least at this point.

 

Hope that helps! Also watcha, I'm quite interested in the source code for that dandy little program!

Wilbert_78

Edit: I was misinformed.

temetvince
Wilbert_78 wrote:

Actually, most chess-engines have 1 array per sort of piece (knight, queen, pawns etc) per color. But the idea of using an 8x8 2d array is good. In practice it's just easier to use multiple arrays.

What do they store in the arrays per piece?

Wilbert_78
[COMMENT DELETED]
Wilbert_78

I take that back... I seem to have been informed wrong. These days they tend to use bigger arrays and other systems. More info here:

http://www.top-5000.nl/ps/SomeAspectsOfChessProgramming.pdf

And here:

http://en.wikipedia.org/wiki/Board_representation_%28chess%29