Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I meed help implementing my play game function. We were given some code but need

ID: 3907288 • Letter: I

Question

I meed help implementing my play game function. We were given some code but needed to program one function to play the game. Here’s the main.cpp: #include <iostream> #include <cmath> #include <stdio.h> #include <stdlib.h> #include <ctype.h>
using namespace std;
// check if a certain position on the board is occupied by a human ship. 1 indicates health ship, 2 indicated sunk portion of ship string checkPos(int x, int y, int human[][6]) { string str = ""; if(human[x][y] == 1) str = "O"; else if(human[x][y] == 2) str = "X"; else str = " ";
return str; }
//print the human's side of the board. This will show if the ships placed are hit void printBoard(int human[][6], int computer[][6]) { cout << " "; cout << " ___ ___ ___ ___ ___ ___ " << endl; cout << " | | | | | | |" << endl; cout << "5 | " << checkPos(0, 5, human) << " | " << checkPos(1, 5, human) <<" | " << checkPos(2, 5, human) << " | " << checkPos(3, 5, human) << " | " << checkPos(4, 5, human) << " | " << checkPos(5, 5, human) << " |" << endl; cout << " |___|___|___|___|___|___|" << endl; cout << " | | | | | | |" << endl; cout << "4 | " << checkPos(0, 4, human) << " | " << checkPos(1, 4, human) << " | " << checkPos(2, 4, human) << " | " << checkPos(3, 4, human) << " | " << checkPos(4, 4, human) << " | " << checkPos(5, 4, human) << " |" << endl; cout << " |___|___|___|___|___|___|" << endl; cout << " | | | | | | |" << endl; cout << "3 | " << checkPos(0, 3, human) << " | " << checkPos(1, 3, human) << " | " << checkPos(2, 3, human) << " | " << checkPos(3, 3, human) << " | " << checkPos(4, 3, human) << " | " << checkPos(5, 3, human) << " |" << endl; cout << " |___|___|___|___|___|___|" << endl; cout << " | | | | | | |" << endl; cout << "2 | " << checkPos(0, 2, human) << " | " << checkPos(1, 2, human) << " | " << checkPos(2, 2, human) << " | " << checkPos(3, 2, human) << " | " << checkPos(4, 2, human) << " | " << checkPos(5, 2, human) << " |" << endl; cout << " |___|___|___|___|___|___|" << endl; cout << " | | | | | | |" << endl; cout << "1 | " << checkPos(0, 1, human) << " | " << checkPos(1, 1, human) << " | " << checkPos(2, 1, human) << " | " << checkPos(3, 1, human) << " | " << checkPos(4, 1, human) << " | " << checkPos(5, 1, human) << " |" << endl; cout << " |___|___|___|___|___|___|" << endl; cout << " | | | | | | |" << endl; cout << "0 | " << checkPos(0, 0, human) << " | " << checkPos(1, 0, human) << " | " << checkPos(2, 0, human) << " | " << checkPos(3, 0, human) << " | " << checkPos(4, 0, human) << " | " << checkPos(5, 0, human) << " |" << endl; cout << " |___|___|___|___|___|___|" << endl; cout << " 0 1 2 3 4 5 " << endl; }
void initializePieces(int human[][6], int computer[][6]) { for(int i = 0; i < 6; i++) for(int j = 0; j < 6; j++) { human[i][j] = 0; computer[i][j] = 0; } }
void playGame(int human[][6], int computer[][6]) { ///complete this function }
int main() { int human[6][6]; int computer[6][6]; initializePieces(human, computer); //playGame(human, computer); return 0; } #include <iostream> #include <cmath> #include <stdio.h> #include <stdlib.h> #include <ctype.h>
using namespace std;
// check if a certain position on the board is occupied by a human ship. 1 indicates health ship, 2 indicated sunk portion of ship string checkPos(int x, int y, int human[][6]) { string str = ""; if(human[x][y] == 1) str = "O"; else if(human[x][y] == 2) str = "X"; else str = " ";
return str; }
//print the human's side of the board. This will show if the ships placed are hit void printBoard(int human[][6], int computer[][6]) { cout << " "; cout << " ___ ___ ___ ___ ___ ___ " << endl; cout << " | | | | | | |" << endl; cout << "5 | " << checkPos(0, 5, human) << " | " << checkPos(1, 5, human) <<" | " << checkPos(2, 5, human) << " | " << checkPos(3, 5, human) << " | " << checkPos(4, 5, human) << " | " << checkPos(5, 5, human) << " |" << endl; cout << " |___|___|___|___|___|___|" << endl; cout << " | | | | | | |" << endl; cout << "4 | " << checkPos(0, 4, human) << " | " << checkPos(1, 4, human) << " | " << checkPos(2, 4, human) << " | " << checkPos(3, 4, human) << " | " << checkPos(4, 4, human) << " | " << checkPos(5, 4, human) << " |" << endl; cout << " |___|___|___|___|___|___|" << endl; cout << " | | | | | | |" << endl; cout << "3 | " << checkPos(0, 3, human) << " | " << checkPos(1, 3, human) << " | " << checkPos(2, 3, human) << " | " << checkPos(3, 3, human) << " | " << checkPos(4, 3, human) << " | " << checkPos(5, 3, human) << " |" << endl; cout << " |___|___|___|___|___|___|" << endl; cout << " | | | | | | |" << endl; cout << "2 | " << checkPos(0, 2, human) << " | " << checkPos(1, 2, human) << " | " << checkPos(2, 2, human) << " | " << checkPos(3, 2, human) << " | " << checkPos(4, 2, human) << " | " << checkPos(5, 2, human) << " |" << endl; cout << " |___|___|___|___|___|___|" << endl; cout << " | | | | | | |" << endl; cout << "1 | " << checkPos(0, 1, human) << " | " << checkPos(1, 1, human) << " | " << checkPos(2, 1, human) << " | " << checkPos(3, 1, human) << " | " << checkPos(4, 1, human) << " | " << checkPos(5, 1, human) << " |" << endl; cout << " |___|___|___|___|___|___|" << endl; cout << " | | | | | | |" << endl; cout << "0 | " << checkPos(0, 0, human) << " | " << checkPos(1, 0, human) << " | " << checkPos(2, 0, human) << " | " << checkPos(3, 0, human) << " | " << checkPos(4, 0, human) << " | " << checkPos(5, 0, human) << " |" << endl; cout << " |___|___|___|___|___|___|" << endl; cout << " 0 1 2 3 4 5 " << endl; }
void initializePieces(int human[][6], int computer[][6]) { for(int i = 0; i < 6; i++) for(int j = 0; j < 6; j++) { human[i][j] = 0; computer[i][j] = 0; } }
void playGame(int human[][6], int computer[][6]) { ///complete this function }
int main() { int human[6][6]; int computer[6][6]; initializePieces(human, computer); //playGame(human, computer); return 0; }
Can someone help me complete the playGame function? It needs to be on a 6x6 board. There will only be 2 ships both of length 2. A healthy ship is marked by “O” and a hit shit is marked by “X” We need to First ask the player to enter the coordinates of the first ship. Make sure that this input is on the board and actually makes up a ship of length 2. Next ask the player to enter the coordinates of the second ship. Again, make sure that these inputs are on the board and make up a ship of length 2. However, this time, also check that the ships are not on top of each other. Next you need to have the computer select the coordinates for his ship. Have the computer first generate a coordinate randomly. After this have the computer pick a random direction to extend the ship. Make sure that these coordinates are valid. Use this same logic to generate the position of the computer’s second ship making sure the ships do not overlap again. Now the game will begin. Have the human select a coordinate the attack and check if it is a hit or miss. Next have the computer randomly select a location to attack. If it computer registers a hit, print the board to show the outcome of the attack. Once the computer or the human has sunk both ships, print the winner and end the game.
Please help! CAUserst dhaslam DesktopBattleShipSolution binDebug)BattleShip.exe 5 10 3 10 0 1 23 4 5 Computer has placed his ships randomly Player. enter the position you would like to attack (ex: 1 1>

Explanation / Answer

here is your modified program : ------------->>>>>>>>

#include <iostream>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include<ctime>
#include <ctype.h>

using namespace std;

// check if a certain position on the board is occupied by a human ship. 1 indicates health ship, 2 indicated sunk portion of ship
string checkPos(int x, int y, int human[][6])
{
string str = "";
if(human[x][y] == 1)
str = "O";
else if(human[x][y] == 2)
str = "X";
else
str = " ";

return str;
}

//print the human's side of the board. This will show if the ships placed are hit
void printBoard(int human[][6], int computer[][6])
{
cout << " ";
cout << " ___ ___ ___ ___ ___ ___ " << endl;
cout << " |   |   |   |   |   |   |" << endl;
cout << "5| " << checkPos(0, 5, human) << " | " << checkPos(1, 5, human) <<" | " << checkPos(2, 5, human) << " | " << checkPos(3, 5, human) << " | " << checkPos(4, 5, human) << " | " << checkPos(5, 5, human) << " |" << endl;
cout << " |___|___|___|___|___|___|" << endl;
cout << " |   |   |   |   |   |   |" << endl;
cout << "4| " << checkPos(0, 4, human) << " | " << checkPos(1, 4, human) << " | " << checkPos(2, 4, human) << " | " << checkPos(3, 4, human) << " | " << checkPos(4, 4, human) << " | " << checkPos(5, 4, human) << " |" << endl;
cout << " |___|___|___|___|___|___|" << endl;
cout << " |   |   |   |   |   |   |" << endl;
cout << "3| " << checkPos(0, 3, human) << " | " << checkPos(1, 3, human) << " | " << checkPos(2, 3, human) << " | " << checkPos(3, 3, human) << " | " << checkPos(4, 3, human) << " | " << checkPos(5, 3, human) << " |" << endl;
cout << " |___|___|___|___|___|___|" << endl;
cout << " |   |   |   |   |   |   |" << endl;
cout << "2| " << checkPos(0, 2, human) << " | " << checkPos(1, 2, human) << " | " << checkPos(2, 2, human) << " | " << checkPos(3, 2, human) << " | " << checkPos(4, 2, human) << " | " << checkPos(5, 2, human) << " |" << endl;
cout << " |___|___|___|___|___|___|" << endl;
cout << " |   |   |   |   |   |   |" << endl;
cout << "1| " << checkPos(0, 1, human) << " | " << checkPos(1, 1, human) << " | " << checkPos(2, 1, human) << " | " << checkPos(3, 1, human) << " | " << checkPos(4, 1, human) << " | " << checkPos(5, 1, human) << " |" << endl;
cout << " |___|___|___|___|___|___|" << endl;
cout << " |   |   |   |   |   |   |" << endl;
cout << "0| " << checkPos(0, 0, human) << " | " << checkPos(1, 0, human) << " | " << checkPos(2, 0, human) << " | " << checkPos(3, 0, human) << " | " << checkPos(4, 0, human) << " | " << checkPos(5, 0, human) << " |" << endl;
cout << " |___|___|___|___|___|___|" << endl;
cout << "   0   1   2   3   4   5 " << endl;
}

void initializePieces(int human[][6], int computer[][6])
{
for(int i = 0; i < 6; i++)
for(int j = 0; j < 6; j++)
{
human[i][j] = 0;
computer[i][j] = 0;
}
}

void getHumanShip(int human[][6]){
int i,j;
cout<<" Enter The First ship co-ordinate : ex(1,1) : ";
cin>>i;
cin>>j;
human[i][j] = 1;
if(i != 5)
  human[i+1][j] = 1;
else if(j != 5)
  human[i][j+1] = 1;
else if(i != 0)
  human[i-1][j] = 1;
else if(j != 0)
  human[i][j-1] = 1;
while(1){
  cout<<" Enter The Second ship co-ordinate : ex(1,1) : ";
  cin>>i;
  cin>>j;
  if(human[i][j] == 1)
   continue;
  human[i][j] = 1;
  if(i != 5 && human[i+1][j] != 1)
   human[i+1][j] = 1;
  else if(j != 5 && human[i][j+1] != 1)
   human[i][j+1] = 1;
  else if(i != 0 && human[i-1][j] != 1)
   human[i-1][j] = 1;
  else if(j != 0 && human[i][j-1] != 1)
   human[i][j-1] = 1;
  break;
}
}

void getComputerShip(int computer[][6],int human[][6]){
srand(time(0));
int i = rand()%6;
int j = rand()%6;
while(1){
  if(human[i][j] != 1){
   break;
  }
  i = rand()%6;
  j = rand()%6;
}
computer[i][j] = 1;
if(i != 5 && human[i+1][j] != 1)
   computer[i+1][j] = 1;
else if(j != 5 && human[i][j+1] != 1)
  computer[i][j+1] = 1;
else if(i != 0 && human[i-1][j] != 1)
  computer[i-1][j] = 1;
else if(j != 0 && human[i][j-1] != 1)
  computer[i][j-1] = 1;
  
i = rand()%6;
j = rand()%6;
while(1){
  if(human[i][j] != 1){
   break;
  }
  i = rand()%6;
  j = rand()%6;
}
computer[i][j] = 1;
if(i != 5 && human[i+1][j] != 1 && computer[i+1][j] != 1)
   computer[i+1][j] = 1;
else if(j != 5 && human[i][j+1] != 1 && computer[i][j+1] != 1)
  computer[i][j+1] = 1;
else if(i != 0 && human[i-1][j] != 1 && computer[i-1][j] != 1)
  computer[i-1][j] = 1;
else if(j != 0 && human[i][j-1] != 1 && computer[i][j-1] != 1)
  computer[i][j-1] = 1;
}

int win(int arr[][6]){
for(int i = 0;i<6;i++){
  for(int j = 0;j<6;j++){
   if(arr[i][j] == 1){
    return -1;
   }
  }
}

return 1;
}

void playGame(int human[][6], int computer[][6])
{
///complete this function
getHumanShip(human);
getComputerShip(computer,human);
int chance = 0;
int i,j;
int start = 0;
while(1){
  system("cls");
  if(chance == 0 && start != 0){
   if(human[i][j] == 1){
    human[i][j] = 2;
    cout<<" Computer Hits Your Ship ";
    if(win(human) == 1){
     cout<<" Computer Win The game !!! ";
     break;
    }
   }
  }
  printBoard(human,computer);
  if(chance == 0){
   cout<<" Player, Enter the position you would like to attack (ex:1 1): ";
   cin>>i>>j;
   if(computer[i][j] == 1){
    computer[i][j] = 2;
    cout<<" You Hit The Computer Ship ";
    if(win(computer) == 1){
     cout<<" You Win The Game !!! ";
     break;
    }
    cin.get();
    cin.ignore();
   }
   chance = 1;
  }else{
   i = rand()%6;
   j = rand()%6;
   chance = 0;
  }
  start = 1;
}
}

int main()
{
int human[6][6];
int computer[6][6];
initializePieces(human, computer);
playGame(human, computer);
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote