View Single Post
Old 05-14-2011   #2
Adam
Registered Member
 
Adam's Avatar
 
Last Online: 06-03-2013
Join Date: Feb 2008
Posts: 1,410
Thanks: 1,444
Thanked 1,235 Times in 689 Posts
Groans: 25
Groaned at 21 Times in 19 Posts
Default

I made this when a while ago, it's a text based version of the game battleship.
cons: It doesn't contain any images and it's written in main.

Code:
#include <iostream>
#include <string>

using namespace std;

void setNames(string names[]);
int  setNrOfShips();
void setPositions(int** board, string names[], int nrOfShips);
bool checkPositionVacancy(int** board, int pos, int nrOfShips, int i);
void printInfo(int** board, string names[], int nrOfShips, int i);
int  getNrOfShipsLeft(int** boards, int nrOfShips, int i);
bool checkForWinner(int** boards, int nrOfShips);
void play(int** boards, string names[], int nrOfShips);
void AnnounceWinner(int** boards, string names[], int nrOfShips);

int main()
{
    int nrOfShips = setNrOfShips();
    system("CLS");
    int** boards = new int*[2];
    for(int i = 0; i < 2; i++)
    {
        boards[i] = new int[nrOfShips];
    }
    string names[2] = {""};
    setNames(names);
    system("CLS");
    setPositions(boards, names, nrOfShips);

    do
    {
        system("CLS");
        play(boards, names, nrOfShips);
    }
    while(getNrOfShipsLeft(boards, nrOfShips, 0) != 0 && getNrOfShipsLeft(boards, nrOfShips, 1) != 0);

    system("CLS");

    AnnounceWinner(boards, names, nrOfShips);

    for(int i = 0; i < 2; i++)
    {
        delete [] boards[i];
    }
    delete [] boards;

    return 0;
}

void setNames(string names[])
{
    string name;
    cout << "Input Names!\n\n";
    cin.ignore();
    for(int i = 0; i < 2;i++)
    {
        cout << "Player #" << i+1 << ": ";
        getline(cin, name);
        names[i] = name;
    }
}

int setNrOfShips()
{
    int nrOfShips = 0;
    bool flag = false;

    do
    {
        system("CLS");
        cout << "Number of ships (max 14): ";
        cin  >> nrOfShips;
        if(nrOfShips > 0 && nrOfShips < 15)
        {
            flag = true;
        }
        else
        {
            system("CLS");
            cout << "Invalid input!\n";
            system("pause");
        }
    }
    while(!flag);
    return nrOfShips;
}

void setPositions(int** board, string names[], int nrOfShips)
{
    int pos = 0;
    bool flag = false;
    for(int i = 0; i < 2; i++)
    {
        for(int j = 0; j < nrOfShips; j++)
        {
            system("CLS");
            do
            {
                flag = false;
                cout << names[i] << endl;
                for(int k = 0; k < j; k++)
                {
                    cout << "Ship #" << k+1 << ": " << board[i][k] << endl;
                }
                cout << "Ship #" << j+1 << ": ";
                cin  >> pos;
                if(pos > 0 && pos < 16 && checkPositionVacancy(board, pos, nrOfShips, i))
                {
                    board[i][j] = pos;
                    flag = true;
                }
                else if(pos > 0 && pos < 16)
                {
                    cout << "Position is not vacant!\n";
                    system("pause");
                    system("CLS");
                }
                else
                {
                    cout << "Invalid position!\n";
                    system("pause");
                    system("CLS");
                }
            }
            while(!flag);
        }
    }
}

bool checkPositionVacancy(int** board, int pos, int nrOfShips, int i)
{
    int counter = -1;
    bool vacant = true;

    do
    {
        counter++;
        if(board[i][counter] == pos)
        {
            vacant = false;
        }
    }
    while(vacant && counter < nrOfShips);

    return vacant;
}

void printInfo(int** board, string names[], int nrOfShips, int i)
{
    cout << names[i] << "\t";
    cout << "Friendly Ships left: " << getNrOfShipsLeft(board, nrOfShips, i) << "/" << nrOfShips << "\t";
    cout << "Enemy Ships Left: " << getNrOfShipsLeft(board, nrOfShips, !i) << "/" << nrOfShips << endl;
}

int getNrOfShipsLeft(int** boards, int nrOfShips, int i)
{
    int shipsLeft = nrOfShips;
    for(int k = 0; k < nrOfShips; k++)
    {
        if(boards[i][k] == 0)
        {
            shipsLeft--;
        }
    }

    return shipsLeft;
}

void play(int** boards, string names[], int nrOfShips)
{
    int target = 0,
        counter = -1;
    bool flag = false,
         hit = false;

    for(int i = 0; i < 2; i++)
    {
        do
        {
            flag = false;
            system("CLS");
            printInfo(boards, names, nrOfShips, i);
            cout << "\nTarget (1-15): ";
            cin  >> target;
            if(target < 1 || target > 15)
            {
                cout << "Invalid target!\n";
                system("pause");
            }
            else
            {
                flag = true;
            }
        }
        while(!flag);

        counter = -1;

        do
        {
            hit = false;
            counter++;
            if(boards[!i][counter] == target)
            {
                boards[!i][counter] = 0;
                hit = true;
            }
        }
        while(!hit && counter < nrOfShips);

        if(hit)
        {
            cout << "Target Hit!\n";
            system("pause");
        }
        else
        {
            cout << "Miss!\n";
            system("pause");
        }
    }
}

void AnnounceWinner(int** boards, string names[], int nrOfShips)
{
    if(getNrOfShipsLeft(boards, nrOfShips, 0) == 0 && getNrOfShipsLeft(boards, nrOfShips, 1) == 0)
    {
        cout << "It's a tie!\n\n";
    }
    else if(getNrOfShipsLeft(boards, nrOfShips, 0) == 0)
    {
        cout << names[1] <<  " is the Winner!\n\n";
    }
    else if(getNrOfShipsLeft(boards, nrOfShips, 1) == 0)
    {
        cout << names[0] <<  " is the Winner!\n\n";
    }
}

Last edited by Adam; 05-14-2011 at 11:09 PM.
Adam is offline   Reply With Quote