Question

Imagine we are using a two-dimensional array as the basis for creating the game battleship. In...

Imagine we are using a two-dimensional array as the basis for creating the game battleship. In the game of battleship a '~' character entry in the array represents ocean, a '#' character represents a place ion the ocean where part of a ship is present, and an 'H' character represents a place in the ocean where part of a ship is present and has been hit by a torpedo. Thus, a ship with all 'H' characters means the ship has been sunk. Declare a two-dimensional array that is 25x25 that represents the entire ocean and an If statement that prints "HIT" if a torpedo hits a ship given the coordinates X and Y. Write a C++ progam that will read in a file representing a game board with 25 lines where each line has 25 characters corresponding to the description above. An example file might look like:

-------------------------
-------------------------
-------------------------
-----#####---------------
-------------------------
---------------#---------
---------------#---------
---------------#---------
---------------#---------
---------------#---------
-------------------------
-----------#####---------
-------------------------
-#####-------------------
-------------------------
---------#####-----------
------------------#------
------------------#------
------------------#------
------------------#------
--#####-----------#------
-------------------------
-------------------------

-------------------------
-------------------------

(not displaying exactly, should be 25x25)

You should write a function called Fire that will take an X and Y coordinate and print "HIT" if a ship is hit and "MISS" if a ship is missed. If a ship is HIT you shoul update the array with an 'H' character to indicate the ship was hit. If a ship is hit that has already been hit at that location you should print "HIT AGAIN". You should write a second function called FleetSunk that will determine if all the ships have been sunk. Your C++ program must then call these functions until all the ships have been sunk, at which point the program should display "The fleet was destroyed!".

0 0
Add a comment Improve this question Transcribed image text
Answer #1


#include <iostream>
#include <fstream>
#include <string>

using namespace std;

string Fire(int x, int y, char ocean[][25]);
bool FleetSunk(char ocean[][25]);


int main() {
   int x{ 0 }, y{ 0 };
   char exiter = 'N';
   char ocean[25][25] = { "~" };
   ifstream OceanFile{ "ocean.txt" };
   string wave, report;

   // Populate the ocean from the file
   if (!OceanFile) cout << "File not found!" << endl;
   for (int i = 0; i < 25; i++) {
           getline(OceanFile, wave);
           for (int j = 0; j < 25; j++) {
               ocean[i][j] = wave[j];
           }
   }


   // Take in and validate user input until all ships are sunk
   while (FleetSunk(ocean) && exiter != 'Y') {
       cout << "Enter x,y coordinates separated by a space to fire or negative coordinates to exit:" << endl;
       cin >> x >> y;
       if (!cin.fail() && x >= 0 && x < 25 && y >= 0 && y < 25) {
           report = Fire(x, y, ocean);
           if (report == "HIT") {
               ocean[x][y] = 'H';
           }
           cout << report << endl;
       }
       else if (!cin.fail() && x < 0 || y < 0) {
           cout << "Would you like to exit? (Y/N)" << endl;
           cin >> exiter;

           // Secret function to view current game status
           if (exiter == 'P') {
               for (int i = 0; i < 25; i++) {
                   for (int j = 0; j < 25; j++) {
                       cout << ocean[i][j];
                   }
                   cout << endl;
               }
           }
       }
       else {
           cout << "Please enter two coordinates between 0 and 24 separated by a space." << endl;
           cin.clear();
           cin.ignore(256, '\n');
       }
   }

   OceanFile.close();
   system("Pause");
   return 0;
}


string Fire(int x, int y, char ocean[][25]) {
   string report = "MISS";
   char c = ocean[x][y];
   if (c == '#') {
       report = "HIT";
   } else if(c == 'H') {
       report = "HIT AGAIN";
   }

   return report;
}


bool FleetSunk(char ocean[][25]) {

   bool boats = false;

   // Iterate through the ocean looking for ships
   for (int i = 0; i < 25; i++) {
       for (int j = 0; j < 25; j++) {
           // Calculate character offset and check for unsunk ships
           if (char(ocean[i][j]) == '#') {
               boats = true;
           }
       }
   }

   if (!boats) {
       cout << "The fleet was destroyed." << endl;
   }

   return boats;
}

Add a comment
Know the answer?
Add Answer to:
Imagine we are using a two-dimensional array as the basis for creating the game battleship. In...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For your Project, you will develop a simple battleship game. Battleship is a guessing game for...

    For your Project, you will develop a simple battleship game. Battleship is a guessing game for two players. It is played on four grids. Two grids (one for each player) are used to mark each players' fleets of ships (including battleships). The locations of the fleet (these first two grids) are concealed from the other player so that they do not know the locations of the opponent’s ships. Players alternate turns by ‘firing torpedoes’ at the other player's ships. The...

  • Assignment - Battleship In 1967, Hasbro toys introduced a childrens game named “Battleship”. In the next...

    Assignment - Battleship In 1967, Hasbro toys introduced a childrens game named “Battleship”. In the next two assignments you will be creating a one-player version of the game. The game is extremely simple. Each player arranges a fleet of ships in a grid. The grid is hidden from the opponent. Here is an ASCII representation of a 10x10 grid. The ‘X’s represent ships, the ‘~’s represent empty water. There are three ships in the picture: A vertical ship with a...

  • i need to to create a battleship game in c++ using 2d array and functions the...

    i need to to create a battleship game in c++ using 2d array and functions the pograms has to be 10*10 and have 2 enemy ship and 2 friendly ships each taking 3 spaces randomly placed first the program would display the user the full ocean and the issuer would need to input cordinates and much check that the corinates are correct if not ask again . when a the two enemies ships are drestroyed the game ends or if...

  • Write a program that deletes an element of a one-dimensional array of characters. The program should:...

    Write a program that deletes an element of a one-dimensional array of characters. The program should: Ask user to input the number of characters in the array the values of the array a character to be deleted Call a method to delete the character Print the resulting array or, if the character is not found, print “Value not found” The method called by the main program should: Pass the array and the character to be found as parameters If the...

  • i need this in C# please can any one help me out and write the full...

    i need this in C# please can any one help me out and write the full code start from using system till end i am confused and C# is getting me hard.I saw codes from old posts in Chegg but i need the ful complete code please thanks. Module 4 Programming Assignment – OO Design and implementation (50 points) Our Battleship game needs to store a set of ships. Create a new class called Ships. Ships should have the following...

  • Make sure to include: Ship.java, ShipTester.java, Location.java, LocationTester.java, Grid.java, GridTester.java, Player.java, PlayerTester.java, battleship.java. please do every...

    Make sure to include: Ship.java, ShipTester.java, Location.java, LocationTester.java, Grid.java, GridTester.java, Player.java, PlayerTester.java, battleship.java. please do every part, and include java doc and comments Create a battleship program. Include Javadoc and comments. Grade 12 level coding style using java only. You will need to create a Ship class, Location class, Grid Class, Add a ship to the Grid, Create the Player class, the Battleship class, Add at least one extension. Make sure to include the testers. Starting code/ sample/methods will be...

  • I haven't code in awhile and would like some help getting back on my feet. Need to code this in C++. Thank you! In...

    I haven't code in awhile and would like some help getting back on my feet. Need to code this in C++. Thank you! In this homework, you will implement a simple version of the game Battleship, in which the player tries to destroy ships hidden on a map using a system of coordinates. In this program, the map will be represented by a 4x4 matrix of integers. A 1 in the matrix represents the presence of a ship. There are...

  • Create a base class called WaterVehicle that has: -length of ship (in number of grid spaces)...

    Create a base class called WaterVehicle that has: -length of ship (in number of grid spaces) starting grid location -horizontal or vertical orientation on grid -sunk (boolean) Then create a class called Submarine that is derived from WaterVehicle and has the following additional properties: -dive depth -surfaced (Boolean) Be sure your classes have a reasonable complement of constructors, accessor, and mutator methods including a public function to determine if the Submarine was hit by a torpedo and whether a hit...

  • C#: Implement a multiplayer Battleship game with AI The rules are the same as before. The...

    C#: Implement a multiplayer Battleship game with AI The rules are the same as before. The game is played on an NxN grid. Each player will place a specified collection of ships: The ships will vary in length (size) from 2 to 5; There can be any number or any size ship. There may be no ships of a particular size; EXCEPT the battleship – which there will always be 1 and only 1. Player order will be random but...

  • 1.The code box below includes a live 2 dimensional array variable called gridNums. This array holds...

    1.The code box below includes a live 2 dimensional array variable called gridNums. This array holds integers. You cannot see its declaration or initialization. What value is in the zeroth (initial) position of the second row? Print this array entry to the console. (Note: remember that the second row is actually row 1). Enter your code in the box below. 2.The code box below includes a live 3x3 2-dimensional array variable called fredsNums. This array holds integers. You cannot see...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT