Make the following for a battleship game (I need to write the program for it, ne
ID: 3681742 • Letter: M
Question
Make the following for a battleship game (I need to write the program for it, need a basis):
1. Data structure definitions (in battleship.h file):
a. Playing board
b. Ship
c. Any other needed constants/enums/typedefs
2. Commented function prototypes (in battleship.h file):
a. Initialize board (InitializeBoard)
b. Display board (DisplayBoard)
c. Initialize all ships (InitializeAllShips)
d. Initialize individual ship (InitializeShip)
e. Place ship on board (PlaceShip)
f. Check requested position is valid for a ship (IsValidPosition)
3. Pseudocode for a main() function that uses the above functions to:
a. Create/initialize a playing field
b. Test that a given ship can be placed in all valid positions
Explanation / Answer
Program plan: Here i wrote the code in c programming language and mentioned the proper comments to understand the code.
Program:
#include <stdio.h>
#include <stdlib.h>
//Iniialize board method definition here we are initialising the board
void initilizeBoard(int b[][5])
{
int l, c;
for(l=0 ; l< 5 ; l++ )
for(c=0 ; c< 5 ; c++ )
b[l][c]=-1;
}
//display method definition for displaying the ~,*,X
void displayBoard(int b[][5])
{
int l, c;
printf(" 1 2 3 4 5");
printf(" ");
for(l=0 ; l< 5 ; l++ ){
printf("%d",l+1);
for(c=0 ; c < 5 ; c++ ){
if(b[l][c]==-1){
printf(" ~");
}else if(b[l][c]==0){
printf(" *");
}else if(b[l][c]==1){
printf(" X");
}
}
printf(" ");
}
}
//definition of initializeship method
void initilizeShips(int ships[][2])
{
int s,l;
for(s=0 ; s < 3 ; s++){
ships[s][0]= rand()%5;
ships[s][1]= rand()%5;
//let's check if this shot was not tried
//if it was, just get out of the 'do while' loop when draws a pair that was not tried
for(l=0 ; l < s ; l++){
if( (ships[s][0] == ships[l][0])&&(ships[s][1] == ships[l][1]) )
do{
ships[s][0]= rand()%5;
ships[s][1]= rand()%5;
}while( (ships[s][0] == ships[l][0])&&(ships[s][1] == ships[l][1]) );
}
}
}
//definition of giveshot method read the user input
void giveShot(int shot[2])
{
printf("Line: ");
scanf("%d",&shot[0]);
shot[0]--;
printf("Column: ");
scanf("%d",&shot[1]);
shot[1]--;
}
//definition of hitshot method compares the user shot with random initialized ship values
int hitship(int shot[2], int ships[][2])
{
int s;
for(s=0 ; s < 3 ; s++){
if( shot[0]==ships[s][0] && shot[1]==ships[s][1]){
printf("You hit a ship with the shot (%d,%d) ",shot[0]+1,shot[1]+1);
return 1;
}
}
return 0;
}
//tip method provides the tip for the user about the hit information
void tip(int shot[2], int ships[][2], int a)
{
int l=0,c=0,row;
//count how many ships there is line/column
for(row=0 ; row < 3 ; row++){
if(ships[row][0]==shot[0])
l++;
if(ships[row][1]==shot[1])
c++;
}
printf(" %d: line %d -> %d ships column %d -> %d ships ",a,shot[0]+1,l,shot[1]+1,c);
}
//to change the board
void changeBoard(int shot[2], int ships[][2], int board[][5]){
if(hitship(shot,ships))
board[shot[0]][shot[1]]=1;
else
board[shot[0]][shot[1]]=0;
}
int main() {
int board[5][5];
int ships[3][2];
int shot[2];
int attempts=0,
hits=0;
//calling the start board function
initializeBoard(board);
initilizeShips(ships);
printf(" ");
//do..while loop to run the game and runs until 3 shots given
do{
displayBoard(board);
giveShot(shot);
attempts++;
if(hitship(shot,ships)){
tip(shot,ships,attempts);
hits++;
}
else
tip(shot,ships,attempts);
changeBoard(shot,ships,board);
}while(hits!=3);
printf(" Finished game. You hit the three ships in %d attempts", attempts);
displayBoard(board);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.