switch c++ chess game

Started by
0 comments, last by Moagly 21 years, 2 months ago
I am creating a chess game using c++, i am going to be using the ray method for move generation as described in the chess programming series in artificial intelligence section on gamedev, i was wondering which would be the quickest(in terms of CPU execution) this way using switch statements :
  

// one of these for each piece

bool makeKingMoves(square, ray)
{

   switch(square)
   {
      case 0 :
      {
         switch(ray)
         { 
            case 0 : destination = 8;
            case 1 : destination = 1;
            // etc, etc... u get the picture

         }
      case 1 : 
      {
         switch(ray)
         {
            case 0 : destination = 0;
            case 1 : destination = 8;
            // etc, etc

         }
      }
   
      // and so on and so forth for the entire board

   }
}
  
or this way using arrays :
  

kingMoves[0][0] = 8;
kingMoves[0][1] = 1;

  
I guess my question is basically how does the switch statement in c++ work, how slow is it? If anyone has any ideas or thoughts wud be greatly appreciated.
Advertisement
That switch statement will end up huge (64 sq * 8 rays).
Anyway, switch can be implemented as a chain of cmp/jcc, as a decision tree, or by means of a jump table (if there are lots of values in a tight range).

I don''t think performance matters here - it''s just a one time init thing.

Here''s how I did it (yuck )
for each square on board (padded => no move leads ''out of bounds'')  for each piece (w pawn, b pawn, all others)    for each ray (dependent on piece)      do        advance along ray        if on valid square          SETBIT(move_array[piece][from_sq], to_sq)      while(piece is ranged && on valid square) 


Hope that helps a little
Jan
E8 17 00 42 CE DC D2 DC E4 EA C4 40 CA DA C2 D8 CC 40 CA D0 E8 40E0 CA CA 96 5B B0 16 50 D7 D4 02 B2 02 86 E2 CD 21 58 48 79 F2 C3

This topic is closed to new replies.

Advertisement