Code:
#include <iostream.h>
#include <fstream.h>
#include <math.h>
#include <iomanip.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
const int Rows = 10;
const int Columns = 11;
int Index1, Index2;
int BattleShipPoint, DestroyerPoint, CarrierPoint;
int PatrolPoint, SubmarinePoint;
int BattleShipDirection, DestroyerDirection, CarrierDirection;
int PatrolDirection, SubmarineDirection;
int BattleShipRow, DestroyerRow, CarrierRow;
int PatrolRow, SubmarineRow;
int PositionRow, PositionColumn, Direction;
int Size, Error = 0;
int Temporary, Temporary2;
char val [Rows][Columns] = {65,43,43,43,43,43,43,43,43,43,43,
66,43,43,43,43,43,43,43,43,43,43,
67,43,43,43,43,43,43,43,43,43,43,
68,43,43,43,43,43,43,43,43,43,43,
69,43,43,43,43,43,43,43,43,43,43,
70,43,43,43,43,43,43,43,43,43,43,
71,43,43,43,43,43,43,43,43,43,43,
72,43,43,43,43,43,43,43,43,43,43,
73,43,43,43,43,43,43,43,43,43,43,
74,43,43,43,43,43,43,43,43,43,43};
void Splash();
void Instructions();
void Display();
void ShipPlacement();
void PlacementError();
void Overlap();
int main(){
//int Escape;
Splash();
Display();
Instructions();
/*_cputs( "Type 'Y' when finished typing keys: " );
do
{
Escape = _getch();
Escape = toupper( Escape );
} while( Escape != 'Y' );
_putch( Escape );
_putch( '\r' );
_putch( '\n' );
system("pause");
system("cls");
*/
ShipPlacement();
return 0;
}
void Splash(){
//Splash Screen
cout<<" __ "<<endl;
cout<<" || // // || "<<endl;
cout<<" ---- // // --- "<<endl;
cout<<" | || //---// \\\\ ||| | "<<endl;
cout<<" ------------------ ----------- "<<endl;
cout<<" | Yeah, it's big / \\ SS Lloyd \\ "<<endl;
cout<<" | / \\ \\ "<<endl;
cout<<" //____//____//____//____//____//____//____//____//____// "<<endl<<endl<<endl;
cout<<"BBBB T T L SSSS H"<<endl;
cout<<"B B T T L S S H I"<<endl;
cout<<"B B TTTTTTTTTT L S H"<<endl;
cout<<"BBBB AAA T T L EEE SSSS H HH I PPPP"<<endl;
cout<<"B B A A T T L E E S HH H I P P"<<endl;
cout<<"B B A A T T L E E S S H H I P P"<<endl;
cout<<"BBBB AAAA T T L EEEEE SSSS H H I PPPP"<<endl;
cout<<" P"<<endl;
cout<<" P"<<endl;
cout << "Welcome to Battleship!!!!\n\n";
cout << "Your mission if you chose to except it is to obliterate your oppenent."
<< "\n\n";
cout << "If you chose to except your mission hit enter to the game"
<< " or otherwise leave.\n" << endl;
system("pause");
system("cls");
}
void Instructions(){
//Game Instructions
cout << "\n" << endl;
cout << "The following is an example of the gameboard." << endl;
cout << "The rules of the game are as follows.\n" << endl;
cout << "The player will attack the computer first.\n" << endl;
cout << "During either the players or computer turn, if either hit"
<< " their opponent then you recieve another turn immediately.\n" << endl;
cout << "The first player to win two out of three rounds wins." << endl;
system("pause");
system("cls");
}
void Display(){
//Game Board
cout << Error << endl;
if(Error == 1){
PositionRow = 0;
PositionColumn = 0;
Direction = 0;
for(PositionRow = 1; PositionRow < 11; PositionRow++){
for(PositionColumn = 0; PositionColumn < 10; PositionColumn++)
{
val[PositionRow][PositionColumn] = 43;
}
val[PositionRow][PositionColumn] = 43;
}
for(PositionRow = 0; PositionRow < 11; PositionRow++){
val[PositionRow][0] = PositionRow + 65;
}
Error = 0;
ShipPlacement();
}
if(Direction == 3 && PositionColumn < Size){
PositionRow = 0;
PositionColumn = 0;
Direction = 0;
Error = 0;
for(PositionRow = 1; PositionRow < 11; PositionRow++){
for(PositionColumn = 0; PositionColumn < 10; PositionColumn++)
{
val[PositionRow][PositionColumn] = 43;
}
val[PositionRow][PositionColumn] = 43;
}
}
if(Direction == 4 && PositionColumn > 11 - Size){
PositionRow = 0;
PositionColumn = 0;
Direction = 0;
Error = 0;
for(PositionRow = 1; PositionRow < 11; PositionRow++){
for(PositionColumn = 0; PositionColumn < 10; PositionColumn++)
{
val[PositionRow][PositionColumn] = 43;
}
val[PositionRow][PositionColumn] = 43;
}
}
if(Direction == 1 && PositionRow < Size){
PositionRow = 0;
PositionColumn = 0;
Direction = 0;
Error = 0;
for(PositionRow = 1; PositionRow < 11; PositionRow++){
for(PositionColumn = 0; PositionColumn < 10; PositionColumn++)
{
val[PositionRow][PositionColumn] = 43;
}
val[PositionRow][PositionColumn] = 43;
}
}
if(PositionColumn > 10 || PositionRow > 11 || PositionColumn < 0 || PositionRow < 0){
PositionRow = 0;
PositionColumn = 0;
Direction = 0;
Error = 0;
for(PositionRow = 1; PositionRow < 11; PositionRow++){
for(PositionColumn = 0; PositionColumn < 10; PositionColumn++)
{
val[PositionRow][PositionColumn] = 43;
}
val[PositionRow][PositionColumn] = 43;
}
}
if(PositionRow > 11 - Size){
PositionRow = 0;
PositionColumn = 0;
Direction = 0;
Error = 0;
for(PositionRow = 1; PositionRow < 11; PositionRow++){
for(PositionColumn = 0; PositionColumn < 10; PositionColumn++)
{
val[PositionRow][PositionColumn] = 43;
}
val[PositionRow][PositionColumn] = 43;
}
}
if(Direction > 4){
PositionRow = 0;
PositionColumn = 0;
Direction = 0;
Error = 0;
for(PositionRow = 1; PositionRow < 11; PositionRow++){
for(PositionColumn = 0; PositionColumn < 10; PositionColumn++)
{
val[PositionRow][PositionColumn] = 43;
}
val[PositionRow][PositionColumn] = 43;
}
}
cout << " 1 2 3 4 5 6 7 8 9 10" << endl;
switch(Direction){
case 1:
for(Index1 = PositionRow; Index1 > PositionRow - Size; Index1--)
val[Index1][PositionColumn] = 2;
break;
case 2:
for(Index1 = PositionRow; Index1 < Size + PositionRow; Index1++)
val[Index1][PositionColumn] = 2;
break;
case 3:
for(Index2 = PositionColumn; Index2 > PositionColumn - Size; Index2--)
val[PositionRow][Index2] = 2;
break;
case 4:
for(Index2 = PositionColumn; Index2 < Size + PositionColumn; Index2++)
val[PositionRow][Index2] = 2;
break;
}
for(int Rows2 = 0; Rows2 < 10; Rows2++){
for(int Columns2 = 0; Columns2 < 11; Columns2++){
cout << val[Rows2][Columns2] <<" ";
}
cout << endl << endl;
Error = 0;
}
}
void PlacementError()
{
if(PositionColumn == 0){
cout << "Invalid, Please choose another place for your ship!" << endl;
Error = 1;
return;
}
if(Direction == 3 && PositionColumn < Size){
cout << "Invalid, Please choose another place for your ship!" << endl;
Error = 1;
return;
}
if(Direction == 4 && PositionColumn > 11 - Size){
cout << "Invalid, Please choose another place for your ship!" << endl;
Error = 1;
return;
}
if(Direction == 1 && PositionRow < Size){
cout << "Invalid, Please choose another place for your ship!" << endl;
Error = 1;
return;
}
if(PositionColumn > 10 || PositionRow > 11 || PositionColumn < 0 || PositionRow < 0){
cout << "Invalid, Please choose another place for your ship!" << endl;
Error = 1;
return;
}
if(PositionRow > 11 - Size){
cout << "Invalid, Please choose another place for your ship!" << endl;
Error = 1;
return;
}
if(Direction > 4){
cout << "Invalid, Please choose another place for your ship!" << endl;
Error = 1;
return;
}
}
void Overlap()
{
Temporary = 99;
Temporary2 = 99;
for(Temporary = BattleShipPoint; Temporary < 3 + BattleShipPoint; Temporary++){
for(Temporary2 = DestroyerPoint; Temporary2 < 2 + DestroyerPoint; Temporary2++){
if(Temporary == Temporary2){
cout << "Invalid, Please choose another place for your ship!" << endl;
Error = 1;
}
}
}
return;
}
/*for(Temporary = BattleShipPoint; Temporary < 3 + BattleShipPoint; Temporary++){
for(Temporary2 = CarrierPoint; Temporary2 < 4 + CarrierPoint; Temporary2++){
if(Temporary == Temporary2){
cout << "Invalid, Please choose another place for your ship!" << endl;
Error = 1;
return;
}
}
}
for(Temporary = BattleShipPoint; Temporary < 3 + BattleShipPoint; Temporary++){
for(Temporary2 = PatrolPoint; Temporary2 < 1 + PatrolPoint; Temporary2++){
if(Temporary == Temporary2){
cout << "Invalid, Please choose another place for your ship!" << endl;
Error = 1;
return;
}
}
}
for(Temporary = BattleShipPoint; Temporary < 3 + BattleShipPoint; Temporary++){
for(Temporary2 = SubmarinePoint; Temporary2 < 2 + SubmarinePoint; Temporary2++){
if(Temporary == Temporary2){
cout << "Invalid, Please choose another place for your ship!" << endl;
Error = 1;
return;
}
}
}
for(Temporary = DestroyerPoint; Temporary < 2 + DestroyerPoint; Temporary++){
for(Temporary2 = CarrierPoint; Temporary2 < 4 + CarrierPoint; Temporary2++){
if(Temporary == Temporary2){
cout << "Invalid, Please choose another place for your ship!" << endl;
Error = 1;
return;
}
}
}
for(Temporary = DestroyerPoint; Temporary < 2 + DestroyerPoint; Temporary++){
for(Temporary2 = PatrolPoint; Temporary2 < 1 + PatrolPoint; Temporary2++){
if(Temporary == Temporary2){
cout << "Invalid, Please choose another place for your ship!" << endl;
Error = 1;
return;
}
}
}
for(Temporary = DestroyerPoint; Temporary < 2 + DestroyerPoint; Temporary++){
for(Temporary2 = SubmarinePoint; Temporary2 < 2 + SubmarinePoint; Temporary2++){
if(Temporary == Temporary2){
cout << "Invalid, Please choose another place for your ship!" << endl;
Error = 1;
return;
}
}
}
for(Temporary = CarrierPoint; Temporary < 4 + CarrierPoint; Temporary++){
for(Temporary2 = PatrolPoint; Temporary2 < 1 + PatrolPoint; Temporary2++){
if(Temporary == Temporary2){
cout << "Invalid, Please choose another place for your ship!" << endl;
Error = 1;
return;
}
}
}
for(Temporary = CarrierPoint; Temporary < 4 + CarrierPoint; Temporary++){
for(Temporary2 = SubmarinePoint; Temporary2 < 2 + SubmarinePoint; Temporary2++){
if(Temporary == Temporary2){
cout << "Invalid, Please choose another place for your ship!" << endl;
Error = 1;
return;
}
}
}
for(Temporary = PatrolPoint; Temporary < 1 + PatrolPoint; Temporary++){
for(Temporary2 = SubmarinePoint; Temporary2 < 2 + SubmarinePoint; Temporary2++){
if(Temporary == Temporary2){
cout << "Invalid, Please choose another place for your ship!" << endl;
Error = 1;
return;
}
}
}
}
*/
void ShipPlacement()
{
Error = 0;
BattleShipPoint = 0;
DestroyerPoint = 0;
CarrierPoint = 0;
PatrolPoint = 0;
SubmarinePoint = 0;
BattleShipDirection = 0;
DestroyerDirection = 0;
CarrierDirection = 0;
PatrolDirection = 0;
SubmarineDirection = 0;
BattleShipRow = 0;
DestroyerRow = 0;
CarrierRow = 0;
PatrolRow = 0;
SubmarineRow = 0;
cout << "In which column do you want your BATTLESHIP(A NUMBER!!!)!" << endl;
cin >> BattleShipPoint;
cout << "Which row would you place your BATTLESHIP! " << endl;
cout << "(A = 0, B = 1, C = 2, ETC.)" << endl;
cin >> BattleShipRow;
cout << "What direction do you wish the ship to go?\n" << endl;
cout << "(Use UP(1), DOWN(2), LEFT(3), AND RIGHT(4) AS DIRECTIONS)" << endl;
cin >> BattleShipDirection;
PositionRow = BattleShipRow;
PositionColumn = BattleShipPoint;
Direction = BattleShipDirection;
Size = 4;
Overlap();
Error=0;
if(Error == 0)
PlacementError();
Display();
Error = 0;
cout << "In which column do you want your DESTROYER(A NUMBER!!!)!" << endl;
cin >> DestroyerPoint;
cout << "Which row would you place your DESTROYER! " << endl;
cout << "(A = 0, B = 1, C = 2, ETC.)" << endl;
cin >> DestroyerRow;
cout << "What direction do you wish the ship to go?\n" << endl;
cout << "(Use UP(1), DOWN(2), LEFT(3), AND RIGHT(4) AS DIRECTIONS)" << endl;
cin >> DestroyerDirection;
PositionRow = DestroyerRow;
PositionColumn = DestroyerPoint;
Direction = DestroyerDirection;
Size = 3;
Overlap();
if(Error == 0)
PlacementError();
Display();
Error = 0;
cout << "In which column do you want your CARRIER(A NUMBER!!!)!" << endl;
cin >> CarrierPoint;
cout << "Which row would you place your CARRIER!" << endl;
cout << "(A = 0, B = 1, C = 2, ETC.)" << endl;
cin >> CarrierRow;
cout << "What direction do you wish the ship to go?\n" << endl;
cout << "(Use UP(1), DOWN(2), LEFT(3), AND RIGHT(4) AS DIRECTIONS)" << endl;
cin >> CarrierDirection;
PositionRow = CarrierRow;
PositionColumn = CarrierPoint;
Direction = CarrierDirection;
Size = 5;
Overlap();
if(Error == 0)
PlacementError();
Display();
Error = 0;
cout << "In which column do you want your PATROL BOAT(A NUMBER!!!)!" << endl;
cin >> PatrolPoint;
cout << "Which row would you place your PATROL BOAT! " << endl;
cout << "(A = 0, B = 1, C = 2, ETC.)" << endl;
cin >> PatrolRow;
cout << "What direction do you wish the ship to go?\n" << endl;
cout << "(Use UP(1), DOWN(2), LEFT(3), AND RIGHT(4) AS DIRECTIONS)" << endl;
cin >> PatrolDirection;
PositionRow = PatrolRow;
PositionColumn = PatrolPoint;
Direction = PatrolDirection;
Size = 2;
Overlap();
if(Error == 0)
PlacementError();
Display();
Error = 0;
cout << "In which column do you want your SUBMARINE(A NUMBER!!!)!" << endl;
cin >> SubmarinePoint;
cout << "Which row would you place your SUBMARINE! " << endl;
cout << "(A = 0, B = 1, C = 2, ETC.)" << endl;
cin >> SubmarineRow;
cout << "What direction do you wish the ship to go?\n" << endl;
cout << "(Use UP(1), DOWN(2), LEFT(3), AND RIGHT(4) AS DIRECTIONS)" << endl;
cin >> SubmarineDirection;
PositionRow = SubmarineRow;
PositionColumn = SubmarinePoint;
Direction = SubmarineDirection;
Size = 3;
Overlap();
if(Error == 0)
PlacementError();
Display();
Error = 0;
}