Battleship console game This is a battleship console game I wrote in C++.

#include &lt;string&gt;
#include &lt;iostream&gt;
#include &lt;process.h&gt;
#include &lt;cstdlib&gt;
#include &lt;time.h&gt;
using namespace std;




int const ROW = 3;
int const COL = 3;


int subX, subY;


int guesses = 5;


int board[ROW][COL];


bool hasWon = false;


void winScreen()
{
    cout &lt;&lt; "You won with " &lt;&lt; guesses &lt;&lt; " guesses left! :) " &lt;&lt; endl;
}


void generateGrid()
{
    for (int x = 0; x &lt; ROW; x++)
    {
        for (int y = 0; y &lt; COL; y++)
        {
            //0 = empty
            //1 = has guessed
            //2 = sub


            //empty board
            board[x][y] = 0;
            cout &lt;&lt; "-" &lt;&lt; x &lt;&lt; "," &lt;&lt; y &lt;&lt; "- ";
        }
        cout &lt;&lt; endl;
    }
}


void generateSubCoords()
{
    subX = 0 + (rand() % ROW);
    subY = 0 + (rand() % COL);
    //2 = sub
    board[subX][subY] = 2;
}


void displayGrid(int changeX, int changeY)
{

    cout &lt;&lt; "_______________________________________" &lt;&lt; endl;
    for (int x = 0; x &lt; ROW; x++)
    {
        for (int y = 0; y &lt; COL; y++)
        {
            if (x == changeX &amp;&amp; y == changeY)
            {
                if (board[x][y] != 2)
                {
                    board[x][y] = 1;
                }
            }

            if (board[x][y] == 0)
            {
                cout &lt;&lt; "-" &lt;&lt; x &lt;&lt; "," &lt;&lt; y &lt;&lt; "- ";
            }
            else if (board[x][y] == 1)
            {
                cout &lt;&lt; "-X,X- ";
            }

            if (board[x][y] == 2 )
            {
                if (x == changeX &amp;&amp; y == changeY)
                {
                    cout &lt;&lt; "-HIT- ";
                }
                else
                {
                    cout &lt;&lt; "-" &lt;&lt; x &lt;&lt; "," &lt;&lt; y &lt;&lt; "- ";
                }
            }

        }
        cout &lt;&lt; endl;
    }
}


void playerGuess()
{

    int guessX, guessY;


    bool goodInput = false;

    cout &lt;&lt; endl;
    do {
        cout &lt;&lt; guesses &lt;&lt; " guesses left. " &lt;&lt; endl;


        //get X pick
        cout &lt;&lt; "X: ";
        cin &gt;&gt; guessX;
        if (guessX &lt; 0 || guessX&gt;2)
        {
            goodInput = false;
            cin.clear();
            cin.ignore(256, '\n');
            cout &lt;&lt; "Out of Range!" &lt;&lt; endl;
            //display grid again with X's on board where guessed
            break;
        }
        else
        {
            goodInput = true;
        }




        //get Y pick
        cout &lt;&lt; "Y: ";
        cin &gt;&gt; guessY;
        if (guessY &lt; 0 || guessY&gt;2)
        {
            goodInput = false;
            cin.clear();
            cin.ignore(256, '\n');
            cout &lt;&lt; "Out of Range!" &lt;&lt; endl;
            //display grid again with X's on board where guessed
            break;
        }
        else
        {
            goodInput = true;
        }





        displayGrid(guessX, guessY);
        cout &lt;&lt; endl;
        guesses--;


    } while (!goodInput);


    //if guessed right
    if (guessX == subX &amp;&amp; guessY == subY)
    {
        hasWon = true;
        winScreen();
    }
}




void main()
{
    srand(time(NULL));
    cout &lt;&lt; "BATTLE-SHIP 2000" &lt;&lt; endl &lt;&lt; endl;
    int board[ROW][COL];


    generateGrid();
    generateSubCoords();



    do
    {
        if (hasWon)
        {
            break;
        }


        playerGuess();
    } while (guesses &gt; 0);

    //if lost
    if (!hasWon)
        cout &lt;&lt; "Sorry, you lost. :(" &lt;&lt; endl;

    cout &lt;&lt; "The sub was located at -" &lt;&lt; subX &lt;&lt; "," &lt;&lt; subY &lt;&lt; "-." &lt;&lt; endl;
}


I really feel like I'm overcomplicating things. I feel like I overuse do whiles and I should try to use a catch and throw option or something a little more creative. Any suggestions would be appreciated.
Using namespace std

Please review this post on why you should not use using namespace std: Why is “using namespace std” considered bad practice?

Enums


//0 = empty
//1 = has guessed
//2 = sub



You should use an enum to represent those values:

enum SquareType {
    Empty = 0,
    Guessed = 1,
    Sub = 2
};


Now, instead of doing the confusing:


if (board[x][y] == 0)
{
    ...
}
else if (board[x][y] == 1)
{
    ...
}
if (board[x][y] == 2)
{
    ...
}



You can do:

if (board[x][y] == SquareType.Empty)
{
    ...
}
else if (board[x][y] == SquareType.Guessed)
{
    ...
}
if (board[x][y] == SquareType.Sub)
{
    ...
}


Style

You have way too much vertical space in your program.  You should really only use 1 empty line at a time.

Your bracing is inconsistent.  Either always use them (preferred), or use them as little as possible.

Input

You never use the do-while loop in playerGuess.  If you carefully check the runtime, you break directly out of the loop every time you have bad input.  That gives the loop a chance to continue if, and only if, you get two good inputs, but getting two good inputs automatically invalidates the continuation condition.

Notice how similar the two sections in the following code is.  Duplication everywhere.


//get X pick
cout &lt;&lt; "X: ";
cin &gt;&gt; guessX;
if (guessX &lt; 0 || guessX&gt;2)
{
    goodInput = false;
    cin.clear();
    cin.ignore(256, '\n');
    cout &lt;&lt; "Out of Range!" &lt;&lt; endl;
    //display grid again with X's on board where guessed
    break;
}
else
{
    goodInput = true;
}

//get Y pick
cout &lt;&lt; "Y: ";
cin &gt;&gt; guessY;
if (guessY &lt; 0 || guessY&gt;2)
{
    goodInput = false;
    cin.clear();
    cin.ignore(256, '\n');
    cout &lt;&lt; "Out of Range!" &lt;&lt; endl;
    //display grid again with X's on board where guessed
    break;
}
else
{
    goodInput = true;
}



You should join these into a single input function:

int input(string prompt)
{
    int guess = -1;

    do {
        cout &lt;&lt; prompt;
        cin &gt;&gt; guess;
        if (guess &lt; 0 || guess &gt; 2)
        {
            guess = -1;
            cin.clear();
            cin.ignore(256, '\n');
            cout &lt;&lt; "Out of Range!" &lt;&lt; endl;
        }
    } while (guess == -1)

    return guess;
}


Then call it like:

//get X pick
guessX = input("X: ")

//get Y pick
guessY = input("Y: ")


This will also ensure the function gets good input back the first time input() returns.

main()

Your main() function should only be the entrance to your program.  You typically should not use main() to do work--create a function who's sole responsibility is doing that work.  Right now, you gave main() two responsibilities--starting the program and running it.

Exceptions

Only use exceptions for exceptional behavior, not for control flow.  Getting bad input is never exceptional--it is to be expected.  Simple programs like this should almost never throw exceptions deliberately.