Question

Step L First, you need to create a structure game_piece. It should contain the variable, label (char [30]) In addition, the f

Step 2. You will be creating a structure called game_board in the same code file. The structure will contain a 2-dimensional

#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.\n");

scanf("%d", &rowNum);

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

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.\n");

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.\n");

scanf("%d", &row);

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

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.\n",

game_piece_get_label(&piece));

}

else

{

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

}

}

else

{

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

}

/* 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.\n");

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.\n");

}

else

{

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

}

}

else

{

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

}

/* 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.\n");

scanf("%s", input_string);

}

return 0;

}

IN LANGUAGE C

Step L 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 unction oid game_piece_init default(struct Assign the default string "---" to the game piece's label. me piece piece oid game_ piece_init(struct ame piece piece, char new labprovided. escription Assign the game piece's label with the new label turns the piece's label. har* game_piece_get label(struct me piece piece har game piece to string struct ame piece piece) structs a C string of length 3 from the piece's label ote: this length does not include the null character). If e label is shorter than length 3, then the new string should 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 contain a 2-dimensional array called "board" of game piece type and 2 ints, rows and columns. Define the following functions: unction escription tiates the 2-dimensional array "board" to the size oid game_board mit struct ame board* me board, int rows. S"by "columnsspecified by the parameters, then cols) ets the game board's rows and columns values. Then it izes each game piece element of this arrav using the ame piece init default function. So, each piece will ve the default value for its label game board is space_valid(struct The function checks if the parameters row and col are ame board" game board, int row, int alid. 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 at the number of rows and columns can be different), en it retum 0 (false). Otherwise it returs1 (true). game_board_add_piece(struct is function should validate that the space specified by ame board" game board, struct ro and col is valid and that the space is not occupied by a ame piece* piece, nt ow, int col) piece. 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 ld be replaced by the parameter "piece" and the ethod should retum 1. Otherwise, retun 0 game board move piecestruct s method should validate that both the src and dest paces are valid and that the dest space is not occupied. If ame board* game board, int conditions pass, then the piece located at (sre row, c col) should be moved to (dest row, destcol). The pace at (src row, src col) should be replaced by the row, int src col, int dest row, int est col) lt game piece. If this method moves the piece, re otherwise return 0 oid game_board print(struct ame board* game board) t prints infonmation of the "board". It should show the list fpieces placed on the board using the e _piece to string function (it shows the first 3 haracters of each piece). Use the following format: he GameBoard -Kin--- aw-- Paw lease see the sample output listed below
1 0
Add a comment Improve this question Transcribed image text
Answer #1

//Here is your answer..

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct game_piece
{
char lable[30];
};

struct game_board
{
struct game_piece* board[50][50];
int rows;
int columns;
};

void game_piece_init_default(struct game_piece* piece)
{
piece= (struct game_piece*)malloc(sizeof(struct game_piece));
// strcpy(piece->lable,"---");
piece->lable[0]='-';
piece->lable[1]='-';
piece->lable[2]='-';
piece->lable[3]='\0';


}
void game_piece_init(struct game_piece* piece, char* new_label)
{
strcpy(piece->lable,new_label);
}

char* game_piece_get_label(struct game_piece* piece)
{
return piece->lable;
}

char* game_piece_to_string(struct game_piece* piece)
{
char* str=(char*)malloc(sizeof(char)*4);
str[0]=' ';
str[1]=' ';
str[2]=' ';
str[3]='\0';
int len=strlen(piece->lable);
printf("%d",len);
int i;
if(len<=3)
{
for(i=0;i<len;i++)
str[i]=piece->lable[i];
}
else
{
for(i=0;i<3;i++)
str[i]=piece->lable[i];
}
return str;
}

void game_board_init(struct game_board* game_board, int rows, int cols)
{
int i,j;
game_board->rows=rows;
game_board->columns=cols;
for(i=0;i<rows;i++)
{
for( j=0;j<cols;j++)
{
game_piece_init_default(game_board->board[i][j]);

}

}
}

int game_board_is_space_valid(struct game_board* game_board, int row, int col)
{
if(row<0 || col<0 || row>game_board->rows || col> game_board->columns)
return 0;
return 1;
}

int game_board_add_piece(struct game_board* game_board, struct game_piece* piece, int row, int col)
{
if(game_board_is_space_valid(game_board,row,col) && strcasecmp( game_board->board[row][col]->lable,"---")==1)
{
game_board->board[row][col]=piece;
return 1;
}
return 0;

}

int game_board_move_piece(struct game_board* game_board, int src_row, int src_col, int dest_row, int dest_col)
{
if(game_board_is_space_valid(game_board,src_row,src_col) && game_board_is_space_valid(game_board,dest_row,dest_col)&& strcasecmp( game_board->board[dest_row][dest_col]->lable,"---")==0)
{
struct game_piece* temp=game_board->board[dest_row][dest_col];
game_board->board[dest_row][dest_col]=game_board->board[src_row][src_col];
game_board->board[src_row][src_col]=temp;
return 1;
}
return 0;
}

void game_board_print(struct game_board* game_board)
{
int i,j;
printf("The GameBoard\n");
printf("------------------------------------------------\n");
for(i=0;i<game_board->rows;i++)
{
for( j=0;j<game_board->columns;j++)
{
printf("%s ",game_piece_to_string(game_board->board[i][j]));

}
printf("\n");
}
}

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.\n");

scanf("%d", &rowNum);

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

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.\n");

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.\n");

scanf("%d", &row);

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

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.\n", game_piece_get_label(&piece));
}
else
{
printf("A piece is already at that space.\n");
}
}
else
{
printf("Invalid row and/or column.\n");
}

/* 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.\n");

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.\n");
}
else
{
printf("A piece is already in that space.\n");

}

}

else

{

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

}

/* 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.\n");

scanf("%s", input_string);

}
return 0;

}

Add a comment
Know the answer?
Add Answer to:
#include <stdio.h> #include <stdlib.h> #include <string.h> struct game_piece { ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IrmaMoves.h : #include "IrmaMoves.h" typedef struct Move { Irma irma;            // an instance of Irma L...

    IrmaMoves.h : #include "IrmaMoves.h" typedef struct Move { Irma irma;            // an instance of Irma L Location from_loc; // location where Irma is moving from Location current_loc; // location where Irma is passing over Location to_loc; // location where Irma is moving to } Move; typedef struct Location { char col; // the square's column ('a' through 'h') int row; // the square's row (0 through 7) } Location; typedef struct Irma { int ws; // wind speed (MPH) int...

  • Can someone tell me why this is not printing in the screen. I know i commented...

    Can someone tell me why this is not printing in the screen. I know i commented out the first couple functions but the last one is not working. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- #include <stdio.h> #include <stdlib.h> #include <time.h> #define ROWS 5 #define COLS 5 #define FREE 2 #define SCALE 15 #define SHIFT 1 #define MAXVAL 75 #define FALSE 0 #define TRUE 1 void welcomeScreen(); void clearScreen(); void displayExplicitCard(); void displayCard(); void displayRandomCard(); void fillCardRand(); void setValue(); void displayBingoCard(); void initializeArrays (); int main...

  • Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner...

    Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner method, it declares a winner for horizontal wins, but not for vertical. if anyone can see what im doing wrong. public class ConnectFour { /** Number of columns on the board. */ public static final int COLUMNS = 7; /** Number of rows on the board. */ public static final int ROWS = 6; /** Character for computer player's pieces */ public static final...

  • C program help 2. For the following code: #include "stdafx.h" struct State char out struct State...

    C program help 2. For the following code: #include "stdafx.h" struct State char out struct State *next[2]; b; typedef struct State States; #define STe &FSM[8] #define ST1 &FSM[1] #define ST2 &FSM[2] States FSM[3] { {"L', {STe, ST1)), {'1', {STe, sT2)), {ST1, = {.2', ST2))); int main() States "ptr &FSM[e]; int in; while (1) printf("cIn", ptr->out); printf("Enter a e or 1 to operate this machine: "); scanf s("Xd", &in); ptr ptr->next[in]; return e; Draw the finite state machine diagram for this...

  • c++ help please! Create a 2D character array in your main function and use nested for...

    c++ help please! Create a 2D character array in your main function and use nested for loops to fill the array with the letter ‘e’ to represent empty spaces. Create a function to print the board on the screen using a nested for loop. The function header is: void printBoard (char board [][3]) Create a function that checks whether a particular space has already been filled. If the space is filled it returns a boolean value of true, otherwise false....

  • This is my code for my game called Reversi, I need to you to make the...

    This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...

  • I need a basic program in C to modify my program with the following instructions: Create...

    I need a basic program in C to modify my program with the following instructions: Create a program in C that will: Add an option 4 to your menu for "Play Bingo" -read in a bingo call (e,g, B6, I17, G57, G65) -checks to see if the bingo call read in is valid (i.e., G65 is not valid) -marks all the boards that have the bingo call -checks to see if there is a winner, for our purposes winning means...

  • Can somebody help me with this coding the program allow 2 players play tic Tac Toe....

    Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...

  • Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game....

    Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game. The objective of this project is to demonstrate your understanding of various programming concepts including Object Oriented Programming (OOP) and design. Tic Tac Toe is a two player game. In your implementation one opponent will be a human player and the other a computer player. ? The game is played on a 3 x 3 game board. ? The first player is known as...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT