Code:
/**/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//function declarations
int welcome_screen(void);
void initialize_game_board (char game_board[10][10], int num_rows, int num_cols);
void print_game_board (char game_board[10][10], int num_rows, int num_cols);
int check_shot(char target, int target_row, int target_col);
int is_winner(char game_board[10][10]);
void manually_place_ships_on_board(char *game_boardptr, char ship);
void randomly_place_ships_on_board(char *game_boardptr, char ship, int dir);
void generate_start_pt (int len_ship, int *row_coord, int *col_coord, int dir);
void generate_direction (int *dir);
int print_menu (void);
void get_target (int *target_row, int *target_col);
void rand_target (int *target_row, int *target_col);
void update_board(int hit_or_miss, char *target);
int search_ship(char game_board[10][10], int num_rows, int num_cols, char ship);
/*+++++++++++++++++++++++++++++main++++++++++++++++++++*/
int main (void)//main
{//main
//local declaration and assignments
char game_board_1[][10] = {{'~', '~'}, {'~'}};
char game_board_2[][10] = {{'~', '~'}, {'~'}};
char game_board_target[10][10] = {{'~', '~'}, {'~'}};
int direction = 0, row_coord = 0, col_coord = 0, choice = 0, turn = 0, hit_or_miss = 0, target_row = 0, target_col = 0;
int number_of_ships_1 = 0, number_of_ships_2 = 0;
srand ((unsigned int)time(NULL));//random
choice = welcome_screen();
if (choice == 1)
{//if choice
initialize_game_board(game_board_1, 10, 10);//these are function calls for if statement
initialize_game_board(game_board_2, 10, 10);
initialize_game_board(game_board_target, 10, 10);
choice = print_menu();
if (choice == 1)
{//if
print_game_board(game_board_1, 10, 10);
system("pause");//in order to update board
system("cls");
manually_place_ships_on_board(*game_boardptr, 'c');
print_game_board(game_board_1, 10, 10);
system("pause");
system("cls");
manually_place_ships_on_board(game_board_1, 'b');
print_game_board(game_board_1, 10, 10);
system("pause");
system("cls");
manually_place_ships_on_board(game_board_1, 'r');
print_game_board(game_board_1, 10, 10);
system("pause");
system("cls");
manually_place_ships_on_board(game_board_1, 's');.
print_game_board(game_board_1, 10, 10);
system("pause");
system("cls");
manually_place_ships_on_board(game_board_1, 'd');
}//if choice 1
else if (choice == 2)
{
generate_direction(&direction);//if play is computer, ie randomly placed fleet
randomly_place_ships_on_board(game_board_1, 'c', direction);
generate_direction(&direction);
randomly_place_ships_on_board(game_board_1, 'b', direction);
generate_direction(&direction);
randomly_place_ships_on_board(game_board_1, 'r', direction);
generate_direction(&direction);
randomly_place_ships_on_board(game_board_1, 's', direction);
generate_direction(&direction);
randomly_place_ships_on_board(game_board_1, 'd', direction);
}
generate_direction(&direction);
randomly_place_ships_on_board(game_board_2, 'c', direction);
generate_direction(&direction);
randomly_place_ships_on_board(game_board_2, 'b', direction);
generate_direction(&direction);
randomly_place_ships_on_board(game_board_2, 'r', direction);
generate_direction(&direction);
randomly_place_ships_on_board(game_board_2, 's', direction);
generate_direction(&direction);
randomly_place_ships_on_board(game_board_2, 'd', direction);
print_game_board(game_board_1, 10, 10);
printf("Target Board\n");
print_game_board(game_board_target, 10, 10);
number_of_ships_1 = 5;
number_of_ships_2 = 5;
if (turn == 1)
{
get_target(&target_row, &target_col);
hit_or_miss = check_shot(game_board_2, target_row, target_col);
update_board(hit_or_miss, game_board_2[target_row][target_col]);
update_board(hit_or_miss, game_board_target [target_row][target_col]);
printf("Target Board\n");
print_game_board(game_board_target, 10, 10);
system("pause");
system("cls");
}
else
{
rand_target(&target_row, &target_col);
hit_or_miss = check_shot(game_board_1, target_row, target_col);
update_board(hit_or_miss, game_board_1[target_row][target_col]);
printf("Player 1 Board\n");
print_game_board(game_board_1, 10, 10);
}
turn = (turn + 1) % 2 + 1;
while (number_of_ships_2 != 0 || number_of_ships_2 != 0)
{
if (turn == 1)
{
get_target(&target_row, &target_col);
hit_or_miss = check_shot(game_board_2, target_row, target_col);
update_board(hit_or_miss, game_board_2[target_row][target_col]);
number_of_ships_2 = is_winner(game_board_2);
printf("Target Board\n");
print_game_board(game_board_target, 10, 10);
system("pause");
system("cls");
}
else
{
rand_target(&target_row, &target_col);
hit_or_miss = check_shot(game_board_1, target_row, target_col);
update_board(hit_or_miss, game_board_1[target_row][target_col]);
number_of_ships_1 = is_winner(game_board_1);
printf("Player 1 Board\n");
print_game_board(game_board_1, 10, 10);
}
}
if (number_of_ships_1 == 0)
{
printf("Sorry you lost. Better luck next time.\n");
}
else if (number_of_ships_2 == 0)
{ printf("Congratulations! You won!\n");
}
}
return 0;
}
/*++++++++++++++++++++++++++++++++++welcomeScreen+++++++++++++++++*/
int welcome_screen(void) /* prints out the menu */
{
int choice = 0;
printf("****************Welcome to ***************\n");
printf("***************BattleFleet****************\n");
printf("*****************************************\n");
printf("1. To start playing please press one (1).\n2. Or to quit please press two (2).\n");
/* sets the value of choice to the user's choice. */
scanf("%d", &choice);
return choice;
}
/*++++++++++++++++++++initialize game board+++++++++++++++++++++++++*/
void initialize_game_board (char game_board[][10], int num_rows, int num_cols)
{
int row_index = 0, col_index = 0;
for (row_index = 0; row_index < num_rows; row_index++)
{
for (col_index = 0; col_index < num_cols ; col_index++)
{
game_board[row_index][col_index] = '~';
}
}
}
/*++++++++++++++++++++++++++++++printGameBoard++++++++++++++++++++++*/
void print_game_board (char game_board[10][10], int num_rows, int num_cols)
{
int row_index = 0, col_index = 0;
printf(" ");
for (col_index = 0; col_index < num_cols; col_index++)
{
printf(" %d", col_index);
}
putchar ('\n');
for (row_index = 0; row_index < num_rows; row_index++)
{
printf("%d ", row_index);
for (col_index = 0; col_index < num_cols ; col_index++)
{
printf("%c ", game_board[row_index][col_index]);
}
putchar ('\n');
}
}
/*+++++++++++++++++++++++++++++checkShot++++++++++++++++++++++++++*/
int check_shot(char game_board[10][10], int target_row, int target_col)
{
if (game_board[target_row][target_col] == '~')
{
printf("Miss...");
return 0;
}
else
{
printf("Hit!");
return 1;
}
}
/*++++++++++++++++++++++++++++search Ship++++++++++++++++++++++++++*/
int search_ship(char game_board[10][10], int num_rows, int num_cols, char ship)
{
int row = 0, col = 0, status = 0;
for (row = 0; row < num_rows; row++)
{
for (col = 0; col < num_cols; col++)
{
if (game_board[row][col] == ship)
status = 1;
break;
}
}
return status;
}
/*++++++++++++++++++++++++++++Update board+++++++++++++++++++++++++++*/
void update_board(int hit_or_miss, char *target)
{
if (hit_or_miss == 1)
{
*target = '*';
}
else
{*target = 'm';
}
}
/*++++++++++++++++++++++++++++++++print Menu++++++++++++++++++++*/
int print_menu (void)/* prints out the menu */
{
int choice = 0;
printf("*************************\n");
printf("********* Menu **********\n");
printf("*************************\n");
printf("1. Player vs Player press one (1).\n2. Or Computer vs Player press two (2).\n");
/* sets the value of choice to the user's choice. */
scanf("%d", &choice);
return choice;
}
/*+++++++++++++++++++++++++++++++++manually place ships++++++++++++++++++++++++++++*/
void manually_place_ships_on_board(char *game_boardptr,char ship) /*int row_index1, int col_index1, int row_index2, int col_index2, int row_index3, int col_index3, int row_index4, int col_index4, int row_index5, int col_index5)*/
{
int size = 0, row_index = 0, col_index = 0, count = 0;
if (ship == 'c')
{size = 5;
printf("Please select where to place the Cruiser by entering five sets of coordinates.\n");
}
else if (ship == 'b')
{size = 4;
printf("Please select where to place the Battleship by entering four sets of coordinates.\n");
}
else if (ship == 'r' || ship == 's')
{size = 3;
if (ship == 'r')
{
printf("Please select where to place the Cruiser by entering three sets of coordinates.\n");
}
else
{ printf("Please select where to place the Submarine by entering three sets of coordinates.\n");
}
}
else
{ size = 2;
printf("Please select where to place destroyer by entering two sets of coordinates.\n");
}
while (count < size)
{
printf("Enter a set of coordinates: \n");
printf("Row: ");
scanf("%d", &row_index);
printf("Column: ");
scanf("%d", &col_index);
if (game_board[row_index][col_index] == '~')
{
game_board[row_index][col_index] = ship;
count++;
}
else
{
printf("Error illegal ship placement. Please enter another set of coordinates.");
}
}
}
/*+++++++++++++++++++++++++++generate direction++++++++++++++++*/
void generate_direction (int *dir)
{
*dir = rand () % 2;
}
void generate_start_pt (int len_ship, int *row_coord, int *col_coord, int dir)
{
switch (dir)
{
case 0:
*row_coord = rand () % 10;
*col_coord = rand () % (10 - len_ship + 1);
break;
case 1:
*row_coord = rand () % (10 - len_ship + 1);
*col_coord = rand () % 10;
break;
}
}
/*++++++++++++++++++++++++++++++randomly placed ships/ computer ++++++++++++++++*/
void randomly_place_ships_on_board(char game_board[10][10], char ship, int dir)
{
int row_coord = 0, col_coord = 0, size = 0, count = 0;
if (ship == 'c')
{size = 5;
}
else if (ship == 'b')
{size = 4;
}
else if (ship == 'r' || ship == 's')
{size = 3;
}
else
{
size = 2;
}
/*+++++++++++++++++++++++++++++generate start point+++++++++++++*/
generate_start_pt(size, &row_coord, &col_coord, dir);
while (count < size)
{
if (game_board[row_coord][col_coord] == '~')
{
game_board[row_coord][col_coord] = ship;
if (dir == 0)
{
col_coord++;
}
else
{
row_coord++;
}
count++;
}
}
}
/*++++++++++++++++++++++++++++getTargetManuallly+++++++++++++++++++++++++++*/
void get_target (int *target_row, int *target_col)
{
printf("Please enter a target coordinate: \n");
printf("Row: ");
scanf("%d", target_row);
printf("Column: ");
scanf("%d", target_col);
}/*++++++++++++++++++++++++++randomTarget/computer++++++++++++++++++++++*/
void rand_target (int *target_row, int *target_col)
{
*target_row = rand() % 9 + 1;
*target_col = rand() % 9 + 1;
}
/*++++++++++++++++++++++++++++++winner+++++++++++++++++++++++++*/
int is_winner (char game_board[10][10])
{
int number_of_ships = 0;
number_of_ships += search_ship(game_board, 10, 10, 'c');
number_of_ships += search_ship(game_board, 10, 10, 'b');
number_of_ships += search_ship(game_board, 10, 10, 'r');
number_of_ships += search_ship(game_board, 10, 10, 's');
number_of_ships += search_ship(game_board, 10, 10, 'd');
return number_of_ships;
}