0

I am writing a chess engine using an array (single dimension) in C?

#define ROW(sq)         (sq >> 3)
#define COL(sq)         (sq & 7)

int board[64] = {
   0,1,2,3,4,5,6,7,
   8,9,10,11,12,13,,14,15,
   .......................
   .......................
   56,57,48,59,60,61,62,63
}

Now: To get the row and col of number 11 I use this:

int r = ROW(11);
int c = COL(11);

It gives:

r = 1
c = 3

Please help me to write a function so that it takes the parameter as r and c and gives the correct square like:

sq = fnc(r,c);
sq = fnc(1,3);
sq = 11;
Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
shahil4242
  • 25
  • 8
  • 5
    have you considered `int board[8][8]`?,, or is single dimension a requirement? – yano Jun 27 '17 at 17:49
  • @GAURANGVYAS: A 2D array is not the same as a 1D array. Even for a variable sized array, it makes no sense not to use a 2D array. – too honest for this site Jun 27 '17 at 18:06
  • @Olaf My fault. Commented without properly seeing the question. – GAURANG VYAS Jun 27 '17 at 18:16
  • I know this is super old but I disagree with "there is no reason not to use a 2D array". A 2D array is laid out in memory exactly the same as a 1D array. Its literally an array of arrays. There's nothing wrong with using a 1D array as your data and choosing to draw it as a 2D visual grid. Its perfectly fine to split the data configuration from its visual representation. – Josh Sanders Jan 25 '20 at 09:12

3 Answers3

2

You just have to calculate row * MAX_COL + col where MAX_COL = 8. With row * MAX_COL you will go into the next row, like the following figure shows:

                   1 * 8 ---                 2 * 8 ---
                           |                         |
|         row = 0        | v       row = 1         | v       row = 2
| 0  1  2  3  4  5  6  7 | 8  9  10 11 12 13 14 15 | 16 ...

If you want a macro which uses the global variable board:

#define FIELD(row,col) board[(row) * 8 + (col)]

or as function which also uses the global defined board variable:

inline int FIELD (int row, int col)
{
   return board[row * 8 + col]
}

if you want to pass the board as array to the function you can do the following, where int boardArr[] decays to a pointer:

inline int FIELD (int boardArr[], int row, int col)
{
   return boardArr[row * 8 + col]
}

Also you should change your macro definitions, because they are unsafe if the parameter that is passed is a value that is used in a calculation. You can read it here on SO. So you should use braces around the parameters sq:

#define ROW(sq)   ((sq) >> 3)
#define COL(sq)   ((sq) & 7)

It would be even better to use inline functions as it is also said in the SO answer.

Andre Kampling
  • 4,867
  • 2
  • 15
  • 41
1

If your board is always 8x8, you can see that each r starts at 0, 8, 16... and each c is just the offset from those initial points, so given an r and c, you can calculate the array index by board[(r * 8) + c] to get the corresponding piece.

Felix Guo
  • 2,632
  • 12
  • 20
1

This is best approached using a two-dimensional array because that way you can simply access board[r][c].

If you (or your professor) insist on a one-dimensional array, this macro will do what you want:

#define GET_PIECE(r, c) (board[((c) + (8) * (r))])

Notice how everything is wrapped in parentheses. This is very important with macros!

A better solution would be to use an inline function.

Govind Parmar
  • 18,500
  • 6
  • 49
  • 78