Question
I'm tasked with writing a C++ 2 dimensional array as the basis for battleship. Including the following: Board is 25x25 ~ = spaces # =
I'm tasked with writing a C++ 2 dimensional array as the basis for battleship. Including the following:
Board is 25x25
~ = spaces
# = ships
H= Hit
After fixing the code errors I cannot seem to get the program to indicate a hit. No matter what coordinate I enter the program returns a miss even when a hit has happened. Is the error with the reading of the game board? I have included what the input file looks like below my code. There are no code errors being returned after compiling.
*********************************************************************************************************************************************************************************************************************
#include
using namespace std;
ifstream infile;
const int widthGameBoard = 25; const int heightGameBoard = 25;
void Fire(const int widthGameBoard, const int heightGameBoard, int X, int Y, char gameBoard[][25]); void FleetSunk(const int widthGameBoard, const int heightGameBoard, int X, int Y, char gameBoard[][25], bool& GameOver);
int main() { //Takes Input File infile.open("BattleShip_Board.txt");
//Vairables char gameBoard[widthGameBoard][heightGameBoard]; int X; int Y; bool GameOver;
for (int i = 0; i < 25; i++) { for (int j = 0; j < 25; j++) infile>> gameBoard[i][j]; }
for (int i = 0; i < 25; i++) { for (int j = 0; j < 25; j++) infile >>gameBoard[i][j]; }
GameOver = false; while (!GameOver)
{ cout << endl; cout << " To attack enter X coordinate from 0 to 25" << endl << endl; cin >> X; cout << endl; cout << "To attack enter Y coordinate from 0 to 25" << endl << endl; cin >> Y; cout << endl;
Fire(heightGameBoard, widthGameBoard, X, Y, gameBoard); FleetSunk(heightGameBoard, widthGameBoard, X, Y, gameBoard, GameOver); }
//Closing Program Statements infile.close(); system("pause"); return 0;
}
//function declarations void Fire(const int widthGameBoard, const int heightGameBoard, int X, int Y, char gameBoard[][25])
{ if (gameBoard[X][Y] == '#') { cout << "HIT" << endl; gameBoard[X][Y] = 'H'; }
else if (gameBoard[X][Y] == 'H') { cout << "HIT AGAIN" << endl; }
else { cout << "MISS" << endl; } }
void FleetSunk(const int widthGameBoard, const int heightGameBoard, int X, int Y, char gameBoard[][25], bool& GameOver)
{ bool NoPound; NoPound = false;
for (int i = 0; i < 25; i++) { for (int j = 0; j < 25; i++) if (gameBoard[i][j] == '~' || gameBoard[i][j] == 'H') NoPound = true; else { NoPound = false; break; } if (NoPound == false) break; }
if (NoPound == true)
{ GameOver = true; cout << "Enemey Fleet Destroyed!" << endl; cout << "Game Over" << endl << endl; } }
*********************************************************************************************************************************************************************************************************************
Input file looks like this:
~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~ ~~#~~~~~~~~~~~~~~~~~~~~~~ ~~#~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~###~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~#~~~~~~~ ~~~###~~~~~~~~~~~#~~~~~~~ ~~~~~~~~~~~~~~~~~#~~~~~~~ ~~~~~~~~~~~~~~~~~#~~~~~~~ ~~~~~~~~~~~~~~~~~#~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~#~~~~~~~~~~~~~~~~~~ ~~~~~~#~~~~~~~~~~~~~~~~~~ ~~~~~~#~~~~~~~~~~~~~~~~~~ ~~~~~~#~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started