Hi everyone,

I'm writting old "Battle ship" game. I know, that is very funny, but I want my own version, with one objects and inheretancy levels.

Here is a plan what I'm reaching for:
----------------------------
object:
A-level:
- aship - keeps common parameters of floating things
B-level
- ship - can move not depending on wind
- mine - fixed and can explode
- yacht - can move depending on wind
C-level- torpedo - derived from ship and mine, thus can move not depending onwind and can explode

constructors of ship, mine, yacht and torpedo classes must set ashipproperties (moveable or fixed, can explode and is it wind-dependant ship)

globally I must set wind and it direction.

movement:
an object:
if object.canmove {
if object.needswind {object.setwind(w,a)}
object.goahead(1) }
- if it is a ship or torpedo - it must change x and y depending onspeed and time, if it is mine - it will be skipped, if it is yacht -wind parameters set then x and y will be changed depending on wind andangle and on is sail up or down (if down - yacht stay fixed) meeting:if two or more objects have same (rounded) coordinates - if any mayexplode - explosion, otherwise - just wreck.
--------------------------
Some program steps I done. Movement test of "torpedo" is completed, but I messed up with finishing.
Need to done meeting of objects and other complex thing, wich I can't to find as a result.
By the way I'm using "Borland C++ Builder 5.02" for my project - if nessecary to know.

Please, someone look in my situation and try to help me.
I attached my work in zip.

Thanks in advance.
Egsonas

A correction!

Sorry, I attached older my works version, the new one is in "Newfolder.zip".

So, I try to code this:

create ship, yacht, mine and torpedo
ask parameters for ship, yacht and mine.
then suggest "launch" torpedo - set params for torpedo.
then "run" situation: or manually, or with delay call "goahead()" foreach object, then check distances from torpedo to all other and frommine to all other - if distance is less then 1 (2, 5 or 10, orwhatever you want, or set it) - then explosion occured (stop"game").

Just say:"yacht exploded at mine" or "torpedo hit ship". Before need to add classmethod to return object:
cout<<"I am "<<tt.id

Trully, I need help. Brains don't work clearly.
Please, help.

My last hange is in attachment.

I'd say people aren't downloading because it's a big zip file. There's actually not much code in there at all, but people see a big zip file, figure it's thousands of lines of code, and ignore the post.

You have to point out a particular problem you're dealing with, generally. I'd say stop coding, take a step back, and design the overall classes and how you want to split the work. You have a whole bunch of things (i.e. speed and angle of a ship) that aren't in any Battleship game I've ever seen. So design the organization first. You have an ocean, which is a 10 x 10 grid. You have ships. Each ship has a direction and a length and an owner and a location on the ocean.

You have players. They own ships and place them on the ocean. They also have torpedos and a way to keep track of them. So decide exactly what you want to do and the information that you need, set up classes to manipulate that information/data, and go from there. First thing class data members and class hierarchy. Do this before implementation. You're getting into the nitty gritty details a little too soon in my opinion.

Ok, I then I wanna do like this.

I have some values, and can do this.

Firstly: I enter 3 values: for example - a, b, c. (values can be only 1 or 0) for deciding what "aship" class object.
a - stands for "Can move";
b - stands for "Can explode";
c - stands for "Needs wind".

When program decide which "aship" class object is - prints information.
cout<<" It is a "<<.... (there must be object, just I don't know how to write.)

Secondly: then I enter x and y for movement, depending on object.
a) in ship situation - x will be "speed", y - "angle". Because it uses "setspeed" for movement.
b) in yacht situation - x will be "wind", y -"angle". "Setwind" operation for movement. Maybe, ust forget about sails.

I need that polymorphism stay. Also, I'll forget "meeting", in other word - battle. Just show movement.

Try to help in this case.

P.S.
All changes need done in "New Folder.zip" attached file, because I stay with this example.

You'll do far better if you post the relevant code that you want people to look at it. People aren't going to spend the time to download a zip file, then search around for the relevant code. Post the code itself, not as an attachment. It's short enough.

You can have function in your aship class that returns a string:

string getShipType ()

Have it return "yacht" or "battleship" or whatever, then do this:

aship a;
// code

cout<<" It is a "<< a.getShipType ();

I would rename your class from aship to ship. aship sounds like it should be an instance of the ship class. It gets confusing.

All changes need done in "New Folder.zip" attached file, because I stay with this example.

I don't know what you mean by this. If you are expecting people to make changes, put it in a new folder, and upload it, I doubt that is going to happen. I wouldn't post the zip file, or if you do, post it, but post the code itself too so people don't have to download. Expecting people to download, make changes, then upload, is asking a lot if that is what you are expecting. You need to post something that we can run quickly, and point out exactly where the problem is.

Sorry, if I messing up with "*.rar".
Ok, I done this, what I had in mind (when I enter 3 values of 1 or 0 - program checks for object). It looks not good, that I used "if" sentences, but gets to a goal.

Where I need insert your sentence?

Now need, depending on founded object, to insert correct movement sentences.

Here's a code.
------------------ classes.cpp --------------------
#include <iostream>
#include <math.h>
#include "classes.h"
#include "ships.h"


//bool func() { // Function returns a bool type
//return NULL; // NULL is converted to Boolean false
// return false; // This statement is Boolean equivalent to the one above.
//}


int main()
{
bool val = false; // Boolean variable
bool a, b, c;
cout<<"Sea Battle\n\n";
cout<<"Exit program - enter x\n";
cout<<"Please enter numbers 1 or 0 for x, y, z values to indentify ship\n";
cout<<"\n";
cout<<"Object can move: ";
cin>>a;
cout<<"Object needs wind: ";
cin>>b;
cout<<"Object may explode: ";
cin>>c;
cout<<"\n";

if ((bool(a)==TRUE) && (bool(b)==TRUE) && (bool(c)==FALSE))
cout<<"It's a yacht"<< endl;
if ((bool(a)==TRUE) && (bool(b)==FALSE) && (bool(c)==FALSE))
cout<<"It's a ship"<< endl;
if ((bool(a)==FALSE) && (bool(b)==FALSE) && (bool(c)==TRUE))
cout<<"It's a mine"<< endl;
if ((bool(a)==TRUE) && (bool(b)==FALSE) && (bool(c)==TRUE))
cout<<"It's a torpedo"<< endl;
cout<<"\n";

torpedo tt;
char aa;

tt.x(3);tt.y(5); // set start coordinates

cout<<"Object can move: "<<tt.canmove()<<"\n";
cout<<"Object needs wind: "<<tt.needswind()<<"\n";
cout<<"Object may explode: "<<tt.canexplode()<<"\n";
cout<<"x="<<tt.x()<<" y="<<tt.y()<<"\n";

// test movement

if (tt.canmove()) {
tt.setspeed(2,45); // do not use yacht a while!
}
while (aa!='x') {
tt.goahead(1);
cout<<"x="<<tt.x()<<" y="<<tt.y()<<"\n";
cin>>aa;
};

}
-----------------------------------------------------

Sorry, if I messing up with "*.rar".
Ok, I done this, what I had in mind (when I enter 3 values of 1 or 0 - program checks for object). It looks not good, that I used "if" sentences, but gets to a goal.

Where I need insert your sentence?

Now need, depending on founded object, to insert correct movement sentences.

Here's a code.

------------------ classes.cpp --------------------
#include <iostream>
#include <math.h>
#include "classes.h"
#include "ships.h"


   //bool func() {    // Function returns a bool type
   //return NULL;  // NULL is converted to Boolean false
// return false;  // This statement is Boolean equivalent to the one above.
   //}


int main()
{
  bool val = false;  // Boolean variable
  bool a, b, c;
  cout<<"Sea Battle\n\n";
  cout<<"Exit program - enter x\n";
  cout<<"Please enter numbers 1 or 0 for x, y, z values to indentify ship\n";
  cout<<"\n";
  cout<<"Object can move:    ";
  cin>>a;
  cout<<"Object needs wind:  ";
  cin>>b;
  cout<<"Object may explode: ";
  cin>>c;
  cout<<"\n";

  if ((bool(a)==TRUE) && (bool(b)==TRUE) && (bool(c)==FALSE))
  cout<<"It's a yacht"<< endl;
  if ((bool(a)==TRUE) && (bool(b)==FALSE) && (bool(c)==FALSE))
  cout<<"It's a ship"<< endl;
  if ((bool(a)==FALSE) && (bool(b)==FALSE) && (bool(c)==TRUE))
  cout<<"It's a mine"<< endl;
  if ((bool(a)==TRUE) && (bool(b)==FALSE) && (bool(c)==TRUE))
  cout<<"It's a torpedo"<< endl;
  cout<<"\n";

  torpedo tt;
  char aa;

  tt.x(3);tt.y(5); // set start coordinates

  cout<<"Object can move:    "<<tt.canmove()<<"\n";
  cout<<"Object needs wind:  "<<tt.needswind()<<"\n";
  cout<<"Object may explode: "<<tt.canexplode()<<"\n";
  cout<<"x="<<tt.x()<<" y="<<tt.y()<<"\n";

  // test movement

  if (tt.canmove()) {
     tt.setspeed(2,45); // do not use yacht a while!
     }
    while (aa!='x') {
      tt.goahead(1);
      cout<<"x="<<tt.x()<<" y="<<tt.y()<<"\n";
      cin>>aa;
    };

}
-----------------------------------------------------

------------------   classes.h   --------------------
#define TRUE 1
#define FALSE 0
#define pi 3.1415
------------------------------------------------------

-------------------  ships.h  ------------------------ 
class aship { // class to keep common ship data
    //char[32] id;        // object id
    float fx;           // x
    float fy;           // y
    bool bcanmove;       // moveable or fixed
    bool bcanexplode;    // can explode
    bool bneedswind;     // wind-dependant ship

public:
    aship() {bcanmove=FALSE;bcanexplode=FALSE;bneedswind=FALSE;}
    void setparams (bool canmove,
                    bool canexplode,
                    bool needswind) {
         bcanmove = bcanmove | canmove;
         bcanexplode = bcanexplode | canexplode;
         bneedswind = bneedswind | needswind; }
    void x(float ax) { fx = ax; }
    void y(float ay) { fy = ay; }
    float x() { return fx; }
    float y() { return fy; }
    bool canmove() {return bcanmove; }
    bool canexplode() {return bcanexplode; }
    bool needswind() {return bneedswind; }
};

class ship : public virtual aship { // a ship class
    float fspeed; // speed by engine
    float fangle; // ship direction
public:
    void goahead(float time); // update coordinates after "time"
    void setspeed(float speed, float angle) // set speed & angle
        { fspeed = speed; fangle = angle; }
    float speed() {return fspeed; } // get speed
    float angle() {return fangle; } // get angle
    void speed(float speed) {fspeed = speed; } // set speed only
    void angle(float angle) {fangle = angle; } // set angle
    ship() ;
};

ship::ship() { // ship constructor
    fspeed=0; fangle=0;
    setparams(TRUE,   // can move
              FALSE,  // can explode
              FALSE); // needs wind
}

void ship::goahead(float time) {
    x(x()+fspeed*time*cos(fangle*pi/180));
    y(y()+fspeed*time*sin(fangle*pi/180));
}

class mine : public virtual aship { // static sea-mine
public:
    float explosive; // explosive on-board
    mine() { // mine constructor
    setparams(FALSE,   // can move
              TRUE,    // can explode
              FALSE);} // needs wind
};


class yacht : public virtual aship { // wind-powered ship
    bool sail;      // are sail up or down
    float dewind;   // wind speed / ship speed ratio
    float wind;     // wind speed
    float angle;    // wind angle
public:
    void sail_up()    { sail = TRUE;  } // set up sail
    void sail_down()  { sail = FALSE; } // remove sail
    bool get_sail() { return sail;  }   // is sail up?
    void goahead(float time); // update coordinates after "time"
    void setwind(float wind, float angle); // update wind params
    yacht();
};

yacht::yacht() { // yacht constructor
    dewind=0.5; // ratio - two times slower then wind speed
    sail=FALSE; wind=0; angle=0;
    setparams(TRUE,   // can move
              FALSE,  // can explode
              FALSE); // needs wind
}

void yacht::goahead(float time) {
    if (sail) {
        x(x()+wind*dewind*time*cos(angle*pi/180));
        y(y()+wind*dewind*time*sin(angle*pi/180));
    }
}

class torpedo : public ship, public mine { // moveable mine


};
-------------------------------------------

If my way.
How to add virtual property to coordclass, say, "type", which has to be replaced with pointer toappropriate string in each class constructor.
Then I will able to do this:
cout<<tt.type()<<" - x: "<<tt.x()<<", y: "<<tt.y()<<"\n"
It will be like this:YACHT - x: 4, y: 5.

How make "type" property for objects?
When I try anything even in examples which are written - mistakes happened.
I messing up.

Sounds like you want something like this...
Note that this code won't compile. It was just an example, with it still being your work to implement it.

class Ship
{
std::String GetType() {return "Ship";}
}

Ship aShip;
std::cout << "This is a " <<  aShip.GetType();

Aha, thanks. But.... i will stay to this

if ((bool(a)==TRUE) && (bool(b)==TRUE) && (bool(c)==FALSE))
  cout<<"It's a yacht"<< endl;
  if ((bool(a)==TRUE) && (bool(b)==FALSE) && (bool(c)==FALSE))
  cout<<"It's a ship"<< endl;
  if ((bool(a)==FALSE) && (bool(b)==FALSE) && (bool(c)==TRUE))
  cout<<"It's a mine"<< endl;
  if ((bool(a)==TRUE) && (bool(b)==FALSE) && (bool(c)==TRUE))
  cout<<"It's a torpedo"<< endl;
  cout<<"\n";

I have torpedo movement test:

torpedo tt;
  char aa;

  tt.x(3);tt.y(5); // set start coordinates
  cout<<"x="<<tt.x()<<" y="<<tt.y()<<"\n";

  // test movement

  if (tt.canmove()) {
     tt.setspeed(2,45); // do not use yacht a while!
     }
    while (aa!='x') {
      tt.goahead(1);
      cout<<"x="<<tt.x()<<" y="<<tt.y()<<"\n";
      cin>>aa;
    };

How me to write something like that to other object. In other words, this part include in my "if" statements (to combine)?

Could you show in which area of "ships.h" or "classes.cpp" I need insert (parts), because when i watching to code, my eyes are scrolling up & down, but mind sleep. Pls.

class Ship
{
std::String GetType() {return "Ship";}
}

Ship aShip;
std::cout << "This is a " <<  aShip.GetType();

Please write these things in my code. From examples I still don't get it. I'm tiered and feel gloom.

if ((bool(a)==TRUE) && (bool(b)==TRUE) && (bool(c)==FALSE))
  cout<<"It's a yacht"<< endl;
  if ((bool(a)==TRUE) && (bool(b)==FALSE) && (bool(c)==FALSE))
  cout<<"It's a ship"<< endl;
  if ((bool(a)==FALSE) && (bool(b)==FALSE) && (bool(c)==TRUE))
  cout<<"It's a mine"<< endl;
  if ((bool(a)==TRUE) && (bool(b)==FALSE) && (bool(c)==TRUE))
  cout<<"It's a torpedo"<< endl;
  cout<<"\n";

Does this compile?

bool(a)==TRUE

a is already a bool. Seems to me you just want:

a ==  TRUE

or just a itself:

if ((a && b && !c)
  cout<<"It's a yacht"<< endl;

Your code is pretty hard to follow in my opinion. I don't know what the variables represent (like aa), and keep in mind that you have to initialize something before comparing it or you get undefined behavior. As I've said before, I think you should step back and really look at the structure of your program and classes BEFORE writing the implementation. In particular, I really think you should not have the menu in main. Put it in a function. You had a question about where to put the word "virtual". I think you should take a tutorial on polymorphism and virtual functions first, then come back to this project. A good book will have good examples on these things. See the top link of the C++ function. It will give you working examples that you can play with. From there, write your battleship program. But I think you need to familiarize yourself with polymorphism before attempting your own battleship program. I'm not trying to discourage you. I just think the better approach is to learn from an example that works perfectly already, change it a little bit till you understand, then write the battleship program. Consider looking at the four tutorials in the Object Oriented Programming section of the link below.

http://www.cplusplus.com/doc/tutorial/

OK, now I have "cout", which prints object type.
Now I don't know, how to write and insert movement (like a "torpedo" sample in my code), depending of what object is. In other words: if we that it is a torpedo, movement comes, if it is a ship - its movement.
Please, show me.
Here's my code:

----------------classes.cpp-------------
#include <iostream>
#include <math.h>
#include <typeinfo.h>
#include "classes.h"
#include "ships.h"


int main(void) {
    ship shipinst;           // Instantiate class ship
    mine mineinst;           // Instantiate class mine
    yacht yachtinst;           // Instantiate class yacht
    torpedo torpedoinst;           // Instantiate class torpedo
    ship *shipptr;           // Declare a ship-type pointer
    mine *mineptr;           // Declare a mine-type pointer
    yacht *yachtptr;           // Declare a yacht-type pointer
    torpedo *torpedoptr;           // Declare a torpedo-type pointer
    shipptr = &shipinst;     // Initialize the pointer
    mineptr = &mineinst;     // Initialize the pointer
    yachtptr = &yachtinst;     // Initialize the pointer
    torpedoptr = &torpedoinst;     // Initialize the pointer
    bool a, b, c;

    cout<<"Sea Battle\n\n";
    cout<<"Exit program - enter x\n";
    cout<<"Please enter numbers 1 or 0 for x, y, z values to indentify ship\n";
    cout<<"\n";
    cout<<"Object can move:    ";
    cin>>a;
    cout<<"Object needs wind:  ";
    cin>>b;
    cout<<"Object may explode: ";
    cin>>c;
    cout<<"\n";

    if (a==TRUE && b==TRUE && c==FALSE)
       cout << "It's a: " << typeid( *yachtptr).name()<<endl;

    if (a==TRUE && b==FALSE && c==FALSE)
       cout << "It's a: " << typeid( *shipptr).name()<<endl;

    if (a==FALSE && b==FALSE && c==TRUE)
       cout << "It's a: " << typeid( *mineptr).name()<<endl;

    if (a==TRUE && b==FALSE && c==TRUE)
       cout << "It's a: " << typeid( *torpedoptr).name()<<endl;
       cout<<"\n";

  torpedo tt;
  char aa;

  tt.x(3);tt.y(5); // set start coordinates
  cout<<"x="<<tt.x()<<" y="<<tt.y()<<"\n";

  // test movement

   if (tt.canmove()) {
      tt.setspeed(2,45); // do not use yacht a while!
      }
     while (aa!='x') {
       tt.goahead(1);
       cout<<"x="<<tt.x()<<" y="<<tt.y()<<"\n";
       cin>>aa;
     };

}
-------------------------------------
-----------------------ships.h----------
class aship { // class to keep common ship data
    //char[32] id;        // object id
    float fx;           // x
    float fy;           // y
    bool bcanmove;       // moveable or fixed
    bool bcanexplode;    // can explode
    bool bneedswind;     // wind-dependant ship
public:
    aship() {bcanmove=FALSE;bcanexplode=FALSE;bneedswind=FALSE;}
    void setparams (bool canmove,
                    bool canexplode,
                    bool needswind) {
         bcanmove = bcanmove | canmove;
         bcanexplode = bcanexplode | canexplode;
         bneedswind = bneedswind | needswind; }
    void x(float ax) { fx = ax; }
    void y(float ay) { fy = ay; }
    float x() { return fx; }
    float y() { return fy; }
    bool canmove() {return bcanmove; }
    bool canexplode() {return bcanexplode; }
    bool needswind() {return bneedswind; }
};

class ship : public virtual aship { // a ship class

int main(void) {
  ship shipinst;           // Instantiate class ship
   ship *shipptr;           // Declare a ship-type pointer
    shipptr = &shipinst;     // Initialize the pointer
    }
    float fspeed; // speed by engine
    float fangle; // ship direction
public:
    void goahead(float time); // update coordinates after "time"
    void setspeed(float speed, float angle) // set speed & angle
        { fspeed = speed; fangle = angle; }
    float speed() {return fspeed; } // get speed
    float angle() {return fangle; } // get angle
    void speed(float speed) {fspeed = speed; } // set speed only
    void angle(float angle) {fangle = angle; } // set angle
    ship () ;
};

ship::ship() { // ship constructor
    fspeed=0; fangle=0;
    setparams(TRUE,   // can move
              FALSE,  // can explode
              FALSE); // needs wind
}

void ship::goahead(float time) {
    x(x()+fspeed*time*cos(fangle*pi/180));
    y(y()+fspeed*time*sin(fangle*pi/180));
}

class mine : public virtual aship { // static sea-mine

int main(void) {
   mine mineinst;           // Instantiate class mine
   mine *mineptr;           // Declare a mine-type pointer
   mineptr = &mineinst;     // Initialize the pointer
   }
public:
    float explosive; // explosive on-board
    mine() { // mine constructor
    setparams(FALSE,   // can move
              TRUE,    // can explode
              FALSE);} // needs wind
};


class yacht : public virtual aship { // wind-powered ship

int main(void) {
    yacht yachtinst;           // Instantiate class yacht
    yacht *yachtptr;           // Declare a yacht-type pointer
    yachtptr = &yachtinst;     // Initialize the pointer
    }
    bool sail;      // are sail up or down
    float dewind;   // wind speed / ship speed ratio
    float wind;     // wind speed
    float angle;    // wind angle
public:
    void sail_up()    { sail = TRUE;  } // set up sail
    void sail_down()  { sail = FALSE; } // remove sail
    bool get_sail() { return sail;  }   // is sail up?
    void goahead(float time); // update coordinates after "time"
    void setwind(float wind, float angle); // update wind params
    yacht();
};

yacht::yacht() { // yacht constructor
    dewind=0.5; // ratio - two times slower then wind speed
    sail=FALSE; wind=0; angle=0;
    setparams(TRUE,   // can move
              FALSE,  // can explode
              FALSE); // needs wind
}

void yacht::goahead(float time) {
    if (sail) {
        x(x()+wind*dewind*time*cos(angle*pi/180));
        y(y()+wind*dewind*time*sin(angle*pi/180));
    }
}

class torpedo : public ship, public mine { // moveable mine
int main(void) {
    torpedo torpedoinst;           // Instantiate class torpedo
    torpedo *torpedoptr;           // Declare a torpedo-type pointer
    torpedoptr = &torpedoinst;     // Initialize the pointer
  }

};
-------------------------------------
class ship : public virtual aship { // a ship class

int main(void) {
  ship shipinst;           // Instantiate class ship
   ship *shipptr;           // Declare a ship-type pointer
    shipptr = &shipinst;     // Initialize the pointer
    }
    float fspeed; // speed by engine
    float fangle; // ship direction
public:
    void goahead(float time); // update coordinates after "time"
    void setspeed(float speed, float angle) // set speed & angle
        { fspeed = speed; fangle = angle; }
    float speed() {return fspeed; } // get speed
    float angle() {return fangle; } // get angle
    void speed(float speed) {fspeed = speed; } // set speed only
    void angle(float angle) {fangle = angle; } // set angle
    ship () ;
};

Confusing. "ship" and "aship". What does "aship" represent? Why do you have a main function here as well as in classes.cpp? You still have stuff like:

if (a==TRUE && b==TRUE && c==FALSE)

if we that it is a torpedo, movement comes, if it is a ship - its movement.

I have no idea what this means. You are doing way too much at one time without compiling and your questions are too vague, in my opinion. I think your program needs a complete redesign. I get errors when I use TRUE and FALSE rather than true and false. Did you look at the tutorial?

Just try to write your variant for my program. Then, maybe something will go to the goal.

Just try to write your variant for my program. Then, maybe something will go to the goal.

Sorry, it's your program, not mine. I'm not going to write it for you. That's not the way this forum works. Best of luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.