#include
#include
#include
#include
//Function: opener
//Input: nothing
//Output: nothing
void opener();
//Function: menu
//Input: nothing
//Output: nothing
int menu(int shiparray[10][10], int y);
//Function: instructions
//Input: nothing
//Output: nothing
void instructions();
//Function: cleararray
//Input: shiparray
//Output: shiparray
void cleararray(int shiparray[10][10]);
//Function: setup
//Input: nothing
//Output: shiparray a value determining if setup was completed
int setup(int shiparray[10][10]);
//Function: displayboard
//Input: shiparray
//Output: shiparray
void displayboard(int shiparray[10][10]);
//Function: play
//Input: shiparray
//Output: nothing
void play(int shiparray[10][10]);
//Function: cleararray2
//Input: array
//Output: array
int cleararray2(int shiparray[10][10]);
//Function: displayboardsetup
//Input: array
//Output: nothing
void displayboardsetup(int shiparray[10][10]);
//Function: askagain
//Input: nothing
//Output: choice
int askagain();
//Function: mainprog
//Input: nothing
//Output: nothing
int mainprog();
//Main body
int main()
{
int answer = 0;
do
{
//go to the main program
answer = mainprog();
}while(answer != 2);
//clear the screen and exit
clrscr();
return 0;
}
//Function: mainprog
//Input: nothing
//Output: nothing
int mainprog()
{
clrscr();
//initiate graphics mode
int graphdriver = DETECT, graphmode;
initgraph(&graphdriver, &graphmode, "c:..\\bgi");
//initialize and clear the array
int answer = 0;
int shiparray[10][10];
cleararray(shiparray);
int num = 0;
//display some text in graphics mode
opener();
//what a waste of graphics
closegraph();
//go on to the menu
answer = menu(shiparray, num);
return answer;
}
//Function: askagain
//Input: nothing
//Output: choice
int askagain()
{
//variables
int choice = 0;
//do while loop asking if they want to go again
do
{
clrscr();
cout << "*************************" << endl
<< "* 1.)Play again? " << endl
<< "* 2.)Quit " << endl
<< "*************************" << endl
<< "Enter your choice: ";
cin >> choice;
//error handling
}while(choice != 1 && choice != 2);
//return their choice to the menu function
return choice;
}
//Function: displayboardsetup
//Input: array
//Output: nothing
void displayboardsetup(int shiparray[10][10])
{
clrscr();
//display the numbers identifying the columns
cout << " 0 1 2 3 4 5 6 7 8 9";
for(int row = 0; row < 10; row++)
{
cout << endl<< " ------------------------------------------" << endl
//display the row number
<< row << " | ";
for(int col = 0; col < 10; col++)
{
//if its a 0, which all of them are except ships
if(shiparray[row][col] == 0)
cout << " " << " | ";
//if its a ship display a 1
else
cout << shiparray[row][col] << " | ";
}
}
cout << endl << " ------------------------------------------" << endl;
}
//Function: play
//Input: shiparray
//Output: nothing
void play(int shiparray[10][10])
{
//variables
int x;
int row = 0;
int col = 0;
int hits = 0;
int guesses = 0;
//loop allowing them to play until they win or lose
do
{
//loop making sure their rows and columns are valid
do
{
clrscr();
displayboard(shiparray);
//display their game info
cout << "Hits: " << hits << " Guesses left: " << 50-guesses << endl;
//input the coordinates
cout << "Guess where a ship is (ex: 3 4): ";
cin >> row >> col;
//if special characters
//instructions
if(row == 111 && col == 111)
instructions();
//quit
if(row == 555 && col == 555)
{
//to allow to quit
x = 1;
break;
}
//error handling validating rows and columns
}while(row < 0 || row > 9 || col < 0 || col > 9);
//making sur they haven't already guessed there
if(shiparray[row][col] == 2 || shiparray[row][col] == 8)
{
clrscr();
displayboard(shiparray);
cout << "You've already guessed there!!";
delay(1000);
}
if(shiparray[row][col] == 1)
{
shiparray[row][col] = 8;
hits++;
}
//if they guess wrong, their guess counter increments
if(shiparray[row][col] == 0)
{
shiparray[row][col] = 2;
guesses++;
}
//making sure they haven't won
if(hits == 16)
{
clrscr();
cout << "You've won!!!";
delay(2000);
}
//if they guess right an 8 is displayed as a hit
if(guesses == 50)
{
clrscr();
cout << "You've lost!!!";
delay(2000);
}
//until they win or lose
}while(guesses < 50 && hits < 16 && x != 1);
}
//Function: displayboard
//Input: shiparray
//Output: shiparray
void displayboard(int shiparray[10][10])
{
clrscr();
//output the column numbers
cout << " 0 1 2 3 4 5 6 7 8 9";
for(int row = 0; row < 10; row++)
{
cout << endl<< " ------------------------------------------" << endl
//output the row number
<< row << " | ";
for(int col = 0; col < 10; col++)
{
//for the game if its a 0 replace w/ space
if(shiparray[row][col] == 0)
cout << " " << " | ";
//if its a ship replace w/ a space
if(shiparray[row][col] == 1)
cout << " " << " | ";
//if its a hit, display a hit
if(shiparray[row][col] == 8)
cout << "8" << " | ";
//if its a miss, display the miss
if(shiparray[row][col] == 2)
cout << "2" << " | ";
}
}
cout << endl << " ------------------------------------------" << endl;
}
//Function: setup
//Input: shiparray
//Output: shiparray a value determining if setup was completed
int setup(int shiparray[10][10])
{
//variables
int x = 0;
int row = 0;
int col = 0;
int menuchoice = 0;
int pass = 0;
int completed = 0;
//make them setup 4 ships
for(int a = 0; a < 4; a++)
{
//loop until valid input is gotten
do
{
displayboardsetup(shiparray);
//enter coordinates
cout << "Enter a pair of coordinates (ex: 3 4): ";
cin >> row >> col;
//special characters
//display instructions
if(row == 111 && col == 111)
instructions();
//exit the game
if(row == 555 && col == 555)
{
//allows you to quit
x = 1;
break;
}
//validating rows and columns
}while(row < 0 || row > 9 || col < 0 || col > 9 || shiparray[row][col] == 1);
//validating if direction is ok
do
{
//if 555 was entered, exit loop
if(x == 1)
break;
//loop until valid input
do
{
clrscr();
displayboardsetup(shiparray);
//display choices
cout << "Which way: 1.)Left 2.)Right" << endl
<< " 3.)Up 4.)Down" << endl
<< "So... Which one? ";
cin >> menuchoice;
//loop until valid info
}while(menuchoice < 1 || menuchoice > 4 && menuchoice != 999);
//if they want to restart setup
if(menuchoice == 999)
menu(shiparray, 0);
//validate direction
switch(menuchoice)
{
case 1:
//if left is valid
if(col - 3 >= 0)
//if a ship isn't there
if(shiparray[row][col] == 0 &&
shiparray[row][col-1] == 0 &&
shiparray[row][col-2] == 0 &&
shiparray[row][col-3] == 0)
{
//put a ship there
shiparray[row][col] = 1;
shiparray[row][col-1] = 1;
shiparray[row][col-2] = 1;
shiparray[row][col-3] = 1;
pass = 1;
}
break;
case 2:
//if right
if(col + 3 < 10)
//if a ship isn't there
if(shiparray[row][col] == 0 &&
shiparray[row][col+1] == 0 &&
shiparray[row][col+2] == 0 &&
shiparray[row][col+3] == 0)
{
//put a ship there
shiparray[row][col] = 1;
shiparray[row][col+1] = 1;
shiparray[row][col+2] = 1;
shiparray[row][col+3] = 1;
pass = 1;
}
break;
case 3:
//if up
if(row - 3 >= 0)
//if a ship isn't there
if(shiparray[row][col] == 0 &&
shiparray[row-1][col] == 0 &&
shiparray[row-2][col] == 0 &&
shiparray[row-3][col] == 0)
{
//put a ship there
shiparray[row][col] = 1;
shiparray[row-1][col] = 1;
shiparray[row-2][col] = 1;
shiparray[row-3][col] = 1;
pass = 1;
}
break;
case 4:
//if down
if(row + 3 >= 0)
//if a ship isn't there
if(shiparray[row][col] == 0 &&
shiparray[row+1][col] == 0 &&
shiparray[row+2][col] == 0 &&
shiparray[row+3][col] == 0)
{
//put a ship there
shiparray[row][col] = 1;
shiparray[row+1][col] = 1;
shiparray[row+2][col] = 1;
shiparray[row+3][col] = 1;
pass = 1;
}
break;
}
//loop until a valid direction is chosen
}while(pass != 1);
//reset pass to 0
pass = 0;
//if they chose to exit
if(x == 1)
break;
if(a == 3)
completed = 5;
}
//reset x to 0
x = 0;
clrscr();
displayboardsetup(shiparray);
//wait until all go
cout << "You've completed setup, press any key to continue to game";
getch();
return completed;
}
//Function: cleararray
//Input: array
//Output: array
void cleararray(int shiparray[10][10])
{
//clear the contents of the array
for(int row = 0; row < 10; row++)
for(int col = 0; col < 10; col++)
shiparray[row][col] = 0;
}
//Function: instructions
//Input: nothing
//Output: nothing
void instructions()
{
//display the instruction stuff
clrscr();
cout << "***********************" << endl
<< "* Setup Instructions: *" << endl
<< "***********************" << endl
<< "* In setup, enter the coordinates of where you" << endl
<< " want to playce a ship." << endl
<< "* It will display the choices of which" << endl
<< " directions you want it to go." << endl
<< "* Choose a direction." << endl
<< "* TO START SETUP OVER WHEN CHOOSING A DIRECTION" << endl
<< " TYPE 999" << endl
<< "* Repeat this 4 times." << endl
<< "* Enter 555 555 in the rows and" << endl
<< " columns at any time to exit the game." << endl
<< "* Enter 111 111 in the rows and" << endl
<< " columns at any time to view the instructions." << endl
<< "* A 1 indicates the placement of a ship." << endl << endl
<< "Press any key to continue...";
getch();
clrscr();
cout << "**********************" << endl
<< "* Play Instructions: *" << endl
<< "**********************" << endl << endl
<< "* Enter row and column pairs to guess the" << endl
<< " position of a ship." << endl
<< "* You will have 50 tries to sink all ships." << endl
<< "* Hits will be marked by a 8." << endl
<< "* Misses will be marked by a 2." << endl
<< "* Enter 555 555 in the rows and" << endl
<< " columns at any time to exit the game." << endl
<< "* Enter 111 111 in the rows and" << endl
<< " columns at any time to view the instructions." << endl
<< "* Have fun, and good luck!!!" << endl << endl
<< "Press any key to continue...";
getch();
}
//Function: menu
//Input: nothing
//Output: nothing
int menu(int shiparray[10][10], int y)
{
//declare variables
int answer = 0;
int menuchoice = 0;
int complete;
do
{
do
{
clrscr();
cout << "**********************" << endl
<< "* 1.)Instructions *" << endl
<< "* 2.)Setup *" << endl
<< "* 3.)Play *" << endl
<< "* 4.)Quit *" << endl
<< "**********************" << endl
<< "Enter your choice: ";
cin >> menuchoice;
}while(menuchoice < 0 || menuchoice > 5);
switch(menuchoice)
{
case 1:
instructions();
y=1;
break;
case 2:
if(y==1)
{
complete = setup(shiparray);
if(complete == 5)
y=2;
}
break;
case 3:
if(y==2)
{
play(shiparray);
answer == askagain();
}
break;
case 4:
clrscr();
cout << "Thanks for playing";
answer = 2;
delay(1000);
break;
}
}while(menuchoice != 4);
return answer;
}
//Function: opener
//Input: nothing
//Output: nothing
void opener()
{
//output "battleship"
settextstyle(DEFAULT_FONT, 0, 6);
setcolor(YELLOW);
outtextxy(65, 150, "BATTLESHIP");
settextstyle(DEFAULT_FONT, 0, 0);
outtextxy(220, 250, "Press any key to continue...");
getch();
cleardevice();
}