#include using namespace std; const int rows=10; const int columns=10; int computer_board1[rows][columns]; int computer_board2[rows][columns]; int player_board[rows][columns]; int x,y; void setboard() { for(int i=0;i>x; cout<<"Vertical component:"; cin>>y; if(x>=10||y>=10) { cout<<"Invalid component added"<>a; cout<<"Vertical component:"; cin>>b; place_computer_ships(); if(computer_board1[a][b]==1) { cout<<"You hit"<

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter6: Arrays
Section: Chapter Questions
Problem 12PE
icon
Related questions
Question
100%

#include<iostream>
using namespace std;
const int rows=10;
const int columns=10;
int computer_board1[rows][columns];
int computer_board2[rows][columns];
int player_board[rows][columns];
int x,y;
void setboard()
{
for(int i=0;i<rows;i++)
{
for(int j=0;j<columns;j++)
{
computer_board1[i][j]=0;
player_board[i][j]=0;
}
}
}
void display_computer_board1()
{
cout<<"Computer's Board"<<endl;
cout<<" 0 1 2 3 4 5 6 7 8 9 "<<endl;
for(int i=0;i<rows;i++)
{
for(int j=0;j<columns;j++)
{
if(j==0)
{
cout<<i<<" ";
}
cout<<computer_board1[i][j]<<" ";

}
cout<<endl;
}
cout<<endl;
}
void display_player_board()
{
cout<<"Your Board"<<endl;
cout<<" 0 1 2 3 4 5 6 7 8 9 "<<endl;
for(int i=0;i<rows;i++)
{
for(int j=0;j<columns;j++)
{
if(j==0)
{
cout<<i<<" ";
}
cout<<player_board[i][j]<<" ";

}


cout<<endl;
}

}
void place_player_ships()
{
for(int i=1;i<=15;i++)
{
cout<<"Horizontal component:";
cin>>x;
cout<<"Vertical component:";
cin>>y;
if(x>=10||y>=10)
{
cout<<"Invalid component added"<<endl;
return ;
}
player_board[x][y]=1;
display_computer_board1();
display_player_board();
}
}
void display_computer_board2()
{
cout<<"Computer's Board"<<endl;
cout<<" 0 1 2 3 4 5 6 7 8 9 "<<endl;
for(int i=0;i<rows;i++)
{
for(int j=0;j<columns;j++)
{
if(j==0)
{
cout<<i<<" ";
}
cout<<computer_board2[i][j]<<" ";

}
cout<<endl;
}
}
void place_computer_ships()
{
for(int i=1;i<=15;i++)
{
x=rand()%rows;
y=rand()%columns;
computer_board1[x][y]=1;
}
}
void guess_computer_ships()
{
int a,b;
cout<<"Horizontal component:";
cin>>a;
cout<<"Vertical component:";
cin>>b;
place_computer_ships();
if(computer_board1[a][b]==1)
{
cout<<"You hit"<<endl;
computer_board2[a][b]=1;
display_computer_board2();
display_player_board();
}
if(computer_board1[a][b]!=1)
{
cout<<"You missed"<<endl;
computer_board2[a][b]=2;
display_computer_board2();
display_player_board();
}
}
void guess_player_ships()
{
int a,b;
int turn=0;
if(turn%2==0)
{
for(turn;turn<100;turn++)
{
a=rand()%rows;
b=rand()%columns;
}
if(player_board[a][b]==1)
{
cout<<"Computer hit"<<endl;
player_board[a][b]=2;
display_computer_board2();
display_player_board();

}

else if(player_board[a][b]!=1)
{
cout<<"Computer missed"<<endl;
display_computer_board2();
display_player_board();
}
}

}
bool check_winner()
{
int s=0;
for(int i=0;i<rows;i++)
{
for(int j=0;j<columns;j++)
{
if(computer_board2[i][j]==1)
{
s++;
if(s==15)
{
cout<<"You won the game:))"<<endl;
return true;
}
}
}
}
}

int main()
{
int gametype;
cout<<"****BATTLESHIP****"<<endl;
cout<<"Are You Ready"<<endl;
setboard();
display_computer_board1();
cout<<"Instructions"<<endl;
cout<<"There are 15 ships in this game: "<<endl;
//cout<<"1.Carrier 5\n2.Battleship 4\n3.Cruiser 3\n4.Submarine 3\n";*/
cout<<"You have to sink all of the ships."<<endl;
cout<<"Computer's board will show 1 when you hit and 2 when you miss"<<endl;
cout<<"Your board will show 1 where you place your ship and 2 when computer hits your ship"<<endl;
display_computer_board1();
display_player_board();
cout<<endl;
cout<<"Now you have to place your 15 ships anywhere on your board by adding horizontal and vertical components"<<endl;
place_player_ships();
cout<<"You ships are placed successfully"<<endl;
cout<<"Let's start the game"<<endl;
cout<<"Add Horizontal and vertical components to guess the ships on computer's board"<<endl;
int turn=1;
for(int i=1;i<=200;i++)
{
if(turn%2!=0)
{
cout<<"Your turn"<<endl;
guess_computer_ships();

}
if(turn%2==0)

{
cout<<"Computer's turn"<<endl;
guess_player_ships();

}
turn++;
}


}

Can anyone tell me how to decide the winner and break the loop. Please write the code if possible.

Thank you.

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT