Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstdlib>
- using namespace std;
- void p1deployer(int p1field, int spelplan1[], string playername[], int x, int numberOfShips, int inmv);
- void p2deployer(int p2field, int spelplan2[], string playername[], int x, int numberOfShips, int inmv);
- void battle(int player1ships, int player2ships, string playername[], int bomb, int p1hits, int p2hits, int spelplan1[], int spelplan2[]);
- int main(){
- const int p1field = 15;
- const int p2field = 15;
- const int players = 2;
- int spelplan1[p1field] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
- int spelplan2[p2field] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
- string playername[players];
- int numberOfShips;
- int inmv=0;
- int x=0;
- int bomb=0;
- int player1ships;
- int player2ships;
- int p1hits = 0;
- int p2hits = 0;
- cout << "Welcome to a game of battleship!" << endl;
- cout<<"How many battleships will you be using for this conquest? "<<endl <<"(max 15 ships each) NUMBER OF SHIPS: ";
- cin >> numberOfShips;
- if (numberOfShips>15)
- {
- cout << "Please, remember that 15 the maximum amount of ships:" << endl;
- cin >> numberOfShips;
- }
- if (numberOfShips <= 0)
- {
- cout << "Please enter a positive value:" << endl;
- cin >> numberOfShips;
- }
- player1ships = numberOfShips;
- player2ships = numberOfShips;
- cin.ignore();
- cout << numberOfShips << " ships?. Very good.. " << endl;
- cout<<"Please enter your name Player One: ";
- getline(cin, playername[0]);
- cout << "Please enter your name Player Two: ";
- getline(cin, playername[1]);
- system("cls");
- cout << playername[1] << ", look away! It's time for " << playername[0] << " to deploy his/her forces!" << endl;
- system("pause");
- system("cls");
- p1deployer(p1field , spelplan1, playername, x, numberOfShips, inmv);
- system("pause");
- system("cls");
- cout << endl << "Now, press enter to let " << playername[1] << " enter his/her positions.";
- system("pause");
- system("cls");
- p2deployer( p2field, spelplan2, playername, x, numberOfShips, inmv);
- system("pause");
- system("cls");
- cout << "Very good!, now the time has come for battle! "<<endl
- << playername[0] << ", you go first!" << endl;
- battle(player1ships,player2ships, playername, bomb, p1hits, p2hits, spelplan1, spelplan2);
- system ("pause");
- return 0;
- }
- void p1deployer(int p1field, int spelplan1[], string playername[1], int x, int numberOfShips, int inmv){
- cout << playername[0] << " please station your naval forces." << endl;
- for (x = 0; x < numberOfShips; x++){
- cout << "Avaliable areas: ";
- if (spelplan1[0] == 0){
- cout << "0" << " ";
- }
- if (spelplan1[1] == 0){
- cout << "1" << " ";
- }
- if (spelplan1[2] == 0){
- cout << "2" << " ";
- }
- if (spelplan1[3] == 0){
- cout << "3" << " ";
- }
- if (spelplan1[4] == 0){
- cout << "4" << " ";
- }
- if (spelplan1[5] == 0){
- cout << "5" << " ";
- }
- if (spelplan1[6] == 0){
- cout << "6" << " ";
- }
- if (spelplan1[7] == 0){
- cout << "7" << " ";
- }
- if (spelplan1[8] == 0){
- cout << "8" << " ";
- }if (spelplan1[9] == 0){
- cout << "9" << " ";
- }if (spelplan1[10] == 0){
- cout << "10" << " ";
- }if (spelplan1[11] == 0){
- cout << "11" << " ";
- }if (spelplan1[12] == 0){
- cout << "12" << " ";
- }if (spelplan1[13] == 0){
- cout << "13" << " ";
- }if (spelplan1[14] == 0){
- cout << "14" << " ";
- }
- cout << endl << "Please enter an area to occupy: ";
- cin >> inmv;
- if ((inmv != 0) && (inmv != 1) && (inmv != 2) && (inmv != 3) && (inmv != 4) && (inmv != 5) && (inmv != 6) && (inmv != 7) && (inmv != 8) && (inmv != 9) && (inmv != 10) && (inmv != 11) && (inmv != 12) && (inmv != 13) && (inmv != 14) )
- {
- do {
- cout << "Incorrect input, please try again: ";
- cin >> inmv;
- }
- while ((inmv != 0) && (inmv != 1) && (inmv != 2) && (inmv != 3) && (inmv != 4) && (inmv != 5) && (inmv != 6) && (inmv != 7) && (inmv != 8) && (inmv != 9) && (inmv != 10) && (inmv != 11) && (inmv != 12) && (inmv != 13) && (inmv != 14));
- }
- if (inmv == 0){
- spelplan1[0] = 1;
- }
- if (inmv == 1){
- spelplan1[1] = 1;
- }
- if (inmv == 2){
- spelplan1[2] = 1;
- }
- if (inmv == 3){
- spelplan1[3] = 1;
- }
- if (inmv == 4){
- spelplan1[4] = 1;
- }
- if (inmv == 5){
- spelplan1[5] = 1;
- }
- if (inmv == 6){
- spelplan1[6] = 1;
- }
- if (inmv == 7){
- spelplan1[7] = 1;
- }
- if (inmv == 8){
- spelplan1[8] = 1;
- }
- if (inmv == 9){
- spelplan1[9] = 1;
- }
- if (inmv == 10){
- spelplan1[10] = 1;
- }
- if (inmv == 11){
- spelplan1[11] = 1;
- }
- if (inmv == 12){
- spelplan1[12] = 1;
- }
- if (inmv == 13){
- spelplan1[13] = 1;
- }
- if (inmv == 14){
- spelplan1[14] = 1;
- }
- }
- system("pause");
- system("cls");
- cout << "You have chosen to place your ships in the following areas: " << endl;
- if (spelplan1[0] == 1){
- cout << "0" << " ";
- }
- if (spelplan1[1] == 1){
- cout << "1" << " ";
- }
- if (spelplan1[2] == 1){
- cout << "2" << " ";
- }
- if (spelplan1[3] == 1){
- cout << "3" << " ";
- }
- if (spelplan1[4] == 1){
- cout << "4" << " ";
- }
- if (spelplan1[5] == 1){
- cout << "5" << " ";
- }
- if (spelplan1[6] == 1){
- cout << "6" << " ";
- }
- if (spelplan1[7] == 1){
- cout << "7" << " ";
- }
- if (spelplan1[8] == 1){
- cout << "8" << " ";
- }if (spelplan1[9] == 1){
- cout << "9" << " ";
- }if (spelplan1[10] == 1){
- cout << "10" << " ";
- }if (spelplan1[11] == 1){
- cout << "11" << " ";
- }if (spelplan1[12] == 1){
- cout << "12" << " ";
- }if (spelplan1[13] == 1){
- cout << "13" << " ";
- }if (spelplan1[14] == 1){
- cout << "14" << " ";
- }
- }
- void p2deployer(int p2field, int spelplan2[], string playername[], int x, int numberOfShips, int inmv){
- cout << playername[1] << ", now it has become time to station your naval forces." << endl;
- for (x = 0; x < numberOfShips; x++){
- cout << "Avaliable areas: ";
- if (spelplan2[0] == 0){
- cout << "0" << " ";
- }
- if (spelplan2[1] == 0){
- cout << "1" << " ";
- }
- if (spelplan2[2] == 0){
- cout << "2" << " ";
- }
- if (spelplan2[3] == 0){
- cout << "3" << " ";
- }
- if (spelplan2[4] == 0){
- cout << "4" << " ";
- }
- if (spelplan2[5] == 0){
- cout << "5" << " ";
- }
- if (spelplan2[6] == 0){
- cout << "6" << " ";
- }
- if (spelplan2[7] == 0){
- cout << "7" << " ";
- }
- if (spelplan2[8] == 0){
- cout << "8" << " ";
- }if (spelplan2[9] == 0){
- cout << "9" << " ";
- }if (spelplan2[10] == 0){
- cout << "10" << " ";
- }if (spelplan2[11] == 0){
- cout << "11" << " ";
- }if (spelplan2[12] == 0){
- cout << "12" << " ";
- }if (spelplan2[13] == 0){
- cout << "13" << " ";
- }if (spelplan2[14] == 0){
- cout << "14" << " ";
- }
- cout << endl << "Please enter an area to occupy: ";
- cin >> inmv;
- if ((inmv != 0) && (inmv != 1) && (inmv != 2) && (inmv != 3) && (inmv != 4) && (inmv != 5) && (inmv != 6) && (inmv != 7) && (inmv != 8) && (inmv != 9) && (inmv != 10) && (inmv != 11) && (inmv != 12) && (inmv != 13) && (inmv != 14))
- {
- do {
- cout << "Incorrect input, please try again: ";
- cin >> inmv;
- }
- while ((inmv != 0) && (inmv != 1) && (inmv != 2) && (inmv != 3) && (inmv != 4) && (inmv != 5) && (inmv != 6) && (inmv != 7) && (inmv != 8) && (inmv != 9) && (inmv != 10) && (inmv != 11) && (inmv != 12) && (inmv != 13) && (inmv != 14));
- }
- if (inmv == 0){
- spelplan2[0] = 1;
- }
- if (inmv == 1){
- spelplan2[1] = 1;
- }
- if (inmv == 2){
- spelplan2[2] = 1;
- }
- if (inmv == 3){
- spelplan2[3] = 1;
- }
- if (inmv == 4){
- spelplan2[4] = 1;
- }
- if (inmv == 5){
- spelplan2[5] = 1;
- }
- if (inmv == 6){
- spelplan2[6] = 1;
- }
- if (inmv == 7){
- spelplan2[7] = 1;
- }
- if (inmv == 8){
- spelplan2[8] = 1;
- }
- if (inmv == 9){
- spelplan2[9] = 1;
- }
- if (inmv == 10){
- spelplan2[10] = 1;
- }
- if (inmv == 11){
- spelplan2[11] = 1;
- }
- if (inmv == 12){
- spelplan2[12] = 1;
- }
- if (inmv == 13){
- spelplan2[13] = 1;
- }
- if (inmv == 14){
- spelplan2[14] = 1;
- }
- }
- cout << "You have chosen to place your ships in the following areas: " << endl;
- if (spelplan2[0] == 1){
- cout << "0" << " ";
- }
- if (spelplan2[1] == 1){
- cout << "1" << " ";
- }
- if (spelplan2[2] == 1){
- cout << "2" << " ";
- }
- if (spelplan2[3] == 1){
- cout << "3" << " ";
- }
- if (spelplan2[4] == 1){
- cout << "4" << " ";
- }
- if (spelplan2[5] == 1){
- cout << "5" << " ";
- }
- if (spelplan2[6] == 1){
- cout << "6" << " ";
- }
- if (spelplan2[7] == 1){
- cout << "7" << " ";
- }
- if (spelplan2[8] == 1){
- cout << "8" << " ";
- }if (spelplan2[9] == 1){
- cout << "9" << " ";
- }if (spelplan2[10] == 1){
- cout << "10" << " ";
- }if (spelplan2[11] == 1){
- cout << "11" << " ";
- }if (spelplan2[12] == 1){
- cout << "12" << " ";
- }if (spelplan2[13] == 1){
- cout << "13" << " ";
- }if (spelplan2[14] == 1){
- cout << "14" << " ";
- }
- }
- void battle(int player1ships, int player2ships, string playername[], int bomb, int p1hits, int p2hits, int spelplan1[], int spelplan2[]){
- while (player1ships > 0 && player2ships > 0){
- cout << " [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] " << endl;
- cout << playername[0] << " , select an area to strike: ";
- cin >> bomb;
- do
- {
- cout << "incorrect input, try again:";
- cin >> bomb;
- }
- while ((bomb != '0') && (bomb != '1') && (bomb != '2') && (bomb != '3') && (bomb != '4') && (bomb != '5') && (bomb != '6') && (bomb != '7') && (bomb != '8') && (bomb != '9') && (bomb != '10') && (bomb != '11') && (bomb != '12') && (bomb != '13') && (bomb != '14'));
- if (spelplan2[bomb] == 1){
- cout << "BOOM! " << endl;
- spelplan2[bomb] = 0;
- player2ships--;
- p1hits++;
- cout << " Number of hits so far: " << p1hits << endl;
- if (player2ships == 0){
- cout << playername[0] << " wins!" << endl;
- }
- }
- else{ cout << "*splash*" << endl; }
- system("pause");
- system("cls");
- if (player2ships > 0){
- cout << " [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] " << endl;
- cout << playername[1] << ",select an area to strike: ";
- cin >> bomb;
- do
- {
- cout << "incorrect input, try again:";
- cin >> bomb;
- }
- while ((bomb != '0') && (bomb != '1') && (bomb != '2') && (bomb != '3') && (bomb != '4') && (bomb != '5') && (bomb != '6') && (bomb != '7') && (bomb != '8') && (bomb != '9') && (bomb != '10') && (bomb != '11') && (bomb != '12') && (bomb != '13') && (bomb != '14'));
- if (spelplan1[bomb] == 1){
- cout << "BOOM! " << endl;
- spelplan1[bomb] = 0;
- player1ships--;
- p2hits++;
- cout << " Number of hits so far: " << p2hits << endl;
- if (player1ships == 0){
- cout << playername[1] << " wins!" << endl;
- }
- }
- else{ cout << "*splash*" << endl; }
- system("pause");
- system("cls");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement