Question

First step is to draw a structure chart to help you understand the decomposition of functions...

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!):

*       Create a function welcome_screen() that displays an initial program welcome message along with the rules of Battleship.

*       Create a function initialize_game_board() that sets each cell in a game board to '-'.

*       Create a function select_who_starts_first() that determines if Player1 or Player2 goes first in the game.

*       Create a function manually_place_ships_on_board() that allows the user to place each of the 5 types of ships on his/her game board.

*       Create a function randomly_place_ships_on_board() that randomly places the 5 types of ships on a given board.

*       Create a function check_shot() that determines if the shot taken was a hit or a miss.

*       Create a function is_winner() that determines if a winner exists.

*       Create a function update_board() that updates the board every time a shot is taken. '*' indicates a hit and 'm' indicates a miss.

*       Create a function display_board() that displays a board to the screen. Note that Player1's board should be displayed differently than Player2's board (see above).

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.

*       Create a function output_current_move() that writes the position of the shot taken by the current player to the log file. It also writes whether or not it was a hit, miss, and if the ship was sunk.

*       Create a function check_if_sunk_ship() that determines if a ship was sunk.

*       Create a function output_stats() that writes the statistics collected on each player to the log file.

*       Other functions that you think are necessary!

*       A main function that does the following:

Opens an output file battleship.log for writing;

Simulates the game of Battleship

Outputs data to logfile

Outputs stats to logfile

Closes logfile

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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 = '\0', type = '\0';
   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("\t **** Enemy's Board ****\n");
   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("\n\t   **** Your Board ****\n");
   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("\t **** Enemy's Board ****\n");
               initialize_game_board(x1, y1, x2, y2, player2, 1, 2, xtarget, ytarget, &player1_hits, visible, initial);
               printf("Hits: %d\tShots: %d\n", player2_hits, shots2);
               printf("\n\t   **** Your Board ****\n");
               initialize_game_board(x1, y1, x2, y2, player1, 1, 1, xtarget, ytarget, &player2_hits, visible, initial);
               printf("Hits: %d\tShots: %d\n", player1_hits, shots1);
               //Prints Boards

               shoot_manual(&xtarget, &ytarget, player2);
               shots1++;
               system("cls");
               printf("\t **** Enemy's Board ****\n");
               player2[10][10] = initialize_game_board(x1, y1, x2, y2, player2, 1, 2, xtarget, ytarget, &player1_hits, visible, initial);
               printf("Hits: %d\tShots: %d   Target(%d,%d)", player2_hits, shots2, ytarget, xtarget);
               if(player2[xtarget][ytarget] == '*')
               {
                   printf("\tHit!\n");
                   hit = 1;
               }
               if(player2[xtarget][ytarget] == 'm')
               {
                   printf("\tMiss...\n");
                   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("\n\t   **** Your Board ****\n");
               initialize_game_board(x1, y1, x2, y2, player1, 1, 1, xtarget, ytarget, &player2_hits, visible, initial);
               printf("Hits: %d\tShots: %d\n", player1_hits, shots1);
               system("Pause");
               system("cls");
               //Player1 shot
               if(player1_hits < 17)
               {
               printf("\t **** Enemy's Board ****\n");
               xtarget = -1, ytarget = -1;
               initialize_game_board(x1, y1, x2, y2, player2, 1, 2, xtarget, ytarget, &player1_hits, visible, initial);
               printf("Hits: %d\tShots: %d\n", player2_hits, shots2);
               printf("\n\t   **** Your Board ****\n");
               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\tShots: %d   Target: (%d,%d)", player1_hits, shots1, ytarget, xtarget);
               if(player1[xtarget][ytarget] == '*')
               {
                   printf("\tHit!\n");
                   hit = 1;
               }
               if(player1[xtarget][ytarget] == 'm')
               {
                   printf("\tMiss...\n");
                   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("\t **** Enemy's Board ****\n");
               initialize_game_board(x1, y1, x2, y2, player2, 1, 2, xtarget, ytarget, &player1_hits, visible, initial);
               printf("Hits: %d\tShots: %d\n", player2_hits, shots2);
               printf("\n\t   **** Your Board ****\n");
               initialize_game_board(x1, y1, x2, y2, player1, 1, 1, xtarget, ytarget, &player2_hits, visible, initial);
               printf("Hits: %d\tScore: %d\n", player1_hits, shots1);
               system("cls");
               //Prints Boards

               printf("\t **** Enemy's Board ****\n");
               initialize_game_board(x1, y1, x2, y2, player2, 1, 2, xtarget, ytarget, &player1_hits, visible, initial);
               printf("Hits: %d\tShots: %d\n", player2_hits, shots2);
               printf("\n\t   **** Your Board ****\n");
               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\tShots: %d   Target: (%d,%d)", player1_hits, shots1, ytarget, xtarget);
               if(player1[xtarget][ytarget] == '*')
               {
                   printf("\tHit!\n");
                   hit = 1;
               }
               if(player1[xtarget][ytarget] == 'm')
               {
                   printf("\tMiss...\n");
                   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("\t **** Enemy's Board ****\n");
               player2[10][10] = initialize_game_board(x1, y1, x2, y2, player2, 1, 2, xtarget, ytarget, &player1_hits, visible, initial);
               printf("Hits: %d\tShots: %d   Target: (%d,%d)", player2_hits, shots2, ytarget, xtarget);
               if(player2[xtarget][ytarget] == '*')
               {
                   printf("\tHit!\n");
                   hit = 1;
               }
               if(player2[xtarget][ytarget] == 'm')
               {
                   printf("\tMiss...\n");
                   hit = 0;
               }
               check_ship(player2, &sunkc, &sunkb, &sunkr, &sunks, &sunkd);
               printf("\n\t **** Your Board ****\n");
               initialize_game_board(x1, y1, x2, y2, player1, 1, 1, xtarget, ytarget, &player2_hits, visible, initial);
               printf("Hits: %d\tShots: %d\n", 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("\nPlayer1 Wins!!!\n\n");
       player_1.win = 1;
       player_2.win = 0;
   }
   if(player2_hits == 17)
   {
       printf("\nPlayer2 Wins!!!\n\n");
       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***\nHits: %d\nMisses: %d\nShots: %d\nAccuracy: %.2lf%%\n\n", player1_hits, misses1, shots1, accuracy1);
   printf("***Player2 Stats***\nHits: %d\nMisses: %d\nShots: %d\nAccuracy: %.2lf%%\n\n", 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("\n\t\t\tBATTLESHIP!!!\n");
   printf("\n\t\t\tRules of Battleship: \n\n");
   printf("Players place their 'fleet' of 5 ships on their 'ocean', hidden from the\n");
   printf("opponent's view. Taking turns, players call out their 'shots' attempting to\n");
   printf("get 'hits' on the opponent's ships in order to sink them. Strategy and some\n");
   printf("luck must be combined to be the first to locate and sink all 5 opponent's\n");
   printf("ships to win the game. The object of the game is to be the first player to\n");
   printf("sink all five of his opponent's ships. Each player SECRETLY places his fleet\n");
   printf("of 5 ships on his ocean grid. Ships may be placed in any horizontal or vertical\n");
   printf("position - but NOT diagonally. You MAY NOT change the position of any ship.\n");
   printf("To do so would be cheating!\n\n");
   printf("This is a one player game where you are player1, and the computer is player2.\n\n");
   printf("Types of Ships: \tSize:\n\n");
   printf("Aircraft Carrier \t5 \nBattleship \t\t4 \nCruiser \t\t3 \nSubmarine \t\t3 \nDestroyer \t\t2\n\n");
   printf("Symbols: \n c - Aircraft Carrier\n b - Battleship\n r - Cruiser\n s - Submarine\n d - Destroyer\n");
   printf("\n ~ - Water\n * - Hit\n m - Miss\n");
}
/ 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("\n\t    0 1 2 3 4 5 6 7 8 9\n");
       printf("\t +---------------------+\n\t");
   }
   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\n\t", x);
           }
       }
   }
   if(print != 0)
   {
       printf(" +---------------------+\n");
       printf("\t    0 1 2 3 4 5 6 7 8 9\n\n");
   }
   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. . .\n\n");
       player = 1;
   }
   if(select == 1)
   {
       printf("Player2 (CPU) goes first. . .\n\n");
       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?\n1) Manually\n2) Randomly\n\n");
   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: \n", size);
   scanf("%d %d %d %d", y1, x1, y2, x2);
   if((*x1 != *x2)&&(*y1 != *y2))
   {
       printf("The ship cannot be diagonal!\n");
   }
   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!\n");
   }
   }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!\n");
   }
   }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: \n");
   scanf("%d %d", ytarget, xtarget);
   if((*xtarget > 9)||(*xtarget < 0)||(*ytarget > 9)||(*ytarget < 0))
   {
       printf("Your target coordinates must be from 0-9!\n");
   }
   }while((*xtarget > 9)||(*xtarget < 0)||(*ytarget > 9)||(*ytarget < 0));
   if((board[*xtarget][*ytarget] == '*')||(board[*xtarget][*ytarget] == 'm'))
   {
       printf("You have already shot there! \n");
   }
   }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!\n");
       *sunkc = 1;
   }
   if((b == 0)&&(*sunkb != -1))
   {
       printf("You sunk my Battleship!\n");
       *sunkb = 1;
   }
   if((r == 0)&&(*sunkr != -1))
   {
       printf("You sunk my Cruiser!\n");
       *sunkr = 1;
   }
   if((s == 0)&&(*sunks != -1))
   {
       printf("You sunk my Submarine!\n");
       *sunks = 1;
   }
   if((d == 0)&&(*sunkd != -1))
   {
       printf("You sunk my Destroyer!\n");
       *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)\t", player, xtarget, ytarget);
   }
   if(hit == 1)
   {
       fprintf(outfile, "Hit!\n");
   }
   if(hit == 0)
   {
       fprintf(outfile, "Miss...\n");
   }
   if(*sunkc == 1)
   {
       fprintf(outfile, "Sunk Aircraft Carrier!\n");
       *sunkc = -1;
   }
   if(*sunkb == 1)
   {
       fprintf(outfile, "Sunk Battleship!\n");
       *sunkb = -1;
   }
   if(*sunkr == 1)
   {
       fprintf(outfile, "Sunk Cruiser!\n");
       *sunkr = -1;
   }
   if(*sunks == 1)
   {
       fprintf(outfile, "Sunk Submarine!\n");
       *sunks = -1;
   }
   if(*sunkd == 1)
   {
       fprintf(outfile, "Sunk Destroyer!\n");
       *sunkd = -1;
   }
   fprintf(outfile, "\n");
}
/**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, "\nPlayer%d Wins!!\n", player.player_num);
   }
   if(player.win == 0)
   {
       fprintf(outfile, "\nPlayer%d Losses... \n", player.player_num);
   }
   fprintf(outfile, "\n***Player%d Stats***\nHits: %d\nMisses: %d\nTotal Shots: %d\nAccuracy: %.2lf%%\n\n",
           player.player_num, player.hits, player.misses, player.shots, player.accuracy);
}

Add a comment
Know the answer?
Add Answer to:
First step is to draw a structure chart to help you understand the decomposition of functions...
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
  • For your Project, you will develop a simple battleship game. Battleship is a guessing game for...

    For your Project, you will develop a simple battleship game. Battleship is a guessing game for two players. It is played on four grids. Two grids (one for each player) are used to mark each players' fleets of ships (including battleships). The locations of the fleet (these first two grids) are concealed from the other player so that they do not know the locations of the opponent’s ships. Players alternate turns by ‘firing torpedoes’ at the other player's ships. The...

  • Imagine we are using a two-dimensional array as the basis for creating the game battleship. In...

    Imagine we are using a two-dimensional array as the basis for creating the game battleship. In the game of battleship a '~' character entry in the array represents ocean, a '#' character represents a place ion the ocean where part of a ship is present, and an 'H' character represents a place in the ocean where part of a ship is present and has been hit by a torpedo. Thus, a ship with all 'H' characters means the ship has...

  • Assignment - Battleship In 1967, Hasbro toys introduced a childrens game named “Battleship”. In the next...

    Assignment - Battleship In 1967, Hasbro toys introduced a childrens game named “Battleship”. In the next two assignments you will be creating a one-player version of the game. The game is extremely simple. Each player arranges a fleet of ships in a grid. The grid is hidden from the opponent. Here is an ASCII representation of a 10x10 grid. The ‘X’s represent ships, the ‘~’s represent empty water. There are three ships in the picture: A vertical ship with a...

  • In C++ program use the new style od C++ not the old one. Simple Battleship You...

    In C++ program use the new style od C++ not the old one. Simple Battleship You will make a game similar to the classic board game Battleship. You will set up a 5 x 5, 2 dimensional array. In that array, you will use a random number generator to select 5 elements that will act as the placeholders for your "battleships". Your user will get 10 guesses to "seek and destroy" the battleships. After their 10 guesses, you will tell...

  • I need help finishing my C++ coding assignment! My teacher has provided a template but I...

    I need help finishing my C++ coding assignment! My teacher has provided a template but I need help finishing it! Details are provided below and instructions are provided in the //YOU: comments of the template... My teacher has assigned a game that is similar to battleship but it is single player with an optional multiplayer AI... In the single player game you place down a destroyer, sub, battleship, and aircraft carrier onto the grid.... Then you attack the ships you...

  • i need this in C# please can any one help me out and write the full...

    i need this in C# please can any one help me out and write the full code start from using system till end i am confused and C# is getting me hard.I saw codes from old posts in Chegg but i need the ful complete code please thanks. Module 4 Programming Assignment – OO Design and implementation (50 points) Our Battleship game needs to store a set of ships. Create a new class called Ships. Ships should have the following...

  • 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;...

  • I haven't code in awhile and would like some help getting back on my feet. Need to code this in C++. Thank you! In...

    I haven't code in awhile and would like some help getting back on my feet. Need to code this in C++. Thank you! In this homework, you will implement a simple version of the game Battleship, in which the player tries to destroy ships hidden on a map using a system of coordinates. In this program, the map will be represented by a 4x4 matrix of integers. A 1 in the matrix represents the presence of a ship. There are...

  • Make sure to include: Ship.java, ShipTester.java, Location.java, LocationTester.java, Grid.java, GridTester.java, Player.java, PlayerTester.java, battleship.java. please do every...

    Make sure to include: Ship.java, ShipTester.java, Location.java, LocationTester.java, Grid.java, GridTester.java, Player.java, PlayerTester.java, battleship.java. please do every part, and include java doc and comments Create a battleship program. Include Javadoc and comments. Grade 12 level coding style using java only. You will need to create a Ship class, Location class, Grid Class, Add a ship to the Grid, Create the Player class, the Battleship class, Add at least one extension. Make sure to include the testers. Starting code/ sample/methods will be...

  • In C++ please.. I only need the game.cpp. thanks. Game. Create a project titled Lab8_Game. Use...

    In C++ please.. I only need the game.cpp. thanks. Game. Create a project titled Lab8_Game. Use battleship.h, battleship.cpp that mentioned below; add game.cpp that contains main() , invokes the game functions declared in battleship.h and implements the Battleship game as described in the introduction. ——————————————- //battleship.h #pragma once // structure definitions and function prototypes // for the battleship assignment // 3/20/2019 #include #include #ifndef BATTLESHIP_H_ #define BATTLESHIP_H_ // // data structures definitions // const int fleetSize = 6; // number...

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