Hi there!
I am working on a simple text based console application for a game of battleship. I am not really looking for a raw code just ideas on where to go next with my code. Some things i would like to put in it that I am flat out confused about: the computer should narrow down its target area when it scores a hit, the player can either randomly or manually set their ships (I started these functions but am a little lost), and how the main function driver should run because mine is almost blank at the moment.
Here are the functions I have written so far:
int welcome_screen(void)
{
int choice = 0;
/* prints out the menu */
printf("*****************************************");
printf("***************Battleship****************");
printf("*****************************************");
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);
/* returns choice */
return choice;
}
void initialize_game_board (char game_board[10][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] = '~';
}
}
}
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');
}
}
void select_who_starts_first(int *first_turn)
{
int random = 1;
random = rand() % 1 + 1;
switch (random)
{
case 1:
*first_turn = 1;
break;
case 2:
*first_turn = 2;
break;
default:
printf("Error: first turn player could not be randomly selected.");
break;
}
}
int check_shot(char target)
{
if (target == '~')
{
return 1;
}
else
{
return 0;
}
}
int is_winner(int number_of_ships)
{
if (number_of_ships == 0)
{
return 1;
}
else
{
return 0;
}
}
void update_board(int hit_or_miss, char *target)
{
if (hit_or_miss == 1)
{
*target = '*';
}
else
{
*target = 'm';
}
}
int print_menu (void)
{
int choice = 0;
/* prints out the menu */
printf("*************************\n");
printf("********* Menu **********\n");
printf("*************************\n");
printf("1. To manually place your ships press one (1).\n2. Or have them randomly placed press two (2).\n");
/* sets the value of choice to the user's choice. */
scanf("%d", &choice);
/* returns choice */
return choice;
}
void manually_place_ships_on_board(char *game_board[10][10])
{
int row_index1 = 0, row_index2 = 0, row_index3 = 0, row_index4 = 0, row_index5 = 0;
int col_index1 = 0, col_index2 = 0, col_index3 = 0, col_index4 = 0, col_index5 = 0;
/*
int row_index[5] = {0, 0, 0, 0, 0};
int col_index[5] = {0, 0, 0, 0, 0};
int count = 1;*/
printf("Please enter the five cells to place the Carrier across: ");
scanf("%d %d %d %d %d %d %d %d %d %d", &row_index1, &col_index1, &row_index2, &col_index2, &row_index3, &col_index3, &row_index4, &col_index4, &row_index5, &col_index5);
/*
for (count = 1; count <= 5; count++)
{
if (game_board[row_index[count]][col_index[count]] == '~')
{
*game_board[row_index[count]][col_index[count]] = 'c';
}
*/
game_board[row_index1][col_index1] = 'c';
game_board[row_index2][col_index2] = 'c';
game_board[row_index3][col_index3] = 'c';
game_board[row_index4][col_index4] = 'c';
game_board[row_index5][col_index5] = 'c';
}
void generate_direction (int *dir)
{
*dir = rand () % 2;
}
void generate_start_pt (int len_ship, int *row_coord, int *col_coord)
{
int dir = 0;
*row_coord = rand () % 10;
*col_coord = rand () % 10;
generate_direction(&dir);
switch (dir)
{
case 0:
*row_coord = rand () % 10;
*col_coord = rand () % (10 - len_ship);
Like I said most of it is still half written and Im still fidgitting with it. And any help would be appreciated, but I am still starting in C and would prefer to keep it fairly simple so I can actually learn from any feedback I get. So if any replies could be kept to simple constructs up until about arrays, pointers, and if, for, while statements it would be very helpful. But all suggestions are appreciated! Thank you in advance for any feedback.