Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: making code color independant

Author: Steffen Jakob

Date: 10:01:23 12/08/03

Go up one level in this thread


On December 08, 2003 at 12:28:55, Georg v. Zimmermann wrote:

>This has been recently discussed, but on a very sophisticated level, while I am
>looking for a simple solution.
>
>Right now I just have 2 makeMove() , 2 genAllMove(), 2 genCaptures() etc etc
>
>In there are statements like
>
>if (to >> 4 == 7) // is it a promotion ?
>{...}
>
>To make the code color independent, my first idea was to have
>
>if (to >> 4 == lastRank[onMove])
>{..}
>
>and have such similar arrays for colorMask[2], fifthRank[2], e1Square[2],
>pawnDirection[2] etc.
>
>First question: how slow is this ? Is it comparable slow as having
>
>if (onMove)
>  if (to >> 4 == 7)
>  {...}
>else
>  if (to >> 4 == 0)
>  {...}
>
>which is horrible ?
>
>
>Then my next idea was to have a structure where I put all the relevant info in.
>struct colorDep
>{
>  int lastRank;
>  [...]
>};
>
>and then fill 2 such structures with the correct info for white and black. And
>have a pointer which always points to the correct one. Then I could do
>
>if (to >> 4 == cD->lastRank)
>{..}
>
>Is this faster as the array solution, as I think it is ?
>
>
>Thanks a lot for any help - and I do accept RTM as answer, if it comes with a
>good link :-).

How about this:

/**
 * Square definition for a 0x88 board representation.
 */
typedef int Square;
enum {
  A8 = 0x00, B8, C8, D8, E8, F8, G8, H8,
  A7 = 0x10, B7, C7, D7, E7, F7, G7, H7,
  A6 = 0x20, B6, C6, D6, E6, F6, G6, H6,
  A5 = 0x30, B5, C5, D5, E5, F5, G5, H5,
  A4 = 0x40, B4, C4, D4, E4, F4, G4, H4,
  A3 = 0x50, B3, C3, D3, E3, F3, G3, H3,
  A2 = 0x60, B2, C2, D2, E2, F2, G2, H2,
  A1 = 0x70, B1, C1, D1, E1, F1, G1, H1
};

typedef int Rank;
enum {
  RANK_8 = 0, RANK_7, RANK_6, RANK_5, RANK_4, RANK_3, RANK_2, RANK_1
};

/**
 * Get the rank from "col"s point of view.
 * E.g.: getRank<WHITE>(E2) == RANK_2
 *       getRank<BLACK>(E2) == RANK_7
 */
template <Color col> inline Rank getRank(Square sq);
template <> inline Rank getRank<WHITE>(Square sq) { return sq >> 4; }
template <> inline Rank getRank<BLACK>(Square sq) { return 7 - (sq >> 4); }


Then you have to write your functions only once. E.g.:

template <Color col> makeMove() {
   // ...
   if (getRank<col>(to) == RANK_8) {
      // move promotes a pawn ...
   }
   // ...
}

Greetings,
Steffen.



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.