Here's the case. I'm trying to make a Chess engine. Not that I believe that I can make anything better than what's already out there, but I want to see how it works. I've made some of the important parts, such as board representation and the evaluation function. The evaluation function seems to produce decent results, so now I need to implement the search (minimax?) function. I won't be doing any kind of transposition tables, book openings etc. yet, but I want to do a full-width search for a reasonable depth (say 4 plies). How do I do that?
The important functions and types that are available to you, my dear Stack Overflow-helper, are:
type
TMoves = record
Move : integer; //Bit-encoded,6 LSB=To,6 next=From,3 next=Promote to
Score : integer;
end;
TMoveList = class
//Regular list with an iterator - for TMove in TMovelist do ... etc
end;
TChessBoard = record
<snip>
procedure SetInitialPosition;
procedure GenerateLegalMoves(aList : TMoveList);
procedure DoMove(aMove : TChessMove); //Doesn't use the score-part, obviously
property Turn : TChessColor; //ccWhite or ccBlack
function WhiteInCheck : boolean;
function BlackInCheck : boolean;
end;
//Evaluate Returns >0 for white advantage, <0 for black advantage
function Evaluate(aBoard : TChessBoard) : integer;
Now I want to implement this function
function BestMove(aBoard : TChessBoard; aMaxDepth : integer) : TMove;
and probably a helper-function or two (preferably just one)
I've read about minimax on Wikipedia, but I just don't understand it. It's not that it's recursive, I get that, but there's something mystical going on that I don't get. And since I don't understand it, I turn to Stack Overflow :-) Could someone please explain this function for me? I want it to be very simple, and no "smarty stuff". No optimizations, transposition tables, pruning etc, just a function to evaluate every possible move up to given depth (and, yes, I know there will be many nodes for depths above about 4 plies)
Thanks in advance. Whoever helps me will receive a fair share of the sales of the finished game. We're potentially talking billions of dollars here ;-)