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

1.Play Battle Ship on a 4x4 grid 2.Place two ships (size 3 and 2) randomly 3.cho

ID: 3625288 • Letter: 1

Question

1.Play Battle Ship on a 4x4 grid

2.Place two ships (size 3 and 2) randomly

3.choice to be first or second player

4.graphical display of the board

5.intuitive way to place moves using position numbers below. Program must verify user move (input) is correct before placing the move (attack).

6.Each program is responsible for checking the desired moves of the opponent. If opponent makes an illegal move, message should display the illegal move. It is you program's job to recognize that it is an illegal move and state as such. If your program makes an illegal move and is caught by the opponent program, then it will result in a loss. If your program incorrectly state that opponent made an illegal move, then you forfeit the game.

7.after each game, determine and display the winner

8.after the match (two games), display game score

9.If the statistics of points and such is incorrectly displayed, then it will count as a loss.

10.If a player gets a hit, they are allowed to take another shot. Even if the player gets another hit, their turn is done
Positions are as follows
0,0 0,1 0,2 0,3
1,0 1,1 1,2 1,3
2,0 2,1 2,2 2,3
3,0 3,1 3,2 3,3

This is what I have so far: The problems I am havin is that the game does not state invalid moves nor does the game actually end, the program quits after one ship is sunk

 

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <ctime>

using namespace std;

const bool notSunk = true;

void instructions();
void displayBoard(const char board[4][4]);
void resetBoards(char x[4][4], char y[4][4]);
void guessSpot(char x[4][4], char y[4][4]);

void instructions()
{
cout << "Battle Ship ";
cout << "These are the possible positions: ";
cout << "0,0 0,1 0,2 0,3 ";
cout << "1,0 1,1 1,2 1,3 ";
cout << "2,0 2,1 2,2 2,3 ";
cout << "3,0 3,1 3,2 3,3 ";
cout << " X = not guessed H = hit M = miss S = sunk ";
cin.ignore();
cout <<endl<<endl;
}

void displayBoard(const char board[4][4])
{
for (int i = 0; i <= 3; ++i)
{
for (int j = 0; j <= 3; ++j)
cout << board[i][j] << ", ";
cout <<endl<<endl;
}
}

void resetBoards(char x[4][4], char y[4][4])
{
for (int i = 0; i <= 3; ++i)
for (int j = 0; j <= 3; ++j)
x[i][j] = 'X';

for (int i = 0; i <= 3; ++i)
for (int j = 0; j <= 3; ++j)
y[i][j] = 'X';
}

void guessSpot(char board[4][4], char exboard[4][4], bool &destroyer, bool &sub)
{
int a;
int b;
int hitNumber = 0;
cout << "Enter 1st coordinate: ";
cin >> a;
cout << "Enter 2nd coordinate: ";
cin >> b;

 

if (exboard[a][b] == 'D')
{
if (destroyer == notSunk)
{
board[a][b] = 'H';
cout << "Hit! ";
for (int i = 0; i <= 3; ++i)
for (int j = 0; j <= 3; ++j)
if ((exboard[i][j] == 'D') && (board[i][j] == 'H'))
++hitNumber;

if (hitNumber == 3)
{
destroyer =! notSunk;
cout << "Destroyer sunk! ";
for (int i = 0; i <= 3; ++i)
for (int j = 0; j <= 3; ++i)
if ((exboard[i][j] == 'D') && (board[i][j] == 'H'))
board[i][j] = 'S';
}
return;
}
else
{
cout << "Already hit. ";
return;
}
}

if (exboard[a][b] == 'Y')
{
if (sub == notSunk)
{
board[a][b] = 'H';
cout << "Hit! ";
for (int i = 0; i <= 3; ++i)
for (int j = 0; j <= 3; ++j)
if ((exboard[i][j] == 'Y') && (board[i][j] == 'H'))
++hitNumber;

if (hitNumber == 2)
{
sub =! notSunk;
cout << "Submarine sunk! ";
for (int i = 0; i <= 3; ++i)
for (int j = 0; j <= 3; ++i)
if ((exboard[i][j] == 'Y') && (board[i][j] == 'H'))
board[i][j] = 'S';
}
return;

}
else
{
cout << "Already hit. ";
return;
}

 

}

if (exboard[a][b] == 'X')
{
cout << "Miss! ";
board[a][b] = 'M';
}

 

else
cout << "Already hit. ";

}

int main()
{
int choice;
bool gameOver = false;
bool p1destroyer = notSunk;
bool p1sub = notSunk;
bool p2destroyer = notSunk;
bool p2sub = notSunk;
bool &r_p1destroyer = p1destroyer;
bool &r_p1sub = p1sub;
bool &r_p2destroyer = p2destroyer;
bool &r_p2sub = p2sub;
const char x = 'X'; //not guessed
const char h = 'H'; //hit
const char d = 'D'; //destroyer
const char s = 'S'; //sunk
const char y = 'Y'; //sub
const char m = 'M'; //miss
char p1grid[4][4];
char p2grid[4][ 4];

char exgrid1[ 4 ][ 4] = {{x, d, d, d},
{x, x, x, x},
{x, y, x, x},
{x, y, x, x}};

char exgrid2[ 4 ][ 4 ] = {{x, x, y, x},
{x, x, y, x},
{d, d, d, x},
{x, x, x, x}};

instructions();

resetBoards(p1grid, p2grid);

cout << "Enter 1 to start a game, 2 to exit: ";
cin >> choice;

switch(choice)
{
case 1:
while (gameOver == false)
{
cout << "Player 1 turn."<<endl;
displayBoard(p2grid);
guessSpot(p2grid, exgrid2, r_p2destroyer, r_p2sub);
if(r_p2destroyer != notSunk && r_p2sub != notSunk)
{
cout << "Player 1 wins!"<<endl;
gameOver = true;
}

if (gameOver == false)
{
cout << "Player 2 turn."<<endl;
displayBoard(p1grid);
guessSpot(p1grid, exgrid1, r_p1destroyer, r_p1sub);
if(r_p1destroyer != notSunk && r_p1sub != notSunk)
{
cout << "Player 2 wins!"<<endl;
gameOver = true;
}
}

if (gameOver == true)
{
cout << "Enter 1 to play again, enter 2 to exit: ";
cin >> choice;
if (choice == 1)
gameOver = false;
}
}
break;
case 2:
break;
default:
cout << "Invalid response."<<endl;
break;
}

 

int mm;
cin>>mm;
return 0;
}

Explanation / Answer

please rate - thanks

illegal moves now checked for, and game ends

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <ctime>

using namespace std;

const bool notSunk = true;

void instructions();
void displayBoard(const char board[4][4]);
void resetBoards(char x[4][4], char y[4][4]);
void guessSpot(char x[4][4], char y[4][4]);

void instructions()
{
cout << "Battle Ship ";
cout << "These are the possible positions: ";
cout << "0,0 0,1 0,2 0,3 ";
cout << "1,0 1,1 1,2 1,3 ";
cout << "2,0 2,1 2,2 2,3 ";
cout << "3,0 3,1 3,2 3,3 ";
cout << " X = not guessed H = hit M = miss S = sunk ";
cin.ignore();
cout <<endl<<endl;
}

void displayBoard(const char board[4][4])
{
for (int i = 0; i <= 3; ++i)
{
for (int j = 0; j <= 3; ++j)
cout << board[i][j] << ", ";
cout <<endl<<endl;
}
}

void resetBoards(char x[4][4], char y[4][4])
{
for (int i = 0; i <= 3; ++i)
for (int j = 0; j <= 3; ++j)
x[i][j] = 'X';

for (int i = 0; i <= 3; ++i)
for (int j = 0; j <= 3; ++j)
y[i][j] = 'X';
}

void guessSpot(char board[4][4], char exboard[4][4], bool &destroyer, bool &sub)
{
int a;
int b;
int hitNumber = 0;
cout << "Enter 1st coordinate: ";
cin >> a;
cout << "Enter 2nd coordinate: ";
cin >> b;
while(board[a][b]!='X')
    {cout<<"Invalid move ";
     cout << "Enter 1st coordinate: ";
     cin >> a;
     cout << "Enter 2nd coordinate: ";
     cin >> b;
     }


if (exboard[a][b] == 'D')
{
if (destroyer == notSunk)
{
board[a][b] = 'H';
cout << "Hit! ";
for (int i = 0; i <= 3; ++i)
for (int j = 0; j <= 3; ++j)
if ((exboard[i][j] == 'D') && (board[i][j] == 'H'))
++hitNumber;

if (hitNumber == 3)
{
destroyer =! notSunk;
cout << "Destroyer sunk! ";
for (int i = 0; i <= 3; ++i)
for (int j = 0; j <= 3; ++j)
if ((exboard[i][j] == 'D') && (board[i][j] == 'H'))
board[i][j] = 'S';
}
return;
}
else
{
cout << "Already hit. ";
return;
}
}

if (exboard[a][b] == 'Y')
{
if (sub == notSunk)
{
board[a][b] = 'H';
cout << "Hit! ";
for (int i = 0; i <= 3; ++i)
for (int j = 0; j <= 3; ++j)
     if ((exboard[i][j] == 'Y') && (board[i][j] == 'H'))
           ++hitNumber;

if (hitNumber == 2)
{
sub =! notSunk;
cout << "Submarine sunk! ";
for (int i = 0; i <= 3; ++i)
for (int j = 0; j <= 3; ++j)
if ((exboard[i][j] == 'Y') && (board[i][j] == 'H'))
     board[i][j] = 'S';

}
return;

}
else
{
cout << "Already hit. ";
return;
}



}

if (exboard[a][b] == 'X')
{
cout << "Miss! ";
board[a][b] = 'M';
}



else
cout << "Already hit. ";

}

int main()
{
int choice;
bool gameOver = false;
bool p1destroyer = notSunk;
bool p1sub = notSunk;
bool p2destroyer = notSunk;
bool p2sub = notSunk;
bool &r_p1destroyer = p1destroyer;
bool &r_p1sub = p1sub;
bool &r_p2destroyer = p2destroyer;
bool &r_p2sub = p2sub;
const char x = 'X'; //not guessed
const char h = 'H'; //hit
const char d = 'D'; //destroyer
const char s = 'S'; //sunk
const char y = 'Y'; //sub
const char m = 'M'; //miss
char p1grid[4][4];
char p2grid[4][ 4];

char exgrid1[ 4 ][ 4] = {{x, d, d, d},
                         {x, x, x, x},
                         {x, y, x, x},
                         {x, y, x, x}};

char exgrid2[ 4 ][ 4 ] = {{x, x, y, x},
                          {x, x, y, x},
                          {d, d, d, x},
                          {x, x, x, x}};

instructions();

resetBoards(p1grid, p2grid);

cout << "Enter 1 to start a game, 2 to exit: ";
cin >> choice;

switch(choice)
{
case 1:
while (gameOver == false)
{
cout << "Player 1 turn."<<endl;
displayBoard(p2grid);
guessSpot(p2grid, exgrid2, r_p2destroyer, r_p2sub);
if(r_p2destroyer != notSunk && r_p2sub != notSunk)
{
cout << "Player 1 wins!"<<endl;
gameOver = true;
}

if (gameOver == false)
{
cout << "Player 2 turn."<<endl;
displayBoard(p1grid);
guessSpot(p1grid, exgrid1, r_p1destroyer, r_p1sub);
if(r_p1destroyer != notSunk && r_p1sub != notSunk)
{
cout << "Player 2 wins!"<<endl;
gameOver = true;
}
}

if (gameOver == true)
{
cout << "Enter 1 to play again, enter 2 to exit: ";
cin >> choice;
if (choice == 1)
gameOver = false;
}
}
break;
case 2:
break;
default:
cout << "Invalid response."<<endl;
break;
}



int mm;
cin>>mm;
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