Question: Hey! I need help figuring out if I'm on the right track with my code. Quick summary of assignment requirements: ---populate a (10x10) board to
Hey! I need help figuring out if I'm on the right track with my code.
Quick summary of assignment requirements:
---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)
---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
---the user is given the option to start a new game or quit at the end
Solution:
So far I know it populates the board fine and I think the random placement function is okay (probably not), but I know the callShots function is not right. I tried following examples as best as I could understand them.
#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 intBoard(char board[][COLS]); void printBoard(char board[][COLS]); void placeShips(char board[][COLS], char shipType[]);
int main() { system("COLOR B0"); char board[ROW][COLS]; char shipType[SHIPS] = {'S', 'R', 'G', 'L', 'F'}; char choice; do{ printf("***Welcome Menu**** "); printf(" Please choose an option below: "); printf("1. Play New Game "); printf("2. Exit The Game "); scanf("%d", &choice); switch(choice){ case 1: intBoard(board); printBoard(board); break; case 2: printf("Thank you for playing!"); break; default: printf("Invalid selection."); break; } }while (choice != 2);
return 0; }
void intBoard(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 "); for (i = 0; i < ROW; i++) { printf("%c ", 'A' + i); for (j = 0; j < COLS; j++) printf("%c ", board[i][j]); printf(" "); } }
void placeShips(char board[][COLS], char shipType[]){ int randomrow, randomcol; int i; srand(time(0)); for(i = 0; i < SHIPS; i++){ randomrow = rand() % 10; randomcol = rand() % 10; if (board[randomrow][randomcol]== ' '){ board[randomrow][randomcol] = shipType[i]; } } }
void callShots(char board[][COLS]){ bool shot= true; int i; int j; for( i = 0; i < 10; i++){ for (j = 0; j < 10; j++){ if(board[i][j]!= ' ' && board[i][j] != HIT){ shot = false; break; } } } return shot; }
As Always, All help is appreciated !!!!!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
