2021年5月3日星期一

C++ Input validation rockpaperscissors game

Im trying to write this code which I apologize if its hard to refine or read, that is suppose to make a RockPaperScissors game using random numbers and correct input validation but struggling around the input validation part. I will provide an example of how the code is supposed to run alongside my code which I again apologize if it is to messy to work around with. So basically when its asking for the user to type their choice, the program should keep repeating the question until it inputs the correct (r, (r)ock, rock). When there's also a tie, im trying to get the program to also display the very question again. I appreciate any of the help I can get. Thank you so much :)

#include <iostream>  #include <cstdlib>  #include <ctime>    using namespace std;    //Get computer's choice  char getComp()  {      //Default value      char compChoice = 'X';        int choice = rand() % 3;        if (choice == 0)          compChoice = 'R';      else if (choice == 1)          compChoice = 'P';      else if (choice == 2)          compChoice = 'S';        return compChoice;  }    //Get user's choice  char getUser()  {      //Default choice      char userChoice = 'X';      cout << "Enter (R)ock, (P)aper, or (S)cissors for your move: " << endl;      cin >> userChoice;        return userChoice;  }    //Generate decision  void game(char user, char comp)  {      if (user == comp || user == comp - 32 || user == comp + 32) //added and subtracted 32 to remove case sensitivity          cout << "Tie!" << endl;      else if ((user == 'R' || user == 'r') && (comp == 'P' || comp == 'p'))          cout << "The AI wins! :(" << endl;      else if ((user == 'R' || user == 'r') && (comp == 'S' || comp == 's'))          cout << "You win!\n";      else if ((user == 'P' || user == 'p') && (comp == 'S' || comp == 's'))          cout << "The AI wins :(" << endl;      else if ((user == 'P' || user == 'p') && (comp == 'R' || comp == 'r'))          cout << "You win!" << endl;      else if ((user == 'S' || user == 's') && (comp == 'R' || comp == 'r'))          cout << "The AI wins :(\n";      else if ((user == 'S' || user == 's') && (comp == 'P' || comp == 'p'))          cout << "You win!\n";  }  int main()  {      //Seed random number generator      unsigned seed = time(0);        srand(seed);        //Declare variables, default values      char user = 'X',           comp = 'X',           playAgain = 'X';        //Loop of program      do {          //Get computer's choice          comp = getComp(); //Earlier you were not storing your returned value in a variable. Which leaded you to your main problem.            //Get user's choice          user = getUser(); //Earlier you were not storing your returned value in a variable. Which leaded you to your main problem.            //Display choices            switch (comp) {          case 'R':              cout << "The ai played rock." << endl;              break;          case 'P':              cout << "The ai played paper." << endl;              break;          case 'S':              cout << "The ai played scissors." << endl;              break;          }          //Determine winner          game(user, comp);            //Play again?          cout << "Would you like to replay the game?" << endl;          cout << "Enter (Y)es or (N)o: ";          cin >> playAgain;          if (playAgain != 'Y' && playAgain != 'y' && playAgain != 'N' && playAgain != 'n') { //Earlier if statement was logically wrong              cout << "Invalid choice, please choose a valid choice" << endl;          }      } while (playAgain == 'Y' || playAgain == 'y');      return 0;  }  

The example is here:

Enter (R)ock, (P)aper, or (S)cissors for your move: job

Enter (R)ock, (P)aper, or (S)cissors for your move: rock hen

Enter (R)ock, (P)aper, or (S)cissors for your move: Rock

The ai played rock.

You and the AI both played rock.

Keep playing until someone wins.

Enter (R)ock, (P)aper, or (S)cissors for your move: p

The ai played rock.

You win!

Would you like to replay the game?

Enter (Y)es or (N)o: Y

Enter (R)ock, (P)aper, or (S)cissors for your move: s(c)

Enter (R)ock, (P)aper, or (S)cissors for your move: (s)cIssOrs

The ai played scissors.

You and the AI both played scissors.

Keep playing until someone wins.

https://stackoverflow.com/questions/67378538/c-input-validation-rockpaperscissors-game May 04, 2021 at 11:47AM

没有评论:

发表评论