Using The C programming code: Write a program that simulates the game of Battles
ID: 3681926 • Letter: U
Question
Using The C programming code:
Write a program that simulates the game of Battleship. The game will be completely text-based (see Sample Execution). Battleship is a two player Navy game. The objective of the game is to sink all ships in your enemy's fleet. The Player to sink his/her enemy's fleet first wins. Both players' fleets consist of 5 ships that are hidden from the enemy. Each ship may be differentiated by its "size" (besides the Cruiser and Submarine) or number of cells it expands on the game board. The Carrier has 5 cells, Battleship has 4 cells, Cruiser has 3 cells, Submarine has 3 cells, and the Destroyer has 2 cells.
The program should be built such that the user is Player1 and the computer is Player2. Two boards exist within the game. Hint: each board should be implemented as a 2-dimensional array. Each 2-dimensional array should be 10 X 10. One represents Player1's board and one represents Player2's board. At the beginning of the game each Players' game board should be initialized to all '-' indicating that no ships have been placed on either board. Before the game starts, Player1 should have the option to either manually place each of the 5 ships in his/her fleet or to have them randomly placed on the board. If Player1 decides to place the ships manually, then he/she should be prompted to place the Carrier first, Battleship second, Cruiser third, Submarine fourth, and the Destroyer last. Note that ships cannot be placed diagonally on the board, they can only be placed vertically or horizontally. You program must check to see if the user tries to place a ship outside the boundaries of the board or on top of a ship that has already been placed. Each cell on the board that contains part of the ship must be indicated by 'c' for Carrier, 'b' for Battleship, 'r' for Cruiser, 's' for Submarine, or 'd' for Destroyer. For example, if the Carrier was placed then the board should contain 5 'c' s for each cell on the board that has a piece of the Carrier, etc. Once Player1's ships have been placed, Player2's ships must be randomly placed. Note that the placement of Player2's ships must be unknown. Thus, Player2's board will only display '-' in each cell after the placement of each ship. The program should randomly select Player1 or Player2 to go first.
Once it has been decided on which player goes first, the game starts. Whenever it's Player1's turn, a prompt should be displayed asking for a position to target (specifying where to "shoot") on the enemy's (Player2's) board (2-dimensional array). The position should be specified in terms of a row and a column on the board. The row and column should always be displayed along with the board. If the position specified happens to hit a ship, then a '*' should replace the '-' on Player2's board. If the positioned specified misses any one of the ships in the fleet, then a 'm' should replace the '-' on Player2's board. Note that from turn-to-turn each player should NOT be allowed to enter the same position. Also, between turns clear the screen (system("cls")). In one turn, a player can only take one shot at the enemy's (other player's) fleet. When Player2 takes a shot at Player1's board, each hit should be specified with a '*' and each miss with a 'm' on Player1's board. The game is over win Player1 or Player2 has sunk all of the ships in the fleet of the enemy.
For each move made by Player1 and Player2, the results should be echoed to a file called "battleship.log". In this file, you should log the targeted position by each player on each move and whether it was a hit on one of the ships in the fleet. Also, if one of the ships happens to sink, then note this in the log file. For more information about the rules of Battleship visit: Rules of Battleship.
At the end of the game, Player1's and Player2's statistics should be written to "battleship.log". The stats include total number of hits, total number of misses, total number of shots, hits to misses ratio (as a percentage), and won or lost the game. Note that the statistics should be placed into a structure called Stats. You need two variables of type Stats, one for Player1 and one for Player2. Once the game has ended you should write the contents of each struct variable to the "battleship.log" file.
Functional Decomposition
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
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.