First step is to draw a structure chart to help you understand the decomposition
ID: 3806092 • Letter: F
Question
First step is to draw a structure chart to help you understand the decomposition of functions for this program. Remember to start with the overall problem and break it down into inputs, computations, and outputs. One possible functional decomposition includes the following (Note: you are NOT required to apply these functions in your program!):
Hint: pass in a flag (int) that stores whether or not you just passed in Player1's or Player2's board. Then perform the different logic for Player1's board versus Player2's board.
Opens an output file battleship.log for writing;
Simulates the game of Battleship
Outputs data to logfile
Outputs stats to logfile
Closes logfile
Explanation / Answer
main.c
#include"header.h"
int main(void)
{
int select = 0, x1 = 0, y1 = 0, x2 = 0, y2 = 0, deploy = 0, carrier = 5, battleship = 4, cruiser = 3, submarine = 3, destroyer = 2, x = 0, y = 0,
shots1 = 0, shots2 = 0, status = 0, player = 0, player1_hits = 0, player2_hits = 0, xtarget = -1, ytarget = -1,
sunkc = 0, sunkb = 0, sunkr = 0, sunks = 0, sunkd = 0, misses1 = 0, misses2 = 0, hit = 0, hit2 = 0,
c = 0, b = 0, r = 0, s = 0, d = 0;
stats player_1, player_2;
char player1[10][10], player2[10][10], visible[10][10], carr = 'c', battle = 'b', cruise = 'r', sub = 's', destroy = 'd',
initial = '', type = '';
double accuracy1 = 0.0, accuracy2 = 0.0;
FILE *output_file = NULL;
srand(time(NULL));
output_file = fopen("Battleship.log", "w");
welcome_screen();
system("Pause");
system("cls");
deploy = deploy_ships();
printf(" **** Enemy's Board **** ");
randomly_place_ships_on_board(&x1, &y1, &x2, &y2, carrier, player2);
player2[10][10] = initialize_game_board(x1, y1, x2, y2, player2, 0, 2, xtarget, ytarget, &player2_hits, visible, carr);
randomly_place_ships_on_board(&x1, &y1, &x2, &y2, battleship, player2);
player2[10][10] = initialize_game_board(x1, y1, x2, y2, player2, 0, 2, xtarget, ytarget, &player2_hits, visible, battle);
randomly_place_ships_on_board(&x1, &y1, &x2, &y2, cruiser, player2);
player2[10][10] = initialize_game_board(x1, y1, x2, y2, player2, 0, 2, xtarget, ytarget, &player2_hits, visible, cruise);
randomly_place_ships_on_board(&x1, &y1, &x2, &y2, submarine, player2);
player2[10][10] = initialize_game_board(x1, y1, x2, y2, player2, 0, 2, xtarget, ytarget, &player2_hits, visible, sub);
randomly_place_ships_on_board(&x1, &y1, &x2, &y2, destroyer, player2);
player2[10][10] = initialize_game_board(x1, y1, x2, y2, player2, 1, 2, xtarget, ytarget, &player2_hits, visible, destroy);
//Places Enemy's ships on his board
printf(" **** Your Board **** ");
if(deploy == 1)
{
manually_place_ships_on_board(&x1, &y1, &x2, &y2, carrier, player1);
player1[10][10] = initialize_game_board(x1, y1, x2, y2, player1, 1, 1, xtarget, ytarget, &player1_hits, visible, carr);
manually_place_ships_on_board(&x1, &y1, &x2, &y2, battleship, player1);
player1[10][10] = initialize_game_board(x1, y1, x2, y2, player1, 1, 1, xtarget, ytarget, &player1_hits, visible, battle);
manually_place_ships_on_board(&x1, &y1, &x2, &y2, cruiser, player1);
player1[10][10] = initialize_game_board(x1, y1, x2, y2, player1, 1, 1, xtarget, ytarget, &player1_hits, visible, cruise);
manually_place_ships_on_board(&x1, &y1, &x2, &y2, submarine, player1);
player1[10][10] = initialize_game_board(x1, y1, x2, y2, player1, 1, 1, xtarget, ytarget, &player1_hits, visible, sub);
manually_place_ships_on_board(&x1, &y1, &x2, &y2, destroyer, player1);
player1[10][10] = initialize_game_board(x1, y1, x2, y2, player1, 1, 1, xtarget, ytarget, &player1_hits, visible, destroy);
}//Manually places your ships on your board
if(deploy == 2)
{
randomly_place_ships_on_board(&x1, &y1, &x2, &y2, carrier, player1);
player1[10][10] = initialize_game_board(x1, y1, x2, y2, player1, 0, 1, xtarget, ytarget, &player1_hits, visible, carr);
randomly_place_ships_on_board(&x1, &y1, &x2, &y2, battleship, player1);
player1[10][10] = initialize_game_board(x1, y1, x2, y2, player1, 0, 1, xtarget, ytarget, &player1_hits, visible, battle);
randomly_place_ships_on_board(&x1, &y1, &x2, &y2, cruiser, player1);
player1[10][10] = initialize_game_board(x1, y1, x2, y2, player1, 0, 1, xtarget, ytarget, &player1_hits, visible, cruise);
randomly_place_ships_on_board(&x1, &y1, &x2, &y2, submarine, player1);
player1[10][10] = initialize_game_board(x1, y1, x2, y2, player1, 0, 1, xtarget, ytarget, &player1_hits, visible, sub);
randomly_place_ships_on_board(&x1, &y1, &x2, &y2, destroyer, player1);
player1[10][10] = initialize_game_board(x1, y1, x2, y2, player1, 1, 1, xtarget, ytarget, &player1_hits, visible, destroy);
}//Randomly places your ships on your board
x1 = -1, y1 = -1, x2 = -1, y2 = -1;
player = select_who_starts_first();
system("Pause");
if(player == 2)
{
shots2 = 1;
}
switch(player)//Runs game based on who goes first
{//Player 1 goes first:
case 1: do{ xtarget = -1, ytarget = -1;
system("cls");
printf(" **** Enemy's Board **** ");
initialize_game_board(x1, y1, x2, y2, player2, 1, 2, xtarget, ytarget, &player1_hits, visible, initial);
printf("Hits: %d Shots: %d ", player2_hits, shots2);
printf(" **** Your Board **** ");
initialize_game_board(x1, y1, x2, y2, player1, 1, 1, xtarget, ytarget, &player2_hits, visible, initial);
printf("Hits: %d Shots: %d ", player1_hits, shots1);
//Prints Boards
shoot_manual(&xtarget, &ytarget, player2);
shots1++;
system("cls");
printf(" **** Enemy's Board **** ");
player2[10][10] = initialize_game_board(x1, y1, x2, y2, player2, 1, 2, xtarget, ytarget, &player1_hits, visible, initial);
printf("Hits: %d Shots: %d Target(%d,%d)", player2_hits, shots2, ytarget, xtarget);
if(player2[xtarget][ytarget] == '*')
{
printf(" Hit! ");
hit = 1;
}
if(player2[xtarget][ytarget] == 'm')
{
printf(" Miss... ");
hit = 0;
}
check_ship(player2, &sunkc, &sunkb, &sunkr, &sunks, &sunkd);
output_move(output_file, xtarget, ytarget, 1, hit, &sunkc, &sunkb, &sunkr, &sunks, &sunkd);
xtarget = -1, ytarget = -1;
printf(" **** Your Board **** ");
initialize_game_board(x1, y1, x2, y2, player1, 1, 1, xtarget, ytarget, &player2_hits, visible, initial);
printf("Hits: %d Shots: %d ", player1_hits, shots1);
system("Pause");
system("cls");
//Player1 shot
if(player1_hits < 17)
{
printf(" **** Enemy's Board **** ");
xtarget = -1, ytarget = -1;
initialize_game_board(x1, y1, x2, y2, player2, 1, 2, xtarget, ytarget, &player1_hits, visible, initial);
printf("Hits: %d Shots: %d ", player2_hits, shots2);
printf(" **** Your Board **** ");
shoot_random(&xtarget, &ytarget, player1);
shots2++;
player1[10][10] = initialize_game_board(x1, y1, x2, y2, player1, 1, 1, xtarget, ytarget, &player2_hits, visible, initial);
printf("Hits: %d Shots: %d Target: (%d,%d)", player1_hits, shots1, ytarget, xtarget);
if(player1[xtarget][ytarget] == '*')
{
printf(" Hit! ");
hit = 1;
}
if(player1[xtarget][ytarget] == 'm')
{
printf(" Miss... ");
hit = 0;
}
check_ship(player1, &sunkc, &sunkb, &sunkr, &sunks, &sunkd);
output_move(output_file, xtarget, ytarget, 2, hit, &sunkc, &sunkb, &sunkr, &sunks, &sunkd);
}
system("Pause");
system("cls");
//Player2 shot
}while((player1_hits < 17)&&(player2_hits < 17));
break;
//Player 2 goes first:
case 2: do{ xtarget = -1, ytarget = -1;
system("cls");
printf(" **** Enemy's Board **** ");
initialize_game_board(x1, y1, x2, y2, player2, 1, 2, xtarget, ytarget, &player1_hits, visible, initial);
printf("Hits: %d Shots: %d ", player2_hits, shots2);
printf(" **** Your Board **** ");
initialize_game_board(x1, y1, x2, y2, player1, 1, 1, xtarget, ytarget, &player2_hits, visible, initial);
printf("Hits: %d Score: %d ", player1_hits, shots1);
system("cls");
//Prints Boards
printf(" **** Enemy's Board **** ");
initialize_game_board(x1, y1, x2, y2, player2, 1, 2, xtarget, ytarget, &player1_hits, visible, initial);
printf("Hits: %d Shots: %d ", player2_hits, shots2);
printf(" **** Your Board **** ");
shoot_random(&xtarget, &ytarget, player1);
player1[10][10] = initialize_game_board(x1, y1, x2, y2, player1, 1, 1, xtarget, ytarget, &player2_hits, visible, initial);
printf("Hits: %d Shots: %d Target: (%d,%d)", player1_hits, shots1, ytarget, xtarget);
if(player1[xtarget][ytarget] == '*')
{
printf(" Hit! ");
hit = 1;
}
if(player1[xtarget][ytarget] == 'm')
{
printf(" Miss... ");
hit = 0;
}
check_ship(player1, &sunkc, &sunkb, &sunkr, &sunks, &sunkd);
output_move(output_file, xtarget, ytarget, 2, hit, &sunkc, &sunkb, &sunkr, &sunks, &sunkd);
//Player2 shot
if(player2_hits < 17)
{
shoot_manual(&xtarget, &ytarget, player2);
shots1++;
system("cls");
printf(" **** Enemy's Board **** ");
player2[10][10] = initialize_game_board(x1, y1, x2, y2, player2, 1, 2, xtarget, ytarget, &player1_hits, visible, initial);
printf("Hits: %d Shots: %d Target: (%d,%d)", player2_hits, shots2, ytarget, xtarget);
if(player2[xtarget][ytarget] == '*')
{
printf(" Hit! ");
hit = 1;
}
if(player2[xtarget][ytarget] == 'm')
{
printf(" Miss... ");
hit = 0;
}
check_ship(player2, &sunkc, &sunkb, &sunkr, &sunks, &sunkd);
printf(" **** Your Board **** ");
initialize_game_board(x1, y1, x2, y2, player1, 1, 1, xtarget, ytarget, &player2_hits, visible, initial);
printf("Hits: %d Shots: %d ", player1_hits, shots1);
output_move(output_file, xtarget, ytarget, 1, hit, &sunkc, &sunkb, &sunkr, &sunks, &sunkd);
}
shots2++;
system("Pause");
system("cls");
//Player1 shot
}while((player1_hits < 17)&&(player2_hits < 17));
break;
}
if(player1_hits == 17)
{
printf(" Player1 Wins!!! ");
player_1.win = 1;
player_2.win = 0;
}
if(player2_hits == 17)
{
printf(" Player2 Wins!!! ");
player_1.win = 0;
player_2.win = 1;
}
misses1 = shots1 - player1_hits;
misses2 = shots2 - player2_hits;
accuracy1 = 100 * ((double)player1_hits)/((double)shots1);
accuracy2 = 100 * ((double)player2_hits)/((double)shots2);
player_1.player_num = 1;
player_1.hits = player1_hits;
player_1.misses = misses1;
player_1.shots = shots1;
player_1.accuracy = accuracy1;
player_2.player_num = 2;
player_2.hits = player2_hits;
player_2.misses = misses2;
player_2.shots = shots2;
player_2.accuracy = accuracy2;
output_stats(output_file, player_1);
output_stats(output_file, player_2);
printf("***Player1 Stats*** Hits: %d Misses: %d Shots: %d Accuracy: %.2lf%% ", player1_hits, misses1, shots1, accuracy1);
printf("***Player2 Stats*** Hits: %d Misses: %d Shots: %d Accuracy: %.2lf%% ", player2_hits, misses2, shots2, accuracy2);
//Prints stats to screen and to file
fclose(output_file);
return 0;
}
header.c
#ifndef header_h
#define header_h
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
typedef struct
{
int player_num;
int hits;
int misses;
int shots;
double accuracy;
int win;
} stats;
//Funtion Prototypes: (each function description is in 'function.c')
void welcome_screen(void);
char initialize_game_board(int x1, int y1, int x2, int y2, char board[10][10], int print, int player,
int xtarget, int ytarget, int *hits, char visible[10][10], char type);
int select_who_starts_first(void);
int deploy_ships(void);
void manually_place_ships_on_board(int *x1, int *y1, int *x2, int *y2, int size, char board[10][10]);
void randomly_place_ships_on_board(int *x1, int *y1, int *x2, int *y2, int size, char board[10][10]);
void shoot_manual(int *xtarget, int *ytarget, char board[10][10]);
void shoot_random(int *xtarget, int *ytarget, char board[10][10]);
int check_ship(char board[10][10], int *sunkc, int *sunkb, int *sunkr, int *sunks, int *sunkd);
void output_move(FILE *outfile, int xtarget, int ytarget, int player, int hit, int *sunkc, int *sunkb, int *sunkr, int *sunks, int *sunkd);
void output_stats(FILE *outfile, stats player);
#endif
function.c
#include"header.h"
void welcome_screen(void)
{
printf(" BATTLESHIP!!! ");
printf(" Rules of Battleship: ");
printf("Players place their 'fleet' of 5 ships on their 'ocean', hidden from the ");
printf("opponent's view. Taking turns, players call out their 'shots' attempting to ");
printf("get 'hits' on the opponent's ships in order to sink them. Strategy and some ");
printf("luck must be combined to be the first to locate and sink all 5 opponent's ");
printf("ships to win the game. The object of the game is to be the first player to ");
printf("sink all five of his opponent's ships. Each player SECRETLY places his fleet ");
printf("of 5 ships on his ocean grid. Ships may be placed in any horizontal or vertical ");
printf("position - but NOT diagonally. You MAY NOT change the position of any ship. ");
printf("To do so would be cheating! ");
printf("This is a one player game where you are player1, and the computer is player2. ");
printf("Types of Ships: Size: ");
printf("Aircraft Carrier 5 Battleship 4 Cruiser 3 Submarine 3 Destroyer 2 ");
printf("Symbols: c - Aircraft Carrier b - Battleship r - Cruiser s - Submarine d - Destroyer ");
printf(" ~ - Water * - Hit m - Miss ");
}
/ Description: This function uses a two-dimensional array *
* to intitialize the game board, place ships on the*
* board, and update the board after each shot. *
/
char initialize_game_board(int x1, int y1, int x2, int y2, char board[10][10], int print, int player,
int xtarget, int ytarget, int *hits, char visible[10][10], char type)
{
int x = 0, y = 0;
if(print != 0)
{
printf(" 0 1 2 3 4 5 6 7 8 9 ");
printf(" +---------------------+ ");
}
for(x = 0; x < 10; x++)
{
if((x < 9)&&(print != 0))
{
printf("%d | ", x);
}
if((x == 9)&&(print != 0))
{
printf("%d | ", x);
}
for(y = 0; y < 10; y++)
{
if((board[x][y] != 'c')&&(board[x][y] != 'b')&&(board[x][y] != 'r')&&(board[x][y] != 's')&&(board[x][y] != 'd')
&&(board[x][y] != '*')&&(board[x][y] != 'm'))
{
board[x][y] = '~';
if((((x == x1)&&(y == y1))||((x == x2)&&(y == y2)))&&(print != -1))
{
board[x][y] = type;
}
if((((x < x1)&&(x > x2)&&(y == y1))||((x > x1)&&(x < x2)&&(y == y1)))&&(print != -1))
{
board[x][y] = type;
}
if((((y < y1)&&(y > y2)&&(x == x1))||((y > y1)&&(y < y2)&&(x == x1)))&&(print != -1))
{
board[x][y] = type;
}
}
if(board[x][y] == '~')
{
visible[x][y] = '~';
}
if((board[x][y] >= 'b')&&(board[x][y] <= 's')&&(board[x][y] != 'm'))
{
visible[x][y] = '~';
}
if(board[x][y] == 'm')
{
visible[x][y] = 'm';
}
if(board[x][y] == '*')
{
visible[x][y] = '*';
}
if((x == xtarget)&&(y == ytarget))
{
if((board[x][y] >= 'b')&&(board[x][y] <= 's'))
{
visible[x][y] = '*';
board[x][y] = '*';
*hits = *hits + 1;
}
if(board[x][y] == '~')
{
visible[x][y] = 'm';
board[x][y] = 'm';
}
}
//player = 1; //Uncomment this to see the enemy's ships!!!
if((print != 0)&&(player == 1))
{
printf("%c ", board[x][y]);
}
if((print != 0)&&(player == 2))
{
printf("%c ", visible[x][y]);
}
if((y == 9)&&(print != 0))
{
printf("| %d ", x);
}
}
}
if(print != 0)
{
printf(" +---------------------+ ");
printf(" 0 1 2 3 4 5 6 7 8 9 ");
}
return board;
}
/**Description: This function decides and returns which *
* player goes first. */
int select_who_starts_first(void)
{
int select = 0, player = 0;
select = rand() % 2;
if(select == 0)
{
printf("Player1 (Human) goes first. . . ");
player = 1;
}
if(select == 1)
{
printf("Player2 (CPU) goes first. . . ");
player = 2;
}
return player;
}
/**Description: This function prompts the user to decide *
* whether to place their ships randomly or manually*/
int deploy_ships(void)
{
int deploy = 0;
printf("How would you like to deploy your ships? 1) Manually 2) Randomly ");
scanf("%d", &deploy);
return deploy;
}
/* manually_place_ships_on_board() *
* Description: This function prompts the user to enter the *
* endpoint coordinates for each ship. */
void manually_place_ships_on_board(int *x1, int *y1, int *x2, int *y2, int size, char board[10][10])
{
int check = 0, x_1 = 0, y_1 = 0, x_2 = 0, y_2 = 0, x_3 = 0, y_3 = 0, x = 0, y = 0;
do{
do{
check = 0; x_1 = 0, y_1 = 0, x_2 = 0, y_2 = 0, x_3 = 0, y_3 = 0, *x1 = 0, *y1 = 0, *x2 = 0, *y2 = 0;
printf("Enter the endpoint coordinates for your Ship, size: %d: ", size);
scanf("%d %d %d %d", y1, x1, y2, x2);
if((*x1 != *x2)&&(*y1 != *y2))
{
printf("The ship cannot be diagonal! ");
}
if(*y1 == *y2)
{
check = abs(*x1 - *x2) + 1;
}
if(*x1 == *x2)
{
check = abs(*y1 - *y2) + 1;
}
if(check != size)
{
printf("The ship does not fit those coordinates! ");
}
}while(((*x1 < 0)||(*x1 > 9))||((*y1 < 0)&&(*y1 > 9))||((*x2 < 0)||(*x2 > 9))||((*y2 < 0)&&(*y2 > 9))||
((*x1 != *x2)&&(*y1 != *y2))||(check != size));
if(*y1 == *y2)
{
if(*x1 > *x2)
{
if(size > 2)
{
x_1 = *x2 + 1;
y_1 = *y1;
}
if(size > 3)
{
x_2 = *x2 + 2;
y_2 = *y1;
}
if(size > 4)
{
x_3 = *x2 + 3;
y_3 = *y1;
}
}
if(*x1 < *x2)
{
if(size > 2)
{
x_1 = *x1 + 1;
y_1 = *y1;
}
if(size > 3)
{
x_2 = *x1 + 2;
y_2 = *y1;
}
if(size > 4)
{
x_3 = *x1 + 3;
y_3 = *y1;
}
}
}
if(*x1 == *x2)
{
if(*y1 < *y2)
{
if(size > 2)
{
x_1 = *x1;
y_1 = *y1 + 1;
}
if(size > 3)
{
x_2 = *x1;
y_2 = *y1 + 2;
}
if(size > 4)
{
x_3 = *x1;
y_3 = *y1 + 3;
}
}
if(*y1 > *y2)
{
if(size > 2)
{
x_1 = *x1;
y_1 = *y2 + 1;
}
if(size > 3)
{
x_2 = *x1;
y_2 = *y2 + 2;
}
if(size > 4)
{
x_3 = *x1;
y_3 = *y2 + 3;
}
}
}
if((((board[*x1][*y1] > 'a')&&(board[*x1][*y1] < 'z'))||((board[*x2][*y2] > 'a')&&(board[*x2][*y2] < 'z'))||(((board[x_1][y_1] > 'a')&&(board[x_1][y_1] < 'z'))&&(size > 2))||
(((board[x_2][y_2] > 'a')&&(board[x_2][y_2] < 'z'))&&(size > 3))||(((board[x_3][y_3] > 'a')&&(board[x_3][y_3] < 'z')&&(size > 4)))))
{
printf("Your ships cannot overlap! ");
}
}while((((board[*x1][*y1] > 'a')&&(board[*x1][*y1] < 'z'))||((board[*x2][*y2] > 'a')&&(board[*x2][*y2] < 'z'))||(((board[x_1][y_1] > 'a')&&(board[x_1][y_1] < 'z'))&&(size > 2))||
(((board[x_2][y_2] > 'a')&&(board[x_2][y_2] < 'z'))&&(size > 3))||(((board[x_3][y_3] > 'a')&&(board[x_3][y_3] < 'z')&&(size > 4)))));
}
/* Description: This function randomly generates the *
* endpoint coordinates (x1, y1, x2, y2) for each *
* ship. */
void randomly_place_ships_on_board(int *x1, int *y1, int *x2, int *y2, int size, char board[10][10])
{
int way = 0, x_1 = 0, y_1 = 0, x_2 = 0, y_2 = 0, x_3 = 0, y_3 = 0;
do{
*x1 = rand() % 10;
*y1 = rand() % 10;
way = rand() % 2;
if(way == 0)
{
*x2 = *x1 - size + 1;
*y2 = *y1;
if(size > 2)
{
x_1 = *x2 + 1;
y_1 = *y1;
}
if(size > 3)
{
x_2 = *x2 + 2;
y_2 = *y1;
}
if(size > 4)
{
x_3 = *x2 + 3;
y_3 = *y1;
}
}
if(way == 1)
{
*y2 = *y1 - size + 1;
*x2 = *x1;
if(size > 2)
{
x_1 = *x1;
y_1 = *y2 + 1;
}
if(size > 3)
{
x_2 = *x1;
y_2 = *y2 + 2;
}
if(size > 4)
{
x_3 = *x1;
y_3 = *y2 + 3;
}
}
}while((*x2 < 1)||(*y2 < 1)||(*x2 > 9)||(*y2 > 9)||((board[*x1][*y1] > 'a')&&(board[*x1][*y1] < 'z'))||
((board[*x2][*y2] > 'a')&&(board[*x2][*y2] < 'z'))||((board[x_1][y_1] > 'a')&&(board[x_1][y_1] < 'z'))||
((board[x_2][y_2] > 'a')&&(board[x_2][y_2] < 'z'))||((board[x_3][y_3] > 'a')&&(board[x_3][y_3] < 'z')));
}
/*Description: This function prompts the user to enter the *
* target coordinates (xtarget, ytarget) */
void shoot_manual(int *xtarget, int *ytarget, char board[10][10])
{
do{
do{
printf("Enter target coordinates: ");
scanf("%d %d", ytarget, xtarget);
if((*xtarget > 9)||(*xtarget < 0)||(*ytarget > 9)||(*ytarget < 0))
{
printf("Your target coordinates must be from 0-9! ");
}
}while((*xtarget > 9)||(*xtarget < 0)||(*ytarget > 9)||(*ytarget < 0));
if((board[*xtarget][*ytarget] == '*')||(board[*xtarget][*ytarget] == 'm'))
{
printf("You have already shot there! ");
}
}while((board[*xtarget][*ytarget] == '*')||(board[*xtarget][*ytarget] == 'm'));
}
void shoot_random(int *xtarget, int *ytarget, char board[10][10])
{
do{
*xtarget = rand() % 10;
*ytarget = rand() % 10;
}while((board[*xtarget][*ytarget] == '*')||(board[*xtarget][*ytarget] == 'm'));
}
int check_ship(char board[10][10], int *sunkc, int *sunkb, int *sunkr, int *sunks, int *sunkd)
{
int x = 0, y = 0, c = 0, b = 0, r = 0, s = 0, d = 0;
for(x = 0; x < 10; x++)
{
for(y = 0; y < 10; y++)
{
if((board[x][y] == 'c')&&(*sunkc == 0))
{
c++;
}
if((board[x][y] == 'b')&&(*sunkb == 0))
{
b++;
}
if((board[x][y] == 'r')&&(*sunkr == 0))
{
r++;
}
if((board[x][y] == 's')&&(*sunks == 0))
{
s++;
}
if((board[x][y] == 'd')&&(*sunkd == 0))
{
d++;
}
}
}
if((c == 0)&&(*sunkc != -1))
{
printf("You sunk my Aircraft Carrier! ");
*sunkc = 1;
}
if((b == 0)&&(*sunkb != -1))
{
printf("You sunk my Battleship! ");
*sunkb = 1;
}
if((r == 0)&&(*sunkr != -1))
{
printf("You sunk my Cruiser! ");
*sunkr = 1;
}
if((s == 0)&&(*sunks != -1))
{
printf("You sunk my Submarine! ");
*sunks = 1;
}
if((d == 0)&&(*sunkd != -1))
{
printf("You sunk my Destroyer! ");
*sunkd = 1;
}
}
/*Description: This function prints to file, the info *
* associated with each player's shot: Target Coords*
* hit or miss, if a ship was sunk */
void output_move(FILE *outfile, int xtarget, int ytarget, int player, int hit, int *sunkc, int *sunkb, int *sunkr, int *sunks, int *sunkd)
{
if((xtarget != -1)&&(ytarget != -1))
{
fprintf(outfile, "Player%d: (%d,%d) ", player, xtarget, ytarget);
}
if(hit == 1)
{
fprintf(outfile, "Hit! ");
}
if(hit == 0)
{
fprintf(outfile, "Miss... ");
}
if(*sunkc == 1)
{
fprintf(outfile, "Sunk Aircraft Carrier! ");
*sunkc = -1;
}
if(*sunkb == 1)
{
fprintf(outfile, "Sunk Battleship! ");
*sunkb = -1;
}
if(*sunkr == 1)
{
fprintf(outfile, "Sunk Cruiser! ");
*sunkr = -1;
}
if(*sunks == 1)
{
fprintf(outfile, "Sunk Submarine! ");
*sunks = -1;
}
if(*sunkd == 1)
{
fprintf(outfile, "Sunk Destroyer! ");
*sunkd = -1;
}
fprintf(outfile, " ");
}
/**Description: This function prints to file, the game stats *
* for each player.(Hits, Misses, Shots, Accuracy) */
void output_stats(FILE *outfile, stats player)
{
if(player.win == 1)
{
fprintf(outfile, " Player%d Wins!! ", player.player_num);
}
if(player.win == 0)
{
fprintf(outfile, " Player%d Losses... ", player.player_num);
}
fprintf(outfile, " ***Player%d Stats*** Hits: %d Misses: %d Total Shots: %d Accuracy: %.2lf%% ",
player.player_num, player.hits, player.misses, player.shots, player.accuracy);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.