How to Program Battle Ship Game

Battle Ship Game Design Pattern

I have tried to make a simple game using C++.The name of my game is Battleship game. In this game there is an ocean field which is a square 10 X 10 grid. One of the co-ordinate of the grid is number from 1-10.This program randomly place 10 ship in different location. Multiple ship cannot be placed at same location. The user fire the ship by specified the co-ordinates of the shot. Example : If you give input 1 1, it will search for the ship on that location . If the ship is in that location then the ship will sink and the output will be ‘you got me’ and also show the number of remaining ship. If you surrender then it will output the total 10 X 10 grid and the location of the ships.
 
#include 
#include 
#include 
#include 
#include 
 
using namespace std;
const int row = 10;
const int element = 10;
int maxship = 10;
int matrix[row][element];
int s=0;
void clr()
{
 
    for(int i=0;i<row;i++)
        for(int j=0;j<element;j++)
           matrix[i][j] = 0;
}
void show()
{
    for(int i=0; i<row; i++)
    {
        for(int j=0;j<element;j++)
        {
            cout << matrix[i][j] << ” “;
        }
        cout << endl;
    }
}
void setshiploc()
{
    int x,y;
 
    int s=0;
 
    while(s<maxship)
    {
    x = rand() % 10;
    y = rand() % 10;
 
          if(matrix[x][y] != 1)
          {
              s++;
           matrix[x][y] = 1;
          }
    }
}
bool attackship(int x,int y)
{
    if(matrix[x][y]==1)
     {
         matrix[x][y]=2;
         maxship–;
         return true;
     }
     return false;
}
int main()
{
    int x,y;
    char ch;
    while (1)
    {
        clr();
    setshiploc();
        cout  x >> y ;
        if(attackship(x,y))
        {
            cout << “You got me !” << endl;
            cout << “Total ship left: ” << maxship << endl;
            if(getch())
            system(“cls”);
        }
        else
           {cout << “You can’t find me bitch!!” << “nTry again bitch :-p “<< endl;
           cout  ch;
           if(ch==’y’)
           {
            show();
               break;
           }
           else
           system(“cls”);
    }
    }
    return 0;
}
Check Out This :

November Challenge 2020 SOLUTION CodeChef

October Lunchtime 2020 CodeChef SOLUTIONS

RELATED :

Related :

Related :

Leave a Comment

close
error: Content is protected !!