Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you add an IPO chart to this code about a battleship game?#include #include #include #define GRID _ SIZE 1 0 #define NUM _ SHIPS

Can you add an IPO chart to this code about a battleship game?#include
#include
#include
#define GRID_SIZE 10
#define NUM_SHIPS 5
typedef struct {
char *name;
char symbol;
int size;
int x;
int y;
int direction; //0= horizontal, 1= vertical
int hits;
} Ship;
typedef struct {
char grid[GRID_SIZE][GRID_SIZE];
Ship ships[NUM_SHIPS];
int missiles_fired;
} GameState;
// Function Prototypes
void initialize_game(GameState *game);
void place_ships(GameState *game);
int is_valid_position(GameState *game, Ship ship);
void print_grid(GameState *game);
void fire_missile(GameState *game);
void check_sink(GameState *game);
void save_scores(GameState *game);
void load_scores();
void display_scores();
int main(){
GameState game;
char command;
int game_over =0;
srand(time(NULL)); // Seed random number generator
initialize_game(&game);
place_ships(&game);
while (!game_over){
print_grid(&game);
printf("Missiles fired: %d
", game.missiles_fired);
printf("Enter 'f' to fire a missile, 'q' to quit: ");
scanf("%c", &command);
if (command =='f'){
fire_missile(&game);
game.missiles_fired++;
check_sink(&game);
if (game.missiles_fired >=17){
printf("Reached maximum efficient missile count.
");
}
} else if (command =='q'){
printf("Exiting game.
");
save_scores(&game);
game_over =1;
}
}
display_scores();
return 0;
}
void initialize_game(GameState *game){
for (int i =0; i < GRID_SIZE; i++){
for (int j =0; j < GRID_SIZE; j++){
game->grid[i][j]='.';
}
}
game->ships[0]=(Ship){"Seminole State Ship", 'S',3,0,0,0,0};
game->ships[1]=(Ship){"Air Force Academy", 'A',5,0,0,0,0};
game->ships[2]=(Ship){"Valencia Destroyer", 'V',4,0,0,0,0};
game->ships[3]=(Ship){"Eskimo University", 'E',3,0,0,0,0};
game->ships[4]=(Ship){"Deland High School", 'D',2,0,0,0,0};
game->missiles_fired =0;
}
void place_ships(GameState *game){
for (int i =0; i < NUM_SHIPS; i++){
int placed =0;
while (!placed){
game->ships[i].x = rand()% GRID_SIZE;
game->ships[i].y = rand()% GRID_SIZE;
game->ships[i].direction = rand()%2;
if (is_valid_position(game, game->ships[i])){
for (int j =0; j < game->ships[i].size; j++){
if (game->ships[i].direction ==0){
game->grid[game->ships[i].y][game->ships[i].x + j]= game->ships[i].symbol;
} else {
game->grid[game->ships[i].y + j][game->ships[i].x]= game->ships[i].symbol;
}
}
placed =1;
}
}
}
}
int is_valid_position(GameState *game, Ship ship){
for (int i =0; i < ship.size; i++){
int x = ship.x +(ship.direction ==0? i : 0);
int y = ship.y +(ship.direction ==1? i : 0);
if (x >= GRID_SIZE || y >= GRID_SIZE || game->grid[y][x]!='.'){
return 0;
}
}
return 1;
}
void print_grid(GameState *game){
for (int i =0; i < GRID_SIZE; i++){
for (int j =0; j < GRID_SIZE; j++){
printf("%c ", game->grid[i][j]);
}
printf("
");
}
}
void fire_missile(GameState *game){
int x, y;
printf("Enter coordinates to fire (x y): ");
scanf("%d %d", &x, &y);
if (x <0|| x >= GRID_SIZE || y <0|| y >= GRID_SIZE){
printf("Invalid coordinates.
");
return;
}
if (game->grid[y][x]=='.'){
game->grid[y][x]='M';
} else if (game->grid[y][x]!='M' && game->grid[y][x]!='H'){
game->grid[y][x]='H';
}
}
void check_sink(GameState *game){
for (int k =0; k < NUM_SHIPS; k++){
int hits =0;
for (int i =0; i < game->ships[k].size; i++){
int x = game->ships[k].x +(game->ships[k].direction ==0? i : 0);
int y = game->ships[k].y +(game->ships[k].direction ==1? i : 0);
if (game->grid[y][x]=='H'){
hits++;
}
}
if (hits == game->ships[k].size){
for (int i =0; i < game->ships[k].size; i++){
int x = game->ships[k].x +(game->ships[k].direction ==0? i : 0);
int y

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

Introduction To Data Mining

Authors: Pang Ning Tan, Michael Steinbach, Vipin Kumar

1st Edition

321321367, 978-0321321367

More Books

Students also viewed these Databases questions

Question

Recognize and discuss the causes of culture shock

Answered: 1 week ago