Skip to content

Instantly share code, notes, and snippets.

@eligundry
Created March 12, 2012 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eligundry/2024889 to your computer and use it in GitHub Desktop.
Save eligundry/2024889 to your computer and use it in GitHub Desktop.
Battleship
#include "battleship.h"
int main()
{
ship battleships[FLEET_SIZE];
location target;
deploy(battleships);
printFleet(battleships);
do {
target = fire();
match(battleships, target);
} while (operational(battleships));
cout << "You sunk all my ships!" << endl;
return 0;
}
void deploy(ship battleship[], int num_of_ships)
{
for (int i = 0, j = 0; i < num_of_ships; ++i, j = 0) {
srand(time(NULL));
battleship[i].sunk = false;
battleship[i].loc.x = (random() % FIELD_SIZE) + 1;
battleship[i].loc.y = (random() % FIELD_SIZE) + 'a';
while (j < i) {
if (battleship[j].loc.x == battleship[i].loc.x && battleship[j].loc.y == battleship[i].loc.y) {
i = i - 1;
}
++j;
}
}
}
void printFleet(const ship battleship[])
{
cout << endl << "List of ships!" << endl;
for (int i = 0; i < FLEET_SIZE; ++i) {
cout << "Ship " << i + 1 << ": ";
printShip(battleship[i]);
cout << endl;
}
}
void printShip(ship battleship)
{
cout << battleship.loc.y << battleship.loc.x;
if (!battleship.sunk) {
cout << " (Alive)";
} else {
cout << " (Sunk)";
}
}
bool match(ship battleship[], location target)
{
for (int i = 0; i < FLEET_SIZE; ++i) {
if (battleship[i].loc.x == target.x && battleship[i].loc.y == target.y && !battleship[i].sunk) {
cout << "You hit my ship!" << endl << endl;
sink(battleship[i]);
return true;
} else if (battleship[i].loc.x == target.x && battleship[i].loc.y == target.y && battleship[i].sunk) {
cout << "You already sunk that ship!" << endl << endl;
return false;
}
}
cout << "You missed!" << endl << endl;
return false;
}
location fire()
{
location target;
cout << "Enter your coordinates: ";
cin >> target.y >> target.x;
return target;
}
bool operational(const ship battleship[])
{
for (int i = 0; i < FLEET_SIZE; ++i) {
if (!battleship[i].sunk) {
return true;
}
}
return false;
}
void sink(ship& battleship)
{
battleship.sunk = true;
}
// structure definitions and function prototypes
// for the battleship assignment
// Mikhail Nesterenko
// 10/21/2009
#ifndef BATTLESHIP_H_
#define BATTLESHIP_H_
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
//
// data structures definitions
//
const int FLEET_SIZE=5; // number of battleships
const int FIELD_SIZE=5; // the field (ocean) is FIELD_SIZExFIELD_SIZE
// coordinates (location) of the ship and shots
struct location{
char y; // 'a' through FIELD_SIZE
int x; // 1 through FIELD_SIZE
};
// contains ship's coordinates (location) and whether is was sunk
struct ship{
location loc;
bool sunk;
};
//
// initialization functions
//
void deploy(ship[], int num_of_ships = FLEET_SIZE); // places an array of battleships in
// random locations in the ocean
location pick(void); // generates a random location
bool match(ship[], location); // returns true if this location matches
// the location of the ship
// returns false otherwise
//
// display functions
//
void printFleet(const ship[]); // prints the locations of all the ships and whether they
// are sunk
void printShip(ship); // prints the location and status (sunk or not) of a single ship
//
// battle functions
//
bool operational(const ship[]); // returns true if at least one ship in the array
// is not sunk
location fire(); // asks the user to input the coordinates of the next
// shot
// note that match() is also used in the battle
void sink(ship&); // sets "sunk" member variable of the ship to true
#endif /* BATTLESHIP_H_ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment