| | |
Help me to finish program. Urgently!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 12
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Nov 2008
Posts: 12
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Jan 2008
Posts: 3,836
Reputation:
Solved Threads: 503
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.
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.
•
•
Join Date: Nov 2008
Posts: 12
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Jan 2008
Posts: 3,836
Reputation:
Solved Threads: 503
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:
Have it return "yacht" or "battleship" or whatever, then do this:
I would rename your class from aship to ship. aship sounds like it should be an instance of the ship class. It gets confusing.
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.
You can have function in your aship class that returns a string:
C++ Syntax (Toggle Plain Text)
string getShipType ()
Have it return "yacht" or "battleship" or whatever, then do this:
C++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Nov 2008
Posts: 12
Reputation:
Solved Threads: 0
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;
};
}
-----------------------------------------------------
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;
};
}
-----------------------------------------------------
•
•
Join Date: Nov 2008
Posts: 12
Reputation:
Solved Threads: 0
[QUOTE=egsonas;734483]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.
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.
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.
C++ Syntax (Toggle Plain Text)
------------------ 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 }; -------------------------------------------
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.
Last edited by cscgal; Nov 13th, 2008 at 1:16 pm. Reason: Added code tags
•
•
Join Date: Sep 2008
Posts: 55
Reputation:
Solved Threads: 10
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.
Note that this code won't compile. It was just an example, with it still being your work to implement it.
C++ Syntax (Toggle Plain Text)
class Ship { std::String GetType() {return "Ship";} } Ship aShip; std::cout << "This is a " << aShip.GetType();
Last edited by BeyondTheEye; Nov 13th, 2008 at 3:33 pm.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: An equation.... it doesn't work.
- Next Thread: Working with mp3 files and folders.
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






