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

Write a program with the following specifications: The program should simulate a

ID: 3817293 • Letter: W

Question

Write a program with the following specifications: The program should simulate a one-player game of battleship. (See earlier activities/assignments for reference.) The user should be able to specify the board size and the number of ships that will be placed on the board. (Ships should be of varied sizes.) Once the board is generated with random ship placement, the user should be able to specify coordinates and have the game show them whether a "hit" or "miss" has occurred and display the resulting screen. Additional rules may be specified as needed. Program structural requirements: The board will be constructed with a dynamic array The ships will be constructed with a linked list

This is a C++ programming assignment. It needs to be a game of battleship for one player. They can decide the size of the board and the number of ships they want placed on the board. The board needs to be made using a dynamic array and the ships are placed using a linked list. The ships can have a variable number of hits needed to sink them. Once the board is printed the player guesses a x and y coordinate and the game decides if the guess is a hit or miss. Once all the ships have been sank the game is over.

Explanation / Answer

Very Simple code written in C++.

Copy the code into code editor to view best.Every line of code is explained very well. Happy coding.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

struct ListNode{   /* link list node */
  
   int x;   /* x coordinate */
   int y; /* y coordinate */
   ListNode *next; /* pointer to next node in the list */
};
ListNode* createNode(){   /* this method creates a new node of type ListNode initializes values and returns pointer to it*/
      
       ListNode *new_node=new ListNode();

       new_node->next=NULL;
       new_node->x=0;
       new_node->y=0;

       return new_node;
}

ListNode* add(ListNode *new_node,ListNode *head){   /* method to add new node to the list */
  
   if(head==NULL){ /* if the list is empty*/
      
       head=new_node;   /* add first node */
   }else{

       new_node->next=head; /* point new_node to the head of the list */
       head=new_node;   /* point head to the new_node */
   }

   return head; /* return new head */
}

ListNode* remove(ListNode* head,int x, int y){ /* removes the node with specified x and y co ordinate and returns new head */
  
   ListNode *previous=NULL;    /* initialize pointer to the previous node */
   ListNode *current=head;       /* initialize current pointer to head of the list */
  
   if(head->x==x && head->y==y){   /* if head node is to be removed */
      
       current=current->next;   /* move current to next node */
       head->next=NULL;       /* break node away from list*/
       delete head;           /* free the memory pointed by head*/
       head=current;           /* point head to current i.e. the node after the deleted node */
   }
   else{ /* if node to be deleted is not head */

       current=head->next; /* point current to second node */
       previous=head;       /* point previous to first node */

       while(current!=NULL){   /* travere till we reach end of the list */

           if(current->x==x && current->y==y){   /* if current node is to be removed */

                   previous->next=current->next;   /* point previous node to the node next to current */
                   current->next=NULL;               /* break away current node from list */
                   delete current;               /* free the memory pointed by current */
                   break;
           }

           previous=current;
           current=current->next;  
       }
   }
   return head;

}

/* this method generates random unique co-ordinates for nxn grid and places ships in them
* it returns head of link list to ships
*/
ListNode* placeShips(int n, ListNode *head, int ships, int **board ){
  
   for(int i=0;i<n;i++){
       for(int j=0;j<n;j++){
           board[i][j]=0;       /* initialize every board co ordinate to zero. i.e no ships any where*/
       }
   }

   srand(time(0));   /* seed random number */

   for(int i=0;i<ships;i++){

       ListNode *current=createNode();   /* create a node for the link list*/

       int x=rand()%n;   /* generate random x coordinate */
       int y=rand()%n; /* generate random y coordinate */

       while(board[x][y]!=0){   /* while we dont get a emptry slot in board to place ship keep generating random coordinates */

           x=rand()%n;  
           y=rand()%n;
       }

       current->x=x;   /* assign current x coordinate to ship */
       current->y=y; /* assign current y co ordinate to ship */

       head=add(current,head); /* add current node to the list */
       board[x][y]=1;       /* update board to reflect that there is a ship at x and y*/

   }
   return head;
}

void print(int **board,int n){ /* prints board */
  
   /* board is printed in cartesian plane
   * positive x is to the right and positive y to the top in cartesian plane
   *
   *
   *   dynamic array                 cartesian plane
   * 00 01 02 03 .. 0n           0n 1n 2n 3n .. nn
   * 10 11 12 13 .. 1n           .
   * 20 21 22 23 .. 2n           .
   * .                           .
   * .                           02 21 22 32 .. 2n
   * .                           01 11 21 31 .. 1n
   *   n0 n1 n2 n3 .. nn            00 10 20 30 .. 3n
   *
   *
   *   rotate dynamic array 90 degree anticlockwise to match cartesian plane
   *
   *
   */

   for(int i=n-1;i>=0;i--){

       for(int j=0;j<n;j++){

           if(board[j][i]==0){   /* if no ship is placed */
               cout<<".";
           }else{
               cout<<"S";
           }
       }
       cout<<endl;
   }
   cout<<endl;
}
int main(){

   ListNode *head=NULL; /* Initialize list head to null */

   int n;   /* board size */
   int ships;   /* no of ships to be placed at random */

   cout<<"Enter board size : "; /* prompt user for board size */
   cin>>n;

   cout<<"Enter number of random ships to be placed: "; /* prompt user for ships. ships should be less than n^2 */
   cin>>ships;

   int **board=new int*[n]; /* create array of pointers*/
   for(int i=0;i<n;i++){   /* point every pointer to array of integers */
       board[i]=new int[n];
   }

   head=placeShips(n,head,ships,board);   /* place ships at random locations */
   print(board,n);


   while(head!=NULL) { /* until all ships are sinked keep running */

       int x;   /* x coordinate */
       int y;   /* y coordinate*/
       cout<<"Give coordinates to hit the ship in the form of x y : "; /* prompt user for input */
       cin>>x>>y;

       if(board[x][y]==1){   /* Its a hit */

           cout<<"You have a hit!!"<<endl; /* inform user about hit */
           head=remove(head,x,y);   /* remove node from list */
           board[x][y]=0;       /* remove ship from coordinates */
           print(board,n);

       }else{

           cout<<"You have a miss!! Try again"<<endl<<endl;
       }

   }
   /*
   *
   * This implementation sinks the ship in only one hit
   * To implement variable number of hits scheme do the following
   * Instead of assigning board "1" at ships co ordinate, assign it some positive number N
   * Everytime a ship is hit decrement that number by 1.
   * Once the number becomes zero then delete it.
   *
   */
}

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