Minimalist UCI chess engine written by self learner from scratch

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
maksimKorzh
Posts: 771
Joined: Sat Sep 08, 2018 5:37 pm
Location: Ukraine
Full name: Maksim Korzh

Re: Minimalist UCI chess engine written by self learner from scratch

Post by maksimKorzh »

Thanks, Marco! Everything is clear now :D Now I need to dive into the work :D
elcabesa
Posts: 855
Joined: Sun May 23, 2010 1:32 pm

Re: Minimalist UCI chess engine written by self learner from scratch

Post by elcabesa »

good work, and good luck
User avatar
maksimKorzh
Posts: 771
Joined: Sat Sep 08, 2018 5:37 pm
Location: Ukraine
Full name: Maksim Korzh

Re: Minimalist UCI chess engine written by self learner from scratch

Post by maksimKorzh »

I'm stuck with collecting mating scored moves, please help

Code: Select all

static inline int NegaMaxSearch(int alpha, int beta, CHESSBOARD *board, SEARCH *info, int depth)
{
	int bestScore = 0;
	int bestMove = 0;
	int legalMoves = 0;
	
	info->nodes++;

	if(InCheck(board, side))
		depth++;

	if(!depth)
		return QuiescenceSearch(alpha, beta, board, info);
		
	MOVELIST list[1];
	GenerateMoves(board, list);
	
	for(int moveNum = 0; moveNum < list->moveCount; ++moveNum)
	{
		CHESSBOARD boardStored[1];
		boardStored[0] = board[0];
		
		if(!MakeMove(board, list->moves[moveNum].move, all))
			continue;
		
		legalMoves++;
		int score = -NegaMaxSearch(-beta, -alpha, board, info, depth - 1);
		TakeBack(board, boardStored);
		
		if(score >= beta)
			return beta; // fail hard beta-cutoff, mating score 100000 is cut here
			
		if(score > alpha)
		{
			alpha = score;
			
			bestScore = alpha;
			bestMove = list->moves[moveNum].move;
			
			info->bestScore = bestScore;			
			info->bestMove = bestMove;
		}
	}
		
	if(!legalMoves)
	{
		if(InCheck(board, side))
			return -100000; // on checkmate
			
		else
			return 0; // on stalemate
	}
	
	else if(legalMoves)
	{
		info->bestScore = bestScore;			
		info->bestMove = bestMove;
	}
	
	return alpha;
}
if there are legal moves everything works well, but when returning -100000 on mate - on depth 1 it returns the right move, but increasing the depth leads to returning any move but mating one. I have no idea why plese help. Thanks in advance.
User avatar
WinPooh
Posts: 267
Joined: Fri Mar 17, 2006 8:01 am
Location: Russia
Full name: Vladimir Medvedev

Re: Minimalist UCI chess engine written by self learner from scratch

Post by WinPooh »

> static inline int QuiescenceSearch

Since this function recursively calls itself, I doubt it can be inlined.
elcabesa
Posts: 855
Joined: Sun May 23, 2010 1:32 pm

Re: Minimalist UCI chess engine written by self learner from scratch

Post by elcabesa »

Maybe I'm wrong but in you don't handle mate in quiescence search and also when in check in quiescent search you only test capture move... what if the only option is to escape instead of capturing?
Usually a quiescence search when in check shall test all the moves, not only captures.
User avatar
hgm
Posts: 27789
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Minimalist UCI chess engine written by self learner from scratch

Post by hgm »

It depends on how you score the position. If you return (at least) the static evaluation even when in check, this is only slightly wrong. In effect you then make the assumption that one of the non-captures in general will be able to solve the problem (i.e. evade the check). This is the common case, as the overwhelming majority of checks aren't checkmates. Just like when your Queen is attacked you assume that you will be able to rescue it if you have the move.

The catch is that for Queens can escape threats much easier than Kings, as they are so much more mobile. Weak pieces don't have that advantage to the King, but threats on those are much rarer, as they are usually protected, and even if not, can be easily protected. Which would deter most attackers. The King is unique in that it is very valuable despite its low mobility, so the stand-pat assumption fails more often for Kings than for any other piece. This is why it deserves special treatment.

But the benefits are not huge, as this special treatment can be quite expensive. You have to detect check in QS, and you have to extend when that happens.

What would be wrong is to return -INF when in check and none of the captures solve it. Then the engine would imagine all kind of mates at the horizon tjat in reality are almost never there.
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Minimalist UCI chess engine written by self learner from scratch

Post by Sven »

elcabesa wrote: Fri Sep 14, 2018 11:02 am Maybe I'm wrong but in you don't handle mate in quiescence search and also when in check in quiescent search you only test capture move... what if the only option is to escape instead of capturing?
Usually a quiescence search when in check shall test all the moves, not only captures.
When using check extension you never run into that problem at all since you never even call quiescence search when being in check. But even without check extension you can write your quiescence search roughly like this:

Code: Select all

int qsearch(int alpha, int beta)
{
    if (isInCheck()) return fullSearch(alpha, beta, 1);
    ...
which is only a small overhead (for a non-world-class engine at least).

My engine Jumbo already detects being in check at the parent node by detecting moves that give check before making them on the board, and calls the appropriate search function depending on that information.
Sven Schüle (engine author: Jumbo, KnockOut, Surprise)
User avatar
cdani
Posts: 2204
Joined: Sat Jan 18, 2014 10:24 am
Location: Andorra

Re: Minimalist UCI chess engine written by self learner from scratch

Post by cdani »

Sven wrote: Fri Sep 14, 2018 7:46 pm When using check extension you never run into that problem at all since you never even call quiescence search when being in check.
A few months ago Andscacs didn't went to quiesce in check. I changed it and was a slight improvement. I search all the evasions, so is like normal search but simplified.
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Minimalist UCI chess engine written by self learner from scratch

Post by Sven »

cdani wrote: Fri Sep 14, 2018 10:29 pm
Sven wrote: Fri Sep 14, 2018 7:46 pm When using check extension you never run into that problem at all since you never even call quiescence search when being in check.
A few months ago Andscacs didn't went to quiesce in check. I changed it and was a slight improvement. I search all the evasions, so is like normal search but simplified.
Recently I implemented an evasion generator for Jumbo as well, very small improvement here too.
Sven Schüle (engine author: Jumbo, KnockOut, Surprise)