#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"<

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

#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.

Thank you.

Expert Solution
steps

Step by step

Solved in 2 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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education