Can someone write code for me? Many thanks!! Vectors, Multidimentional Arrays Mu
ID: 3692197 • Letter: C
Question
Can someone write code for me? Many thanks!!
Vectors, Multidimentional Arrays
Multidimensional arrays. Create a project Lab13_MultArrays. Implement a program that keeps track of shots fired in a battleship game. Your program should ask the user if she wants to take another shot, ask for shot coordinates and then print the locations of all shots fired so far in a grid. Here is an example dialog:
You do not have to keep track if multiple shots were fired in the same location, i.e. you should show a single star to indicate that the shot was fired there. You do not have to join this program with previously developed battleship program.
You should store the shots in a two-dimensional array of booleans. You should use the following code(check below) as a starting point.
Refer to this program(check below) for multidimensional array example usage.
Make sure your programs adhere to proper programming style. Submit your projects to the subversion repository. Do not forget to verify your submission on the web.
*********the following code********
******** this program***********
Explanation / Answer
#include <vector>
#include <iostream>
using namespace std;
//function prototypes
void initialize(int oceanLength,int oceanWidth,vector <vector <bool>> shots);
void rowPrint(int rownumber,int oceanWidth,vector <vector <bool>> shots);
void shoot(char userColumn,int userInteger,vector <vector <bool>> &shots);
int main(){
cout << "Enter ocean length: ";
int oceanLength;
cin >> oceanLength;
//It should work from letters a-z which is 26 characters
cout << "Enter ocean width ";
int oceanWidth;
cin >> oceanWidth;
char tmpWidth = 'a';
tmpWidth = tmpWidth + (oceanWidth-1);
vector <vector <bool>> shots; // vector of vectors of shots
vector<bool> shotsRow(oceanWidth); // one row of shots
// consider initializing the row of shots
for(int rows=0; rows < oceanLength; ++rows)
shots.push_back(shotsRow);
// place your code here
bool notStopped = true;
initialize(oceanLength,oceanWidth,shots);
char userYesOrNo,userColumn;
int userInteger;
cout << endl;
while(notStopped){
cout << "Another shot? [y/n] ";
cin >> userYesOrNo;
if(userYesOrNo == 'y'){
cout <<"Location? ";
cin >> userColumn;
cin >> userInteger;
cout << "All fired shots" << endl;
//The user shot so it needs to modify the spot it shot in vector!
shoot(userColumn,userInteger,shots);
//Then it should print out the with in characters... a to z...
cout << " ";
for(char letter='a'; letter <= tmpWidth; ++letter)
cout << letter << ' ';
cout << endl;
for(int i = 1; i <= oceanLength; ++i){
cout << i << " ";
rowPrint((i-1),oceanWidth,shots);
cout << endl;
}
cout << endl;
}else if(userYesOrNo == 'n'){
notStopped = false;
}
}
}
//function definitions
//initialize function definition
// sets all spots to false because they are not hit
void initialize(int oceanLength,int oceanWidth,vector <vector <bool>> shots){
for (int i=0; i < oceanLength; ++i)
for (int j=0; j < oceanWidth; ++j)
shots[i][j] = 0;
}
// rowPrint function definition
void rowPrint(int rownumber,int oceanWidth,vector <vector <bool>> shots){
for(int i=0; i < oceanWidth;++i){
if(shots[rownumber][i] == 0){
cout << " ";
}else if(shots[rownumber][i] == 1){
cout << "* ";
}
}
}
//shoot function definition
void shoot(char userColumn,int userInteger,vector <vector <bool>> &shots){
int tmpInt;
tmpInt = userColumn - 97;
userInteger = userInteger - 1;
shots[userInteger][tmpInt] = 1;
}
sample output
Enter ocean length: 5
Enter ocean width 5
Another shot? [y/n] y
Location? a 1
All fired shots
a b c d e
1 *
2
3
4
5
Another shot? [y/n] n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.