Programming chess engine

Code, algorithms, languages, construction...
spacerocket
Posts: 18
Joined: Sat Mar 22, 2014 1:15 pm

Programming chess engine

Post by spacerocket » Sat Mar 22, 2014 1:23 pm

Hello!

I use opensuse 13.1 and now I`m working with the c-language. What would be the best Linux distro for programming? I want to program own chess engine, I think I need to write a board representation. Is bitboard related to this somehow? I also need GUI I have xboard 4.7.2 but don`t know how to use it with programming. If someone would advise me how to get started but be great, thx.

User923005
Posts: 616
Joined: Thu May 19, 2011 1:35 am

Re: Programming chess engine

Post by User923005 » Sat Mar 22, 2014 1:54 pm

I don't think that your linux flavor matters very much. I like Redhat, but that is just what I am most used to.
To learn how to write a chess program the chess programming wiki is a very good place to start.
http://chessprogramming.wikispaces.com/Programming
You will need to choose a protocol to communicate between your chess program and the GUI.
The choices are Winboard (usually called Xboard in the Unix environment) or UCI.

Here is the Winboard protocol:
http://chessprogramming.wikispaces.com/ ... n+Protocol
http://en.wikipedia.org/wiki/XBoard

Here is the UCI protocol:
https://chessprogramming.wikispaces.com/UCI
http://wbec-ridderkerk.nl/html/UCIProtocol.html

It won't matter much which one you choose.

To make an engine, you will also need to decide on a board representation.
An array is probably the simplest representation to visualize, but I think that the bitboard representation is probably more efficient considering the proliferation of 64 bit machines today.

Have fun.
Don't worry that your program is not very strong when you start out. That will come with time.

spacerocket
Posts: 18
Joined: Sat Mar 22, 2014 1:15 pm

Re: Programming chess engine

Post by spacerocket » Sat Mar 22, 2014 3:41 pm

Ok thanks

Do I need to download the protocol from somewhere? I didn`t see any link. If so, how do I configure xboard to use it? How do I get bitboard, or do I need to build it myself? I think coding is somehow related to this issue, I have text editor(gedit) and I know how to use terminal window.

User923005
Posts: 616
Joined: Thu May 19, 2011 1:35 am

Re: Programming chess engine

Post by User923005 » Mon Mar 24, 2014 10:01 am

The links contain the address to each protocol.
You do not have to configure xboard to use its own protocol.
There are many different bitboard formats.
Yes, coding is related to the issue of writing a chess program.
If you go to the chess Wiki and start reading, your questions will be answered.
BTW, I feel pretty sure that I have been trolled now.

HumbleProgrammer
Posts: 40
Joined: Sat Jun 19, 2010 11:00 pm
Real Name: Lee Neuse

Re: Programming chess engine

Post by HumbleProgrammer » Mon Mar 24, 2014 8:59 pm

You might also look at some sample chess engines that other's have published. I would start with Tom Kerrigan's Simple Chess Program aka TSCP (http://www.tckerrigan.com/Chess/TSCP); it is written in C, and was designed for clarity and is thoroughly commented. Once you get to the point where you can (1) compile it, (2) make it work, and even (3) make a useful improvement to it, you will be well on your way. Until then, the type of help you are asking for is easily gained by reading and learning, neither of which any of us can do for you.

Cheers!
Humble Programmer
,,,^..^,,,

spacerocket
Posts: 18
Joined: Sat Mar 22, 2014 1:15 pm

Re: Programming chess engine

Post by spacerocket » Sat Apr 12, 2014 8:32 pm

Hello!

I downloaded the source code of the engine: there is defs.h file:

/*
* DEFS.H
* Tom Kerrigan's Simple Chess Program (TSCP)
*
* Copyright 1997 Tom Kerrigan
*/


#define BOOL int
#define TRUE 1
#define FALSE 0

#define GEN_STACK 1120
#define MAX_PLY 32
#define HIST_STACK 400

#define LIGHT 0
#define DARK 1

#define PAWN 0
#define KNIGHT 1
#define BISHOP 2
#define ROOK 3
#define QUEEN 4
#define KING 5

#define EMPTY 6

/* useful squares */
#define A1 56
#define B1 57
#define C1 58
#define D1 59
#define E1 60
#define F1 61
#define G1 62
#define H1 63
#define A8 0
#define B8 1
#define C8 2
#define D8 3
#define E8 4
#define F8 5
#define G8 6
#define H8 7

#define ROW(x) (x >> 3)
#define COL(x) (x & 7)


/* This is the basic description of a move. promote is what
piece to promote the pawn to, if the move is a pawn
promotion. bits is a bitfield that describes the move,
with the following bits:

1 capture
2 castle
4 en passant capture
8 pushing a pawn 2 squares
16 pawn move
32 promote

It's union'ed with an integer so two moves can easily
be compared with each other. */

typedef struct {
char from;
char to;
char promote;
char bits;
} move_bytes;

typedef union {
move_bytes b;
int u;
} move;

/* an element of the move stack. it's just a move with a
score, so it can be sorted by the search functions. */
typedef struct {
move m;
int score;
} gen_t;

/* an element of the history stack, with the information
necessary to take a move back. */
typedef struct {
move m;
int capture;
int castle;
int ep;
int fifty;
int hash;
} hist_t;


And data.h :

/*
* DATA.H
* Tom Kerrigan's Simple Chess Program (TSCP)
*
* Copyright 1997 Tom Kerrigan
*/


/* this is basically a copy of data.c that's included by most
of the source files so they can use the data.c variables */

extern int color[64];
extern int piece[64];
extern int side;
extern int xside;
extern int castle;
extern int ep;
extern int fifty;
extern int hash;
extern int ply;
extern int hply;
extern gen_t gen_dat[GEN_STACK];
extern int first_move[MAX_PLY];
extern int history[64][64];
extern hist_t hist_dat[HIST_STACK];
extern int max_time;
extern int max_depth;
extern int start_time;
extern int stop_time;
extern int nodes;
extern move pv[MAX_PLY][MAX_PLY];
extern int pv_length[MAX_PLY];
extern BOOL follow_pv;
extern int hash_piece[2][6][64];
extern int hash_side;
extern int hash_ep[64];
extern int mailbox[120];
extern int mailbox64[64];
extern BOOL slide[6];
extern int offsets[6];
extern int offset[6][8];
extern int castle_mask[64];
extern char piece_char[6];
extern int init_color[64];
extern int init_piece[64];

Where is the board representation? the very first step of writing chess engine. Anyway I don`t fully understand all those symbols if someone would be kind enough to explain.

Please don`t blame me not knowing C++ I already wrote some simple programs and now working on buying a book about C++

lucasart
Posts: 201
Joined: Mon Dec 17, 2012 1:09 pm
Contact:

Re: Programming chess engine

Post by lucasart » Sun Apr 13, 2014 12:58 am

Look, we are not going to write the program for you. If you do not have the programming skills required (as is obviously the case), I suggest you set yourself another goal. Maybe you should program a tic-tac-toe instead.
"Talk is cheap. Show me the code." -- Linus Torvalds.

spacerocket
Posts: 18
Joined: Sat Mar 22, 2014 1:15 pm

Re: Programming chess engine

Post by spacerocket » Sun Apr 13, 2014 9:58 am

OK I agree tic-tac toe seems more suitable project than a chess engine.

Do you understand this code? :

#include <iostream>
using namespace std;

char square[10] = {'o','1','2','3','4','5','6','7','8','9'};

int checkwin();
void board();

int main()
{
int player = 1,i,choice;

char mark;
do
{
board();
player=(player%2)?1:2;

cout << "Player " << player << ", enter a number: ";
cin >> choice;

mark=(player == 1) ? 'X' : 'O';

if (choice == 1 && square[1] == '1')

square[1] = mark;
else if (choice == 2 && square[2] == '2')

square[2] = mark;
else if (choice == 3 && square[3] == '3')

square[3] = mark;
else if (choice == 4 && square[4] == '4')

square[4] = mark;
else if (choice == 5 && square[5] == '5')

square[5] = mark;
else if (choice == 6 && square[6] == '6')

square[6] = mark;
else if (choice == 7 && square[7] == '7')

square[7] = mark;
else if (choice == 8 && square[8] == '8')

square[8] = mark;
else if (choice == 9 && square[9] == '9')

square[9] = mark;
else
{
cout<<"Invalid move ";

player--;
cin.ignore();
cin.get();
}
i=checkwin();

player++;
}while(i==-1);
board();
if(i==1)

cout<<"==>\aPlayer "<<--player<<" win ";
else
cout<<"==>\aGame draw";

cin.ignore();
cin.get();
return 0;
}

lucasart
Posts: 201
Joined: Mon Dec 17, 2012 1:09 pm
Contact:

Re: Programming chess engine

Post by lucasart » Mon Apr 14, 2014 4:33 am

If you are serious about learning C++ programming, I suggest:
  • Stop wasting your time looking at other people's code that you do not understand. Write your own programs. Really simple ones to start with. Besides, the above code is total crap and you should not learn from such crappy code. I'm not gonna waste my time explaining to you what each line of the above (crappy) code does, it's pointless.
  • Get a book that will teach you step by step, where each chapter ends with exercises. You need to do the excercises, otherwise you are not going to learn anything. The only way to understand is to do it yourself. No one can teach you better than yoursself. I will gladly answer questions, once you show me that you have done some research and you have a precise programming question. But I will not do your homework for you.
You can download Bjarn Stroustrup's book "The C++ Programming Language" for free (3rd edition). Just Google it. The 4th edition with C++11 you have to buy though. It's worth it.

Again, I repeat, you have to be serious about this, otherwise you are wasting your time. Learning C++ is not easy. You don't seem to grasp just how hard it is. Perhaps you should learn a modern high level interpreted language instead, like Python. A better choice for a first language than C++ (or C).

PS: The above code has 0 chances to work. To begin with, these two functions are declared and not defined!

Code: Select all

int checkwin();
void board();
"Talk is cheap. Show me the code." -- Linus Torvalds.

Octopus

Re: Programming chess engine

Post by Octopus » Mon Apr 14, 2014 9:00 am

There are some thoughts to be said: to start chess engine programming it is first important to understand programming. If you don't have relevant experiences in that, it might be helpful not to start witch language C or C++. So put the idea aside, to write a top performing chess engine at your first approach. You should primarily start gathering experiences in (game) programming using a simpler language. I recommend to try Xojo, which is a nice multi OS BASIC related development environment, which is free as long you use it for yourself, sufficient for learning purposes. Additionally the IDE supports object oriented language extensions, enabling multiple experiences also into that direction.

See at: http://www.xojo.com/index.php where you also could find a lot of free documentation and demos.

Post Reply