| | |
Rock Paper Scissors: String Problem
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2009
Posts: 149
Reputation:
Solved Threads: 7
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):
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):
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <cstring> using namespace std; int find_winner(char p1, char p2); int main() { char p1choice, p2choice; int p1_score = 0, p2_score = 0; char winner; cout << "Player one, Enter either rock, paper or scissors(r,p,s): "; cin >> p1choice; cout << "Player two, Enter either rock, paper or scissors(r,p,s): "; cin >> p2choice; if ( p1choice == p2choice ) { cout << "Game is a tie\n"; } else { winner = find_winner(p1choice, p2choice); cout << winner << "\n"; if (p1choice == winner) { p1_score++; cout << "The winner is Player one!!\n"; cout << "P1 Score: "<< p1_score << " P2 Score: "<< p2_score << "\n"; } else if (p2choice == winner) { p2_score++; cout << "The winner is Player two!!\n"; cout << "P1 Score: "<< p1_score << " P2 Score: "<< p2_score << "\n"; } } return 0; } int find_winner(char p1, char p2) { char winner; // below is where I am trying to add 2 character into the string choices string choices; choices.insert(p1[0]); choices.insert(p1[0]); if ( choices == "sp" || choices == "ps" ) { return winner = 's'; } else if ( choices == "pr" || choices == "rp" ) { return winner = 'p'; } else if ( choices == "rs" || choices == "sr" ) { return winner = 'r'; } else return winner = 'M'; }
Last edited by gretty; Jun 10th, 2009 at 5:13 am.
•
•
Join Date: Nov 2008
Posts: 397
Reputation:
Solved Threads: 72
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
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
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
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.
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
c++ Syntax (Toggle Plain Text)
char p='x'; std::string A("test"); A+=x; // 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
c++ Syntax (Toggle Plain Text)
// after test for p1,p2 only are s,r,p. if (p1==p2) // draw return 0; if (p1=='r') return (p2=='p') ? 2 : 1; // .. 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
@StuXYZ :
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 :
or better and simple do this :
•
•
•
•
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
c++ Syntax (Toggle Plain Text)
char p='x'; std::string A("test"); A+=x; // A is now testx
Its either :
c++ Syntax (Toggle Plain Text)
char p='x'; std::string A("test"); A+=p; // A is now testx
c++ Syntax (Toggle Plain Text)
//char p='x';//Not at all required std::string A("test"); A+='x'; // A is now testx
Last edited by csurfer; Jun 10th, 2009 at 6:21 am.
I Surf in "C"....
•
•
Join Date: Apr 2009
Posts: 149
Reputation:
Solved Threads: 7
•
•
•
•
Also, don't make a string and then compare, just use the individual result. e.g
c++ Syntax (Toggle Plain Text)
// after test for p1,p2 only are s,r,p. if (p1==p2) // draw return 0; if (p1=='r') return (p2=='p') ? 2 : 1; // .. etc
you might want a switch statement, you might also use a lookup table etc.
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
C++ Syntax (Toggle Plain Text)
if ( find_winner(p1, p2) == 0 ) { cout << "Tie\n"; } else if ( find_winner(p1, p2) == 1 ) { cout << "P1 wins\n"; } else if ( find_winner(p1, p2) == 2 ) { cout << "P2 wins\n"; }
function
C++ Syntax (Toggle Plain Text)
int find_winner(char p1, char p2) { if (p1 == p2) { return 0; } else if ( p1 == 's') { return ( p2 == 'r' ) ? 2:1; } else if ( p1 == 'r') { return ( p2 == 'p' ) ? 2:1; } else if ( p1 == 'p') { return ( p2 == 's' ) ? 2:1; } }
![]() |
Similar Threads
- Help with Rock Paper Scissors game (C++)
- Rock Paper Scissors and how to break out (Python)
- While loop in Rock Paper Scissors (Python)
- Rock paper scissors using functions (C++)
- Rock Paper Scissors scoring help (C++)
Other Threads in the C++ Forum
- Previous Thread: Reading file into a vector!
- Next Thread: *** glibc detected *** free(): invalid pointer
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library linker list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






