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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
void createBoardH(int height, int width)
{
for(int i=0; i<width; ++i)
{
for(int j=0; j<height; ++j)
{
boardH[i][j] = cell;
}
}
}
void showBoardH()//original board
{
cout <<" ";
showwcoord();
for(int i=0;i<width; i++)
{
cout<<coordinate_2[i]<<" ";
for(int j=0; j<height; j++)
{
cout<< boardC[i][j]<<" ";
}
cout<<endl;
}
}
void ManualPlace(int x, int y, int Ship, int ShipOrientation)
{
if (placementfinished())
{
game();
}
int i = 0;
int j = 0;
char cell1 = cell;
cout << "Choose your first coordinate from where you want your ship to start:" <<endl;
cin >> coordinate_1[i];
switch (coordinate_1[i])
{
case 'A':
x = 1;
showBoardH();
break;
case 'B':
x = 2;
showBoardH();
break;
case 'C':
x = 3;
showBoardH();
break;
case 'D':
x = 4;
showBoardH();
break;
case 'E':
x = 5;
showBoardH();
break;
case 'F':
x = 6;
showBoardH();
break;
case 'G':
x = 7;
showBoardH();
break;
case 'H':
x = 8;
showBoardH();
break;
case 'I':
x = 9;
showBoardH();
break;
case 'J':
x = 10;
showBoardH();
break;
default:
cout<<"coordinate error, try again!"<<endl;
ManualPlace(x,y,Ship, ShipOrientation);
break;
}
cout << "Choose your second coordinate from where you want your ship to start:" <<endl;
cin >> coordinate_2[j];
switch(coordinate_2[j])
{
case 1:
y = 1;
showBoardH();
break;
case 2:
y = 2;
showBoardH();
break;
case 3:
y = 3;
showBoardH();
break;
case 4:
y = 4;
showBoardH();
break;
case 5:
y = 5;
showBoardH();
break;
case 6:
y = 6;
showBoardH();
break;
case 7:
y = 7;
showBoardH();
break;
case 8:
y = 8;
showBoardH();
break;
case 9:
y = 9;
showBoardH();
break;
case 0:
y = 10;
showBoardH();
break;
default:
cout <<"coordinate error, try again" <<endl;
ManualPlace(x,y,Ship, ShipOrientation);
}
cout << "Choose your ship type (one of the above) :" <<endl;
cout << "1- aircraft carrier"<<endl;
cout << "2- battleship"<<endl;
cout << "3- cruiser"<<endl;
cout << "4- submarine"<<endl<<endl<<endl;
cin >> Ship;
numberofA = 0;
numberofB = 0;
numberofC = 0;
numberofS = 0;
switch (Ship)
{
case 1:
ShipType = 'A';
ShipSize = 5;
numberofA ++;//number of aircrafts on board
cell1 = 'A';
break;
case 2:
ShipType = 'B';
ShipSize = 4;
numberofB ++;//number of the battleships on board
cell1 = 'B';
break;
case 3:
ShipType = 'C';
ShipSize = 3;
numberofC ++;//number of cruisers on board
cell1 = 'C';
break;
case 4:
ShipType = 'S';
ShipSize = 2;
numberofS ++;//number of submarines on board
cell1 = 'S';
break;
default: // mistypes by the user
cout << "Error in chosen option" <<endl;
cout << "Check your chooose and try again" <<endl;
ManualPlace(x,y,Ship, ShipOrientation);
break;
}
cout << "Choose your ship orientation from the options above:" <<endl;
cout <<"1- right" <<endl;
cout <<"2- left "<<endl;
cout <<"3- up "<<endl;
cout <<"4- down "<<endl<<endl<<endl;
cin >> ShipOrientation;
numberofships = 0;
switch (ShipOrientation)
{
case 1:
putshipright(x,y,Ship,ShipOrientation);
showBoardH();
numberofships ++;
cout <<"You still have ships to place..." ;
ManualPlace(x,y,Ship, ShipOrientation);
cout <<endl;
break;
case 2:
putshipleft(x,y,Ship,ShipOrientation);
showBoardH();
numberofships ++;
cout <<"You still have ships to place..." <<endl;
ManualPlace(x,y,Ship, ShipOrientation);
break;
case 3:
putshipup(x,y,Ship,ShipOrientation);
showBoardH();
numberofships ++;
cout <<"You still have ships to place..." <<endl;
ManualPlace(x,y,Ship, ShipOrientation);
break;
case 4:
putshipdown(x,y,Ship,ShipOrientation);
showBoardH();
numberofships ++;
cout <<"You still have ships to place..." <<endl;
ManualPlace(x,y,Ship, ShipOrientation);
break;
default ://misstyping by user
cout <<"Errors in typing the orientation" <<endl;
cout <<"Check spelling and try again" <<endl<<endl;
ManualPlace(x,y,ShipType, ShipOrientation);
break;
}
bool placementfinished()
{
numberofships = numberofA + numberofB + numberofC + numberofS;
bool fin = true;
if (numberofships = 6)
{
fin = true;
}
else
{
fin = false;
}
if (fin)
{
return true;
}
else
{
return false;
}
}
void putshipright(int x, int y, int Ship, int Shiporientation)
{
int i = 0;
for (i = 0;i < ShipSize;i++)
{
boardH[x+i][y]= 'A';
}
}
void putshipleft(int x, int y, int Ship, int Shiporientation)
{
int i = 0;
for (i = 0;i < ShipSize;i++)
{
boardH[x-i][y]= cell1;
}
}
void putshipup(int x, int y, int Ship, int Shiporientation)
{
int j = 0;
for (j = 0;j < ShipSize;j++)
{
boardH[x][y-j]= cell1;
}
}
void putshipdown(int x, int y, int Ship, int Shiporientation)
{
int j = 0;
for (j = 0;j < ShipSize;j++)
{
boardH[x][y+j]= cell1;
}
}
}
|