Hi Sven. Thanks for the explanations. As I said, I'm new to C++ and I'm learning (and enjoying the learning) as I write the program.Sven Schüle wrote: Hi José,
some points:
1) I hope your InsertPawnCapture() method also covers promotions with capture, otherwise you'd better insert that part into InsertPawnMoves().
2) The assert() for tColor to be either white or black should be moved to the very beginning of the function. C++, as opposed to C, does not require all declarations to appear before the first statement, so you can start with an assert() before accessing the "assert-ed" parameters in the initializer expression of a variable declaration.
3) If you use constants like tAdvanceOne, tRowPromo etc. at several places in the move generator, or even in the whole program, you might consider to provide static inline template functions (maybe in some Board class) returning the appropriate color-dependent value based on accessing an (also static) array of two elements, as in this example:
There is a tiny overhead of typing few more characters but you save repeating all the same code to define and initialize those constants in each function that uses them. The compiler will replace the function call with the correct array element without any overhead since the array index is known at compile time and the array is static, i.e. not part of any object instance and has therefore a fixed address.Code: Select all
// header file: declaration class CBoard { public: static template <CColor::TColores tColor> advanceOne(); private: static CSquare::TDirections tAdvanceOne[2]; }; // static inline template <CColor::TColores tColor> CBoard::advanceOne() { return tAdvanceOne[tColor]; } // cpp file: definition of static variables // static CSquare::TDirections tAdvanceOne[2] = { CSquare::UP, CSquare::DOWN }; // used somewhere: cTo = cFrom + CBoard::advanceOne<tColor>();
As I said, my proposal is not suitable if this is the only place where you use those constants.
Sven
InsertPawnCapture covers promotions, yes, but I've found another error while reading the code I posted: I increment the pointer (pMove), but InsertPawnCapture could find there's no opponent piece in the To square, and then it won't insert the move, so the pointer should not be incremented.
I still don't have enough code to run the program and test it, so I just type and type, hoping it'll work someday...

Ok, I'll move the assert to the top. I think I'll have a hard time to get used to declarations not in the first lines, but I'll survive.
Right now, those constants are used only there. Actually, I had written the function splitting white and black; then I saw templates in Stockfish and I thought they were worth a try. I figured out those constants (I didn't declare them as const until Marco said it) as the way to avoid splitted code. I'm not sure if I'll need them somewhere else in the code, but I'll do what you propose anyway because it'll help me understanding templates better.
Thanks again.