Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: CPP related question on double code (B/W)

Author: Russell Reagan

Date: 09:29:11 11/24/03

Go up one level in this thread


On November 24, 2003 at 06:01:30, Reinhard Scharnagl wrote:

>Hello,
>
>I want to use the same source file (using different macros)
>twice to compile a move generator for the black and white
>side. How could this be done CPP _conform_?
>
>This attempt is for to avoid double (redundant) source code
>and its change management synchronizing.
>
>I hope that there are some experts with matching experiences
>on that.
>
>Regards, Reinhard.

I'm not sure if this is what you are trying to accomplish, but this is an
example that someone posted to another message board recently.

enum Color{White,Black};

template <Color c>
struct ColorTraits;

template <>
struct ColorTraits<White>{
    static const Color Enemy = Black;
    static u64 forward(u64 x){ return x<<8; }
    static u64 backwards(u64 x){ return x>>8; }
    static const Piece Pawn = WhitePawn;
    static const Piece Night = WhiteNight;
    //...
};

template <>
struct ColorTraits<Black>{
    static const Color Enemy = White;
    static u64 forward(u64 x){ return x>>8; }
    static u64 back(u64 x){ return x<<8; }
    static const Piece Pawn = BlackPawn;
    static const Piece Knight = BlackKnight;
    //...
};

template <Color c>
u64 passed_pawns(){
    u64 obstacles = bitboard[ColorTraits<ColorTraits<c>::Enemy>::Pawn];
    obstacles = ColorTraits<c>::back(obstacles);
    obstacles |= Left(obstacles) | Right(obstacles); // Left and Right have
obvious definitions
    obstacles |= ColorTraits<c>::back(obstacles);
    obstacles |= ColorTraits<c>::back( ColorTraits<c>::back(obstacles) );
    obstacles |= ColorTraits<c>::back( ColorTraits<c>::back(
ColorTraits<c>::back( ColorTraits<c>::back(obstacles) ) ) );
    return bitboard[ColorTraits<c>::Pawn] & ~obstacles;
}

Then you can call passed_pawns<Black>() or passed_pawns<White>() and you only
have to write one function.



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.