In: Computer Science
What is an enumerated type in C? Define such a type (called chess_value) to store the
possible values of a chess piece – pawn, rook, knight, bishop, queen and king.
Create a structure called struct piece to store a chess piece. This structure should have two fields, one of which is the value of the piece, and should be of the enumerated type
defined in Q4. The second field is colour, which should be either 1 (white) or 0 (black).
Define an 8x8 array of "struct piece" pointers that can be used to model a chessboard. Each element of the array is pointer to a piece structure, that is either NULL if the square is empty or points to a relevant piece structure if it isn’t. Use malloc to allocate and setup both King pieces on the board. The white king should be placed in row 1, column 5, and the black king in row 8, column 5. You should leave all other squares as NULL.
****************Please give your feedback on Code. If there is problem or not according to your need, please let me known in comment section****************
CODE
#include <stdio.h>
#include<stdlib.h>
int main()
{
// Enum value CHESS VALUE assigned will be king=0, queen=1
,........pawn=5
enum chess_value{king,queen,rook,knight,bishop,pawn};
//Enum value COLOR assigned will be black=0 and white=1
enum chess_color{black,white};
//Structue is used to represent each piece on chess board which
is either colored white or black ;
struct piece {
enum chess_value value;
enum chess_color color;
};
//ChessBoard 8*8 that can contain a chess piece
struct piece * chessboard[8][8];
struct piece * white_king;
struct piece * black_king;
// using malloc to allocate memory
white_king=(struct piece *)malloc(sizeof(struct piece));
//setting chess piece as king
white_king->value=king;
// setting it color to white piece type
white_king->color=white;
//using malloc to allocate memory
black_king=(struct piece *)malloc(sizeof(struct piece));
//setting chess piece as king
black_king->value=king;
// setting it color to white piece type
black_king->color=black;
//Assigning both king to required position
chessboard[0][4]=white_king;
chessboard[7][4]=black_king;
//Display result of one of king i.e black in this example
printf("chess piece is -->%d and it's color is --->
%d",chessboard[7][4]->value,chessboard[7][4]->color);
return 0;
}