Using the C programming languge , how can I create a simple battleship board as
ID: 3568487 • Letter: U
Question
Using the C programming languge, how can I create a simple battleship board as shown in the following image?
The A's represent an aircraft carrier, while the B's represent a battleship.
The only rules are that the ships must be positioned in a random location, with random orientation (vertical or horizontal), not overlap, and not go outside the board.
The user does not need to be able to hit a ship or anything like that, so this should be simple!
I simply need to be able to lay the ships in the board as shown below, given the specifications that I listed above.
Maximum points (1500) will be given for a quality answer from a smart programmer.
DO NOT COPY ANSWERS FROM PREVIOUS WEBSITES OR ANSWERS.
Note: User "clickit" will not recieve best answer due to simply copying and pasting an answer from another website. Please be original...
Explanation / Answer
#include "header.h"
void init_game_board(char game_board[MAX_ROWS][MAX_COLS],
int rows, int cols)
{
int row_index = 0, col_index = 0;
for (row_index = 0; row_index < rows; row_index++)
{
for (col_index = 0; col_index < cols; col_index++)
{
game_board[row_index][col_index] = '~';
}
}
}
void print_game_board(char game_board[MAX_ROWS][MAX_COLS], int rows, int cols)
{
int row_index = 0, col_index = 0;
printf("%3d%2d%2d%2d%2d%2d%2d%2d%2d%2d ", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
for (row_index = 0; row_index < rows; row_index++)
{
printf("%-2d", row_index);
for (col_index = 0; col_index <= cols - 1; ++col_index)
{
printf("%c ", game_board[row_index][col_index]);
}
putchar(' ');
}
}
void generate_direction(char *dir_ptr)
{
int result = 0;
result = rand() % 2;
switch (result)
{
case 0: *dir_ptr = 'h';
break;
case 1: *dir_ptr = 'v';
break;
}
}
void generate_start_pt(int ship_length, char direction, int *row_ptr, int *col_ptr)
{
if (direction == 'h')
{
*row_ptr = rand() % 10;
*col_ptr = (rand() % ((10 - ship_length) + 1));
}
else // vertical
{
*row_ptr = (rand() % ((10 - ship_length) + 1));
*col_ptr = rand() % 10;
}
}
Boolean is_valid_placement(char board[][MAX_COLS], int row_pt, int col_pt,
char dir, int length)
{
Boolean is_valid = TRUE; // appropriate coordinates for ship placement
int index = 0;
while ((is_valid != FALSE) && (index < length))
{
if (dir == 'h') // horizontal
{
if (board[row_pt][col_pt + index] != '~')
{
is_valid = FALSE;
}
}
else // vertical
{
if (board[row_pt + index][col_pt] != '~')
{
is_valid = FALSE;
}
}
index++;
}
return is_valid;
}
void update_stats(Boolean is_hit, Stats *player_stats_ptr)
{
if (is_hit == TRUE) //hit
{
((*player_stats_ptr).num_hits)++;
}
else // miss
{
player_stats_ptr->num_misses += 1; // -> works with pointers to structs only!!!
}
}
void manually_place_ships_on_board(char game_board[MAX_ROWS][MAX_COLS])
{
int destroyer1, destroyer2, destroyer3, destroyer4, cruiser1,
cruiser2, cruiser3, cruiser4, cruiser5, cruiser6, submarine1,
submarine2, submarine3, submarine4, submarine5, submarine6, battleship1,
battleship2, battleship3, battleship4, battleship5, battleship6,
battleship7, battleship8, carrier1, carrier2, carrier3,
carrier4, carrier5, carrier6, carrier7, carrier8, carrier9, carrier10;
printf("Enter first coordinates for destroyer ");
scanf_s("%d%d", &destroyer1, &destroyer2);
game_board[destroyer1][destroyer2] = 'd';
printf("Enter second coordinates for destroyer ");
scanf_s("%d%d", &destroyer3, &destroyer4);
game_board[destroyer3][destroyer4] = 'd';
if ((game_board[destroyer1][destroyer2 + 1] != 'd') &&
(game_board[destroyer1][destroyer2 - 1] != 'd') &&
(game_board[destroyer1 + 1][destroyer2] != 'd') &&
(game_board[destroyer1 - 1][destroyer2] != 'd'))
{
game_board[destroyer3][destroyer4] = '~';
printf("invalid move, please select again. ");
scanf_s("%d%d", &destroyer3, &destroyer4);
game_board[destroyer3][destroyer4] = 'd';
(system("cls"));
}
//start cruiser
printf("Enter first coordinates for cruiser ");
scanf_s("%d%d", &cruiser1, &cruiser2);
game_board[cruiser1][cruiser2] = 'r';
printf("Enter second coordinates for cruiser ");
scanf_s("%d%d", &cruiser3, &cruiser4);
game_board[cruiser3][cruiser4] = 'r';
if ((game_board[cruiser1][cruiser2 + 1] != 'r') &&
(game_board[cruiser1][cruiser2 - 1] != 'r') &&
(game_board[cruiser1 + 1][cruiser2] != 'r') &&
(game_board[cruiser1 - 1][cruiser2] != 'r'))
{
game_board[cruiser3][cruiser4] = '~';
printf("invalid move, please select again. ");
scanf_s("%d%d", &cruiser3, &cruiser4);
game_board[cruiser3][cruiser4] = 'r';
}
printf("Enter third coordinates for cruiser ");
scanf_s("%d%d", &cruiser5, &cruiser6);
game_board[cruiser5][cruiser6] = 'r';
if ((game_board[cruiser3][cruiser4 + 1] != 'r') &&
(game_board[cruiser3][cruiser4 - 1] != 'r') &&
(game_board[cruiser3 + 1][cruiser4] != 'r') &&
(game_board[cruiser3 - 1][cruiser4] != 'r'))
{
game_board[cruiser5][cruiser6] = '~';
printf("invalid move, please select again. ");
scanf_s("%d%d", &cruiser5, &cruiser6);
}
//submarine
printf("Enter first coordinates for submarine ");
scanf_s("%d%d", &submarine1, &submarine2);
game_board[submarine1][submarine2] = 's';
printf("Enter second coordinates for submarine ");
scanf_s("%d%d", &submarine3, &submarine4);
game_board[submarine3][submarine4] = 's';
if ((game_board[submarine1][submarine2 + 1] != 's') &&
(game_board[submarine1][submarine2 - 1] != 's') &&
(game_board[submarine1 + 1][submarine2] != 's') &&
(game_board[submarine1 - 1][submarine2] != 's'))
{
game_board[submarine3][submarine4] = '~';
printf("invalid move, please select again. ");
scanf_s("%d%d", &submarine3, &submarine4);
game_board[submarine3][submarine4] = 's';
}
printf("Enter third coordinates for submarine ");
scanf_s("%d%d", &submarine5, &submarine6);
game_board[submarine5][submarine6] = 's';
if ((game_board[submarine3][submarine4 + 1] != 's') &&
(game_board[submarine3][submarine4 - 1] != 's') &&
(game_board[submarine3 + 1][submarine4] != 's') &&
(game_board[submarine3 - 1][submarine4] != 's'))
{
game_board[submarine5][submarine6] = '~';
printf("invalid move, please select again. ");
scanf_s("%d%d", &submarine5, &submarine6);
}
//battle ship
printf("Enter first coordinates for battle ship ");
scanf_s("%d%d", &battleship1, &battleship2);
game_board[battleship1][battleship2] = 'b';
printf("Enter second coordinates for battle ship ");
scanf_s("%d%d", &battleship3, &battleship4);
game_board[battleship3][battleship4] = 'b';
if ((game_board[battleship1][battleship2 + 1] != 'b') &&
(game_board[battleship1][battleship2 - 1] != 'b') &&
(game_board[battleship1 + 1][battleship2] != 'b') &&
(game_board[battleship1 - 1][battleship2] != 'b'))
{
game_board[battleship3][battleship4] = '~';
printf("invalid move, please select again. ");
scanf_s("%d%d", &battleship3, &battleship4);
game_board[battleship3][battleship4] = 'b';
}
printf("Enter third coordinates for battle ship ");
scanf_s("%d%d", &battleship5, &battleship6);
game_board[battleship5][battleship6] = 'b';
if ((game_board[battleship3][battleship4 + 1] != 'b') &&
(game_board[battleship3][battleship4 - 1] != 'b') &&
(game_board[battleship3 + 1][battleship4] != 'b') &&
(game_board[battleship3 - 1][battleship4] != 'b'))
{
game_board[battleship5][battleship6] = '~';
printf("invalid move, please select again. ");
scanf_s("%d%d", &battleship5, &battleship6);
}
printf("Enter fourth coordinates for battle ship ");
scanf_s("%d%d", &battleship7, &battleship8);
game_board[battleship7][battleship8] = 'b';
if ((game_board[battleship5][battleship6 + 1] != 'b') &&
(game_board[battleship5][battleship6 - 1] != 'b') &&
(game_board[battleship5 + 1][battleship6] != 'b') &&
(game_board[battleship5 - 1][battleship6] != 'b'))
{
game_board[battleship7][battleship8] = '~';
printf("invalid move, please select again. ");
scanf_s("%d%d", &battleship7, &battleship8);
}
//carrier
printf("Enter first coordinates for carrier ");
scanf_s("%d%d", &carrier1, &carrier2);
game_board[carrier1][carrier2] = 'c';
printf("Enter second coordinates for carrier ");
scanf_s("%d%d", &carrier3, &carrier4);
game_board[carrier3][carrier4] = 'c';
if ((game_board[carrier1][carrier2 + 1] != 'c') &&
(game_board[carrier1][carrier2 - 1] != 'c') &&
(game_board[carrier1 + 1][carrier2] != 'c') &&
(game_board[carrier1 - 1][carrier2] != 'c'))
{
game_board[carrier3][carrier4] = '~';
printf("invalid move, please select again. ");
scanf_s("%d%d", &carrier3, &carrier4);
game_board[carrier3][carrier4] = 'c';
}
printf("Enter third coordinates for carrier ");
scanf_s("%d%d", &carrier5, &carrier6);
game_board[carrier5][carrier6] = 'c';
if ((game_board[carrier3][carrier4 + 1] != 'c') &&
(game_board[carrier3][carrier4 - 1] != 'c') &&
(game_board[carrier3 + 1][carrier4] != 'c') &&
(game_board[carrier3 - 1][carrier4] != 'c'))
{
game_board[carrier5][carrier6] = '~';
printf("invalid move, please select again. ");
scanf_s("%d%d", &carrier5, &carrier6);
}
printf("Enter fourth coordinates for carrier ");
scanf_s("%d%d", &carrier7, &carrier8);
game_board[carrier7][carrier8] = 'c';
if ((game_board[carrier5][carrier6 + 1] != 'c') &&
(game_board[carrier5][carrier6 - 1] != 'c') &&
(game_board[carrier5 + 1][carrier6] != 'c') &&
(game_board[carrier5 - 1][carrier6] != 'c'))
{
game_board[carrier7][carrier8] = '~';
printf("invalid move, please select again. ");
scanf_s("%d%d", &carrier7, &carrier8);
}
printf("Enter fifth coordinates for carrier ");
scanf_s("%d%d", &carrier9, &carrier10);
game_board[carrier9][carrier10] = 'c';
if ((game_board[carrier7][carrier8 + 1] != 'c') &&
(game_board[carrier7][carrier8 - 1] != 'c') &&
(game_board[carrier7 + 1][carrier8] != 'c') &&
(game_board[carrier7 - 1][carrier8] != 'c'))
{
game_board[carrier9][carrier10] = '~';
printf("invalid move, please select again. ");
scanf_s("%d%d", &carrier9, &carrier10);
}
system("pause");
}
/*
void randomly_place_ships_on_board(char game_board2[MAX_ROWS][MAX_COLS])
{
int place, des1, des2, des3, des4, sub1, sub2, sub3, sub4, sub5, sup6;
des1 = rand() % 5;
des2 = rand() % 5;
place = rand() % 3;
switch (place) {
case 0:
case 1:
case 2:
case 3:
case 4:
}
}*/
output_current_move(char game_board[MAX_ROWS][MAX_COLS])
{
int guess1 = 0, guess2 = 0;
printf("Enter a coordinate to fire at. ");
scanf_s("%d%d", &guess1, &guess2);
if (game_board[guess1][guess2] == ('c') || ('r') || ('d') || ('s') || ('b'))
{
printf(NULL);
game_board[guess1][guess2] = 'x';
}
else
{
printf("miss!");
game_board[guess1][guess2] = 'm';
}
}
/*check_shot()
{
int hits = 0;
return ++hits;
}
is_winner()
{
if (game_board [0])
{
printf("YOU WIN! ");
}
}
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.