Advertisement Jump to content


This topic is now archived and is closed to further replies.


switch c++ chess game

This topic is 5830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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)

      case 0 :
            case 0 : destination = 8;
            case 1 : destination = 1;
            // etc, etc... u get the picture

      case 1 : 
            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.

Share this post

Link to post
Share on other sites
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)
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

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!