Question: Hello! I'm reposting because I'm still having issues with getting the right output. I'm having a lot of trouble with this assignment. So I'll just
Hello! I'm reposting because I'm still having issues with getting the right output. I'm having a lot of trouble with this assignment. So I'll just post the requirements, the expectation, and the code I have so far implementing everyone's advice. Also, for some reason I think some of the answers I'm getting are like being cut off so something so sorry for posting multiple times.
For the most part this is what I am trying to do:
---populate a (10x10) board to sim a game of battleship
---There are 5 ships randomly placed on the board (this is determined randomly by computer and the user nor programmer will know the placements).
---Each ship will take a certain amount of hits to sink (refer to their lengths) and to enter a hit user needs to enter a row then col ( Ex: J9)
---after each attempt a message populates whether the gamer "hit" or "missed" the target and updates the board with an H or M accordingly.
----enforce preventative coding so if the gamer selects outside of the boards region, they are notified that their selection is invalid
---the user is given the option to start a new game or quit at the end
----use structs and dynamic memory allocation as necessary
Example of Expected outcome

Solution:
#include
#define ROW 10 #define COLS 10
#define SHIPS 5 #define REG_SHIP 'S' #define RUGGED_SHIP 'R' #define GIANT_SHIP 'G' #define LITTLE_SHIP 'L' #define FAVORITE_SHIP 'F' #define LITTLE_SHIP_LEN 2 #define REG_SHIP_LEN 3 #define RUGGED_SHIP_LEN 3 #define FAVORITE_SHIP_LEN 4 #define GIANT_SHIP_LEN 5
#define MISS 'M' #define HIT 'H'
typedef enum { false, true } bool;
void initBoard(char board[][COLS]); void printBoard(char board[][COLS]); void placeShips(char board[][COLS], char shipType[]); bool shotsFired(char board[][COLS], int shipLengths[], char shipType[]);
int main() { system("COLOR B0"); char board[ROW][COLS]; char shipType[SHIPS] = {'S', 'R', 'G', 'B', 'V'}; int shipLengths[SHIPS] = { REG_SHIP_LEN, RUGGED_SHIP_LEN, GIANT_SHIP_LEN, LITTLE_SHIP_LEN, FAVORITE_SHIP_LEN }; char choice; int row, col; do { printf("***Welcome Menu**** "); printf(" Please choose an option below: "); printf("1. Play New Game "); printf("2. Exit The Game "); scanf("%c", &choice); switch(choice) { case '1': initBoard(board); placeShips(board, shipType); printBoard(board); while(!shotsFired(board, shipLengths, shipType)) { printf("Enter row (A-J): "); scanf("%c", &row); row -= 'A'; printf("Enter column (0-9): "); scanf("%d", &col); if(board[row][col] != '.') { board[row][col] = HIT; printf("Hit! "); } else { board[row][col] = MISS; printf("Miss! "); } printBoard(board); } printf("Congratulations you win! "); break; case '2': printf("Exiting the game. Goodbye! "); break; default: printf("Invalid choice! Please try again. "); break; } } while(choice != '2'); return 0; } // **********************************************
void initBoard(char board[][COLS]) { int i,j; for(i = 0; i
printf(" 0 1 2 3 4 5 6 7 8 9 "); // col headers printf(" ------------------- "); for(i = 0; i
void placeShips(char board[][COLS], char shipType[]) { int i, j, k, l; int row, col; int shipLen; srand(time(NULL)); for(i = 0; i COLS) { validPlacement = false; } else { for(j = col; j ROW) { validPlacement = false; } else { for(k = row; k
bool shotsFired(char board[][COLS], int shipLengths[], char shipType[]) { int i, j, k, hits; bool allSunk = true;
for (i = 0; i
for (j = 0; j
if (hits
return allSunk; }
EXAMPLE GAME BOARD
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
