in C programming. 1. Create a structure for a chess piece called chessPiece. che
ID: 3918373 • Letter: I
Question
in C programming.
1. Create a structure for a chess piece called chessPiece. chessPiece should contain, piece type as a char array of 8 (Knight, Pawn, Queen, King...). chessPiece should also contain an int x and an int y to show the current position on the board. chessPiece should contain color that is also a char array of 8 (White, Black). chessPiece should contain an int called alive that is a 1 when the piece is still in the game (0 if out of the game). Write a int main0 that creates an array of 32 chessPieces. Create two for) loops that create all the White Pawns and all the Black Pawns on the board (8 White Pawns in row 2, 8 Black Pawns in row 7). The remaining 16 chessPieces go undefined.Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// structure for chess piece
struct chessPiece{
char type[8];
int x;
int y;
char color[8];
int alive;
};
int main(void) {
struct chessPiece chessPieces[32];
// 8 White Pawns in row 2, col 1,2,...,8
for(int i=0;i<8;i++)
{
strcpy(chessPieces[i].type,"Pawns");
chessPieces[i].x=2;
chessPieces[i].y=i+1;
strcpy(chessPieces[i].color,"White");
chessPieces[i].alive=1;
}
// 8 Black Pawns in row 7, col 1,2,...,8
for(int i=8;i<16;i++)
{
strcpy(chessPieces[i].type,"Pawns");
chessPieces[i].x=7;
chessPieces[i].y=i+1;
strcpy(chessPieces[i].color,"Black");
chessPieces[i].alive=1;
}
return EXIT_SUCCESS;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.