Multidimensional Arrays in C/C++

Started by
13 comments, last by Russell 20 years, 1 month ago
A chess game doesnt even have to worry about ze small stuff like that... and doing a tree search can get real, real expensive; i ran my connect 4 AI game on a uni machine, and some moves took more than a few seconds, so it could get real hairy with chess.

However, with the approach you mentioned youre going to make the code beyond unreadable... and if it turns out that theres a bug in the system, youre going to kill yourself later..
Advertisement
Take a look at how J or SAC treat multidimensional arrays. It is much cleaner and propably faster.

[edited by - Trap on March 20, 2004 7:48:57 PM]
quote:Original post by psamty10
A chess game doesnt even have to worry about ze small stuff like that... and doing a tree search can get real, real expensive; i ran my connect 4 AI game on a uni machine, and some moves took more than a few seconds, so it could get real hairy with chess.

However, with the approach you mentioned youre going to make the code beyond unreadable... and if it turns out that theres a bug in the system, youre going to kill yourself later..


It is true that speed is not the most important thing when it comes to a board game playing program of the tree searching variety, but it's definitely up there.

At this point I'm trying to decide between two approaches to storing the piece lists. I can either do:

Two players (white and black) with a maximum of 16 pieces each
Piece piece_list[2][16];

Two players, six piece types, maximum of 10 of one piece type
Piece piece_list[2][6][10];

The first approach (2D array) means I would have code like this when looping through the piece list:

for (n = 0; n < piece_count[WHITE]; n++){    switch (piece_list[WHITE][n].type)    {        case PAWN:   ...; break;        case KNIGHT: ...; break;        case BISHOP: ...; break;        case ROOK:   ...; break;        case QUEEN:  ...; break;        case KING:   ...; break;    }}
The 3D array allows me to have code like this, which should avoid some (more or less) unpredictable branches in the switch statement above:
int knights_left = piece_count[WHITE][KNIGHT];Piece * knight = piece_list[WHITE][KNIGHT];while (knights_left--)   generate_move(knight++); // or whatever
I was just a little concerned with the "fake" 3D array I'm using in the latter version. As you can see I'm not really using the 3D array inside the loop, and I never actually do a 3D array lookup. I do a 2D lookup to get the piece list for 'white knights', then a series of 1D lookups.

[edited by - Russell on March 21, 2004 2:39:01 AM]
as always do it one way and then profile or do it both ways and compare or whatever...
and... the smaller array might be more cache friendly

This topic is closed to new replies.

Advertisement