ocean.h file
Code:
#ifndef OCEAN_H
#define OCEAN_H
#include <iostream>
using namespace std;
class ocean
{
private:
//private variables
char grid[10][10];
int ship_area;
int num_hits;
public:
//constructors
ocean();
//accessor functions
int get_area() {return ship_area;}
int get_hits() {return num_hits;}
char get_cell(int row, int col) {return grid[row][col];}
//public member functions
void change_grid(int row, int col, char symbol){grid [row][col] = symbol;}
void calc_ship_area(int crus, int sub, int bat);
void add_hit() {num_hits ++;}
bool check_shot(int row, int col);
void print_grid(ostream & fout);
};
#endif
ship.h file
Code:
#ifndef SHIP_H
#define SHIP_H
#include "ocean.h"
#include <iostream>
using namespace std;
struct cell
{
int row;
int col;
};
class ship
{
private:
//private variables
int ship_id;
int length;
cell place_arry[4];
char status;
char placement;
int num_hits;
//private helper functions
void change_status();
void sink_ship(ocean & field);
void report_sunk();
void place_ship(ocean & field);
bool check_space(char dir, int col, int row, ocean field);
public:
//constructors
ship();
ship(int id, int len, ocean & field);
//accessor functions
int get_length() {return length;}
int get_status() {return status;}
int get_id() {return ship_id;}
//public member functions
void inc_hits(ocean & field);
};
#endif;
ocean.cpp file
Code:
#include <iostream>
#include<windows.h>
#include "ocean.h"
using namespace std;
ocean::ocean()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
grid[i][j] = '~';
}
}
ship_area = 0;
num_hits = 0;
}
void ocean::calc_ship_area(int crus, int sub, int bat)
{
if (crus + sub + bat > 0 && crus + sub + bat <= 10)
ship_area = (2 * crus) + (3 * sub) + (4 * bat);
}
bool ocean::check_shot(int row, int col)
{
if (grid[row][col] == '~')
return true;
else
return false;
}
void ocean::print_grid(ostream & fout)
{
cout << "\t\t 1 2 3 4 5 6 7 8 9 10\n";
for (int l = 0; l < 10; l++)
{
if (l != 9)
cout << "\t\t" << l + 1 << " ";
else
cout << "\t\t" << l + 1 << " ";
for (int i = 0; i < 10; i ++)
{
cout << grid[l][i] << " ";
}
cout << endl;
}
cout << endl;
}
ship.cpp file
Code:
#include <iostream>
#include "ship.h"
#include <windows.h>
using namespace std;
ship::ship()
{
ship_id = -1;
for(int i = 0; i < 4; i++)
for(int k=0; k<2;k++)
{
place_arry[i].row = -1;
place_arry[i].col = -1;
}
status = ' ';
placement = ' ';
length = 0;
num_hits = 0;
}
ship::ship(int id, int len, ocean & field)
{
ship_id= id;
length = len;
place_ship(field);
status = 'c';
num_hits = 0;
}
void ship::inc_hits(ocean& play)
{
num_hits++;
change_status();
if (status == 's')
sink_ship(play);
}
void ship::change_status()
{
if (status == 'c')
{
status = 'i';
}
else if (num_hits == length && status == 'i')
{
status = 's';//sunk
}
}
void ship::sink_ship(ocean & play)
{
report_sunk();
for (int l = 0; l < length; l++)
play.change_grid(place_arry[l].row, place_arry[l].col, 'S');
}
void ship::report_sunk()
{
cout << " ***** **** **** **** **** **** **** **** **** \n";
cout << " * * * * * * * * * * * * * * * * * * \n";
cout << " * *** * * * * * * * * * * * * * * * * * \n";
cout << " * * ** * * * * * * * * * * * * * * * * \n";
cout << " * * * * * * * * * * * * * * * * * \n";
cout << " * * * * * * * * * * * * * * * * * \n";
cout << " ** * * * * * * * * * * * * * * **** **** \n";
cout << " * * * * * ***** * * * * * * * * * **** **** \n";
cout << " * * * * * * * * * * * * * * * * * \n";
cout << " ***** ******* **** *** **** ***** **** **** \n";
cout << "\a\a\a\a\a";
Sleep(1000);
}
void ship::place_ship(ocean& field)
{
int i;
int direction; // 0 = vertical, 1 = horizontal
int start_col; // column to start ship placement in
int start_row; // row to start ship placement in
char placed;
direction = rand()%2;
if(direction == 0) // place vertically
{
placed = 'n';
placement = 'v';
do
{
start_col = rand()%10; // place in any column
start_row = rand()%(11-length); // start in top 7 rows
// check available space
if(check_space('v', start_col, start_row, field))
{
for(i=0; i<length; i++)
{
field.change_grid(start_row+i, start_col, ship_id+65);
place_arry[i].row = start_row+i ;
place_arry[i].col = start_col;
}
placed = 'y';
}
}while(placed != 'y');
}
else // place horizontally
{
placed = 'n';
placement = 'h';
do
{
start_col = rand()%(11-length); // start in first 7 columns
start_row = rand()%10; // place in any row
// check available space
if(check_space('h', start_col, start_row, field))
{
for(i=0; i<length; i++)
{
field.change_grid(start_row, start_col+i, ship_id+65);
place_arry[i].row = start_row ;
place_arry[i].col = start_col+i;
}
placed = 'y';
}
}while(placed != 'y');
}
}
bool ship::check_space(char dir, int col, int row, ocean field)
{
if(dir == 'v') // place vertical
{
for(int i(0); i<length; i++)
if(field.get_cell(row+i, col) != '~')
return false;
}
else // place horizontal
{
for(int i(0); i<length; i++)
if(field.get_cell(row, col+i) != '~')
return false;
}
return true;
}
and finally the main..battleship.cpp
Code:
#include<iostream>
#include<windows.h>
#include <ctime>
#include <fstream>
#include "ocean.h"
#include "ship.h"
using namespace std;
void howmany(ship ship_ary[10], int & numships, ocean & field);
void graphic(ostream & fout, char shot);
void pick(int & row, int & col);
int main()
{
ship ship_arry[10];
int numships;
ocean field, play;
ofstream fout;
int guesses;
char newgame = 'y';
int index;
char gameover;
int row = -1, col = -1;
fout.open("a:battle.txt");
srand ((unsigned) time((time_t *)NULL));
while (newgame == 'y')
{
play = ocean();
field = ocean();
guesses = 0;
gameover = 'n';
howmany(ship_arry, numships, field);
field.print_grid(cout);
cout << "\tThis is the actual ship placement. It is provided for debugging only.\n";
cout << "\t";
system("pause");
system("cls");
pick(row, col);
while (row != -1 || gameover == 'n')
{
if (play.get_cell(row-1, col-1) == 'H' || play.get_cell(row-1, col-1) == 'S')
{
cout << "\tI'm sorry, you've already tried at this row and column\n";
cout << "\tUnfortunately this will be counted as a miss on the counter\n";
system("pause");
system("cls");
}
else if (play.get_cell(row-1, col-1) == 'M')
{
cout << "You already guessed this! This counts as a miss\n\n";
system("pause");
system("cls");
}
else if (field.check_shot(row-1, col-1))
{
graphic(cout, 'H');
index = int(field.get_cell(row-1, col-1)) - 65;
field.change_grid(row-1, col-1, '*');
play.change_grid(row-1, col-1, 'H');
ship_arry[index].inc_hits(play);
system ("cls");
play.print_grid(cout);
field.add_hit();
if (field.get_hits() == field.get_area())
{
gameover = 'y';
}
}
else
{
graphic(cout, 'M');
field.change_grid(row-1, col-1, '-');
play.change_grid(row-1,col-1, 'M');
system("cls");
play.print_grid(cout);
}
guesses++;
if (gameover == 'n')
pick(row, col);
}
cout << "\n\n\tYou took " << guesses << " guesses to clear the ocean!\n\n";
cout << "\tYour score is " << guesses - field.get_area() << "!!\n\n";
cout << "\tDo you want to play another game? \n\n\n";
cout << "\tSelect (y) to start a new game, (n) to quit: ";
cin >> newgame;
system ("cls");
}
cout << "END OF BATTLESHIP 1.0!";
Sleep(2000);
fout.close();
return 0;
}
void howmany(ship ship_arry[10], int & numships, ocean& field)
{
int temp2, temp3, temp4;
char cont;
do
{
cout << "\tWELCOME TO JOSH'S GENERIC BATTSHIP 1.0!\n\n\n";
cout << "\tYou can pick up to 10 ships (battleships, cruisers, and submarines)\n";
cout << "\tto be placed on the board by the computer.\n";
cout <<"\n\n\t Please enter the number of: \n\n\n";
cout <<" \t\tBATTLESHIPS: ";
cin >> temp2;
cout <<"\n\n\t\tCRUISERS: ";
cin >> temp3;
cout <<"\n\n\t\tSUBMARINES: ";
cin >> temp4;
cout << "\n\n";
if (temp2 + temp3 + temp4 <=10 && temp2 + temp3 + temp4 > 0)
{
cont = 'n';
}
else if (temp2 + temp3 + temp4 <= 0)
{
cout << "You need to pick at least one ship to be placed!\n\n";
system("pause");
temp2 = 0;
temp3 = 0;
temp4 = 0;
cont = 'y';
}
else
{
cout << "The number of ships you selected exceeds 10!\n\n";
system("pause");
temp2 = 0;
temp3 = 0;
temp4 = 0;
cont = 'y';
}
system ("cls");
}while(cont == 'y');
field.calc_ship_area(temp2, temp3, temp4);
for (int i = 0; i < temp2; i++)
ship_arry[i] = ship(i, 2, field);
for(; i < temp2 + temp3; i++)
ship_arry[i] = ship(i, 3, field);
for(; i < temp2 + temp3 + temp4; i++)
ship_arry[i] = ship(i, 4, field);
numships = temp2 + temp3 + temp4;
}
void graphic(ostream & fout, char shot)
{
if (shot == 'H')
{
fout << " *********** * * * ****\n";
fout << " * * * * * * * * * * * * * *\n";
fout << " * * * * * * * * * * * * *\n";
fout << " * * * * * * * * * * * * * * * *\n";
fout << " * * * * * * * * * * * * * * * * * * *\n";
fout << " * * * * * * * * * * * * * *\n";
fout << " * * * * * * * * * * * * * * * * * * *\n";
fout << " * * * * * * * * * * * * * * ****\n";
fout << " * * * * * * * * * * * * ****\n";
fout << " * * * * * * * * * * * * * * * *\n";
fout << " *********** *** *** *** *** ****\n";
fout << "\a\a\a\a\a";
Sleep(1000);
}
else
{
fout << " * * ******** ***** ***** \n";
fout << " * * * * * * * * * * \n";
fout << " * * * * *** *** * *** * * *** * \n";
fout << " * * * * * * * * ** * * ** \n";
fout << " * * * * * * * * * * * \n";
fout << " * * * * * * * * * * * * \n";
fout << " * * ** * * * * ** * * ** * * \n";
fout << " * * * * *** *** * * * * * * * * \n";
fout << " * * * * * * * * * * * * \n";
fout << " **** **** ******** ***** ***** ";
fout << "\a\a\a\a\a";
Sleep(1000);
}
}
void pick(int & row, int & col)
{
char cont='n';
do
{
cout << "\tHit -1 to end this game.\n\n\n";
cout << "\trow: ";
cin >> row;
cout << "\n\n\tcolumn: ";
cin >> col;
if (row > 10 || row < 0 || col >10 || col < 0)
{
cont = 'n';
cout << "\tThe row or column you picked exceeded 10 or is less than 0\n";
cout <<"\n";
cout <<"\t";
system("pause");
system("cls");
}
else
cont = 'y';
system("cls");
}while(cont == 'n');
}
there it is..LOL HELP