Thread: structure with 2-dim array

  1. #1
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    structure with 2-dim array

    (a) Each square of a chessboard can hold one piece - a pawn,
    knight, bishop, rook, queen, or king - or it may be empty.
    Each piece is either black or white. Define two enumeration
    types: Piece, which has seven poss. values, and Color which
    has two.
    Code:
            enum Piece {empty,pawn,knight,bishop,rook,queen,king};
            enum Color {black,white};
    (b) Using the types from above, define a structure type name
    Square that can store both the type of a piece and its color.
    Code:
      /*      not sure about this one          */
    
    
             struct Square {
             int type, color;
              };
    (c) Using the Square type from above, declare an 8 X 8 array
    named board that can store the entire contents of
    a chessboard.
    Code:
           struct Square board[8][8];
    (d) Add an initializer to the declaration from above so that
    board's initial value corresponds to the usual arrangement
    of pieces at the start of a chess game.

    here's where I am stuck, i think

    is the following necessary??

    Code:
    struct Square board[8][8]= {
      {rook,knight,bishop,king,queen,bishop,knight,rook};
      {pawn,pawn,pawn,pawn,pawn,pawn,pawn,pawn};
       {empty,empty,empty,empty,empty,empty,empty,empty};{empty,empty,empty,empty,empty,empty,empty,empty};{empty,empty,empty,empty,empty,empty,empty,empty};
    {empty,empty,empty,empty,empty,empty,empty,empty};{pawn,pawn,pawn,pawn,pawn,pawn,pawn,pawn};{rook,knight,bishop,queen,king,bishop,knight,rook};
    };
    
    /*  row 1        king before queen
         row 8        queen before king               */

    Am I on the right track with the initializing, or is it more simple than this/??

    seems I am missing the color part

    please help.
    Sue B.

    dazed and confused


  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    (b) Using the types from above, define a structure type name
    Square that can store both the type of a piece and its color.
    Code:
    struct Square
    {
       Piece p;
       Color c;
    };
    (d) Add an initializer to the declaration from above so that
    board's initial value corresponds to the usual arrangement
    of pieces at the start of a chess game.
    For this you need a loop. I'd probably slightly change your enum Type so that the empty is at the end rather than the beginning.
    Code:
    for(Color i = white; i < black; i++)
    {
    for(int ii = 0; ii < 8; ii++)
    {
    
    for(Type iii = pawn; iii < empty; iii++)
    {
    
    board[ii][iii].c = ???
    board[ii][iii].b = ???
    }
    }
    }
    This is not the exact answer but if you see this you might understand what they are wanting. Give it a try. Infact in a chess board there are 2 rows with pieces, than 4 empty rows, than 2 rows with pieces. So these loops are not accurate, but you need to use the enum to accomplish the task. Just think about the loops. I am fairly certain there will be 3 loops.

  3. #3
    Registered User ivandn's Avatar
    Join Date
    Oct 2001
    Posts
    49
    Here is my try, though i didnt compile this:

    _______________________________________

    struct Square
    {
    enum type t;
    enum color c;
    };

    _______________________________________

    struct Square board[8][8]= {{rook, white}, {knight, white}, {bishop, white}, {king, white}, {queen, white}, {bishop, white}, {knight, white},{rook, white},
    {pawn, white}, {pawn, white},{pawn, white},{pawn, white},{pawn, white},{pawn, white},{pawn, white},{pawn, white},
    {empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},
    {pawn, black},{pawn, black},{pawn, black},{pawn, black},{pawn, black},{pawn, black},{pawn, black},{pawn, black},
    {rook, black}, {knight, black}, {bishop, black}, {king, black}, {queen, black}, {bishop, black}, {knight, black},{rook, black}
    };
    Ivan

  4. #4
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Also, if you are using a pure C compiler this would be illegal. You need to define the data types at the beginning of the function rather than inside the loop. I use a C/C++ compiler so I can get away with it:

    for(Type iii = pawn; iii < empty; iii++)
    {

    should be:

    Type iii;

    for(iii=pawn; iii < empty; iii ++)
    {

  5. #5
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    okay, after thinking about this some more, I think that this part so far makes sense:

    Code:
    /*  define enum types Piece and Color  */
            enum Piece {empty,pawn,knight,bishop,rook,queen,king};
            enum Color {black,white};
    /* define structure that stores type of piece and color */

    quoted from Troll_King post
    struct Square
    {
    Piece p;
    Color c;
    };
    /* declare and initialize 8 x 8 array named 'board' for all pieces at start of chess game */
    Code:
    struct Square board[8][8] ={{rook, white}, {knight, white}, {bishop, white}, {king, white}, {queen, white}, {bishop, white}, {knight, white},{rook, white}, 
    {pawn, white}, {pawn, white},{pawn, white},{pawn, white},{pawn, white},{pawn, white},{pawn, white},{pawn, white}, 
    {empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white}.{empty,black},{empty,black},{empty,black},{empty,black},{empty,black},{empty,black},{empty,black},{empty,black},{empty,black},{empty,black},{empty,black},{empty,black},{empty,black},{empty,black},{empty,black},{empty,black},
    {pawn, black},{pawn, black},{pawn, black},{pawn, black},{pawn, black},{pawn, black},{pawn, black},{pawn, black}, 
    {rook, black}, {knight, black}, {bishop, black}, {king, black}, {queen, black}, {bishop, black}, {knight, black},{rook, black} 
    };
    The instructions said to add an initializer TO the declaration of the array named 'board'. It didn't say to use loops of any kind.

    And half the board is designated 'black' so I changed half the empty's to black.

    There is nothing more asked of this problem.
    I think it was more to demonstrate understanding of structures and enumerations (and 2-dim arrays in this case).

    There is no writing of loops or functions yet.
    Sue B.

    dazed and confused


  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Actually, your structure should look like this:
    Code:
    struct ChessPiece {
       PieceType p;
       Color c;
    };
    struct Square {
       ChessPiece cp;
       Color c;
    };
    struct ChessBoard {
       Square board[8][8];
    };

    You're all forgetting one important thing about chess:

    Every piece has a color, additionally, every SQUARE has a color. Remember, every other square is a different color.


    Therefore, you need to represent both. My structures will do this.

    Quzah.

  7. #7
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    Not trying to be a pain in the @SS here, but....

    for the sake of a problem in a textbook I am to follow here,
    I am just supposed to create the array to hold the piece type and the color of the piece not the color of the square on the board.
    Though, that is an important point to make. I think all I am to work with here is the pieces used in chess not the board itself.

    Not making the whole game here. Just need to get the initialization of a 2-dim array that is a structure under wraps here.

    Quzah, you are so right and I like your comments. But please help with the original question in my start of this thread


    (a) Each square of a chessboard can hold one piece - a pawn,
    knight, bishop, rook, queen, or king - or it may be empty.
    Each piece is either black or white. Define two enumeration
    types: Piece, which has seven poss. values, and Color which
    has two. DONE !!
    (b) Using the types from above, define a structure type name
    Square that can store both the type of a piece and its color.
    DONE!!
    (c) Using the Square type from above, declare an 8 X 8 array
    named board that can store the entire contents of
    a chessboard.
    I think I got it
    (d) Add an initializer to the declaration from above so that
    board's initial value corresponds to the usual arrangement
    of pieces at the start of a chess game.
    we're still fighting about this one
    Sue B.

    dazed and confused


  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Muhahahah!
    Code:
    enum { Black, White };
    enum { Empty, Pawn, Knight, Bishop, Rook, Queen, King };
    struct chess {
       char squarecolor:1; /* square color */
       char piece:3; /* piece here */
       char piececolor:1; /* piece color */
    } board[8][8] = 
    {
       { /* Row 0 */
          { Black, Rook, Black },  /* Square 0 */
          { White, Knight, Black },
          { Black, Bishop, Black },
          { White, King, Black },
          { Black, Queen, Black },
          { White, Bishop, Black },
          { Black, Knight, Black },
          { White, Rook, Black },
       },
       {
          { White, Pawn, Black },
          { Black, Pawn, Black },
          { White, Pawn, Black },
          { Black, Pawn, Black },
          { White, Pawn, Black },
          { Black, Pawn, Black },
          { White, Pawn, Black },
          { Black, Pawn, Black },
       },
       {
          { Black, Empty, 0 }, /* third column is irrelevant on empty */
          { White, Empty, 0 },
          { Black, Empty, 0 },
          { White, Empty, 0 },
          { Black, Empty, 0 },
          { White, Empty, 0 },
          { Black, Empty, 0 },
          { White, Empty, 0 },
        },
        /* repeat... */
    };
    There you go! Your teacher will:
    a) love it
    b) hate it
    c) wonder how you came up with it
    d) all of the above

    As for the last thing you're stuck on, you've basicly got it. Just allign the pieces as they would be set up on a chessboard (basicly like what I do at the end of this example up here).

    Quzah.

  9. #9
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    ok would the following be right??

    Code:
    enum Piece {empty,pawn,knight,bishop,rook,queen,king};
    enum Color {black,white};
     
    struct Square
    {
       Piece p;
       Color c;
    };
    
    
    
    struct Square board[8][8] =
    {
    
    /*  row 1 */
    {rook, white}, {knight, white}, {bishop, white}, {king, white}, {queen, white}, {bishop, white}, {knight, white},{rook, white}, 
    
    /* row 2 */
    {pawn, white}, {pawn, white},{pawn, white},{pawn, white},{pawn, white},{pawn, white},{pawn, white},{pawn, white}, 
    
    /* row 3 */
    {empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},
    
    /* row 4  */
    {empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white},{empty, white}. 
    
    /* row 5 */
    {empty,black},{empty,black},{empty,black},{empty,black},{empty,black},{empty,black},{empty,black},{empty,black},
    
    /* row 6 */
    {empty,black},{empty,black},{empty,bla ck},{empty,black},{empty,black},{empty,black},{empty,black},{empty,black},
    
    /* row 7 */
    {pawn, black},{pawn, black},{pawn, black},{pawn, black},{pawn, black},{pawn, black},{pawn, black},{pawn, black}, 
    
    /* row 8 */
    {rook, black}, {knight, black}, {bishop, black}, {king, black}, {queen, black}, {bishop, black}, {knight, black},{rook, black} 
    
    };
    Last edited by sballew; 10-27-2001 at 04:33 PM.
    Sue B.

    dazed and confused


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure or Array?
    By epi_jimenez in forum C Programming
    Replies: 7
    Last Post: 04-01-2009, 02:45 PM
  2. linear search for structure (record) array
    By jereland in forum C Programming
    Replies: 3
    Last Post: 04-21-2004, 07:31 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM