Rock Paper Scissors: String Problem

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2009
Posts: 149
Reputation: gretty is an unknown quantity at this point 
Solved Threads: 7
gretty gretty is offline Offline
Junior Poster

Rock Paper Scissors: String Problem

 
0
  #1
Jun 10th, 2009
Hi

I am making a rock, paper scissors game for fun & for practice.
My problem is; I am trying to create a string by adding 2 character variables together. How do I do this?

Do I use the insert function? Or something I think is called crstrt or something similar to that.

Also I have a question; I have made this game using only one function & it is rather simple. In my text book, I looked over its example of a rock paper scissors game & it uses around 4 functions; a function to check if input is allowed using a switch, a fucntion to determine the winner, some other function I forget & finally a function to print the outcome of the game & it also stores the rock paper scissors input options in an enumeration.

My question is...What is better programming practice, the book version or my version which is simple. I always thought that in programming simple( & less lines of code) is better because it also means the program will preform faster, you know KISS (keep it simple...). If I were working for a business which code would be better/wanted?

Here's my code (the problem is located in the fuction):
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. int find_winner(char p1, char p2);
  9.  
  10. int main()
  11. {
  12. char p1choice, p2choice;
  13. int p1_score = 0, p2_score = 0;
  14. char winner;
  15.  
  16.  
  17. cout << "Player one, Enter either rock, paper or scissors(r,p,s): ";
  18. cin >> p1choice;
  19.  
  20. cout << "Player two, Enter either rock, paper or scissors(r,p,s): ";
  21. cin >> p2choice;
  22.  
  23. if ( p1choice == p2choice )
  24. {
  25. cout << "Game is a tie\n";
  26. }
  27. else
  28. {
  29.  
  30. winner = find_winner(p1choice, p2choice);
  31.  
  32. cout << winner << "\n";
  33.  
  34. if (p1choice == winner)
  35. {
  36. p1_score++;
  37. cout << "The winner is Player one!!\n";
  38. cout << "P1 Score: "<< p1_score << " P2 Score: "<< p2_score << "\n";
  39. }
  40. else if (p2choice == winner)
  41. {
  42. p2_score++;
  43. cout << "The winner is Player two!!\n";
  44. cout << "P1 Score: "<< p1_score << " P2 Score: "<< p2_score << "\n";
  45. }
  46. }
  47.  
  48. return 0;
  49. }
  50.  
  51. int find_winner(char p1, char p2)
  52. {
  53. char winner;
  54.  
  55. // below is where I am trying to add 2 character into the string choices
  56. string choices;
  57.  
  58. choices.insert(p1[0]);
  59. choices.insert(p1[0]);
  60.  
  61. if ( choices == "sp" || choices == "ps" )
  62. {
  63. return winner = 's';
  64. }
  65. else if ( choices == "pr" || choices == "rp" )
  66. {
  67. return winner = 'p';
  68. }
  69. else if ( choices == "rs" || choices == "sr" )
  70. {
  71. return winner = 'r';
  72. }
  73. else
  74. return winner = 'M';
  75. }
Last edited by gretty; Jun 10th, 2009 at 5:13 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 397
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: Rock Paper Scissors: String Problem

 
0
  #2
Jun 10th, 2009
Several things from your question:

First the string part. You have many options (a) you can indeed use
insert but the method requires an iterator to define the position for insertion. (b) the easiest to use simply += e.g
  1. char p='x';
  2. std::string A("test");
  3. A+=x;
  4. // A is now testx

Second :note your mistake in your function, you have used pl[0]
but it is a simple character. It is not an array.

Third: I don't understand why you have the variable winner. you assign a value to it and then return from the function. The means that the variable is immediately lost.
do this return 'M'; instead.

Also you claim to return an integer from find_player but you return a character. That is acceptable to the compile (with warnings) but unlikely to be the intent. Surely you wanted to return 0 if it is a draw and 1 if player one won and 2 (or maybe -1) if player 2 won.

Also, don't make a string and then compare, just use the individual result. e.g
  1. // after test for p1,p2 only are s,r,p.
  2. if (p1==p2) // draw
  3. return 0;
  4. if (p1=='r')
  5. return (p2=='p') ? 2 : 1;
  6. // .. etc

you might want a switch statement, you might also use a lookup table etc.

As for production code, I feel that you need a lot more experience getting code to work and compile before you even ask the question. Nobody checks in code that doesn't compile. Only then do we discuss classes and methods etc. There are many dicussions on the subject and over a time you develop your own style that needs to be adjusted to the project you are working on, however, don't worry until you have written some more c++, then read Scott Myers 50 and 35 C++ Tips, then worry about it.
Last edited by StuXYZ; Jun 10th, 2009 at 5:46 am.
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: Rock Paper Scissors: String Problem

 
0
  #3
Jun 10th, 2009
@StuXYZ :

Originally Posted by StuXYZ View Post
Several things from your question:
First the string part. You have many options (a) you can indeed use
insert but the method requires an iterator to define the position for insertion. (b) the easiest to use simply += e.g
  1. char p='x';
  2. std::string A("test");
  3. A+=x;
  4. // A is now testx
Make sure that the examples you give are correct because in the above code what you have stated is wrong and it would give rise to a compiler error stating that char variable x not declared.
Its either :
  1. char p='x';
  2. std::string A("test");
  3. A+=p;
  4. // A is now testx
or better and simple do this :
  1. //char p='x';//Not at all required
  2. std::string A("test");
  3. A+='x';
  4. // A is now testx
Last edited by csurfer; Jun 10th, 2009 at 6:21 am.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 149
Reputation: gretty is an unknown quantity at this point 
Solved Threads: 7
gretty gretty is offline Offline
Junior Poster

Re: Rock Paper Scissors: String Problem

 
0
  #4
Jun 10th, 2009
Also, don't make a string and then compare, just use the individual result. e.g
  1. // after test for p1,p2 only are s,r,p.
  2. if (p1==p2) // draw
  3. return 0;
  4. if (p1=='r')
  5. return (p2=='p') ? 2 : 1;
  6. // .. etc

you might want a switch statement, you might also use a lookup table etc.
Thanks for your help Finding the winner the way you suggested above is alot smarter I'd like to use this kind of thing in the future, but when I return either 0,1 or 2 back into main how do I still determine the winner?

Is it just a simple case of using if/esle, eg
  1. if ( find_winner(p1, p2) == 0 )
  2. {
  3. cout << "Tie\n";
  4. }
  5. else if ( find_winner(p1, p2) == 1 )
  6. {
  7. cout << "P1 wins\n";
  8. }
  9. else if ( find_winner(p1, p2) == 2 )
  10. {
  11. cout << "P2 wins\n";
  12. }

function

  1. int find_winner(char p1, char p2)
  2. {
  3. if (p1 == p2)
  4. {
  5. return 0;
  6. }
  7. else if ( p1 == 's')
  8. {
  9. return ( p2 == 'r' ) ? 2:1;
  10. }
  11. else if ( p1 == 'r')
  12. {
  13. return ( p2 == 'p' ) ? 2:1;
  14. }
  15. else if ( p1 == 'p')
  16. {
  17. return ( p2 == 's' ) ? 2:1;
  18. }
  19. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,661
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Rock Paper Scissors: String Problem

 
0
  #5
Jun 10th, 2009
i read this as "rock paper scissor string" problem.

and thought the problem is, that "string" doesnt fit the "rock paper scissors" paradigm.

Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC