All code within the ***...*** needs to be implemented!! Multidimensional arrays
ID: 3695863 • Letter: A
Question
All code within the ***...*** needs to be implemented!!
Multidimensional arrays. Create a project Lab12_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:
The output of the code should look like this!!
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 as a starting point. ***http://vega.cs.kent.edu/~mikhail/classes/csi/Labs/Lab12/shotsMultArrays.cpp***
Refer to this program for multidimensional array example usage. http://vega.cs.kent.edu/~mikhail/classes/csi/Labs/Lab12/list.cpp
Explanation / Answer
Answer:
Note: Provided links are not accessable, hence I implemented my own code to implement this task
C++ code:
#include<iostream>
#include<stdlib.h>
using namespace std;
void changeboard1(int shoot1[2], int ships1[5][5], int board1[5][5]);
void initBoard1(int board1[5][5]);
void showBoard1(int board1[5][5]);
void shoot1(int shoot1[2]);
void hint1(int shoot1[2],int ships1[5][5],int attempt1);
void changeboard1(int shoot1[2], int ships1[5][5], int board1[5][5]);
void initShips1(int ships1[5][5]);
bool hit1(int shoot1[2], int ships1[5][5]);
void main()
{
int board1[5][5];
int ships1[5][5];
int shoot1[2];
int attempts1=0,
shotHit1=0;
initBoard(board1);
initShips(ships1);
cout<<""<<endl;
do
{
showBoard(board1);
shoot1;
attempts1++;
if(hit(shoot1,ships1))
{
shotHit1++;
}
else
hint(shoot1,ships1,attempts1);
changeboard(shoot1,ships1,board1);
showBoard(board1);
}while(shotHit1!=3);
cout<<" Battleship Java game finished! You hit 3 ships in "<<attempts1<<" attempts";
showBoard1(board1);
}
void initBoard1(int board1[5][5])
{
for(int row1=0 ; row1 < 5 ; row1++ )
for(int column1=0 ; column1 < 5 ; column1++ )
board1[row1][column1]=-1;
}
void showBoard1(int board1[5][5])
{
cout<<" 1 2 3 4 5";
cout<<" ";
for(int row=0 ; row < 5 ; row++ ){
cout<<(row+1)<<"";
for(int column=0 ; column < 5 ; column++ )
{
if(board1[row][column]==-1)
{
cout<<" "<<"~";
}
else if(board1[row][column]==0)
{
cout<<" "<<"*";
}
else if(board1[row][column]==1)
{
cout<<" "<<"X";
}
}
cout<< " ";
}
}
void initShips1(int ships1[5][5])
{
int random1 = rand() % 10 + 1;
for(int ship1=0 ; ship1 < 3 ; ship1++)
{
ships1[ship1][0]=random1;
ships1[ship1][1]=random1;
for(int last=0 ; last < ship1 ; last++){
if( (ships1[ship1][0] == ships1[last][0])&&(ships1[ship1][1] == ships1[last][1]) )
do{
ships1[ship1][0]=random1;
ships1[ship1][1]=random1;
}while( (ships1[ship1][0] == ships1[last][0])&&(ships1[ship1][1] == ships1[last][1]) );
}
}
}
void shoot1(int shoot1[2])
{
cout<<"Row:";
cin>>shoot1[0];
shoot1[0]--;
cout<<"Row:";"Column: ";
cin>>shoot1[0];
shoot1[1]--;
}
bool hit(int shoot1[2], int ships1[5][5])
{
for(int ship1=0 ; ship1<5 ; ship1++)
{
if( shoot1[0]==ships1[ship1][0] && shoot1[1]==ships1[ship1][1])
{
cout<<"you hit ship"<<shoot1[0]+1,shoot1[1]+1;
return true;
}
else
{
return false;
}
}
}
void hint1(int shoot1[2],int ships1[5][5],int attempt1)
{
int row=0;
int column=0;
for(int line=0 ; line <5 ; line++)
{
if(ships1[line][0]==shoot1[0])
row++;
if(ships1[line][1]==shoot1[1])
column++;
}
cout<<" Hint row ships"<<"column"<<attempt1,shoot1[0]+1,row,shoot1[1]+1,column;
}
void changeboard1(int shoot1[2], int ships1[5][5], int board1[5][5])
{
if(hit(shoot1,ships1))
board1[shoot1[0]][shoot1[1]]=1;
else
board1[shoot1[0]][shoot1[1]]=0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.