Bad ol' ship

venomlash 0 Tallied Votes 122 Views Share

This code will let you play Battleship against the computer (bad graphics, I know) for a while, upon which it quits without so much as an error message. Could an experienced coder look this over and tell me why? Thanks.
Venomlash
:?:

Header file:
#ifndef BATTLESHIP_H
#define BATTLESHIP_H
#include <iostream>
#include <string>
#include <ctime>
#include <vector>

using namespace std;

class battleship{
    private:
        char ugrid[10][10];
        char cgrid[10][10];
        char viewgrid[10][10];
        vector<int> upat;
        vector<int> usub;
        vector<int> udes;
        vector<int> ubat;
        vector<int> ucar;
        vector<int> cpat;
        vector<int> csub;
        vector<int> cdes;
        vector<int> cbat;
        vector<int> ccar;
        char miss;
        char ship;
        char hit;
        char sunk;
        char space;
        int usunk;
        int csunk;
    public:
        battleship();
        int randnum(int num);
        void dispcgrid();
        void dispugrid();
        void uppat();
        void upsub();
        void updes();
        void upbat();
        void upcar();
        void uplace();
        void cppat();
        void cpsub();
        void cpdes();
        void cpbat();
        void cpcar();
        void cplace();
        void uguess();
        void cguess();
        void gameplay();
        void uifsink(string name, vector<int> vec);
        void cifsink(string name, vector<int> vec);
        void usink();
        void csink();
};
#endif

Source code for header:
#include "battleship.h"

using namespace std;

battleship::battleship(){
    for(int i=0; i<10; i++){
        for(int j=0; j<10; j++){
            ugrid[i][j]=' ';
        }
    }
    for(int i=0; i<10; i++){
        for(int j=0; j<10; j++){
            cgrid[i][j]=' ';
        }
    }
    for(int i=0; i<10; i++){
        for(int j=0; j<10; j++){
            viewgrid[i][j]=' ';
        }
    }
    miss='0';
    ship=219;
    hit=178;
    sunk=177;
    space=' ';
    usunk=0;
    csunk=0;
}
int battleship::randnum(int num){
    return rand()%num;
}
void battleship::cppat(){
    int vh=randnum(2);
    cpat.push_back(vh);
    int offset=randnum(10);
    cpat.push_back(offset);
    int start=randnum(9);
    for(int c=start; c<start+2; c++){
        cpat.push_back(c);
    }
    if(cpat[0]==0){
        for(int i=2; i<4; i++){
            cgrid[offset][cpat[i]]=ship;
        }
    }
    if(cpat[0]==1){
        for(int i=2; i<4; i++){
            cgrid[cpat[i]][offset]=ship;
        }
    }
}
void battleship::cpsub(){
    int vh=randnum(2);
    int offset=randnum(10);
    int start=randnum(8);
    if(vh==0){
        if((cgrid[offset][start]==ship)||(cgrid[offset][start+1]==ship)||(cgrid[offset][start+2]==ship)){
            cpsub();
            return;
        }
    }
    if(vh==1){
        if((cgrid[start][offset]==ship)||(cgrid[start+1][offset]==ship)||(cgrid[start+2][offset]==ship)){
            cpsub();
            return;
        }
    }
    csub.push_back(vh);
    csub.push_back(offset);
    for(int c=start; c<start+3; c++){
        csub.push_back(c);
    }
    if(csub[0]==0){
        for(int i=2; i<5; i++){
            cgrid[offset][csub[i]]=ship;
        }
    }
    if(csub[0]==1){
        for(int i=2; i<5; i++){
            cgrid[csub[i]][offset]=ship;
        }
    }
}
void battleship::cpdes(){
    int vh=randnum(2);
    int offset=randnum(10);
    int start=randnum(8);
    if(vh==0){
        if((cgrid[offset][start]==ship)||(cgrid[offset][start+1]==ship)||(cgrid[offset][start+2]==ship)){
            cpdes();
            return;
        }
    }
    if(vh==1){
        if((cgrid[start][offset]==ship)||(cgrid[start+1][offset]==ship)||(cgrid[start+2][offset]==ship)){
            cpdes();
            return;
        }
    }
    cdes.push_back(vh);
    cdes.push_back(offset);
    for(int c=start; c<start+3; c++){
        cdes.push_back(c);
    }
    if(cdes[0]==0){
        for(int i=2; i<5; i++){
            cgrid[offset][cdes[i]]=ship;
        }
    }
    if(cdes[0]==1){
        for(int i=2; i<5; i++){
            cgrid[cdes[i]][offset]=ship;
        }
    }
}
void battleship::cpbat(){
    int vh=randnum(2);
    int offset=randnum(10);
    int start=randnum(7);
    if(vh==0){
        if((cgrid[offset][start]==ship)||(cgrid[offset][start+1]==ship)||(cgrid[offset][start+2]==ship)||
        (cgrid[offset][start+3]==ship)){
            cpbat();
            return;
        }
    }
    if(vh==1){
        if((cgrid[start][offset]==ship)||(cgrid[start+1][offset]==ship)||(cgrid[start+2][offset]==ship)||
        (cgrid[start+3][offset]==ship)){
            cpbat();
            return;
        }
    }
    cbat.push_back(vh);
    cbat.push_back(offset);
    for(int c=start; c<start+4; c++){
        cbat.push_back(c);
    }
    if(cbat[0]==0){
        for(int i=2; i<6; i++){
            cgrid[offset][cbat[i]]=ship;
        }
    }
    if(cbat[0]==1){
        for(int i=2; i<6; i++){
            cgrid[cbat[i]][offset]=ship;
        }
    }
}
void battleship::cpcar(){
    int vh=randnum(2);
    int offset=randnum(10);
    int start=randnum(6);
    if(vh==0){
        if((cgrid[offset][start]==ship)||(cgrid[offset][start+1]==ship)||(cgrid[offset][start+2]==ship)||
        (cgrid[offset][start+3]==ship)||(cgrid[offset][start+4]==ship)){
            cpcar();
            return;
        }
    }
    if(vh==1){
        if((cgrid[start][offset]==ship)||(cgrid[start+1][offset]==ship)||(cgrid[start+2][offset]==ship)||
        (cgrid[start+3][offset]==ship)||(cgrid[start+4][offset]==ship)){
            cpcar();
            return;
        }
    }
    ccar.push_back(vh);
    ccar.push_back(offset);
    for(int c=start; c<start+5; c++){
        ccar.push_back(c);
    }
    if(ccar[0]==0){
        for(int i=2; i<7; i++){
            cgrid[offset][ccar[i]]=ship;
        }
    }
    if(ccar[0]==1){
        for(int i=2; i<7; i++){
            cgrid[ccar[i]][offset]=ship;
        }
    }
}
void battleship::cguess(){
    time_t seconds;
    seconds=time(NULL);
    srand(seconds);
    int x=11;
    int y=11;
    for(int i=0; i<10; i++){
        for(int j=0; j<10; j++){
            if(ugrid[i][j]==hit){
                if((ugrid[i+1][j]==hit)&&((ugrid[i+2][j]==ship)||(ugrid[i+2][j]==space))){
                    x=j;
                    y=i+2;
                    break;
                    break;
                }
                if((ugrid[i-1][j]==hit)&&((ugrid[i-2][j]==ship)||(ugrid[i-2][j]==space))){
                    x=j;
                    y=i-2;
                    break;
                    break;
                }
                if((ugrid[i][j+1]==hit)&&((ugrid[i][j+2]==ship)||(ugrid[i][j+2]==space))){
                    x=j+2;
                    y=i;
                    break;
                    break;
                }
                if((ugrid[i][j-1]==hit)&&((ugrid[i][j-2]==ship)||(ugrid[i][j-2]==space))){
                    x=j-2;
                    y=i;
                    break;
                    break;
                }
                int choice=randnum(4)+1;
                if(choice==1){
                    if((ugrid[i+1][j]==space)||(ugrid[i][j-1]==ship)){
                        x=j;
                        y=i+1;
                    }
                }
                if(choice==2){
                    if((ugrid[i-1][j]==space)||(ugrid[i][j-1]==ship)){
                        x=j;
                        y=i+1;
                    }
                }
                if(choice==3){
                    if((ugrid[i][j+1]==space)||(ugrid[i][j-1]==ship)){
                        x=j;
                        y=i+1;
                    }
                }
                if(choice==4){
                    if((ugrid[i][j-1]==space)||(ugrid[i][j-1]==ship)){
                        x=j;
                        y=i+1;
                    }
                }
            }
        }
    }
    if((x==11) && (y==11)){
        x=randnum(10);
        y=randnum(10);
        while((ugrid[y][x]!=space) && (ugrid[y][x]!=ship)){
            x==randnum(10);
            y==randnum(10);
        }
    }
    if((ugrid[y][x]!=space)&&(ugrid[y][x]!=ship)){
        cguess();
        return;
    }
    char col=y+65;
    cout<<"Computer guesses "<<col<<x+1<<"."<<endl;
    if(ugrid[y][x]==space){
        ugrid[y][x]=miss;
        cout<<"Miss!"<<endl;
    }
    if(ugrid[y][x]==ship){
        ugrid[y][x]=hit;
        cout<<"Hit!"<<endl;
    }
}
void battleship::cplace(){
    cppat();
    cpsub();
    cpdes();
    cpbat();
    cpcar();
}
void battleship::dispcgrid(){
    cout<<"   1  2  3  4  5  6  7  8  9  10"<<endl<<endl;
    char g=64;
    for(int i=0; i<10; i++){
        g++;
        cout<<g<<"  ";
        for(int j=0; j<10; j++){
            cout<<viewgrid[i][j]<<"  ";
        }
        cout<<endl<<endl;
    }
    cout<<endl<<endl;
}
void battleship::dispugrid(){
    cout<<"   1  2  3  4  5  6  7  8  9  10"<<endl<<endl;
    char g=64;
    for(int i=0; i<10; i++){
        g++;
        cout<<g<<"  ";
        for(int j=0; j<10; j++){
            cout<<ugrid[i][j]<<"  ";
        }
        cout<<endl<<endl;
    }
    cout<<endl<<endl;
}
void battleship::uguess(){
    string guess="";
    cout<<"Enter your guess (A2, H9, etc.)"<<endl;
    cin>>guess;
    int x=-2;
    int y=-2;
    if((guess[0]=='A')||(guess[0]=='a'))y=0;
    if((guess[0]=='B')||(guess[0]=='b'))y=1;
    if((guess[0]=='C')||(guess[0]=='c'))y=2;
    if((guess[0]=='D')||(guess[0]=='d'))y=3;
    if((guess[0]=='E')||(guess[0]=='e'))y=4;
    if((guess[0]=='F')||(guess[0]=='f'))y=5;
    if((guess[0]=='G')||(guess[0]=='g'))y=6;
    if((guess[0]=='H')||(guess[0]=='h'))y=7;
    if((guess[0]=='I')||(guess[0]=='i'))y=8;
    if((guess[0]=='J')||(guess[0]=='j'))y=9;
    if((guess[1]=='1')&&(guess[2]=='0'))x=9;
    if(guess.size()==2){
        if(guess[1]=='1')x=0;
        if(guess[1]=='2')x=1;
        if(guess[1]=='3')x=2;
        if(guess[1]=='4')x=3;
        if(guess[1]=='5')x=4;
        if(guess[1]=='6')x=5;
        if(guess[1]=='7')x=6;
        if(guess[1]=='8')x=7;
        if(guess[1]=='9')x=8;
    }
    if(cgrid[y][x]==ship){
        cout<<"Hit!!!"<<endl;
        cgrid[y][x]=hit;
        viewgrid[y][x]=hit;
    }
    else if(cgrid[y][x]==space){
        cout<<"Miss."<<endl;
        cgrid[y][x]=miss;
        viewgrid[y][x]=miss;
    }
    else if((cgrid[y][x]==hit)||(cgrid[y][x]==miss)||(cgrid[y][x]==sunk)){
        cout<<"You have already fired here. Try again."<<endl;
        uguess();
        return;
    }
}
void battleship::upsub(){
    int vh;
    string input;
    int x;
    int y;
    int offset;
    int start;
    cout<<"Now placing your submarine. How should it be oriented?\n1: Horizontal\n2: Vertical"<<endl;
    cin>>vh;
    vh--;
    cout<<"What square will the top left square of your submarine be on? (G8, E5, etc.)"<<endl;
    cin>>input;
    if((input[0]=='A')||(input[0]=='a'))y=0;
    if((input[0]=='B')||(input[0]=='b'))y=1;
    if((input[0]=='C')||(input[0]=='c'))y=2;
    if((input[0]=='D')||(input[0]=='d'))y=3;
    if((input[0]=='E')||(input[0]=='e'))y=4;
    if((input[0]=='F')||(input[0]=='f'))y=5;
    if((input[0]=='G')||(input[0]=='g'))y=6;
    if((input[0]=='H')||(input[0]=='h'))y=7;
    if((input[0]=='I')||(input[0]=='i'))y=8;
    if((input[0]=='J')||(input[0]=='j'))y=9;
    if((input[1]=='1')&&(input[2]=='0'))x=9;
    if(input.size()==2){
        if(input[1]=='1')x=0;
        if(input[1]=='2')x=1;
        if(input[1]=='3')x=2;
        if(input[1]=='4')x=3;
        if(input[1]=='5')x=4;
        if(input[1]=='6')x=5;
        if(input[1]=='7')x=6;
        if(input[1]=='8')x=7;
        if(input[1]=='9')x=8;
    }
    if(vh==0){
        offset=y;
        start=x;
        if((start>7)||(ugrid[offset][start]==ship)||(ugrid[offset][start+1]==ship)||(ugrid[offset][start+2]==ship)){
            cout<<"This placement would intersect another ship or go off the board. Try again."<<endl;
            upsub();
            return;
        }
    }
    if(vh==1){
        offset=x;
        start=y;
        if((start>7)||(ugrid[start][offset]==ship)||(ugrid[start+1][offset]==ship)||(ugrid[start+2][offset]==ship)){
        cout<<"This placement would intersect another ship or go off the board. Try again."<<endl;
            upsub();
            return;
        }
    }
    usub.push_back(vh);
    usub.push_back(offset);
    for(int i=start; i<start+3; i++){
        usub.push_back(i);
    }
    if(usub[0]==0){
        for(int i=2; i<5; i++){
            ugrid[offset][usub[i]]=ship;
        }
    }
    if(usub[0]==1){
        for(int i=2; i<5; i++){
            ugrid[usub[i]][offset]=ship;
        }
    }
    dispugrid();
}
void battleship::uppat(){
    int vh;
    string input;
    int x;
    int y;
    int offset;
    int start;
    cout<<"Now placing your patrol boat. How should it be oriented?\n1: Horizontal\n2: Vertical"<<endl;
    cin>>vh;
    vh--;
    cout<<"What square will the top left square of your patrol boat be on? (G8, E5, etc.)"<<endl;
    cin>>input;
    if((input[0]=='A')||(input[0]=='a'))y=0;
    if((input[0]=='B')||(input[0]=='b'))y=1;
    if((input[0]=='C')||(input[0]=='c'))y=2;
    if((input[0]=='D')||(input[0]=='d'))y=3;
    if((input[0]=='E')||(input[0]=='e'))y=4;
    if((input[0]=='F')||(input[0]=='f'))y=5;
    if((input[0]=='G')||(input[0]=='g'))y=6;
    if((input[0]=='H')||(input[0]=='h'))y=7;
    if((input[0]=='I')||(input[0]=='i'))y=8;
    if((input[0]=='J')||(input[0]=='j'))y=9;
    if((input[1]=='1')&&(input[2]=='0'))x=9;
    if(input.size()==2){
        if(input[1]=='1')x=0;
        if(input[1]=='2')x=1;
        if(input[1]=='3')x=2;
        if(input[1]=='4')x=3;
        if(input[1]=='5')x=4;
        if(input[1]=='6')x=5;
        if(input[1]=='7')x=6;
        if(input[1]=='8')x=7;
        if(input[1]=='9')x=8;
    }
    if(vh==0){
        offset=y;
        start=x;
        if((start>8)||(ugrid[offset][start]==ship)||(ugrid[offset][start+1]==ship)){
            cout<<"This placement would intersect another ship or go off the board. Try again."<<endl;
            uppat();
            return;
        }
    }
    if(vh==1){
        offset=x;
        start=y;
        if((start>8)||(ugrid[start][offset]==ship)||(ugrid[start+1][offset]==ship)){
        cout<<"This placement would intersect another ship or go off the board. Try again."<<endl;
            uppat();
            return;
        }
    }
    upat.push_back(vh);
    upat.push_back(offset);
    for(int i=start; i<start+2; i++){
        upat.push_back(i);
    }
    if(upat[0]==0){
        for(int i=2; i<4; i++){
            ugrid[offset][upat[i]]=ship;
        }
    }
    if(upat[0]==1){
        for(int i=2; i<4; i++){
            ugrid[upat[i]][offset]=ship;
        }
    }
    dispugrid();
}
void battleship::updes(){
    int vh;
    string input;
    int x;
    int y;
    int offset;
    int start;
    cout<<"Now placing your destroyer. How should it be oriented?\n1: Horizontal\n2: Vertical"<<endl;
    cin>>vh;
    vh--;
    cout<<"What square will the top left square of your destroyer be on? (G8, E5, etc.)"<<endl;
    cin>>input;
    if((input[0]=='A')||(input[0]=='a'))y=0;
    if((input[0]=='B')||(input[0]=='b'))y=1;
    if((input[0]=='C')||(input[0]=='c'))y=2;
    if((input[0]=='D')||(input[0]=='d'))y=3;
    if((input[0]=='E')||(input[0]=='e'))y=4;
    if((input[0]=='F')||(input[0]=='f'))y=5;
    if((input[0]=='G')||(input[0]=='g'))y=6;
    if((input[0]=='H')||(input[0]=='h'))y=7;
    if((input[0]=='I')||(input[0]=='i'))y=8;
    if((input[0]=='J')||(input[0]=='j'))y=9;
    if((input[1]=='1')&&(input[2]=='0'))x=9;
    if(input.size()==2){
        if(input[1]=='1')x=0;
        if(input[1]=='2')x=1;
        if(input[1]=='3')x=2;
        if(input[1]=='4')x=3;
        if(input[1]=='5')x=4;
        if(input[1]=='6')x=5;
        if(input[1]=='7')x=6;
        if(input[1]=='8')x=7;
        if(input[1]=='9')x=8;
    }
    if(vh==0){
        offset=y;
        start=x;
        if((start>7)||(ugrid[offset][start]==ship)||(ugrid[offset][start+1]==ship)||(ugrid[offset][start+2]==ship)){
            cout<<"This placement would intersect another ship or go off the board. Try again."<<endl;
            updes();
            return;
        }
    }
    if(vh==1){
        offset=x;
        start=y;
        if((start>7)||(ugrid[start][offset]==ship)||(ugrid[start+1][offset]==ship)||(ugrid[start+2][offset]==ship)){
        cout<<"This placement would intersect another ship or go off the board. Try again."<<endl;
            updes();
            return;
        }
    }
    udes.push_back(vh);
    udes.push_back(offset);
    for(int i=start; i<start+3; i++){
        udes.push_back(i);
    }
    if(udes[0]==0){
        for(int i=2; i<5; i++){
            ugrid[offset][udes[i]]=ship;
        }
    }
    if(udes[0]==1){
        for(int i=2; i<5; i++){
            ugrid[udes[i]][offset]=ship;
        }
    }
    dispugrid();
}
void battleship::upbat(){
    int vh;
    string input;
    int x;
    int y;
    int offset;
    int start;
    cout<<"Now placing your battleship. How should it be oriented?\n1: Horizontal\n2: Vertical"<<endl;
    cin>>vh;
    vh--;
    cout<<"What square will the top left square of your battleship be on? (G8, E5, etc.)"<<endl;
    cin>>input;
    if((input[0]=='A')||(input[0]=='a'))y=0;
    if((input[0]=='B')||(input[0]=='b'))y=1;
    if((input[0]=='C')||(input[0]=='c'))y=2;
    if((input[0]=='D')||(input[0]=='d'))y=3;
    if((input[0]=='E')||(input[0]=='e'))y=4;
    if((input[0]=='F')||(input[0]=='f'))y=5;
    if((input[0]=='G')||(input[0]=='g'))y=6;
    if((input[0]=='H')||(input[0]=='h'))y=7;
    if((input[0]=='I')||(input[0]=='i'))y=8;
    if((input[0]=='J')||(input[0]=='j'))y=9;
    if((input[1]=='1')&&(input[2]=='0'))x=9;
    if(input.size()==2){
        if(input[1]=='1')x=0;
        if(input[1]=='2')x=1;
        if(input[1]=='3')x=2;
        if(input[1]=='4')x=3;
        if(input[1]=='5')x=4;
        if(input[1]=='6')x=5;
        if(input[1]=='7')x=6;
        if(input[1]=='8')x=7;
        if(input[1]=='9')x=8;
    }
    if(vh==0){
        offset=y;
        start=x;
        if((start>6)||(ugrid[offset][start]==ship)||(ugrid[offset][start+1]==ship)||(ugrid[offset][start+2]==ship)||
        (ugrid[offset][start+3]==ship)){
            cout<<"This placement would intersect another ship or go off the board. Try again."<<endl;
            upbat();
            return;
        }
    }
    if(vh==1){
        offset=x;
        start=y;
        if((start>6)||(ugrid[start][offset]==ship)||(ugrid[start+1][offset]==ship)||(ugrid[start+2][offset]==ship)||
        (ugrid[start+3][offset]==ship)){
        cout<<"This placement would intersect another ship or go off the board. Try again."<<endl;
            upbat();
            return;
        }
    }
    ubat.push_back(vh);
    ubat.push_back(offset);
    for(int i=start; i<start+4; i++){
        ubat.push_back(i);
    }
    if(ubat[0]==0){
        for(int i=2; i<6; i++){
            ugrid[offset][ubat[i]]=ship;
        }
    }
    if(ubat[0]==1){
        for(int i=2; i<6; i++){
            ugrid[ubat[i]][offset]=ship;
        }
    }
    dispugrid();
}
void battleship::upcar(){
    int vh;
    string input;
    int x;
    int y;
    int offset;
    int start;
    cout<<"Now placing your aircraft carrier. How should it be oriented?\n1: Horizontal\n2: Vertical"<<endl;
    cin>>vh;
    vh--;
    cout<<"What square will the top left square of your carrier be on? (G8, E5, etc.)"<<endl;
    cin>>input;
    if((input[0]=='A')||(input[0]=='a'))y=0;
    if((input[0]=='B')||(input[0]=='b'))y=1;
    if((input[0]=='C')||(input[0]=='c'))y=2;
    if((input[0]=='D')||(input[0]=='d'))y=3;
    if((input[0]=='E')||(input[0]=='e'))y=4;
    if((input[0]=='F')||(input[0]=='f'))y=5;
    if((input[0]=='G')||(input[0]=='g'))y=6;
    if((input[0]=='H')||(input[0]=='h'))y=7;
    if((input[0]=='I')||(input[0]=='i'))y=8;
    if((input[0]=='J')||(input[0]=='j'))y=9;
    if((input[1]=='1')&&(input[2]=='0'))x=9;
    if(input.size()==2){
        if(input[1]=='1')x=0;
        if(input[1]=='2')x=1;
        if(input[1]=='3')x=2;
        if(input[1]=='4')x=3;
        if(input[1]=='5')x=4;
        if(input[1]=='6')x=5;
        if(input[1]=='7')x=6;
        if(input[1]=='8')x=7;
        if(input[1]=='9')x=8;
    }
    if(vh==0){
        offset=y;
        start=x;
        if((start>5)||(ugrid[offset][start]==ship)||(ugrid[offset][start+1]==ship)||(ugrid[offset][start+2]==ship)||
        (ugrid[offset][start+3]==ship)||(ugrid[offset][start+4]==ship)){
            cout<<"This placement would intersect another ship or go off the board. Try again."<<endl;
            upcar();
            return;
        }
    }
    if(vh==1){
        offset=x;
        start=y;
        if((start>5)||(ugrid[start][offset]==ship)||(ugrid[start+1][offset]==ship)||(ugrid[start+2][offset]==ship)||
        (ugrid[start+3][offset]==ship)||(ugrid[start+4][offset]==ship)){
        cout<<"This placement would intersect another ship or go off the board. Try again."<<endl;
            upcar();
            return;
        }
    }
    ucar.push_back(vh);
    ucar.push_back(offset);
    for(int i=start; i<start+5; i++){
        ucar.push_back(i);
    }
    if(ucar[0]==0){
        for(int i=2; i<7; i++){
            ugrid[offset][ucar[i]]=ship;
        }
    }
    if(ucar[0]==1){
        for(int i=2; i<7; i++){
            ugrid[ucar[i]][offset]=ship;
        }
    }
    dispugrid();
}
void battleship::uplace(){
    uppat();
    upsub();
    updes();
    upbat();
    upcar();
}
void battleship::uifsink(string name, vector<int> vec){
    if(vec[0]==0){
        for(int i=2; i<vec.size(); i++){
            if(cgrid[vec[1]][vec[i]]!=hit){
                return;
            }
        }
        for(int j=2; j<vec.size(); j++){
            cgrid[vec[1]][vec[j]]=sunk;
            viewgrid[vec[1]][vec[j]]=sunk;
        }
        cout<<"You sunk the computer's "<<name<<"!"<<endl;
        usunk++;
    }
    if(vec[0]==1){
        for(int i=2; i<vec.size(); i++){
            if(cgrid[vec[i]][vec[1]]!=hit){
                return;
            }
        }
        for(int j=2; j<vec.size(); j++){
            cgrid[vec[j]][vec[1]]=sunk;
            viewgrid[vec[j]][vec[1]]=sunk;
        }
        cout<<"You sunk the computer's "<<name<<"!"<<endl;
        usunk++;
    }
}
void battleship::usink(){
    uifsink("patrol boat", cpat);
    uifsink("submarine", csub);
    uifsink("destroyer", cdes);
    uifsink("battleship", cbat);
    uifsink("carrier", ccar);
}
void battleship::cifsink(string name, vector<int> vec){
    if(vec[0]==0){
        for(int i=2; i<vec.size(); i++){
            if(ugrid[vec[1]][vec[i]]!=hit){
                return;
            }
        }
        for(int j=2; j<vec.size(); j++){
            ugrid[vec[1]][vec[j]]=sunk;
        }
        cout<<"The computer sunk your "<<name<<"!"<<endl;
        csunk++;
    }
    if(vec[0]==1){
        for(int i=2; i<vec.size(); i++){
            if(ugrid[vec[i]][vec[1]]!=hit){
                return;
            }
        }
        for(int j=2; j<vec.size(); j++){
            ugrid[vec[j]][vec[1]]=sunk;
        }
        cout<<"The computer sunk your "<<name<<"!"<<endl;
        csunk++;
    }
}
void battleship::csink(){
    cifsink("patrol boat", upat);
    cifsink("submarine", usub);
    cifsink("destroyer", udes);
    cifsink("battleship", ubat);
    cifsink("carrier", ucar);
}
void battleship::gameplay(){
    uplace();
    cplace();
    while((usunk<5)&&(csunk<5)){
        uguess();
        usink();
        cin.ignore();
        dispcgrid();
        cin.ignore();
        cguess();
        csink();
        cin.ignore();
        dispugrid();
    }
    if(usunk==5){
        cout<<"Good job! You beat the computer!"<<endl;
        cin.ignore();
        exit(1);
    }
    if(csunk==5){
        cout<<"Too bad. You lost!"<<endl;
        cin.ignore();
        exit(2);
    }
}

Actual source code:
#include "battleship.h"

using namespace std;

int main(){
    battleship game;
    game.gameplay();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.