Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The Game of Battleship Your task is to develop a C program that plays a game of

ID: 3768370 • Letter: T

Question

The Game of Battleship

Your task is to develop a C program that plays a game of Battleship. The game is intended to be played between one player

and the computer.

Basic Rules for the Game:

The game is played on four grids, two for each player (however, since one of the players is the computer, the computer's

two grids are hidden from view). The grids are 10x10 square. The individual squares in the grid are identied by letter

and number. On the major grid the player arranges ships and records the shots by the opponent. On the minor grid the

player records his/her own shots. Before play begins, each player secretly arranges their ships on their major grid. Each ship

occupies a number of consecutive squares on the grid, arranged either horizontally, vertically or diagonally (we will ignore

Pythagoras for this project). The number of squares for each ship is determined by the type of the ship. The ships cannot

overlap (i.e. only one ship can occupy any given square on the grid). The types and numbers of ships allowed are the same

for each player.

Although the player may decide their own ship types and sizes, her are some example ship types and sizes

ShipType              BoardSymbol           Ship Size

Aircraft Carrier                A                        5

Battleship                        B                       4

Cruiser                             C                        3

Submarine                       S                        3

Patrol Boat                       P                      2

Kayak                              K                      1

Row boat                          R                      1

Requirements:

In particular, your program should satisfy the following requirements:

1. To begin the game the computer will ask how many ships each side is given, or optionally, provide a le name to read

in the information.

2. The ships are represented as variables of type ship and stored as a linked list.

typedef struct ship {

char shiptype [20]; // e.g. Aircraft Carrier ,

int shipsize ; // e.q. 5,

int *X_Location ; // Dynamically allocated array of length shipsize .

int *Y_Location ; // Dynamically allocated array of length shipsize .

int *X_Hit ; // Dynamically allocated array of length shipsize .

int *Y_ Hit ; // Dynamically allocated array of length shipsize .

struct ship *pt; // pointer to next ship on the list

} ship ;

3. For each ship added a new node is created in the linked list.

4. The player will be given the option to ll out the structure information at the beginning of the game, or read in the

information from a le. The information to be entered is:

               ship type

               ship size

              X-location

               Y-location

               Other attribute information of your choice.

5. The computer will place the same type, and number of ships, at locations of his own choosing.

               Use a random X,Y location for the bow of the ship.

               The remaining X,Y values must be in a horizontal, vertical, or diagonal sequence.

               No X,Y values of the ship may be off grid. Your program must check for this and not allow players to break this                 rule.

6. Ship positions may not overlap.

7. The player is given the rst turn to shoot, then the computer takes his turn. The game continues in rounds until all

ships of one contestant are destroyed. The player with at least one surviving ship (i.e. one unhit X,Y location on at

least one ship) is declared the winner.

8. Once the game begins each shot is recorded. Both 10x10 grids for the human player are updated with each shot. When

a ship is hit the attributes X hit and Y hit are updated. These attributes are used to update the 10x10 grids during

play.

                On the minor grid a record of each shot is kept, using different symbols for a hit/miss.

               On the major grid a record of the computer's shots are recorded. Again, using different symbols for a hit/miss.

9. The computer will choose his shot locations randomly.

10. After each turn the score is updated and displayed. The scoreboard should display (at a minimum)

               Each player's name.

               The number of hits each player has scored.

               The number of missed attempts for each player.

               The number of sunk ships each player has scored.

11. Once a ship is destroyed the node must be removed from the linked list. However, even though the ship is removed from the linked list, the major and minor grids continue to display the results of the sunken ship.

12. Your C program must keep track of both player's information. Thus two linked lists are needed, one for each player.

13. At the end of the game the major grids of both player's are revealed.

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>

void startBoard(int board[][5])
{
int line, column;
for(line=0 ; line < 5 ; line++ )
for(column=0 ; column < 5 ; column++ )
board[line][column]=-1;
}

void showBoard(int board[][5])
{

int line, column;

printf(" 1 2 3 4 5");
printf(" ");

for(line=0 ; line < 5 ; line++ ){
printf("%d",line+1);
for(column=0 ; column < 5 ; column++ ){
if(board[line][column]==-1){
printf(" ~");
}else if(board[line][column]==0){
printf(" *");
}else if(board[line][column]==1){
printf(" X");
}

}
printf(" ");
}

}

void startShips(int ships[][2]){
srand(time(NULL));
int ship, last;

for(ship=0 ; ship < 3 ; ship++){
ships[ship][0]= rand()%5;
ships[ship][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(last=0 ; last < ship ; last++){
if( (ships[ship][0] == ships[last][0])&&(ships[ship][1] == ships[last][1]) )
do{
ships[ship][0]= rand()%5;
ships[ship][1]= rand()%5;
}while( (ships[ship][0] == ships[last][0])&&(ships[ship][1] == ships[last][1]) );
}

}
}

void giveShot(int shot[2])
{

printf("Line: ");
scanf("%d",&shot[0]);
shot[0]--;

printf("Column: ");
scanf("%d",&shot[1]);
shot[1]--;

}

int hitship(int shot[2], int ships[][2])
{
int ship;

for(ship=0 ; ship < 3 ; ship++){
if( shot[0]==ships[ship][0] && shot[1]==ships[ship][1]){
printf("You hit a ship with the shot (%d,%d) ",shot[0]+1,shot[1]+1);
return 1;
}
}
return 0;
}

void tip(int shot[2], int ships[][2], int attempt)
{
int line=0,
column=0,
row;

//count how many ships there is line/column
for(row=0 ; row < 3 ; row++){
if(ships[row][0]==shot[0])
line++;
if(ships[row][1]==shot[1])
column++;
}

printf(" Dica %d: line %d -> %d ships column %d -> %d ships ",attempt,shot[0]+1,line,shot[1]+1,column);
}

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;

startBoard(board);
startShips(ships);

printf(" ");

do{
showBoard(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);
showBoard(board);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote