Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this assignment, we will be making a program to populate a game board with pieces and to move those pieces around on the board.

In this assignment, we will be making a program to populate a game board with pieces and to move those pieces around on the board. Use the file homework_part_2.c (available on Canvas). You will need to add header comments to each file along with comments for all the methods you will be implementing. You will need to update the condition in the main loops while statement and fill out the procedures below the main procedure. Any changes made to the main method of the file will be penalized unless otherwise instructed. 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 Description 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. 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: Function Description 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 function. 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. After compiling the homework_part_2.c file, you need to execute it.

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

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

Students also viewed these Databases questions