Just a quick 2-player battleship code i made a month ago, thought it might be good here


 * 1) include


 * 1) include


 * 1) include

using namespace std;

int main

{

int p1[25]{ {0} };

int p2[25]{ {0} };

int boatch = 0;

int chose;

while (1)

{

if (boatch == 0) // see what placement player is on

{

cout << "You will input a number between 1 and 25." << endl <<

"This will be the number your boat is placed on."

<< endl << "You will chose 5 numbers." << endl << "Placement #1: ";

cin >> chose;

p1[chose - 1] = 1;

}

if (boatch == 1)

{

cout << "Placement #2: ";

cin >> chose;

p1[chose - 1] = 1;

}

if (boatch == 2)

{

cout << "Placement #3: ";

cin >> chose;

p1[chose - 1] = 1;

}

if (boatch == 3)

{

cout << "Placement #4: ";

cin >> chose;

p1[chose - 1] = 1;

}

if (boatch == 4)

{

cout << "Final Placement: ";

cin >> chose;

p1[chose - 1] = 1;

}

if (boatch == 5)

{

cout << endl << endl << endl << endl << endl << endl << endl <<

endl << endl << endl << endl << endl << endl << endl <<

endl << endl << endl << endl << endl << endl << endl <<

endl << endl << endl << endl << endl << endl << endl << "Hand to second player" << endl;

break;

}

boatch = boatch + 1;

}

boatch = 0;

while (1)

{

if (boatch == 0) // see what placement player is on

{

cout << "You will input a number between 1 and 25." << endl <<

"This will be the number your boat is placed on."

<< endl << "You will chose 5 numbers." << endl << "Placement #1: ";

cin >> chose;

p2[chose - 1] = 1;

}

if (boatch == 1)

{

cout << "Placement #2: ";

cin >> chose;

p2[chose - 1] = 1;

}

if (boatch == 2)

{

cout << "Placement #3: ";

cin >> chose;

p2[chose - 1] = 1;

}

if (boatch == 3)

{

cout << "Placement #4: ";

cin >> chose;

p2[chose - 1] = 1;

}

if (boatch == 4)

{

cout << "Final Placement: ";

cin >> chose;

p2[chose - 1] = 1;

}

if (boatch == 5)

{

cout << endl << endl << endl << endl << endl << endl << endl <<

endl << endl << endl << endl << endl << endl << endl <<

endl << endl << endl << endl << endl << endl << endl <<

endl << endl << endl << endl << endl << endl << endl;

break;

}

boatch = boatch + 1;

}

int phase = 2;

int p1points = 0;

int p2points = 0;

while (1)

{

if (phase == 2)

{

cout << "Player 1's turn." << endl << "Choose target number: ";

cin >> chose;

if (p2[chose - 1] == 1)

{

cout << endl << "Hit!" << endl;

p2[chose - 1] = 0;

p1points = p1points + 1;

}

else

{

cout << endl << "Miss" << endl;

}

if (p1points == 5)

{

cout << "Player 1 wins";

return(0);

}

phase = phase - 1;

}

if (phase == 1)

{

cout << "Player 2's turn." << endl << "Choose target number: ";

cin >> chose;

if (p1[chose - 1] == 1)

{

cout << endl << "Hit!" << endl;

p1[chose - 1] = 0;

p2points = p2points + 1;

}

else

{

cout << endl << "Miss" << endl;

}

if (p2points == 5)

{

cout << "Player 2 wins";

return(0);

}

phase = phase + 1;

}

}

}