Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello! I'm just having a little trouble! I've posted this question a few times and have learned a lot from each of them, but there

Hello! I'm just having a little trouble! I've posted this question a few times and have learned a lot from each of them, but there is just still a few issues. I think the last answer was actually cut off on accident, but I still learned a lot.

When running the code and entering the game, it exits immediately after I make a selection for the coordinates. Also I need to hide the placement of the ships so they are not visible to the user. They are only supposed to appear once the user has sunk the whole ship. I've been implementing everyone's advice, but this assignment is just lost on me.

I appreciate any help!

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

Solution

#include #include #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 < ROW; i++) { for(j = 0; j < COLS; j++) { board[i][j] = '.'; } } } // ********************************************** void printBoard(char board[][COLS]) { int i, j;

printf(" 0 1 2 3 4 5 6 7 8 9 "); // col headers printf(" ------------------- "); for(i = 0; i < ROW; i++) { printf("%c", 'A' + i); // row headers for(j = 0; j < COLS; j++) { printf("%c ", board[i][j]); } printf(" "); } } // ****************************************************

void placeShips(char board[][COLS], char shipType[]) { int i, j, k, l; int row, col; int shipLen; srand(time(NULL)); for(i = 0; i < SHIPS; i++) { bool placed = false; switch(shipType[i]) { case SIMPLE_SHIP: shipLen = SIMPLE_SHIP_LEN; break; case RUGGED_SHIP: shipLen = RUGGED_SHIP_LEN; break; case GIANT_SHIP: shipLen = GIANT_SHIP_LEN; break; case BABY_SHIP: shipLen = BABY_SHIP_LEN; break; case VALENCIA_SHIP: shipLen = VALENCIA_SHIP_LEN; break; default: shipLen = 0; break; } while(!placed) { row = rand() % ROW; col = rand() % COLS; bool horizontal = rand() % 2 == 0 ? true : false; bool validPlacement = true; if(horizontal) { if(col + shipLen > COLS) { validPlacement = false; } else { for(j = col; j < col + shipLen; j++) { if(board[row][j] != '.') { validPlacement = false; break; } } } } else { if(row + shipLen > ROW) { validPlacement = false; } else { for(k = row; k < row + shipLen; k++) { if(board[k][col] != '.') { validPlacement = false; break; } } } }

bool shotsFired(char board[][COLS], int shipLengths[], char shipType[]) { int i, j, k, hits; bool allSunk = true;

for (i = 0; i < SHIPS; i++) { hits = 0;

for (j = 0; j < ROW; j++) { for (k = 0; k < COLS; k++) { if (board[j][k] == shipType[i] && board[j][k] == HIT) { hits++; } } }

if (hits < shipLengths[i]) { allSunk = false; break; } }

return allSunk; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MySQL/PHP Database Applications

Authors: Jay Greenspan, Brad Bulger

1st Edition

978-0764535376

More Books

Students also viewed these Databases questions

Question

6. What is process reengineering? Why is it relevant to training?

Answered: 1 week ago