#includeusing 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<>{for(int j=0;j<>{computer_board1[i][j]=0;player_board[i][j]=0;}}}void display_computer_board1(){cout<"computer's>"computer's><>cout<" 0="" 1="" 2="" 3="" 4="" 5="" 6="" 7="" 8="" 9="">"><>for(int i=0;i<>{for(int j=0;j<>{if(j==0){cout<>cout<><">">}cout<>}cout<>}void display_player_board(){cout<"your>"your><>cout<" 0="" 1="" 2="" 3="" 4="" 5="" 6="" 7="" 8="" 9="">"><>for(int i=0;i<>{for(int j=0;j<>{if(j==0){cout<>cout<><">">}cout<>}}void place_player_ships(){for(int i=1;i<>{cout<"horizontal>"horizontal>cin>>x;cout<"vertical>"vertical>cin>>y;if(x>=10||y>=10){cout<"invalid component="">"invalid><>return ;}player_board[x][y]=1;display_computer_board1();display_player_board();}}void display_computer_board2(){cout<"computer's>"computer's><>cout<" 0="" 1="" 2="" 3="" 4="" 5="" 6="" 7="" 8="" 9="">"><>for(int i=0;i<>{for(int j=0;j<>{if(j==0){cout<>cout<><">">}cout<>}}void place_computer_ships(){for(int i=1;i<>{x=rand()%rows;y=rand()%columns;computer_board1[x][y]=1;}}void guess_computer_ships(){int a,b;cout<"horizontal>"horizontal>cin>>a;cout<"vertical>"vertical>cin>>b;place_computer_ships();if(computer_board1[a][b]==1){cout<"you>"you><>computer_board2[a][b]=1;display_computer_board2();display_player_board();}if(computer_board1[a][b]!=1){cout<"you>"you><>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<>{a=rand()%rows;b=rand()%columns;}if(player_board[a][b]==1){cout<"computer>"computer><>player_board[a][b]=2;display_computer_board2();display_player_board();}else if(player_board[a][b]!=1){cout<"computer>"computer><>display_computer_board2();display_player_board();}}}bool check_winner(){int s=0;for(int i=0;i<>{for(int j=0;j<>{if(computer_board2[i][j]==1){s++;if(s==15){cout<"you won="" the="">"you><>return true;}}}}}int main(){int gametype;cout<><>cout<"are you="">"are><>setboard();display_computer_board1();cout<><>cout<"there are="" 15="" ships="" in="" this="" game:="">"there><>//cout<"1.carrier 5\n2.battleship="" 4\n3.cruiser="" 3\n4.submarine="">"1.carrier>cout<"you have="" to="" sink="" all="" of="" the="">"you><>cout<"computer's board="" will="" show="" 1="" when="" you="" hit="" and="" 2="" when="" you="">"computer's><>cout<"your board="" will="" show="" 1="" where="" you="" place="" your="" ship="" and="" 2="" when="" computer="" hits="" your="">"your><>display_computer_board1();display_player_board();cout<>cout<"now you="" have="" to="" place="" your="" 15="" ships="" anywhere="" on="" your="" board="" by="" adding="" horizontal="" and="" vertical="">"now><>place_player_ships();cout<"you ships="" are="" placed="">"you><>cout<"let's start="" the="">"let's><>cout<"add horizontal="" and="" vertical="" components="" to="" guess="" the="" ships="" on="" computer's="">"add><>int turn=1;for(int i=1;i<>{if(turn%2!=0){cout<"your>"your><>guess_computer_ships();}if(turn%2==0){cout<"computer's>"computer's><>guess_player_ships();}turn++;}}Can anyone tell me how to decide the winner and break the loop.Thank you.
}cout<>}cout<>}void display_player_board(){cout<"your>"your><>cout<" 0="" 1="" 2="" 3="" 4="" 5="" 6="" 7="" 8="" 9="">"><>for(int i=0;i<>{for(int j=0;j<>{if(j==0){cout<>cout<><">">
}
cout<>}
}void place_player_ships(){for(int i=1;i<>{cout<"horizontal>"horizontal>cin>>x;cout<"vertical>"vertical>cin>>y;if(x>=10||y>=10){cout<"invalid component="">"invalid><>return ;}player_board[x][y]=1;display_computer_board1();display_player_board();}}void display_computer_board2(){cout<"computer's>"computer's><>cout<" 0="" 1="" 2="" 3="" 4="" 5="" 6="" 7="" 8="" 9="">"><>for(int i=0;i<>{for(int j=0;j<>{if(j==0){cout<>cout<><">">
}cout<>}}void place_computer_ships(){for(int i=1;i<>{x=rand()%rows;y=rand()%columns;computer_board1[x][y]=1;}}void guess_computer_ships(){int a,b;cout<"horizontal>"horizontal>cin>>a;cout<"vertical>"vertical>cin>>b;place_computer_ships();if(computer_board1[a][b]==1){cout<"you>"you><>computer_board2[a][b]=1;display_computer_board2();display_player_board();}if(computer_board1[a][b]!=1){cout<"you>"you><>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<>{a=rand()%rows;b=rand()%columns;}if(player_board[a][b]==1){cout<"computer>"computer><>player_board[a][b]=2;display_computer_board2();display_player_board();
else if(player_board[a][b]!=1){cout<"computer>"computer><>display_computer_board2();display_player_board();}}
}bool check_winner(){int s=0;for(int i=0;i<>{for(int j=0;j<>{if(computer_board2[i][j]==1){s++;if(s==15){cout<"you won="" the="">"you><>return true;}}}}}
int main(){int gametype;cout<><>cout<"are you="">"are><>setboard();display_computer_board1();cout<><>cout<"there are="" 15="" ships="" in="" this="" game:="">"there><>//cout<"1.carrier 5\n2.battleship="" 4\n3.cruiser="" 3\n4.submarine="">"1.carrier>cout<"you have="" to="" sink="" all="" of="" the="">"you><>cout<"computer's board="" will="" show="" 1="" when="" you="" hit="" and="" 2="" when="" you="">"computer's><>cout<"your board="" will="" show="" 1="" where="" you="" place="" your="" ship="" and="" 2="" when="" computer="" hits="" your="">"your><>display_computer_board1();display_player_board();cout<>cout<"now you="" have="" to="" place="" your="" 15="" ships="" anywhere="" on="" your="" board="" by="" adding="" horizontal="" and="" vertical="">"now><>place_player_ships();cout<"you ships="" are="" placed="">"you><>cout<"let's start="" the="">"let's><>cout<"add horizontal="" and="" vertical="" components="" to="" guess="" the="" ships="" on="" computer's="">"add><>int turn=1;for(int i=1;i<>{if(turn%2!=0){cout<"your>"your><>guess_computer_ships();
}if(turn%2==0)
{cout<"computer's>"computer's><>guess_player_ships();
}turn++;}
Can anyone tell me how to decide the winner and break the loop.
Thank you.
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here