Question: Battleship problem, C++, modify code The rules of Battleship are as follows. Each player first sets up ships on their board in secret. Each player

Battleship problem, C++, modify code

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

Currently, we always assume the ship is a submarine (length 3). Modify the code(battleship.cpp below) to make all five types of pieces possible. Your piece should always be a battleship (length 4). If the computer generates a random position, it should place a battleship as well. If the computer reads from the file, it should place different pieces based on the first char/string: 'D' for Destroyer 'C' for Cruiser 'S' for Submarine 'B' for Battleship 'A' for Aircraft carrier

The sample file oneShip.txt should place an Aircraft carrier (length 5, denoted with 'A's) in the top left going down, while the fiveShips.txt will put a submarine (length 3, denoted with 'S's) in the top left going down.

oneShip.txt:

A 0 0 d 

fiveShips.txt:

S 0 0 d S 0 1 d S 0 2 d S 0 3 d S 0 4 d 

battleship.cpp:

#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 < SIZE; i++) { for(int j=0; j < SIZE; j++) { playerBoard[i][j] = BLANK; } } for(int i =0; i < SIZE; i++) { for(int j=0; j < SIZE; j++) { computerBoard[i][j] = UNKNOWN; } } playerSub = Submarine(); computerSub = Submarine(); } // query user where they wish to place their pieces void placePlayerPieces() { bool correctPlacement; do{ displayBoards(); correctPlacement = placePiece(playerSub); if(!correctPlacement) { cout << " Please enter a valid position and direction "; } }while(!correctPlacement); addPieceToBoard(playerBoard, playerSub); } // places the computers pieces // if input string is "", then it places them randomly in a valid position // if the input string is not blank, it will read from that file void placeComputerPieces(string s) { ifstream in; if(s != "") { in.open(s.c_str()); if(in.fail()) { cout << "Incorrect file, aborting protocol! "; exit(404); } } char tempBoard[SIZE][SIZE]; 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 << "Which coordinate would you like to bombard? "; coordinate = toLower(requeststring()); attackRow = rowCharToInt(getFirstChar(coordinate)); attackColumn = getFirstInt(coordinate)-1; }while( (attackRow < 0 || attackRow > SIZE-1) || (attackColumn < 0 || attackColumn > SIZE-1)); int row = computerSub.getTopLeftRow(); int column = computerSub.getTopLeftColumn(); for(int i = 0; i < computerSub.getLength(); i++) { if(row == attackRow && column == attackColumn) { computerBoard[attackRow][attackColumn] = HIT; if(isSunk(computerBoard,computerSub)) { string result = computerSub.getName(); result +=" sunk at "; result +=toupper(getFirstChar(coordinate)); result +=('0'+getFirstInt(coordinate)); return result; } string result = "Hit at "; result +=toupper(getFirstChar(coordinate)); result +=('0' + getFirstInt(coordinate)); return result; } if(computerSub.isHorizontal()) { column++; } else { row++; } } computerBoard[attackRow][attackColumn] = MISS; string result = "Miss at "; result +=toupper(getFirstChar(coordinate)); result +=('0' + getFirstInt(coordinate)); return result; } // computer shoots a random square it has not already // the returned string tells where (coordinate) and the result (hit/miss/sink) string computerMove() { int attackRow = rand()%SIZE; int attackColumn = rand()%SIZE; string coordinate = ""; coordinate+=(attackRow+'A'); coordinate+=(attackColumn+'1'); int row = playerSub.getTopLeftRow(); int column = playerSub.getTopLeftColumn(); for(int i = 0; i < playerSub.getLength(); i++) { if(row == attackRow && column == attackColumn) { playerBoard[attackRow][attackColumn] = HIT; if(isSunk(playerBoard,playerSub)) { string result = playerSub.getName(); result += " sunk at "; result += coordinate; return result; } string result = "Hit at "; result += coordinate; return result; } if(playerSub.isHorizontal()) { column++; } else { row++; } } playerBoard[attackRow][attackColumn] = MISS; string result="Miss at "; result += coordinate; return result; } // is the game over yet? // 1 = computer wins // 2 = you win // 3 = you tie (rare!) int gameOver() { int result = 0; int computerShipCoordinateTotal = 0; computerShipCoordinateTotal += computerSub.getLength(); int playerShipCoordinateTotal = 0; playerShipCoordinateTotal += playerSub.getLength(); int playerBoardHitTotal = 0; for(int i=0; isize-1="" !piece.ishorizontal()))="" check="" to="" see="" if="" another="" ship="" is="" present="" i="0;" <="" piece.getlength();="" if(board[row][column]="" !="BLANK)" if(piece.ishorizontal())="" column++;="" else="" row++;="" true;="" lazy...="" don't="" ask="" string="" requeststring()="" temp;="" getline(cin,="" temp);="" converts="" a="" all="" lower="" case="" letters="" tolower(string="" s)="" static_cast(s.length());="" s[i]="tolower(s[i]);" s;="" returns="" the="" first="" character="" in="" input="" char="" getfirstchar(string="" temp)="" int="" c;="" if(temp.length()="=0)" c="0;" while(i="" static_cast(temp.length())="" !isalpha(temp[i]))="" i++;="" if(i="" static_cast(temp.length()))="" integer="" value="" getfirstint(string="" 0;="" !isdigit(temp[i]))="" number="temp.substr(i);" if(number="=" "")="" atoi(number.c_str());="" };="" main()="" srand(time(0));="" battleshipgame="" game;="" cout="" <<="" "welcome="" battleship="" v23.1 ";="" fromfile;="" "do="" you="" wish="" computer="" generate="" random="" board="" or="" from="" file?="" (r="" f) ";="" fromfile);="" generation="" if('f'="=" tolower(fromfile[0]))="" "what="" file="" do="" read? ";="" getline(cin,fromfile);="" game.placecomputerpieces(fromfile);="" game.placecomputerpieces("");="" place="" your="" pieces="" game.placeplayerpieces();="" " ";="" game.displayboards();="" do{="" playermove="game.playerMove();" computermove="game.computerMove();" game.displayboards(playermove,="" computermove);="" }while(game.gameover()="=" 0);="" gameover="game.gameOver();" if(gameover="=" 1)="" "you="" lost!="" practice="" more. ";="" 2)="" won!="" have="" cookie. ";="" 3)="" "oh="" my="" gosh,="" tie! ";="" 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!