lichess.org
Donate

How hard would it be to create a chess playing program?

Its an interesting challengue, I think I would be able to do that,
the text logical part but not the gui, interface.
It will be more about, It will take some time you will have to look for special cases, double pin, stalemate, fifty rule, three check,
pawns move one or twice promoting,
in its core its simple its like a matrix I believe but you will have to define each piece with each movement and making sure you wont reach a contradiction or a paradoxical statement, so It would be complex maybe not difficult but complex at least for a newbie in programming like me,
as someone said the difficult part would be to make the code clean effective fast readable.
But I encourage you to do it if You have enough free time It will be a rewarding experience.
I think the principles of linked lists can be useful there. Think of files and diagonals of linked lists of squares (linked in the right direction, depending on the case). A rook just walks a file (a linked list) until it hits something else or the end of the list. A bishop does the same.

en.wikipedia.org/wiki/Linked_list
What worked for me in practice in move generation is that first I generate a table that contains all the pseudo legal moves of all pieces starting from all squares on the board. So you index this table with the rank and file of the square and the piece code and you get a list of possible moves for a piece standing on this square.

final static int move_table_size=20000;
static MoveDescriptor move_table[]=new MoveDescriptor[move_table_size];
static int move_table_ptr[][][]=new int[8][8][64];

Piece codes run from 0 to 63, because they are constructed of bits that speak for themselves:

This definition of a piece proved to be very fruitful for me:

public final static int SLIDING=32;
public final static int STRAIGHT=16;
public final static int DIAGONAL=8;
public final static int SINGLE=4;
public final static int IS_PAWN=2;

public final static int QUEEN=SLIDING|STRAIGHT|DIAGONAL;
public final static int ROOK=SLIDING|STRAIGHT;
public final static int BISHOP=SLIDING|DIAGONAL;

public final static int KNIGHT=SINGLE;
public final static int KING=SINGLE|STRAIGHT|DIAGONAL;
public final static int PAWN=SINGLE|IS_PAWN;

Bit-0 is used for color.

The pseudo legal moves are stored in structures which I call move descriptors. These contain the target square and in the case of sliding pieces the start of the next direction in which the piece can go. So if a rook hits an own piece or captures an opponent piece during move generation in some direction, of course further squares in this direction are illegal, so the move descriptor tells the program where the next direction begins in the table.
I would recommend first to make a pseudochess program,
like only pawns with king vs pawns with king
and once you get around the sintax of the language and how you will approach the board, how will you store the board what type of variables you will use, integer boolean, so to refresh your mind a little bit like a warm up and then you build from there and start putting special cases, more pieces.
Unless you are a programmer already and have some confidence in your programming abilities, its better to make an easy task and then a bigger one.
If you start from scratch you need like a sketch, I think programmers write with pen or paper before writing the code
and Its called the pseudo code, It will save you time and will give you a general idea of how to continue, instead of compiling a small piece of code and losing the general scheme of things.
Hope it helps, programming its beautiful by itseld and if you accompany with chess, It will be even more.
The GUI part is the easy one. There is nothing conceptually difficult in writing a GUI, it is just time consuming, you have to learn how all the widgets work in the given language. But getting the move generation right is a headache. Very ugly bugs can raise their head in this part of the program. For example almost all pseudo legal moves of pieces are capable of giving a check. Pawns of course cannot give check forward, but one ugly thing is that it is easy to forget that castling moves for a king also cannot give check. So a king on e1 cannot check g1 albeit it is legal for it to move to this square. These kind of little things make life difficult in move generation.
For the GUI part this is a the way to go for a beginner:

Copy paste simple example GUI programs into the IDE and try to play around with them. Add a button, try this button to do someting, add some text, try to set this text, try to get this text etc. Step by step you get a feeling how to do it. Once you understand how the building blocks work, you can put together the whole complicated structure by repeating these simple steps. I never made an effort to learn the GUI from the docs, rather learned it by example. The resulting code may be not too elegant but it is good enough for my purposes.
Pretty cool timing for this question. I'm programming a Blind Chess programme based only on text. So the user would have only a command prompt and must type the moves. If it works, I'll probably add tactics and endgame puzzles. Still a bit long way off and will be taking a break next week to be a camp adviser. When I get it to work, I'll come back this thread. Good luck.
The funny thing, its like playing chess for yourself. The Rules are easy but the teaching is horrible. But good luck, it is not a wonder, it just needs your will.

This topic has been archived and can no longer be replied to.