1. Engineering
  2. Computer Science
  3. include ltstdiohgt include ltstdlibhgt include ltstringhgt struct gamepiece ...

Question: include ltstdiohgt include ltstdlibhgt include ltstringhgt struct gamepiece ...

Question details

Step 1. First, you need to create a structure game piece. It should contain the variable, label (char [30]) In addition, the following functions should be defined. Function void game piece init default(struct game piece* piece void game piece init(struct Description ssign the default string --- to the game pieces label ssign the game pieces label with the new label ame piece* piece, char* new_label) provided har* game piece get label(struct eturns the pieces label game piece piece har* game piece to string(struct ame piece* piece) onstructs a C string of length 3 from the pieces label Note: this length does not include the null character). If he label is shorter than length 3, then the new strin;g hould be the label with spaces appended to make it the orrect length. If the label is longer than 3, then use the irst 3 characters of the label Step 2. You will be creating a structure called game board in the same code file. The structure will Define the following functions: Functioin contain a 2-dimensional array called board of game piece type and 2 ints, rows and columns. escription void game board init(struct nstantiates the 2-dimensional array board to the size ame _board* game board, int rows, rows by columns specified by the parameters, then nt cols ets the gameboards rows and columns values. Then it initializes each game piece element of this array using the ame piece init default function. So, each piece will ave the default value for its labelint game board is space valid(struct The function checks if the parameters row and col are ame board* game board, int row, int valid. If at least one of the parameters row or col is col) less than 0 or larger than the last index of the array (note hat the number of rows and columns can be different), hen it return 0 (false). Otherwise it returns 1 (true) nt game_board _add piece(struct ame board* game board, struct ame piece* piece, int row, int col) his function should validate that the space specified by ow and col is valid and that the space is not occupied by piece. If the game piece at the space has the default label, he space should be considered not occupied. If the space is both valid and not already occupied, then the space should be replaced by the parameter piece and the method should return 1. Otherwise, return 0. int game board move piece(struct This method should validate that both the src and dest ame board* game board, int rc_row, int src col, int dest row, int both conditions pass, then the piece located at (src row, spaces are valid and that the dest space is not occupied. If dest col rc col) should be moved to (dest row, dest col). The pace at (src_row, src col) should be replaced by the efault game_piece. If this method moves the piece, return 1, otherwise return 0 t prints information of the board. It should show the list f pieces placed on the board using the ame piece to string function (it shows the first 3 haracters of each piece). Use the following format: oid game board print(struct ame board* game board) he GameBoard Kin Paw Paw lease see the sample output listed below After compiling the homework part 2.c file, you need to execute it.Sample Output: (the inputs entered by a user are shown in red Please enter the number of rows. 3 Please enter the number of columns. Please enter a label for a new piece. Enter Q when done. King Please enter a row for the piece. Please enter a column for the piece. New piece Kingadded Please enter a label for a new piece. Enter Q when done. Pawn Please enter a row for the piece Please enter a column for the piece New piece Pawn added Please enter a label for a new piece. Enter Q when done. Pawn Please enter a row for the piece 2 Please enter a column for the piece 2 New piecePawn added Please enter a label for a new piece. Enter Q when done.Queen Please enter a row for the piece Please enter a column for the piece. Invalid row and/or column Please enter a label for a new piece. Enter Q when done.King Please enter a row for the piece Please enter a column for the piece 2 Invalid row and/or column Please enter a label for a new piece. Enter Q when done.Q The GameBoard Kin

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

struct game_piece

{

};

struct game_board

{

};

void game_piece_init_default(struct game_piece* piece)

{

}

void game_piece_init(struct game_piece* piece, char* new_label)

{

}

char* game_piece_get_label(struct game_piece* piece)

{

return "";

}

char* game_piece_to_string(struct game_piece* piece)

{

return "";

}

void game_board_init(struct game_board* game_board, int rows, int cols)

{

}

int game_board_is_space_valid(struct game_board* game_board, int row, int

col)

{

return 0;

}

int game_board_add_piece(struct game_board* game_board, struct game_piece*

piece, int row, int col)

{

return 0;

}

int game_board_move_piece(struct game_board* game_board, int src_row, int

src_col, int dest_row, int dest_col)

{

return 0;

}

void game_board_print(struct game_board* game_board)

{

}

int main()

{

/* declare local variables */

int row;

int col;

int destRow;

int destCol;

int rowNum;

int colNum;

struct game_board board;

struct game_piece piece;

char input_string[30];

/* get the size of the game board */

printf("Please enter the number of rows. ");

scanf("%d", &rowNum);

printf("Please enter the number of columns. ");

scanf("%d", &colNum);

game_board_init(&board, rowNum, colNum);

/* get the first piece's label */

printf("Please enter a label for a new piece. Enter "Q" when

done. ");

scanf("%s", input_string);

while (strcmp(input_string, "Q") != 0 && strcmp(input_string, "q") !=

0)

{

game_piece_init(&piece, input_string);

/* get the location to place the piece */

printf("Please enter a row for the piece. ");

scanf("%d", &row);

printf("Please enter a column for the piece. ");

scanf("%d", &col);

/* verify the space is valid then add the piece to the board */

if (game_board_is_space_valid(&board, row, col))

{

if (game_board_add_piece(&board, &piece, row, col))

{

printf("New piece "%s" added. ",

game_piece_get_label(&piece));

}

else

{

printf("A piece is already at that space. ");

}

}

else

{

printf("Invalid row and/or column. ");

}

/* get the label for the next piece */

printf("Please enter a label for a new piece. Enter "Q" when

done.");

scanf("%s", input_string);

}

/* print the board and check if user wants to move a piece */

game_board_print(&board);

printf("Would you like to move a piece? Enter "Y" to move a

piece. ");

scanf("%s", input_string);

while (strcmp(input_string, "Y") == 0 || strcmp(input_string, "y") ==

0)

{

/* get the location of the piece */

printf("Please enter the piece's row.");

scanf("%d", &row);

printf("Please enter the piece's column.");

scanf("%d", &col);

/* get the destination for the piece */

printf("Please enter the piece's new row.");

scanf("%d", &destRow);

printf("Please enter the piece's new column.");

scanf("%d", &destCol);

/* verify both spaces are valid then move the piece */

if (game_board_is_space_valid(&board, row, col) &&

game_board_is_space_valid(&board, destRow, destCol))

{

if (game_board_move_piece(&board, row, col, destRow, destCol))

{

printf("Piece moved to new space. ");

}

else

{

printf("A piece is already in that space. ");

}

}

else

{

printf("A row or column is invalid. No piece moved. ");

}

/* print the board and check if the user wants move another piece

*/

****this is all of the information we are given

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution