Question

SOS!!!! Anyone can help me solve this problem?

SOS!!!! Anyone can help me solve this problem?
SOS!!!! Anyone can help me solve this problem? &nb
SOS!!!! Anyone can help me solve this problem? &nb

SOS!!!! Anyone can help me solve this problem? &nb
SOS!!!! Anyone can help me solve this problem? &nb
SOS!!!! Anyone can help me solve this problem? &nb
SOS!!!! Anyone can help me solve this problem? &nb

SOS!!!! Anyone can help me solve this problem? &nb
SOS!!!! Anyone can help me solve this problem? &nb
SOS!!!! Anyone can help me solve this problem? &nb

SOS!!!! Anyone can help me solve this problem? &nb
SOS!!!! Anyone can help me solve this problem? &nb
SOS!!!! Anyone can help me solve this problem? &nb
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include "stdafx.h"

#include <iostream>

#include <time.h>

#include <conio.h>

#include <sstream>

using namespace std;

//Below is an enumerated data type so that I can use the labels 'VERTICAL', and 'HORIZONTAL' to designate the

//direction of a ship.

enum ShipDirection {VERTICAL, HORIZONTAL};

//This is the ship class, it is a blue print for a ship.

//The game of battleship utilizes 5 ships of various sizes.

class Ship

{

        private:

               int numOfHits;

               ShipDirection shipDir;

               int startRow;

               int startCol;

               string shipType;

        public:

              

               Ship()

               {

                       shipType = "";

                       shipDir = VERTICAL;

                       startRow = 0;

                       startCol = 0;

                       numOfHits = 0;

               }

              

               Ship(int nHits, string name)

               {

                       shipType = name;

                       shipDir = VERTICAL;

                       startRow = 0;

                       startCol = 0;

                       numOfHits = nHits;

               }

               //Created sets and gets for all of the data members in the private: section of this class.

               void setStartRow(int sR)

               {

                       startRow = sR;

               }

               int getStartRow()

               {

                       return startRow;

               }

               void setStartCol(int sC)

               {

                       startCol = sC;

               }

               int getStartCol()

               {

                       return startCol;

               }

               int getNumberOfHits()

               {

                       return numOfHits;

               }

               ShipDirection getShipDir()

               {

                       return shipDir;

               }

               void setShipDir(ShipDirection sD)

               {

                       shipDir = sD;

               }

               string getNameOfShip()

               {

                       return shipType;

               }

               void setNameOfShip(string nOS)

               {

                       shipType = nOS;

               }

};

//This is the board class. This class represents the board. The board class contains a 2 dimensional array.

//That array is filled with zeros initially. If there is a ship at a certain coordinate at that array, it will contain a

//'1', if there is a space of a ship that has been hit at a certain coordinate of the board array, it will contain a '2'.

class Board

{

        private:

               int board[10][10];

        public:

               Board()

               {

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

                       {

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

                               {

                                      board[row][col] = 0;

                               }

                       }

               }

              

               //This function places a ship on the board

               void placeShip(Ship ship)

               {

                       //Create a random number between 0 and 1 to represent the direction of the ship

                       int sD = rand()%2;

                       int sR = rand()%10;

                       int sC = rand()%10;

                       if (sD==0)

                       {

                               ship.setShipDir(VERTICAL);

                       }

                       else

                       {

                               ship.setShipDir(HORIZONTAL);

                       }

                      

                       while(!isClearSpace(sR, sC, ship.getNumberOfHits(), ship.getShipDir()))

                       {

                               sR = rand()%10;

                               sC = rand()%10;

                       }

                       ship.setStartRow(sR);

                       ship.setStartCol(sC);

                       setShip(sR, sC,ship.getNumberOfHits(),ship.getShipDir());           

               }

               bool isClearSpace(int startRow, int startCol, int size, ShipDirection shipDir)

               {

                       if(shipDir == VERTICAL)

                       {

                               if(startRow + size > 9)

                               {

                                      return false;

                               }

                               for(int row = startRow; row < startRow+size; row++)

                               {

                                      if(board[row][startCol] !=0)

                                      {

                                              return false;

                                      }

                               }

                       }

                       else if(shipDir==HORIZONTAL)

                       {

                                      if(startCol + size > 9)

                               {

                                      return false;

                               }

                                      for(int col = startCol; col < startCol+size; col++)

                               {

                                      if(board[startRow][col] !=0)

                                      {

                                              return false;

                                      }

                               }

                       }

                       return true;

               }             

               void setShip(int startRow, int startCol, int size, ShipDirection shipDir)

               {

                       if(shipDir == VERTICAL)

                       {

                               for(int row = startRow; row < startRow+size; row++)

                               {

                                      board[row][startCol] = 1;

                               }

                       }

                       else if(shipDir==HORIZONTAL)

                       {

                                      for(int col = startCol; col < startCol+size; col++)

                               {

                                      board[startRow][col] = 1;

                                     

                               }

                       }

               }      

              

              

               //Print board

               void printBoard()

               {

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

                       char rowLabel = 'A';

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

                       {

                               cout << rowLabel << " ";

                               rowLabel++;

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

                               {

                                      cout << board[row][col] << " ";

                               }

                               cout << endl;

                       }

               }

              

              

               //This takes in a row and a col

               //it then checks to see if the guess is a hit or not

               //if there is not a zero at that place it has to be

               //a 1 or a 2, I will consider that to be a hit

               //my version of the game does not take into account

               //whether the user hits the boat at the same place

               //more then once

               bool isHit(int row, int col)

               {

                       if(board[row][col] != '1')

                       {

                               board[row][col] = '2';

                               return true;

                       }

                      

                       if(board[row][col] = '0')

                       {

                               return false;

                       }

                      

               }

               //Checks the entire board

               //if there are any 1's there is boat still on the board that is not yet hit

               //so return false if it finds a 1, otherwise the game is over

               bool isGameWon()

               {

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

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

                                      if(board[row][col] = 1)

                                      {  

                                      return false; //game is not yet won

                                      }

                              

                       return true;

                      

               }

               //Checks to see if this particular ship is sunk by checking to see

               //if the board at this ship's position is all 2s.

               bool isShipSunk(Ship ship, int startRow, int startCol, int size, ShipDirection shipDir)

               {

                       if(shipDir == VERTICAL)

                       {

                               if(startRow + size > 9)

                               {

                                      return false;

                               }

                               for(int row = startRow; row < startRow+size; row++)

                               {

                                      if(board[row][startCol] !='2')

                                      {

                                              return false;

                                      }

                               }

                       }

                       else if(shipDir==HORIZONTAL)

                       {

                                      if(startCol + size > 9)

                               {

                                      return false;

                               }

                                      for(int col = startCol; col < startCol+size; col++)

                               {

                                      if(board[startRow][col] !='2')

                                      {

                                              return false;

                                      }

                               }

                       }

                       return true;

               }

              

};

int _tmain(int argc, _TCHAR* argv[])

{

       

        //Seed the random number generator with the current time

        srand(time(0));

        char guessRow;

        int guessCol;

        char playAgain();

        char keepPlaying;

        bool isGameWon;

        bool isHit;

        //While the player intends to keep playing

        do

               {

               //Create each of the five ships

               Ship carrier = Ship(5, "Carrier");

               Ship battleship = Ship(4, "Battleship");

               Ship submarine = Ship(3, "Submarine");

               Ship destroyer = Ship(3, "Destroyer");

               Ship patrolboat = Ship(2, "Patrol boat");

              

              

               //Create the board

               Board board = Board();

                //place ships on board

               board.placeShip(carrier);

               board.placeShip(battleship);

               board.placeShip(submarine);

               board.placeShip(destroyer);

               board.placeShip(patrolboat);

               //print the board with the ships on it temporarily for testing, when the game is finished I will take this out.

               board.printBoard();

              

              

                       do

                       {

                       //Asks User for Coordinates of their choice.

                cout << "\n\nPlease enter your coordinates: \n";

                       cout << "Enter row: ";

                       cin >> guessRow;

                guessRow = toupper(guessRow);

                       cout << "Enter column: ";

                cin >> guessCol;

                       cout << endl;

                  

                      

                       if (isHit != false)//"Run-Time Check Failure #3 - The variable 'isHit' is being used without being initialized."

                       {

                               cout << "Its a hit \n";

                       }

                      

        //check each ship to see if it was sunk.

                       //Need help here to to work out hits with sinks

               /*if(board.isShipSunk(carrier))

               { cout << "You sunk my Carrier!"; }

              

               if(board.isShipSunk(battleship))

               { cout << "You sunk my Battleship!"; }

               if(board.isShipSunk(submarine))

               { cout << "You sunk my Submarine!"; }

               if(board.isShipSunk(destroyer))

               { cout << "You sunk my Destroyer!"; }

               if(board.isShipSunk(patrolboat))

               { cout << "You sunk my Partrol boat!"; }*/

               //do that on all 5 ships

                  

               else

                       {

                               cout << "It was a miss! \n";

                       }

       

              

               }

               while (isGameWon != true );//"Run-Time Check Failure #3 - The variable 'isGameWon' is being used without being initialized."

       

       

       

        //ask the player if he or she intends to keep playing

                keepPlaying = playAgain();

               if ( keepPlaying == 'N')

               {

                       cout << "Goodbye \n";

                       system("pause");

                       return 0;

               }

        }

        while (keepPlaying == 'Y');

       

}

//play again

char playAgain()

{

        char keepPlaying;

        cout << "Would you like to play again? (y or n) ";

        do

        {

               cin >> keepPlaying;

               keepPlaying = toupper(keepPlaying);

               if (keepPlaying!='Y' && keepPlaying != 'N')

                       cout << "Enter only a 'Y' or an 'N', please!" << endl << endl;

        } while ( keepPlaying !='Y' && keepPlaying != 'N');

        return keepPlaying;

}

Add a comment
Know the answer?
Add Answer to:
SOS!!!! Anyone can help me solve this problem?
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
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