943,864 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 514
  • C++ RSS
Oct 28th, 2008
0

Clash RPG Help

Expand Post »
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:
c++ Syntax (Toggle Plain Text)
  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:
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Xarver is offline Offline
10 posts
since Oct 2008
Oct 28th, 2008
0

Re: Clash RPG Help

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--

c++ Syntax (Toggle Plain Text)
  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--

c++ Syntax (Toggle Plain Text)
  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--

c++ Syntax (Toggle Plain Text)
  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--

c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Oct 28th, 2008
0

Re: Clash RPG Help

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:
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 11
Solved Threads: 5
Light Poster
Evan M is offline Offline
42 posts
since Sep 2007
Oct 28th, 2008
0

Re: Clash RPG Help

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:
c++ Syntax (Toggle Plain Text)
  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:
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Xarver is offline Offline
10 posts
since Oct 2008
Oct 28th, 2008
0

Re: Clash RPG Help

You should probably change entry's data type from char to int and see if you get better results.

-Alex
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Oct 28th, 2008
0

Re: Clash RPG Help

Wow, I must be the worst debugger in the world... -_-

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

Rep+
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Xarver is offline Offline
10 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: I Need Help with Cases in C++.
Next Thread in C++ Forum Timeline: Advanced cout??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC