0

I'm following along a book on teaching yourself C++, and there's a question about creating the layout of a chess board using enums and arrays. Looking at their solution, they call an enum to declare the potential state of any given square on the board. BUt I don't fully understand what's going on after that. Their code looks like this:

int main()
{
    enum Square
    {
        Empty = 0,
        Pawn,
        Rook,
        Knight,
        Bishop,
        King,
        Queen
    };

    Square chessBoard[8][8];

    chessBoard[0][0] = chessBoard[0][7] = Rook;
    //Follow this pattern to initialise the rest of the pieces

    return 0;
}

I don't understand what "Square chessBoard" is actually doing in this case. I'm a complete beginner in this, so any help is hugely appreciated.

AShearman
  • 13
  • 1
  • Could you clarify what part you don't understand? There's nothing special happening here with enums. `Square chessBoard[8][8];` is just an ordering variable declaration. Do you know what `T name;` and `T name[N][M];` mean in general? – Brian61354270 Mar 24 '23 at 23:42
  • 2
    Would you understand `char chessBoard[8][8];` or `int chessBoard[8][8];` ? – drescherjm Mar 24 '23 at 23:43
  • I don't understand what calling "Squaare chessBoard" does, or how it actually generates an array. – AShearman Mar 24 '23 at 23:45
  • It doesn't generate an array per-se. It just states that the name `chessBoard` now refers to an (uninitialized) array in automatic storage. You may find [How do I use arrays in C++?](https://stackoverflow.com/q/4810664/11082165) helpful. – Brian61354270 Mar 24 '23 at 23:48
  • The `enum` is "restricting" the values that can be stored in the chess board. So at chess board location [3][5], you could have any one of the `enum` values. This a newer method than using integers and it takes up less space than using strings to denote the content of a chess board location. – Thomas Matthews Mar 24 '23 at 23:58

1 Answers1

1

<type> <variable>[dim1][dim2] creates a 2 dimension array of things of type <type> with dim1 rows and dim2 columns. For example, int board[10][14] creates grid with 10 rows and 14 columns each of which holds an integer and gives that grid the name board. You could then do things like board[1][3] = 17 to set the 2nd row (0-indexed), 4th column to the value 17.

So, Square chessBoard[8][8] sets up a variable named chessBoard that is an 8 by 8 grid of Square instances. And Square is the type of the enum. So a Square can have values Pawn or Rook, just like an int can have values like 10, or 88.

So, lets say you want to put a rook in the top-left corner of that board. Then chessBoard[0][0] = Square.Rook would do the trick. To put it in the top right corner you'd do chessBoard[0][7] = Square.Rook.

Note: deciding that 0 is "top" instead of 7 being "top" is arbitrary as is deciding that it's rows first then columns or 0 is "left" and 7 is "right".

Oliver Dain
  • 9,617
  • 3
  • 35
  • 48
  • Thank you! I couldn't understand why the chessBoard was declared as Square chessBoard, but this makes perfect sense now. Cheers – AShearman Mar 25 '23 at 00:01