Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C school project #include #include #include struct game_piece { }; struct game_board { }; void game_piece_init_default(struct game_piece* piece) { } void game_piece_init(struct game_piece* piece, char*

C school project

#include #include #include

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 */ game_board_print(&board); printf("Would you like to move a piece? Enter "Y" to move a piece. "); scanf("%s", input_string); }

return 0; }

------------------------------

void game_piece_init_default(struct game_piece* piece) -> Assign the default string "---" to the game pieces label.

void game_piece_init(struct game_piece* piece, char* new_label) --> Assign the game pieces label with the new_label provided.

char* game_piece_get_label(struct game_piece* piece) --> Returns the pieces label.

char* game_piece_to_string(struct game_piece* piece) --> Constructs a C string of length 3 from the pieces label (Note: this length does not include the null character). If the label is shorter than length 3, then the new string should be the label with spaces appended to make it the correct length. If the label is longer than 3, then use the first 3 characters of the label.

creating a structure called

game_board in the same code file. The structure will contain a 2 - dimensional array called board of game_piece type and 2 ints, rows and columns. Define the following functions:

void game_board_init(struct game_board* game_board, int rows, int cols) --> Instantiates the 2-dimensional array board to the size "rows" by "columns" specified by the parameters, then sets the game_boards rows and columns values. Then it initializes each game_piece element of this array using the game_piece_init_default functi on. So, each piece will have the default value for its label.

int game_board_is_space_valid(struct game_board* game_board, int row, int col) --> The function checks if the parameters row and col are valid. If at least one of the parameters "row" or "col" is less than 0 or larger than the last index of the array (note that the number of rows and columns can be different), then it return 0 (false). Otherwise it returns 1 (true).

int game_board_add_piece(struct game_board* game_board, struct game_piece* piece, int row, int col) --> This function should validate that the space specified by row and col is valid and that the space is not occupied by a piece. If the game_piece at the space has the default label, the 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 game_board* game_board, int src_row, int src_col, int dest_row, int dest_col) --> This method should validate that both the src and dest spaces are valid and that the dest space is not occupied. If both conditions pass, then the piece located at (src_row, src_col) should be moved to (dest_row, dest_col). The space at (src_row, src_col) should be replaced by the default game_piece. If this method moves the piece, return 1, otherwise return 0.

void game_board_print(struct game_board* game_board) --> It prints information of the "board". It should show the list of pieces placed on the board using the game_piece_to_string function (it shows the first 3 characters of each piece). Use the following format:

The GameBoard

--------------------

--- Kin ---

Paw --- ---

--- --- Paw

Please see the sample output listed below.

Please enter the number of rows. 3

Please enter the number of columns. 3

Please enter a label for a new piece. Enter "Q" when done. King

Please enter a row for the piece. 0

Please enter a column for the piece. 1

New piece "King" added.

Please enter a label for a new piece. Enter "Q" when done. Pawn

Please enter a row for the piece. 1

Please enter a column for the piece. 0

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 piece "Pawn" added.

Please enter a label for a new piece. Enter "Q" when done. Queen

Please enter a row for the piece. 4

Please enter a column for the piece. 5

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

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

Paw --- ---

--- --- Paw

Would you like to move a piece? Enter "Y" to move a piece. Y

Please enter the piece's row. 0

Please enter the piece's column. 1

Please enter the piece's new row. 0

Please enter the piece's new column. 2

Piece moved to new space.

The GameBoard

--------------------

--- --- Kin

Paw --- ---

--- --- Paw

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

image text in transcribed

image text in transcribed

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. unctionn oid game piece_init default(struct ame piecepiece oid game_piece init(struct escription ssign the default string"-" to the game piece's label. ssign the game piece's label with the new_label ame_piece* piece, char new label) provided. "game_piece_get label(struct ns the piece's label. ame piece piece onstructs a C string of length 3 from the piece's label Note: this length does not include the null character). If he label is shorter than length 3, then the new string hould be the label with spaces appended to make it the orrect length. If the label is longer than 3, then use the first 3 characters of the label game piece_to_string(struct ame piece* piece) Step 2 You will be creating a structure called game board in the same code file. The structure will contain a 2-dimensional array called "board" of game piece type and 2 ints, rows and columns. Define the following functions unction escription oid game_board_init(struct stantiates the 2-dimensional array "board" to the size ame board* game_board, int rows, Tows" by "columns" specified by the parameters, then cols) ets the game_board's rows and columns values. Then it nitializes each game piece element of thi ame piece_init default function. So, each piece will ve the default value for its label s array using the 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 ol) ess than 0 or larger than the last index of the array (note t the number of rows and columns can be different), it return 0 (false). Otherwise it returns 1 (true) game board add piece(struct ame board game board, struct ame piece piece, int row, int col) is function should validate that the space specified by ow and col is valid and that the space is not occupied by e. If the game piece at the space has the default label, e space should be considered not occupied. If the space s both valid and not already occupied, then the space ould be replaced by the parameter piece" and the should return 1. Otherwise, return 0 t game board move piece(struct ame board game board, int c row, int sre_col, int dest_row, int is method should validate that both the src and dest spaces are valid and that the dest space is not occupied. If conditions pass, then the piece located at (src_row, c col) should be moved to (dest_row, dest col). The t_col) space at (src_row, src_col) should be replaced by the efault game piece. If this method moves the piece,r 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. 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. unctionn oid game piece_init default(struct ame piecepiece oid game_piece init(struct escription ssign the default string"-" to the game piece's label. ssign the game piece's label with the new_label ame_piece* piece, char new label) provided. "game_piece_get label(struct ns the piece's label. ame piece piece onstructs a C string of length 3 from the piece's label Note: this length does not include the null character). If he label is shorter than length 3, then the new string hould be the label with spaces appended to make it the orrect length. If the label is longer than 3, then use the first 3 characters of the label game piece_to_string(struct ame piece* piece) Step 2 You will be creating a structure called game board in the same code file. The structure will contain a 2-dimensional array called "board" of game piece type and 2 ints, rows and columns. Define the following functions unction escription oid game_board_init(struct stantiates the 2-dimensional array "board" to the size ame board* game_board, int rows, Tows" by "columns" specified by the parameters, then cols) ets the game_board's rows and columns values. Then it nitializes each game piece element of thi ame piece_init default function. So, each piece will ve the default value for its label s array using the 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 ol) ess than 0 or larger than the last index of the array (note t the number of rows and columns can be different), it return 0 (false). Otherwise it returns 1 (true) game board add piece(struct ame board game board, struct ame piece piece, int row, int col) is function should validate that the space specified by ow and col is valid and that the space is not occupied by e. If the game piece at the space has the default label, e space should be considered not occupied. If the space s both valid and not already occupied, then the space ould be replaced by the parameter piece" and the should return 1. Otherwise, return 0 t game board move piece(struct ame board game board, int c row, int sre_col, int dest_row, int is method should validate that both the src and dest spaces are valid and that the dest space is not occupied. If conditions pass, then the piece located at (src_row, c col) should be moved to (dest_row, dest col). The t_col) space at (src_row, src_col) should be replaced by the efault game piece. If this method moves the piece,r 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data And Databases

Authors: Jeff Mapua

1st Edition

1978502257, 978-1978502253

Students also viewed these Databases questions