1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int x,y,counter = 1;
char userDisp[10][10]=
{
{'-','-','-','-','-','-','-','-','-','-'},
{'-','-','-','-','-','-','-','-','-','-'},
{'-','-','-','-','-','-','-','-','-','-'},
{'-','-','-','-','-','-','-','-','-','-'},
{'-','-','-','-','-','-','-','-','-','-'},
{'-','-','-','-','-','-','-','-','-','-'},
{'-','-','-','-','-','-','-','-','-','-'},
{'-','-','-','-','-','-','-','-','-','-'},
{'-','-','-','-','-','-','-','-','-','-'},
{'-','-','-','-','-','-','-','-','-','-'},
};
char myDisp[10][10]=
{
{'F','F','F','-','-','-','-','-','-','F'},
{'-','-','-','-','-','-','-','-','-','F'},
{'-','-','-','-','-','-','-','-','-','F'},
{'-','-','-','-','-','-','-','-','-','-'},
{'-','-','-','-','-','-','-','-','-','-'},
{'-','-','-','-','-','-','-','-','-','-'},
{'-','-','-','-','-','-','E','E','E','-'},
{'-','-','-','-','-','-','-','-','-','-'},
{'-','-','-','-','-','-','-','-','-','-'},
{'E','E','E','-','-','-','-','-','-','-'}
};
do
{
/*
cout<<"myDisp"<<endl;
for ( int i = 0; i <= 9; i++)
{
for ( int j = 0; j <= 9; j++)
{
cout << setw(4) << myDisp[i][j];
}
cout<<endl;
}
*/
cout<<endl<<"\n >> BATTLESHIP << "<<endl<<endl;
for ( int i = 0; i <= 9; i++)
{
for ( int j = 0; j <= 9; j++)
{
cout << setw(4) << userDisp[i][j];
}
cout<<endl;
}
cout<< "----------------------------------------------------"<<endl<<endl;
cout << "Please enter the X and Y coordinates."<<endl<<endl;
cout << " X: ";
cin >> x;
cout << " \n Y: ";
cin >> y;
cout << "\n--------------------> Shots fired: "<<counter;
counter++;
//Friend Enemy or Splash check ------------------------------------------------
if(myDisp[x][y] == 'F')
{
cout<<"\nFriendly Shot"<<endl;
cout<< endl << endl << "----------------------------------------------------";
userDisp[x][y] = 'F';
}
else if(myDisp[x][y] == 'E')
{
cout<<"\nEnemy shot"<<endl;
cout<< endl << endl << "----------------------------------------------------";
userDisp[x][y] = 'E';
}
else
{
cout<<"\nSplash!!";
cout<< endl << endl << "----------------------------------------------------";
}
//Friendly Ship conditions, this checks to see if user has sunk their friendly ship, if so then the game is over and user loses
if( (userDisp[0][0] == 'F' && userDisp[0][1] == 'F' && userDisp[0][2] == 'F') || (userDisp[0][9] == 'F' && userDisp[1][9] == 'F' && userDisp [2][9] == 'F') )
{
cout<<endl<<endl<<"\n You have lost due to because one of your friendly ships has sunk."<<endl<<endl<<endl;
cout<<endl<<"\n >> BATTLESHIP << "<<endl<<endl;
for ( int i = 0; i <= 9; i++)
{
for ( int j = 0; j <= 9; j++)
{
cout << setw(4) << userDisp[i][j];
}
cout<<endl<<endl<<endl;
}
return 0;
}
else
continue;
//Enemy Ship conditions, this checks to see if the user sunk both of the eneny ship, if so the user wins
if( (userDisp[6][6] == 'E' && userDisp[6][7] == 'E' && userDisp[6][8] == 'E') && (userDisp[9][0] == 'E' && userDisp[9][1] == 'E' && userDisp [9][2] == 'E') )
{
cout<<"\n You have WON! YOU SUNK MY BATTLESHIP! ."<<endl;
return 0;
}
else
continue;
}while( x!='a' );
}
|