Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 #include #include #include #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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Security XI Status And Prospects

Authors: T.Y. Lin, Shelly Qian

1st Edition

0412820900, 978-0412820908

More Books

Students also viewed these Databases questions

Question

1. The evaluation results can be used to change the program.

Answered: 1 week ago

Question

5. To determine the financial benefits and costs of the program.

Answered: 1 week ago