Clash RPG Help

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

Join Date: Oct 2008
Posts: 10
Reputation: Xarver is an unknown quantity at this point 
Solved Threads: 0
Xarver Xarver is offline Offline
Newbie Poster

Clash RPG Help

 
0
  #1
Oct 28th, 2008
I'm refreshing my mind of my C++ skills,
and my older brother had the idea of creating a medieval text-based game!
He's making all the ideas and all that,
and I'm the code monkey programming it.
Here's the code:
main.cpp:
  1. #include <iostream>
  2. #include <string>
  3. #include "CLASHinfo.h"
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. bool running = true;
  9. do {
  10. CLASH();
  11. cout << "\nRPG\n\n";
  12. cout << "Programmed by Xarver : Designed by Fishbowl444\n\n";
  13. cout << "[Press 1, Then Enter, to Start a New Adventure]\n"
  14. << "[Press 2, Then Enter, to Load an Adventure]\n"
  15. << "[Press z, Then Enter, to Quit]\n\n";
  16. char entry;
  17. cin >> entry;
  18. if(entry == 1)
  19. {
  20.  
  21. }
  22. cin.get();
  23. running = false;
  24. }while(running == true);
  25.  
  26. return 0;
  27. }
CLASHinfo.h:
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. void YesorNofunc()
  6. {
  7. cout << "Are you sure?\n\n"
  8. << "[1] Yes\n"
  9. << "[2] No\n\n";
  10. char YesorNo;
  11. cin >> YesorNo;
  12. if(YesorNo == 1)
  13. {
  14.  
  15. }else if(YesorNo == 2)
  16. {
  17.  
  18. }else {
  19. cout << "Invalid; Try Again.";
  20. }
  21. }
  22.  
  23. void CLASH()
  24. {
  25. cout
  26. << " :::::::: ::: ::: :::::::: ::: ::: \n"
  27. << ":+: :+: :+: :+: :+: :+: :+: :+: :+: \n"
  28. << "+:+ +:+ +:+ +:+ +:+ +:+ +:+ \n"
  29. << "+#+ +#+ +#++:++#++: +#++:++#++ +#++:++#++ \n"
  30. << "+#+ +#+ +#+ +#+ +#+ +#+ +#+ \n"
  31. << "#+# #+# #+# #+# #+# #+# #+# #+# #+# \n"
  32. << " ######## ########## ### ### ######## ### ### \n";
  33. }

The purpose of the YesorNofunc is it provides two parameters.
For example, if the user enters 1, it will do the first action.
If the user enters 2, it will do the second one.
For example, YesorNofunc(firstaction(), sencondaction());
The firstaction would be a function that does an action, for example:
void firstaction
{
cout << "Pie.";
}
if the user entered 1, it would print "Pie."
I'm not sure how to do this...
I want it when the user enters 1,
it continues and goes onto the new character screen.
If the user enters 2, it would go back to the main menu...
But I want this function re-usable, meaning
if you are at a shop and you are going to buy something,
before you buy it it asks, "Are you sure?"
And I can re-use it for any other thing.

I'm confused;
Can this be done in OOP?
I have been studying OOP a little...
I would like this to work, or
just any alternative for the goto in my situation.

Any help appreciated!

~ Xarver
Last edited by Xarver; Oct 28th, 2008 at 10:56 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Clash RPG Help

 
0
  #2
Oct 28th, 2008
Note: I'm not near a C++ compiler, but hopefully the below logic will be helpful to you.

Create an interface that has two options for every subclass of its type--

  1.  
  2. class DoSomething{
  3.  
  4. public:
  5. DoSomething(){}
  6. virtual void option1() const = 0;
  7. virtual void option2() const = 0;
  8. };

-- then create some subclasses--

  1.  
  2. class DoNothing : public DoSomething{
  3.  
  4. public:
  5. DoNothing(){}
  6. virtual void option1() const {}
  7. virtual void option2() const {}
  8.  
  9. };


Then change your function to (by default) do nothing if no parameter is given,
or when an appropriate DoSomething object is given it will perform the action--

  1.  
  2. void YesorNofunc(const DoSomething& action = DoNothing() )
  3. {
  4. cout << "Are you sure?\n\n"
  5. << "[1] Yes\n"
  6. << "[2] No\n\n";
  7. char YesorNo;
  8. cin >> YesorNo;
  9. if(YesorNo == 1)
  10. {
  11. action.option1();
  12. }else if(YesorNo == 2)
  13. {
  14. action.option2();
  15. }else {
  16. cout << "Invalid; Try Again.";
  17. }
  18. }

-- hopefully this will give you an idea of what to do to make your method reusable. Have fun =)

Edit: Just got to a compiler, here's a running example--

  1. #include <iostream>
  2.  
  3. using std::cin;
  4. using std::cout;
  5. using std::endl;
  6. using std::flush;
  7.  
  8. class DoSomething{
  9.  
  10. public:
  11. DoSomething(){}
  12. virtual void option1() const = 0;
  13. virtual void option2() const = 0;
  14. };
  15.  
  16. class DoNothing : public DoSomething{
  17.  
  18. public:
  19. DoNothing(){}
  20. virtual void option1() const {}
  21. virtual void option2() const {}
  22.  
  23. };
  24.  
  25. class Speak : public DoSomething{
  26.  
  27. public:
  28. Speak(){};
  29. virtual void option1() const {
  30. cout << "Whoo! O_O" << endl;
  31. }
  32.  
  33. virtual void option2() const {
  34. cout << "Oh no >_<" << endl;
  35. }
  36.  
  37. };
  38.  
  39. void YesorNofunc(const DoSomething& action = DoNothing() )
  40. {
  41. cout << "Are you sure?\n\n"
  42. << "[1] Yes\n"
  43. << "[2] No\n\n";
  44. int YesorNo;
  45. cin >> YesorNo;
  46. if(YesorNo == 1)
  47. {
  48. action.option1();
  49. }else if(YesorNo == 2)
  50. {
  51. action.option2();
  52. }else {
  53. cout << "Invalid; Try Again.";
  54. }
  55. }
  56.  
  57. int main(){
  58. YesorNofunc(Speak());
  59. cin.ignore();
  60. cin.get();
  61. return 0;
  62. }
Last edited by Alex Edwards; Oct 28th, 2008 at 12:45 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 39
Reputation: Evan M is an unknown quantity at this point 
Solved Threads: 5
Evan M's Avatar
Evan M Evan M is offline Offline
Light Poster

Re: Clash RPG Help

 
0
  #3
Oct 28th, 2008
Rather than having YesorNofunc() call another function, it would probably be easier to have it return int. You could declare two constants (or use #define) YES and NO. Then whenever you need a yes or no answer, you could use an if/else statement:
  1. const int YES = 1;
  2. const int NO = 2;
  3.  
  4. if( YesorNofunc() == YES ) {
  5. // ... do yes stuff, call a function or w/e ...
  6. }
  7. else {
  8. // ... do no stuff ...
  9. }

You might want to rename your function too; having "func" on the end of a function name doesn't make much sense. It should be obvious a name is a function. If you need "func" to remind yourself it is a function and not a variable, something is wrong with your naming.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 10
Reputation: Xarver is an unknown quantity at this point 
Solved Threads: 0
Xarver Xarver is offline Offline
Newbie Poster

Re: Clash RPG Help

 
0
  #4
Oct 28th, 2008
Thank you Alex, but I have sort of a problem.
I run your program, it works good.
It try to test if it works with mine, and it doesn't.
I tried testing it, and when you press 1 to start a new adventure,
it does nothing and closes...

main.cpp:
  1. #include <iostream>
  2. #include <string>
  3. #include "CLASHinfo.h"
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. bool running = true;
  9. do {
  10. CLASH();
  11. cout << "\nRPG\n\n";
  12. cout << "Programmed by Xarver : Designed by Fishbowl444\n\n";
  13. cout << "[Press 1, Then Enter, to Start a New Adventure]\n"
  14. << "[Press 2, Then Enter, to Load an Adventure]\n"
  15. << "[Press z, Then Enter, to Quit]\n\n";
  16. char entry;
  17. cin >> entry;
  18. cin.ignore();
  19. if(entry == 1)
  20. {
  21. YesorNofunc(Speak());
  22. cin.ignore();
  23. cin.get();
  24. }
  25. running = false;
  26. }while(running == true);
  27.  
  28. return 0;
  29. }
CLASHinfo.h:
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class DoSomething
  6. {
  7.  
  8. public:
  9. DoSomething(){}
  10. virtual void option1() const = 0;
  11. virtual void option2() const = 0;
  12. };
  13.  
  14. class DoNothing : public DoSomething
  15. {
  16.  
  17. public:
  18. DoNothing(){}
  19. virtual void option1() const {}
  20. virtual void option2() const {}
  21.  
  22. };
  23.  
  24. class Speak : public DoSomething
  25. {
  26.  
  27. public:
  28. Speak(){};
  29. virtual void option1() const {
  30. cout << "Whoo! O_O" << endl;
  31. }
  32.  
  33. virtual void option2() const {
  34. cout << "Oh no >_<" << endl;
  35. }
  36.  
  37. };
  38.  
  39. void YesorNofunc(const DoSomething& action = DoNothing() )
  40. {
  41. cout << "Are you sure?\n\n"
  42. << "[1] Yes\n"
  43. << "[2] No\n\n";
  44. int YesorNo;
  45. cin >> YesorNo;
  46. if(YesorNo == 1)
  47. {
  48. action.option1();
  49. }else if(YesorNo == 2)
  50. {
  51. action.option2();
  52. }else {
  53. cout << "Invalid; Try Again.";
  54. }
  55. }
  56.  
  57. void CLASH()
  58. {
  59. cout
  60. << " :::::::: ::: ::: :::::::: ::: ::: \n"
  61. << ":+: :+: :+: :+: :+: :+: :+: :+: :+: \n"
  62. << "+:+ +:+ +:+ +:+ +:+ +:+ +:+ \n"
  63. << "+#+ +#+ +#++:++#++: +#++:++#++ +#++:++#++ \n"
  64. << "+#+ +#+ +#+ +#+ +#+ +#+ +#+ \n"
  65. << "#+# #+# #+# #+# #+# #+# #+# #+# #+# \n"
  66. << " ######## ########## ### ### ######## ### ### \n";
  67. }
Keep in mind this is testing...
I'm not going to "steal" your code.
If you fix this program, can you explain how it works?
I'm fairly new to Object Oriented Programming.

Any help appreciated!

P.S. Evan:
I know that would be easier,
but the whole purpose of this project is to expand my knowledge,
and get better in C++.


~ Xarver
Last edited by Xarver; Oct 28th, 2008 at 6:58 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Clash RPG Help

 
0
  #5
Oct 28th, 2008
You should probably change entry's data type from char to int and see if you get better results.

-Alex
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 10
Reputation: Xarver is an unknown quantity at this point 
Solved Threads: 0
Xarver Xarver is offline Offline
Newbie Poster

Re: Clash RPG Help

 
0
  #6
Oct 28th, 2008
Wow, I must be the worst debugger in the world... -_-

I just put the ' ' around the 1...

Rep+
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C++ Forum


Views: 364 | Replies: 5
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC