Please help me with this need a C code for it Please follow

Please help me with this, need a C++ code for it. Please follow the requirements.

For battleship game, we\'ll be using a 10x10 array of characters. The board will be where the ships are placed. The top left corner of the array is 0,0. The lower right corner is 9,9. To play the game, we\'ll be given an input file that is a series of commands. The first command will be the name of the board file that contains the ship information. The rest of the file will be a series of try commands or display commands. I\'ll put a sample of the command file at the end since it can get long.

The board will be a set of 5 lines. The lines will indicate a ship, it\'s place on the board where it starts and the direction the ship is pointing, left, right, up or down. Errors can occur in the board file. There are 5 errors that can and will occur. They are as follows:

A ship that is trying to be placed and the starting position is off the board.

A ship that is trying to be placed and goes off the board during placement.

A ship that overlaps with a ship that is already placed on the board.

A ship in the file that is not one of the 5 ships we place.

A direction that is not one of the 4 directions we have.

If any of these errors occur, the game ceases and an error is displayed. The messages and the errors will be listed below and in the samples.

Assuming we get past the loading phase, then we will start processing the tries that occur in the file. The try simply is where you want to drop a bomb. If you hit a ship, then an X is stored and if you miss, then an O is stored. Once all the ships are sunk, then the game is over. The game is also over if the input runs out before all the ships are sunk. When the game is over you will say the game is over and display the board (as if an implied display command were given).

The input to the battleship function will be the name of a command file. The command file will contain the board file to load and then a series of commands.

Once the input is over, assuming no errors in loading and during play, the game should be over and the board is displayed to show the hits and misses. If the input ends before the game is over, then we still display Game Over and the board with as much of the hits and misses as we had. If there are errors during loading, then we don\'t show the Game Over message nor the board.

Requirements

You must use a 10x10 array of characters (or integers or other built-in type) for the board. You may have a 3D array if you wish. It must be at least a 2D Array.

You may not use the STL. You may use string, ifstream, ofstream, and the other items we have discussed.

You may not use classes to implement this.

You must implement each command as a function, e.g. a try function, a load function, a display function.

You may not use structs or other aggregate user defined data types, e.g. classes, structs, tuples, etc.

You must declare the following function void battleship( string commands, string output ); in a file named battleship.h

You may not use global variables, global constants are fine.

Input

This is a sample board file that contains the correct ships and directions:

The ships are as follows:

A is an aircraft carrier and is 5 spaces long.

B is a battleship and is 4 spaces long.

C is a carrier and is 3 spaces long.

S is a submarine and is 3 spaces long.

D is a destroyer and is 2 spaces long.

The spaces for the rows are given as letters and are from A-J. The A row corresponds to the top row, index 0. The J row corresponds to the bottom row, index 9. The columns are given 1-10. The 1 column is the first column on the left or index 0. The 10 column is the last column on the right and is the 9 index.

Here\'s a sample of the input file for the commands for the game:

If the board in the sample above is board10.txt and the commands above are given, this is the output:

Solution

Header file:

/*
* battleship.h
*
* Created on: 02-Nov-2016
* Author: Kaju
*/

#ifndef BATTLESHIP_H_
#define BATTLESHIP_H_
#include <string>
using namespace std;

void battleship(string commands, string output);

#endif /* BATTLESHIP_H_ */

battleship.cpp

#include \"battleship.h\"

#include <fstream>

#include <iostream>

#include <sstream>

#include \"string.h\"

#include \"stdlib.h\"

using namespace std;

//variables to hold the values from the file

string ships[5] = {}; // all the ship labels

char rows[5] = {}; // ships row indexes

int column[5] = {}; //ships column indexes

char directions[5] = {}; // ship directions

char tryCommandRow[100] = {}; // try command rows

int tryCommandColumn[100] = {}; // try command columns

char battleships[10][10] = {}; // this is the 10x10 array

void tryCommands() // the try command which will print the command and its result

{

   int num_of_commands = sizeof(tryCommandRow)/sizeof(tryCommandRow[0]);

   int counter = 1;

   int aircraft = 0;

   int cruiser = 0;

   int submarine = 0;

   int battleship =0;

   int destroyer = 0;

   for(int i=0; i<num_of_commands; i++)

   {

       int rowInt = tryCommandRow[i]-65;

       int columnInt = tryCommandColumn[i];

       cout<<\"Command: try: #\"<<counter<<\": \"<<tryCommandRow[i]<<\" \"<<columnInt<<endl;

       if(battleships[rowInt][columnInt] == \'.\')

       {

           cout<<\"\\tHit\"<<endl;

           battleships[rowInt][columnInt] = \'X\';

           //destroyer

           if((tryCommandRow[i] >= rows[0] && tryCommandRow[i] <= rows[0] + 1)&&( columnInt >= column[0]) &&(columnInt<=column[0] + 1))

           {

               destroyer++;

           }

           //battleship

           if((tryCommandRow[i] >= rows[0] && tryCommandRow[i] <= rows[0] + 1)&&( columnInt >= column[0]) &&(columnInt<=column[0] + 3))

           {

               battleship++;

           }

           //cruiser

           if((tryCommandRow[i] >= rows[0] && tryCommandRow[i] <= rows[0] + 1)&&( columnInt >= column[0]) &&(columnInt<=column[0] + 2))

           {

               cruiser++;

           }

           if((tryCommandRow[i] >= rows[0] && tryCommandRow[i] <= rows[0] + 1)&&( columnInt >= column[0]) &&(columnInt<=column[0] + 4))

           {

               aircraft++;

           }

           if((tryCommandRow[i] >= rows[0] && tryCommandRow[i] <= rows[0] + 1)&&( columnInt >= column[0]) &&(columnInt<=column[0] + 2))

           {

               submarine++;

           }

           if(aircraft == 5 || cruiser == 3 || submarine == 3 ||battleship == 4 || destroyer == 2)

           {

               cout<<\"\\t\\tYou sunk my \";

               if(aircraft == 5)

               {

                   cout<<\"Aircraft \";

                   aircraft = 0;

               }

               if(cruiser == 3)

               {

                   cout<<\"Cruiser \";

                   cruiser = 0;

               }

               if(submarine == 3)

               {

                   cout<<\"Submarine \";

                   submarine = 0;

               }

               if(battleship == 4)

               {

                   cout<<\"Battleship \";

                   battleship = 0;

               }

               if(destroyer == 2 )

               {

                   cout<<\"Destroyer \";

                   destroyer = 0;

               }

               cout<<endl;

           }

       }

       else

       {

           cout<<\"\\tMiss\"<<endl;;

       }

       counter++;

   }

}

This function reads the file and loads the data into the arrays

void loadFile(string commandFile)

{

   ifstream infile(commandFile);

   string line;

   string filename;

   getline(infile, filename);

   ifstream boardfile(filename);

   int counter = 0;

   while (getline(boardfile, line))

   {

       istringstream iss(line);

      if (!(iss >> ships[counter] >> rows[counter] >>column[counter]>>directions[counter]))

      {

         break;

      } // error

      counter++;

   }

   int tryCount = 0;

   while (getline(infile, line))

       {

           istringstream iss(line);

           string trycom;

          if (!(iss >> trycom>>tryCommandRow[tryCount] >> tryCommandColumn[tryCount]))

          {

             break;

          } // error

          tryCount++;

       }

}

// This function prints the 10x10 array output.

void display(string output)

{

   cout<<output<<endl;

   cout<<\" 1 2 3 4 5 6 7 8 9 10\"<<endl;

   char a =\'A\';

   for(int i=0; i<10; i++)

   {

   cout<<a++<<\" \";

   for(int j=0; j<10; j++)

       {

           cout<<battleships[i][j]<<\" \";

       }

       cout<<endl;

   }

}

// this function is for filling uo the 10x10 array with . values for ship

bool setupShips(int spaces, int row, int column)

{

   int counter = 0;

   bool dontStop = true;

   int error = false;

   while(counter<spaces)

       {

           if(battleships[row][column] != \'0\')

               error = true;

           else{

               battleships[row][column] = \'.\';

               counter++;

               column++;

               //re initializing for the second row

               if((counter == spaces) && dontStop)

               {

                   row++;

                   counter = 0;

                   column = column-spaces;

                   dontStop = false;

               }

           }

       }

   return error;

}

// this function calls all the remaining functions - handler function

void battleship(string commands, string output)

{

   loadFile(commands);

   //filling up the array with values. ie. 0 for empty places and . for where the ship is

   int row[5];

   for(int i=0; i<5; i++)

   {

       row[i] = rows[i] - 65;

   }

   // filling with zeros first

   for(int i=0; i<10; i++)

       {

           for(int j=0; j<10; j++)

           {

               battleships[i][j] = \'0\';

           }

       }

   // for D - Destroyer carrier 5 spaces long

   bool errorDestroyer = setupShips(2, row[0], column[0]);

   // B - battleship 4 spaces long

   bool errorBattleship = setupShips(4, row[1], column[1]);

   // C - Cruiser 3 spaces long

   bool errorCruiser = setupShips(3, row[2], column[2]);

   // A - Aircraft 4 spaces long

   bool errorAircraft = setupShips(5, row[3], column[3]);

   // S - Submarine 3 spaces long

   bool errorSub = setupShips(3, row[4], column[4]);

   if(!errorDestroyer && !errorBattleship && !errorCruiser && !errorAircraft && !errorSub)

   {

       tryCommands();

       display(output);

   }

}

int main()

{

   battleship(\"board10.txt\", \"Game is Over\");

   return 0;

}

The following output is after changing the board.txt file. The file posted with the question was making ships to overlap. The result of which would be nothing based ont he requirements.
OUTPUT:

Command: try: #1: G 7

   Hit

Command: try: #2: H 5

   Hit

Command: try: #3: E 7

   Miss

Command: try: #4: G 9

   Miss

Command: try: #5: D 7

   Hit

Command: try: #6: I 5

   Hit

Command: try: #7: E 4

   Hit

Command: try: #8: D 10

   Miss

Command: try: #9: A 2

   Miss

Command: try: #10: A 6

   Miss

Command: try: #11: G 2

   Miss

Command: try: #12: C 8

   Hit

Command: try: #13: J 4

   Miss

Command: try: #14: C 7

   Hit

Command: try: #15: E 10

   Miss

Command: try: #16: D 4

   Hit

Command: try: #17: F 3

   Hit

Command: try: #18: J 5

   Miss

Command: try: #19: F 5

   Hit

Command: try: #20: A 8

   Miss

Command: try: #21: B 2

   Miss

Command: try: #22: J 6

   Miss

Command: try: #23: I 2

   Hit

Command: try: #24: D 8

   Hit

Command: try: #25: I 8

   Miss

Command: try: #26: B 5

   Miss

Command: try: #27: B 9

   Miss

Command: try: #28: I 10

   Miss

Command: try: #29: G 1

   Miss

Command: try: #30: B 10

   Miss

Command: try: #31: D 6

   Hit

Command: try: #32: I 6

   Miss

Command: try: #33: B 3

   Hit

Command: try: #34: J 10

   Miss

Command: try: #35: B 6

   Miss

Command: try: #36: J 3

   Miss

Command: try: #37: C 2

   Miss

Command: try: #38: C 4

   Miss

Command: try: #39: A 4

   Hit

       You sunk my Destroyer

Command: try: #40: A 1

   Miss

Command: try: #41: J 9

   Miss

Command: try: #42: J 8

   Miss

Command: try: #43: I 9

   Miss

Command: try: #44: D 1

   Miss

Command: try: #45: E 9

   Miss

Command: try: #46: H 2

   Hit

Command: try: #47: F 9

   Miss

Command: try: #48: A 9

   Miss

Command: try: #49: C 1

   Miss

Command: try: #50: F 8

   Miss

Command: try: #51: G 10

   Miss

Command: try: #52: A 3

   Hit

       You sunk my Cruiser Submarine

Command: try: #53: C 5

   Miss

Command: try: #54: H 3

   Hit

Command: try: #55: I 4

   Hit

Command: try: #56: D 9

   Miss

Command: try: #57: G 4

   Hit

Command: try: #58: J 7

   Miss

Command: try: #59: F 10

   Miss

Command: try: #60: E 8

   Miss

Command: try: #61: A 5

   Miss

Command: try: #62: F 2

   Miss

Command: try: #63: H 1

   Miss

Command: try: #64: G 6

   Hit

Command: try: #65: E 3

   Hit

Command: try: #66: C 9

   Miss

Command: try: #67: F 4

   Hit

Command: try: #68: H 4

   Hit

Command: try: #69: F 7

   Hit

Command: try: #70: H 9

   Miss

Command: try: #71: H 10

   Miss

Command: try: #72: B 7

   Miss

Command: try: #73: I 7

   Miss

Command: try: #74: E 6

   Miss

Command: try: #75: D 3

   Hit

Command: try: #76: F 6

   Hit

Command: try: #77: G 3

   Hit

Command: try: #78: C 3

   Miss

Command: try: #79: C 6

   Hit

Command: try: #80: C 10

   Miss

Command: try: #81: F 1

   Miss

Command: try: #82: E 1

   Miss

Command: try: #83: D 2

   Miss

Command: try: #84: J 1

   Miss

Command: try: #85: G 8

   Miss

Command: try: #86: D 5

   Hit

Command: try: #87: B 1

   Miss

Command: try: #88: H 6

   Miss

Command: try: #89: H 8

   Miss

Command: try: #90: G 5

   Hit

Command: try: #91: E 5

   Hit

Command: try: #92: I 3

   Hit

Command: try: #93: B 8

   Miss

Command: try: #94: H 7

   Miss

Command: try: #95: J 2

   Miss

Command: try: #96: B 4

   Hit

       You sunk my Battleship Destroyer

Command: try: #97: A 7

   Miss

Command: try: #98: A 10

   Miss

Command: try: #99: I 1

   Miss

Command: try: #100: E 2

   Miss

Game is Over

1 2 3 4 5 6 7 8 9 10

A 0 0 0 X X 0 0 0 0 0

B 0 0 0 X X 0 0 0 0 0

C 0 0 0 0 0 0 X X X 0

D 0 0 0 X X X X X X 0

E 0 0 0 X X X 0 0 0 0

F 0 0 0 X X X X X 0 0

G 0 0 0 X X X X X 0 0

H 0 0 X X X X 0 0 0 0

I 0 0 X X X X 0 0 0 0

J 0 0 0 0 0 0 0 0 0 0

Please help me with this, need a C++ code for it. Please follow the requirements. For battleship game, we\'ll be using a 10x10 array of characters. The board wi
Please help me with this, need a C++ code for it. Please follow the requirements. For battleship game, we\'ll be using a 10x10 array of characters. The board wi
Please help me with this, need a C++ code for it. Please follow the requirements. For battleship game, we\'ll be using a 10x10 array of characters. The board wi
Please help me with this, need a C++ code for it. Please follow the requirements. For battleship game, we\'ll be using a 10x10 array of characters. The board wi
Please help me with this, need a C++ code for it. Please follow the requirements. For battleship game, we\'ll be using a 10x10 array of characters. The board wi
Please help me with this, need a C++ code for it. Please follow the requirements. For battleship game, we\'ll be using a 10x10 array of characters. The board wi
Please help me with this, need a C++ code for it. Please follow the requirements. For battleship game, we\'ll be using a 10x10 array of characters. The board wi
Please help me with this, need a C++ code for it. Please follow the requirements. For battleship game, we\'ll be using a 10x10 array of characters. The board wi
Please help me with this, need a C++ code for it. Please follow the requirements. For battleship game, we\'ll be using a 10x10 array of characters. The board wi
Please help me with this, need a C++ code for it. Please follow the requirements. For battleship game, we\'ll be using a 10x10 array of characters. The board wi
Please help me with this, need a C++ code for it. Please follow the requirements. For battleship game, we\'ll be using a 10x10 array of characters. The board wi
Please help me with this, need a C++ code for it. Please follow the requirements. For battleship game, we\'ll be using a 10x10 array of characters. The board wi
Please help me with this, need a C++ code for it. Please follow the requirements. For battleship game, we\'ll be using a 10x10 array of characters. The board wi
Please help me with this, need a C++ code for it. Please follow the requirements. For battleship game, we\'ll be using a 10x10 array of characters. The board wi
Please help me with this, need a C++ code for it. Please follow the requirements. For battleship game, we\'ll be using a 10x10 array of characters. The board wi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site