while (userChoice == compChoice || userChoice <= 0 || userChoice < 4){
cout << "You have either tied the game or it is now restarting due to incorrect input." << endl;
cin >> userChoice;
compChoice = ((rand() % 3) + 1);
if (userChoice == 1 && compChoice == 2){
cout << "Your opponent picked: " << compChoice << endl
<< endl << endl;
cout << "You lose" << endl << endl << endl;
//user rock comp paper
}
else if (userChoice == 1 && compChoice == 3){
cout << "Your opponent picked: " << compChoice << endl
<< endl << endl;
cout << "You win!" << endl << endl << endl;
// user rock comp scissors
}
else if (userChoice == 2 && compChoice == 1){
cout << "Your opponent picked: " << compChoice << endl
<< endl << endl;
cout << "You lose!" << endl << endl << endl;
//user paper comp rock
}
else if (userChoice == 2 && compChoice == 3){
cout << endl << "Your opponent picked: " << compChoice << endl
<< endl << endl;
cout << "You lose!" << endl << endl << endl;
//user paper comp scissor
}
else if (userChoice == 3 && compChoice == 1){
cout << "Your opponent picked: " << compChoice << endl
<< endl << endl;
cout << "You lose!" << endl << endl << endl;
//user scissors comp rock
}
else if (userChoice == 3 && compChoice == 2){
cout << "Your opponent picked: " << compChoice << endl
<< endl;
cout << "You lose!" << endl << endl;
//user scissors comp paper
}
}
Like so? Not going to lie this program is making me smile in frustration and i'm going to be asking how to do this again next class.
That's exactly what people were referring to when they said repeating code fragments is bad. The first thing to wrap your head around when figuring out how programming works, and it can be a little difficult at first, is how to abstract out repeated instructions. Computer programs, especially big ones, spend a lot of time doing things over and over. Your job is to figure out a concise way of telling the computer what you want it to do, so you don't have to copy-paste those same commands again and again. Doing boring, repetitive things is the computer's job, not yours.
The first thing I would do when writing this program is define something like this at the top of the file:
const char* options[] = {"Rock", "Paper", "Scissors"};
This is an array of strings, containing the names of the choices the player could make ("const" just means you can't change it on accident later on). Why would we create this? Well, now we can do something like this further down:
compChoice--;
cout << "Your opponent picked: " << options[compChoice] << endl;
I'm assuming you exit the loop immediately if the user selects 0, otherwise the program would crash here due to accessing array index -1. Next, we need to abstract out the information that tells us whether the player won or not. How do we do that? Well, we could do it algorithmically, but there's so little information here that we can get away with just defining it the same way we did before. So let's add a second data definition that looks like this:
const char* options[] = {"Rock", "Paper", "Scissors"};
const int beats[] = {2, 0, 1};
Okay, lemme explain this. This is another array (of numbers this time), and it tells us what beats what. The number at a given index tells us what the choice at that index beats. So, for example, at index 0 in the array we have the number 2. This means "option 0 beats option 2". Look at the names above, and we see that that's true: Rock beats Scissors. Next, we have "option 1 beats option 0". Paper beats Rock. Last, we have "option 2 beats option 1". Scissors beats Paper.
So, now we can add a block further down like this:
compChoice = rand() % 3;
if(beats[userChoice] == compChoice) {
cout << "You win!" << endl;
}
else if(beats[compChoice] == userChoice) {
cout << "You lose..." << endl;
}
else {
cout << "Draw" << endl;
}
Now you have all the logic you need to tell if the player won or not, and your program is about one tenth of the size it was before. I think I need to start listening in history class now, so I'll let you try to figure this out. Let me know if you have any questions.
(Note that compValue is set to a value between 0 and 2 here, whereas in your version it was 1-3)