I need help understanding what is going wrong with this program.
I am attempting to create a Battleship c++ program for a class project, which I've been halfway successful so far...
It's a fairly simple program but I can't seem to find where I went wrong; The output will always get up to the point where it asks the user to enter the X & Y coordinates to hit the enemy, and will then end up going into an endless loop.
Code:
/***************************************************************************************************************************
This program will simulate a game of Battleship with 10 1x1 boats on a 10x10 grid. *
The program will generate 10 random coordinates(10 numbers for x coordinate and 10 numbers for y coordinate) for the user * *
and the for the opponent. The program will then ask the user to input one x coordinate and one y coordinate, *
which will trigger to program to decide if the user's input matches with any of the opponent's coordinates. *
Then the program will select two random numbers, which will trigger the program to decide *
if those number mathc the user's coordinates. If any inputed coordinates match to the other's *
coordinates that will result a "HIT" for the one who gave the input, which will give *
that person 1 point. If the inputed coordinate IS NOT a match to the other's coordinate, then *
it will result in a "MISS". First person to 10 points wins. *
****************************************************************************************************************************/
int userX[10]; // User's X-axis coordinates.
int userY[10]; // User's Y-axis coordinates.
int enemyX[10]; // Opponent's X-axis coordinates.
int enemyY[10]; // Opponent's Y-axis coordinates.
int counter;
const int userMini = 1; // User's random number minimum value.
const int userMax = 10; // User's random number maximum value.
int i;
int count;
const int enemyMini = 1; // Computer's random number minimum value.
const int enemyMax = 10; // Computer's random number maximum value.
int j;
while (endAnswer == "y")
{
int validate = 0;
endAnswer = "z";
while (startAnswer != "y")
{
cout << "Hello Captain!\n";
cout << "Are you ready to play?\n";
cout << "Please enter 'y' for yes OR 'n' for no: ";
cin >> startAnswer;
cout << endl;
}
goodGuy();
for (counter = 1; counter <= 10; counter++)
{
i = counter - 1;
cout << endl;
cout << "The enemy's boats are set!\n";
system("PAUSE");
cout << endl;
cout << endl;
cout << "The game will now begin!\n";
cout << endl;
gameplay( userX, userY, enemyX, enemyY);
while (validate == 0)
{
cout << "Would you like to restart?\n";
cout << "Please enter 'y' for yes OR 'n' for no: ";
cin >> endAnswer;
if (endAnswer != "y" && endAnswer != "n")
{
validate = 0;
cout << "Invaild input. Try again.\n";
/****************************************************************
This module will display when the user's boats are being set. *
*****************************************************************/
void goodGuy()
{
cout << "Your boats are being set!\n";
rules();
cout << endl;
}
/**************************************************************************************
This module will display when the opponent's (AKA the computer) boats are being set. *
***************************************************************************************/
void badGuy()
{
cout << "The enemy's boats are being set!\n";
cout << "GET READY CAPTAIN!!";
cout << endl;
}
/*******************************************************************************
This module will display the rules of the game as the "boats" are being set. *
********************************************************************************/
void rules()
{
cout << endl;
cout << "***RULES: \n";
cout << "The rules of the game are simple; You will take turns against the computer to defeat thier armada of BATTLESHIPS!\n";
cout << " The program will ask you to enter one number for the X-axis and one number for the Y-axis, each number MUST BE between 1 and 10.\n";
cout << "The program will then display to you if what you entered was a hit or a miss. There are a total of TEN targets, destroy all ten YOU WIN.\n";
cout << "The enemy will go first.\n";
cout << endl;
cout << "GOD SPEED, and GOOD LUCK Captain!\n";
cout << endl;
system("PAUSE");
Code (part 2):
/********************************************************************************
This module is the "behind the scenes" of the majority or the actual gameplay *
*********************************************************************************/
void gameplay(int goodX[], int goodY[], int badX[], int badY[])
{
int loopVar = 0;
const int enemyMini = 1;
const int enemyMax = 10;
int enemyShotX;
int enemyShotY;
int userShotX;
int userShotY;
int goodBoats = 10;
int badBoats = 10;
int goodSearch;
int badSearch;
int valid = 0;
Hello Captain!
Are you ready to play?
Please enter 'y' for yes OR 'n' for no: y
Your boats are being set!
***RULES:
The rules of the game are simple; You will take turns against the computer to defeat thier armada of BATTLESHIPS!
The program will ask you to enter one number for the X-axis and one number for the Y-axis, each number MUST BE between 1 and 10.
The program will then display to you if what you entered was a hit or a miss. There are a total of TEN targets, destroy all ten YOU WIN.
The enemy will go first.
GOD SPEED, and GOOD LUCK Captain!
Press any key to continue . . .
Your boats are set!
Press any key to continue . . .
The enemy's boats are being set!
GET READY CAPTAIN!!
Press any key to continue . . .
The enemy's boats are set!
Press any key to continue . . .
The game will now begin!
You have: 10 Boats.
************************
The enemy has: 10 Boats.
Press any key to continue . . .
MISS! You still have: 10 Boats left, Captain!
It's your turn Captain!
Please enter the X-axis coordinate you would like to fire at: 3
Please enter the Y-axis coordinate you would like to fire at: 5
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
MISS! You still have: 10 Boats left, Captain!
YOU HAVE BEEN HIT!!
You now have: 9 Boats left!
MISS! You still have: 9 Boats left, Captain!
** It will do this process until the user runs out of boats. Which goes on about 1000 more times.