#ifndef BATTLESHIP_H_
#define BATTLESHIP_H_
// coordinates (location) of the ship and shots
class location{
public:
void locationreset(void); // void constructor, assigns -1 to X
void pick(void); // picks a random location
void fire(void); // asks the user to input the
// coordinates of the next shot
void print(void) const; // prints location in format "a1"
// returns true if the two locations match
friendbool compare(location, location);
private:
staticconstint fieldSize=5; // the field (ocean) is fieldSize X fieldSize
int x; // 1 through fieldSize
char y; // 'a' through fieldSize
};
// contains ship's coordinates (location) and whether is was sunk
class ship{
public:
ship(void); // void constructor, sets sunk=false
bool match(location) const; // returns true if this location matches
// the ship's location
bool isSunk(void) const {return(sunk);}; // checks to see if the ship is sunk
void sink(void); // sets "sunk" member variable of the ship to true
void setLocation(location); // deploys the ship at the specified location
void printShip(void) const; // prints location and status of the ship
private:
location loc;
bool sunk;
};
// contains the fleet of the deployed ships
class fleet{
public:
void deployFleet(void); // deploys the ships in random locations
// of the ocean
bool operational(void) const; // returns true if at least
// one ship in the fleet is not sunk
bool isHitNSink(location); // returns true if there was a deployed
// ship at this location (hit) and sinks it
// otherwise returns false (miss)
void printFleet(void) const; // prints out locations of ships in fleet
private:
staticconstint fleetSize=5; // number of battleships
bool check(location); // returns true if one of the ship's locations
// matches the ship, returns false
// if none-match
ship ships[fleetSize]; // battleships of the fleet
};
#endif /* BATTLESHIP_H_ */
#include "battleship.h"
#include <iostream>
using std::cout; using std::cin; using std::endl;
int main (){
char answer;
fleet myFleet; //computer's ships
myFleet.deployFleet(); // fleet is deployed at random locations
cout << "Do ships' positions and status need to be printed? (y for yes)" << endl;
cin >> answer;
if (answer=='y')
myFleet.printFleet();
while (myFleet.operational ()==true){
cout << "there are still ships up";
location userShot;
userShot.locationreset();
cout << "Input location(a-e 1-5): ";
userShot.fire();
if (myFleet.isHitNSink(userShot)==true)
cout << "hit";
else
cout << "miss";
cout << "Do ships' positions and status need to be printed? (y for yes, q to quit)";
cin >> answer;
if (answer== 'y')
myFleet.printFleet();
elseif (answer=='q')
break;
}
}
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "battleship.h"
using std::cout; using std::endl; using std::cin;
void location::locationreset(void){
x=-1;
y='x';
}
void location::pick(void){
srand(time(NULL));
x=rand()%fieldSize +1;
y=rand()%fieldSize;
switch (y) {
case 0: y = 'a'; break;
case 1: y = 'b'; break;
case 2: y = 'c'; break;
case 3: y = 'd'; break;
case 4: y = 'e'; break;
}
}
void location::fire(void){
cout << "input the coordinates of the next shot "; cin >> y >> x;
}
void location::print() const{
cout << y << x;
}
bool compare(location loc1, location loc2) {
return (loc1.x == loc2.x && loc1.y == loc2.y);
}
ship::ship(void){
sunk=false;
}
bool ship::match(location shiploc) const {
return compare(loc,shiploc);
}
void ship::sink(void) {
sunk=true;
}
void ship::setLocation(location shiploc) { // deploys the ship at the specified location
loc=shiploc;
}
void ship::printShip(void) const { // prints location and status of the ship
loc.print();
if (sunk ==true)
cout <<"sunk";
elseif (sunk == false)
cout <<"not sunk"<<endl;
}
void fleet::deployFleet(void) { // deploys the ships in random locations of the ocean
location loc; loc.locationreset();
for(int i = 0; i < fleetSize; i++){
loc.pick();
ships[i].setLocation(loc);
}
}
bool fleet::operational() const{ // returns true if at least one ship in the fleet is not sunk
for(int i = 0; i < fleetSize; ++i)
if (ships[i].isSunk() == false)
returntrue;
returnfalse;
}
bool fleet::isHitNSink(location shiploc){ // returns true if there was a deployed
for(int i = 0; i < fleetSize; ++i){
if(ships[i].match(shiploc) == true && ships[i].isSunk()==false)
{
ships[i].sink();
returntrue;
}
elsereturnfalse;
}
}
void fleet::printFleet(void) const{ // prints out locations of ships in fleet
for(int i = 0; i < fleetSize; ++i)
ships[i].printShip();
}
bool fleet::check(location shiploc){
for (int i=0; i<fleetSize; ++i)
if (ships[i].match(shiploc))
returntrue;
}
elsereturnfalse;
}
most of the commands for fleet class are messed up(lines 61-102 on last code block), it compiles without errors, but i have mistakes in the code, just very buggy idk?
and im getting same coordinate 5x, example:
e5 not sunk
e5 not sunk
e5 not sunk
e5 not sunk
e5 not sunk
The problem is that you are calling srand multiple times, resetting the random sequence to the beginning every time. Call srand once at the start of main, and never again.