Question
(C++) Battleship problem. Write a code by only C++ and follow the instruction clearly please. The rules of Battleship are as follows. Each player first
(C++) Battleship problem. Write a code by only C++ and follow the instruction clearly please.
The rules of Battleship are as follows. Each player first sets up ships on their board in secret. Each player then guesses a coordinate (letter and number combination) to bombard on the enemies board. If a ship is hit in this bombardment, the player must say Hit. If all parts of a ship are hit, then the player must say You sank my [name of ship]. If no ship was hit, the player responds miss. This process continues until one player has sunk all of the opponent's ships. Here is a website about the rules if mine are inadequate: http://boardgames.about.com/od/battleship/a/Rules-of-Battleship.htm. The pieces and lengths of pieces for Battleship are: Aircraft carrier = length 5 Battleship = length 4 Submarine = length 3 Cruiser = length 3 Destroyer = length 2 There is a lot of code here. Familiarize yourself with it some before programming (at the very least, you should look at main() and the names of all the functions in the classes). The navigator (left side of geany) can be quite useful to bounce around between methods. Your coding program can probably use Ctrl+f to search for parts of code as well. You can run the program as it is, and it should play the game Battleship for just one ship (the length 3 submarine) on a 10x10 board. For testing purposes, you could remove the randomization so you dont need to guess the whole board.
Currently, we always assume the ship is a submarine (length 3). Modify the code to make all five types of pieces possible. The possible ships are: 'D' for Destroyer 'C' for Cruiser 'S' for Submarine 'B' for Battleship 'A' for Aircraft carrier Add code so that before you start, you can select the type of ship to place. When the computer generates a random position, it should place the same type of ship as well. You must also ensure all the text is correct. For example, if you select A, your program should not say You sank the enemy submarine. The board should also use the characters above to show the type of ship on the board. In the example below an A is shown as the aircraft carrier is selected.
output :
source code :
#include#include #include #include #include #include using namespace std; const int SIZE = 10; // size of the board const char BLANK = '~'; // DO NOT SET THIS TO A SHIP CHARACTER const char UNKNOWN = '?'; // DO NOT SET THIS TO A SHIP CHARACTER const char HIT = 'X'; // DO NOT SET THIS TO A SHIP CHARACTER const char MISS = 'O'; // DO NOT SET THIS TO A SHIP CHARACTER class Submarine { private: string name; int length; // positions are the top left point of the piece int rowPosition; int columnPosition; bool amIHorizontal; char boardCharacter; public: Submarine() { name = "Submarine"; length = 3; boardCharacter = 'S'; } // fills in the rowPosition, columnPosition and amIHorozontal variables based on the input // (rowPosition and columnPositing are the top and left coordinate respectively) void setPiece(int row, int column, char direction) { if(direction == 'u') { rowPosition = row-(length-1); columnPosition = column; amIHorizontal = false; } else if(direction == 'd') { rowPosition = row; columnPosition = column; amIHorizontal = false; } else if(direction == 'l') { rowPosition = row; columnPosition = column - (length-1); amIHorizontal = true; } else if(direction == 'r') { rowPosition = row; columnPosition = column; amIHorizontal = true; } else { rowPosition = -1; columnPosition = -1; } } bool isHorizontal() const { return amIHorizontal; } int getTopLeftRow() const { return rowPosition; } int getTopLeftColumn() const { return columnPosition; } int getLength() const { return length; } string getName() const { return name; } char getBoardCharacter() const { return boardCharacter; } }; class BattleshipGame { public: // THE MIGHTY CONSTRUCTOR // meekly makes both boards blank and initialize pieces BattleshipGame() { for(int i =0; i > type >> randomRow >> randomColumn >> randomDirection; } computerSub.setPiece(randomRow, randomColumn, getFirstChar(randomDirection)); correctPlacement = isValidPlacement(tempBoard, computerSub); }while(!correctPlacement); addPieceToBoard(tempBoard, computerSub); in.close(); } // query player for where they wish to bombard // returned string is the coordinate with result (hit/miss/sink) string playerMove() { int attackRow; int attackColumn; string coordinate; do { cout SIZE-1) || (attackColumn SIZE-1)); int row = computerSub.getTopLeftRow(); int column = computerSub.getTopLeftColumn(); for(int i = 0; i SIZE-1 && piece.isHorizontal())) { return false; } if(rowSIZE-1 && !piece.isHorizontal())) { return false; } // check to see if another ship is present for(int i =0; i (s.length()); i++) { s[i] = tolower(s[i]); } return s; } // returns the first character in the input string char getFirstChar(string temp) { int i=0; char c; if(temp.length()==0) { c = 0; } else { while(i (temp.length()) && !isalpha(temp[i])) { i++; } if(i (temp.length())) { c = temp[i]; } else { c = 0; } } return c; } // returns the first integer value in the input string int getFirstInt(string temp) { int i =0; if(temp.length()==0) { return 0; } while(i (temp.length()) && !isdigit(temp[i])) { i++; } string number = temp.substr(i); if(number == "") { return 0; } return atoi(number.c_str()); } }; int main() { srand(time(0)); BattleshipGame game; cout Example1 (user input is underlined, not showing right board) Welcome to Battleship v23.1 What type of ship do you want to use? 1 2 3 4 5 6 7 8 9 10 A-~ B -~ D-~ E-~ F-~ G-~ H-~ I- J-~ What coordinate do you want to place the aircraft carrier (length 5) at? cl What direction do you want the piece to go? (up, down, left or right) up Please enter a valid position and direction 1 2 3 4 5 6 7 8 9 10 A-~ D-~ E-~ F-~ G-~ Example1 (user input is underlined, not showing right board) Welcome to Battleship v23.1 What type of ship do you want to use? 1 2 3 4 5 6 7 8 9 10 A-~ B -~ D-~ E-~ F-~ G-~ H-~ I- J-~ What coordinate do you want to place the aircraft carrier (length 5) at? cl What direction do you want the piece to go? (up, down, left or right) up Please enter a valid position and direction 1 2 3 4 5 6 7 8 9 10 A-~ D-~ E-~ F-~ G-~
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