Help me to finish program. Urgently!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 12
Reputation: egsonas is an unknown quantity at this point 
Solved Threads: 0
egsonas egsonas is offline Offline
Newbie Poster

Help me to finish program. Urgently!

 
0
  #1
Nov 12th, 2008
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
Attached Files
File Type: zip Mano.zip (326.4 KB, 6 views)
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 12
Reputation: egsonas is an unknown quantity at this point 
Solved Threads: 0
egsonas egsonas is offline Offline
Newbie Poster

Re: Help me to finish program. Urgently!

 
0
  #2
Nov 12th, 2008
A correction!

Sorry, I attached older my works version, the new one is in "Newfolder.zip".
Attached Files
File Type: zip New Folder.zip (316.7 KB, 4 views)
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 12
Reputation: egsonas is an unknown quantity at this point 
Solved Threads: 0
egsonas egsonas is offline Offline
Newbie Poster

Re: Help me to finish program. Urgently!

 
0
  #3
Nov 12th, 2008
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.
Attached Files
File Type: zip classes[1].0.64.1.zip (40.3 KB, 3 views)
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,836
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Help me to finish program. Urgently!

 
0
  #4
Nov 12th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 12
Reputation: egsonas is an unknown quantity at this point 
Solved Threads: 0
egsonas egsonas is offline Offline
Newbie Poster

Re: Help me to finish program. Urgently!

 
0
  #5
Nov 12th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,836
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Help me to finish program. Urgently!

 
0
  #6
Nov 12th, 2008
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:

  1. string getShipType ()

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

  1. aship a;
  2. // code
  3.  
  4. 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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 12
Reputation: egsonas is an unknown quantity at this point 
Solved Threads: 0
egsonas egsonas is offline Offline
Newbie Poster

Re: Help me to finish program. Urgently!

 
0
  #7
Nov 12th, 2008
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;
};

}
-----------------------------------------------------
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 12
Reputation: egsonas is an unknown quantity at this point 
Solved Threads: 0
egsonas egsonas is offline Offline
Newbie Poster

Re: Help me to finish program. Urgently!

 
0
  #8
Nov 13th, 2008
[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.
  1. ------------------ classes.cpp --------------------
  2. #include <iostream>
  3. #include <math.h>
  4. #include "classes.h"
  5. #include "ships.h"
  6.  
  7.  
  8. //bool func() { // Function returns a bool type
  9. //return NULL; // NULL is converted to Boolean false
  10. // return false; // This statement is Boolean equivalent to the one above.
  11. //}
  12.  
  13.  
  14. int main()
  15. {
  16. bool val = false; // Boolean variable
  17. bool a, b, c;
  18. cout<<"Sea Battle\n\n";
  19. cout<<"Exit program - enter x\n";
  20. cout<<"Please enter numbers 1 or 0 for x, y, z values to indentify ship\n";
  21. cout<<"\n";
  22. cout<<"Object can move: ";
  23. cin>>a;
  24. cout<<"Object needs wind: ";
  25. cin>>b;
  26. cout<<"Object may explode: ";
  27. cin>>c;
  28. cout<<"\n";
  29.  
  30. if ((bool(a)==TRUE) && (bool(b)==TRUE) && (bool(c)==FALSE))
  31. cout<<"It's a yacht"<< endl;
  32. if ((bool(a)==TRUE) && (bool(b)==FALSE) && (bool(c)==FALSE))
  33. cout<<"It's a ship"<< endl;
  34. if ((bool(a)==FALSE) && (bool(b)==FALSE) && (bool(c)==TRUE))
  35. cout<<"It's a mine"<< endl;
  36. if ((bool(a)==TRUE) && (bool(b)==FALSE) && (bool(c)==TRUE))
  37. cout<<"It's a torpedo"<< endl;
  38. cout<<"\n";
  39.  
  40. torpedo tt;
  41. char aa;
  42.  
  43. tt.x(3);tt.y(5); // set start coordinates
  44.  
  45. cout<<"Object can move: "<<tt.canmove()<<"\n";
  46. cout<<"Object needs wind: "<<tt.needswind()<<"\n";
  47. cout<<"Object may explode: "<<tt.canexplode()<<"\n";
  48. cout<<"x="<<tt.x()<<" y="<<tt.y()<<"\n";
  49.  
  50. // test movement
  51.  
  52. if (tt.canmove()) {
  53. tt.setspeed(2,45); // do not use yacht a while!
  54. }
  55. while (aa!='x') {
  56. tt.goahead(1);
  57. cout<<"x="<<tt.x()<<" y="<<tt.y()<<"\n";
  58. cin>>aa;
  59. };
  60.  
  61. }
  62. -----------------------------------------------------
  63.  
  64. ------------------ classes.h --------------------
  65. #define TRUE 1
  66. #define FALSE 0
  67. #define pi 3.1415
  68. ------------------------------------------------------
  69.  
  70. ------------------- ships.h ------------------------
  71. class aship { // class to keep common ship data
  72. //char[32] id; // object id
  73. float fx; // x
  74. float fy; // y
  75. bool bcanmove; // moveable or fixed
  76. bool bcanexplode; // can explode
  77. bool bneedswind; // wind-dependant ship
  78.  
  79. public:
  80. aship() {bcanmove=FALSE;bcanexplode=FALSE;bneedswind=FALSE;}
  81. void setparams (bool canmove,
  82. bool canexplode,
  83. bool needswind) {
  84. bcanmove = bcanmove | canmove;
  85. bcanexplode = bcanexplode | canexplode;
  86. bneedswind = bneedswind | needswind; }
  87. void x(float ax) { fx = ax; }
  88. void y(float ay) { fy = ay; }
  89. float x() { return fx; }
  90. float y() { return fy; }
  91. bool canmove() {return bcanmove; }
  92. bool canexplode() {return bcanexplode; }
  93. bool needswind() {return bneedswind; }
  94. };
  95.  
  96. class ship : public virtual aship { // a ship class
  97. float fspeed; // speed by engine
  98. float fangle; // ship direction
  99. public:
  100. void goahead(float time); // update coordinates after "time"
  101. void setspeed(float speed, float angle) // set speed & angle
  102. { fspeed = speed; fangle = angle; }
  103. float speed() {return fspeed; } // get speed
  104. float angle() {return fangle; } // get angle
  105. void speed(float speed) {fspeed = speed; } // set speed only
  106. void angle(float angle) {fangle = angle; } // set angle
  107. ship() ;
  108. };
  109.  
  110. ship::ship() { // ship constructor
  111. fspeed=0; fangle=0;
  112. setparams(TRUE, // can move
  113. FALSE, // can explode
  114. FALSE); // needs wind
  115. }
  116.  
  117. void ship::goahead(float time) {
  118. x(x()+fspeed*time*cos(fangle*pi/180));
  119. y(y()+fspeed*time*sin(fangle*pi/180));
  120. }
  121.  
  122. class mine : public virtual aship { // static sea-mine
  123. public:
  124. float explosive; // explosive on-board
  125. mine() { // mine constructor
  126. setparams(FALSE, // can move
  127. TRUE, // can explode
  128. FALSE);} // needs wind
  129. };
  130.  
  131.  
  132. class yacht : public virtual aship { // wind-powered ship
  133. bool sail; // are sail up or down
  134. float dewind; // wind speed / ship speed ratio
  135. float wind; // wind speed
  136. float angle; // wind angle
  137. public:
  138. void sail_up() { sail = TRUE; } // set up sail
  139. void sail_down() { sail = FALSE; } // remove sail
  140. bool get_sail() { return sail; } // is sail up?
  141. void goahead(float time); // update coordinates after "time"
  142. void setwind(float wind, float angle); // update wind params
  143. yacht();
  144. };
  145.  
  146. yacht::yacht() { // yacht constructor
  147. dewind=0.5; // ratio - two times slower then wind speed
  148. sail=FALSE; wind=0; angle=0;
  149. setparams(TRUE, // can move
  150. FALSE, // can explode
  151. FALSE); // needs wind
  152. }
  153.  
  154. void yacht::goahead(float time) {
  155. if (sail) {
  156. x(x()+wind*dewind*time*cos(angle*pi/180));
  157. y(y()+wind*dewind*time*sin(angle*pi/180));
  158. }
  159. }
  160.  
  161. class torpedo : public ship, public mine { // moveable mine
  162.  
  163.  
  164. };
  165. -------------------------------------------
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.
Last edited by cscgal; Nov 13th, 2008 at 1:16 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 12
Reputation: egsonas is an unknown quantity at this point 
Solved Threads: 0
egsonas egsonas is offline Offline
Newbie Poster

Re: Help me to finish program. Urgently!

 
0
  #9
Nov 13th, 2008
How make "type" property for objects?
When I try anything even in examples which are written - mistakes happened.
I messing up.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 55
Reputation: BeyondTheEye is an unknown quantity at this point 
Solved Threads: 10
BeyondTheEye BeyondTheEye is offline Offline
Junior Poster in Training

Re: Help me to finish program. Urgently!

 
0
  #10
Nov 13th, 2008
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.

  1. class Ship
  2. {
  3. std::String GetType() {return "Ship";}
  4. }
  5.  
  6. Ship aShip;
  7. std::cout << "This is a " << aShip.GetType();
Last edited by BeyondTheEye; Nov 13th, 2008 at 3:33 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC