Question

In: Computer Science

What is an enumerated type in C? Define such a type (called chess_value) to store the...

  1. 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.

  2. 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).

  3. 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.

Solutions

Expert Solution

****************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;
}


Related Solutions

Written in C++ Define a class for a type called Fraction. This class is used to...
Written in C++ Define a class for a type called Fraction. This class is used to represent a ratio of two integers. Include mutator functions that allow the user to set the numerator and denominator. Also include a member function that returns the values of the numerator divided by the denominator as a double. Include an additional member function that outputs the value of the fraction reduced to lowest terms. This will require finding the greatest common divisor for the...
(C++ ONLY) You will define your own data type. It will be a struct called Fraction....
(C++ ONLY) You will define your own data type. It will be a struct called Fraction. The struct will have 2 integer fields: numerator and denominator. You will write a function called reduce that takes a Fraction as a parameter and returns that Fraction in its reduced form. For example, if the fraction 2/4 is passed to the function, the fraction 1/2 will be returned. Consider any fraction with a denominator of 1 to be in reduced form. You will...
In this program, you'll create a program that utilizes an enumerated data type to manipulate the...
In this program, you'll create a program that utilizes an enumerated data type to manipulate the array.   Here are the requirements: Write a program that contains an enumerated data type named Letters.    In the declaration, include the following three enumerators: ALPHA, BETA, DELTA. Then, create an array of integers three elements long. The array should be initialized to 0 using a 1-element initialization list. Instead of using integers as subscripts, use the enumerators from your enumerated data type to assign...
C Language Let us define a Point type to store two-dimensional coordinates (x, y)! Write the...
C Language Let us define a Point type to store two-dimensional coordinates (x, y)! Write the following functions operating on this data type: dist(): calculates the distance between the two points received (using the Pythagorean theorem) equal(): checks if to points are equal or not read(): reads a point from the keyboard and returns it In the main, define two points, and test all these functions. When all tests are passed, solve the following task by utilizing the structure and...
[ Write in C not C++] 1. Define a C structure Covid19Info to store the information...
[ Write in C not C++] 1. Define a C structure Covid19Info to store the information of COVID 19 cases of countries around the world. It keeps the name of the country, number of COVID 19 cases, number of deaths, current test positive rate, etc. After defining the structure, declare a variable of Covid19Info type with some initial values for Bangladesh. [8]
Java Define a class called BlogEntry that could be used to store an entry for a...
Java Define a class called BlogEntry that could be used to store an entry for a Web log. The class should have instance variables to store : - the poster’s username, - text of the entry, - and the date of the entry using the Date class Date class is: class Date { private int day, year; private String mon; public Date() { mon=" "; day=0; year=0; } public String toString() { return (mon+"/"+day+"/"+year); } public set_date(String m, int d,...
IN C++ Create a class called TextInt. The purpose of a TextInt is to store an...
IN C++ Create a class called TextInt. The purpose of a TextInt is to store an integer and convert it to the English text form of the integer when needed, such as ‘zero’ for 0, ‘one’ for 1, and so on, up to ‘nine thousand nine hundred ninety nine’ for 9999. You do NOT need punctuation (commas, hyphens, ‘and’, etc.) The TextInt class should have its own .h and .cpp files. At least the translate function should be implemented in...
Define a class called Goals that has the following requirements in c++: a function called set...
Define a class called Goals that has the following requirements in c++: a function called set that takes 3 int parameters that are goals for "fame", "happiness" and "money". The function returns true and saves the values if they add up to exactly 60, and returns false otherwise (you may assume the parameters are not negative) a functions satisfies that takes 3 int parameters and returns true if each parameter is at least as large as the saved goal, false...
Write a small Java class called StoreAddStuff that uses generics to store two variables of type...
Write a small Java class called StoreAddStuff that uses generics to store two variables of type T passed in during construction, then returns their sum through a non-static method.
Task Create a class called Mixed. Objects of type Mixed will store and manage rational numbers...
Task Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). The class, along with the required operator overloads, should be written in the files mixed.h and mixed.cpp. Details and Requirements Finish Lab 2 by creating a Mixed class definition with constructor functions and some methods. The Mixed class should have public member functions Evaluate(), ToFraction(), and Simplify(). The Evaluate() function should return a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT