Thread: Simulation - human vs. computer

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    5

    Simulation - human vs. computer

    Hello all. I'm currently working on a program where there is supposed to be 3 functions: 2 value returning functions and 1 void function. The first function I've created is supposed to generate a number from 0-2 to represent the computer's selection, and then assign each of those to be either R, S, or P. The second function is supposed to prompt the user for their answer, also R, S, or P, in addition to Q for Quit. I also have to add a validation loop to it so that it will keep prompting until it receives one of those answers. The void function I'm not worrying about yet, but it's supposed to be where the code goes for printing the results.

    I'm stuck on the parts for the computer and human inputs now, even though I tried each one separately first. So my questions now are as follows:

    1. How do I put in a validation loop within the userChoice function? I've tried a while and do-while loop so far, but they both keep prompting even when the user inputs R,S or P.

    2. How can I fix my code so that the compChoice function will give me a more random answer instead of the same one repeatedly?

    Any hints, suggestions, comments or thoughts would be greatly appreciated!

    Here's my code so far (without the validation loops- they were within the userChoice function before; and char c/h are to be utilized in the void function):


    Code:
    #include <algorithm>
    #include <cctype>
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    #include <string>
    using namespace std;
    
    char compChoice(int a)
    {
      char random;
      random = rand () % 3;
      if (random = 0)
        random = 'R';
      else if (random = 1)
        random = 'S';
      else 
        random = 'P';
      return random;
    }
    
    char userChoice(string q, char a)
    {
    
      char answer;
    
      cout << q;
      cin  >> answer;
      answer = toupper(answer);
     
      return answer;
      
    }
    
    void results(char c, char h)
    {  
    
    }
    
    
    int main()
    {
    
    
      // initialize the computer's random number generator
      srand(time(0));
    
      // declare variables
      char R = 0;
      char S = 0;
      char P = 0;
      char Q = 0;
      char c = 0;
      char h = 0;
      
      // start loop
      while (true)
      {
    
        // determine computer's choice
        char comp = compChoice (S);
        cout << endl << "Computer: "
             << comp << endl;
    
        // prompt for, and read, the human's choice
        char user = userChoice("Choose: [R,P,S,Q to Quit]: ", 'R');
     
        // if human wants to quit, break out of loop
        if (user = 'Q') break;
    
        // print results
        
    
      // end loop
      } 
    
      // end program
      return 0;
    
    } // main

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    1)
    - initialize input to an invalid state
    - while the input is invalid, prompt
    2) you can try a different seed but I really wouldn't worry about it much - it's hard to get good random numbers with mod 3.

    also, don't you think it would be more of a challange if you displayed the computers choice *after* the user chooses?

    calculating the winner is pretty trivial - you could use simple brute force as a last resort...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    5
    Thanks for replying. I *think* I understood what you meant. In any case, I've started from scratch after numerous attempts with my initial code. So now I have the user input function working and hopefully the computer function too.

    I'm having trouble with the output however, which is the void function printResults. How do I pass the answers gathered from the user and computer inputs through to the void function and then call it? I couldn't figure out how to get the returned values from the first two functions...

    Also, do I need the #include <algorithm>, #include <cctype> statements?


    New code:

    Code:
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    char compChoice(int c)
    {
      char random = 0;
      random = rand() % 3;
      if (random = 0)
        random = 'R';
      if (random = 1)
        random = 'P';
      if (random = 2)
        random = 'S';
      return random;
    }
    
    //*****************************************************************
    
    char humChoice(string q, char a)
    {  
      while (a != 'Q')
      {
        cout << q;
        cin  >> a;
        a = toupper(a);
        if (a == 'R') break;
        if (a == 'P') break;
        if (a == 'S') break;
      }
    
      return a;
    }
    
    //*****************************************************************
    
    void printResults(string result1, string result2, char a)
    {
      cout << result1 << result2 << a << endl;
    }
    
    
    //*****************************************************************
    
    int main()
    {
    
      // initialize the computer's random number generator
      srand(time(0));
    
      // declare variables
      char R = 0;
      char P = 0;
      char S = 0;
      char Q = 0;
      char a = 0; 
      // start loop
      while (true)
      {
        // determine computer's choice
        char comp = compChoice(P);
    
        // prompt for, and read, the human's choice
        char user = humChoice("Choose: [Rock,Paper,Scissors,Quit]: ", 'R'
    
    );
     
        // if human wants to quit, break out of loop
        if (user = 'Q') break;
    
    
      // end loop
      }
    
      // print results
      printResults("Computer: ", ", Human: ", 'a');
      
      // end program
      return 0;
    
    } // main

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if (user = 'Q') break;
    Use == for comparison (you do this a lot)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Combine the two functions (human/computer)
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    5
    How do I combine two functions into one? Do I have to pass the parameters through them again? I'm supposed to keep them separate until I call the print function.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Combine the two functions (human/computer)
    IMO that's a bad idea, unless you meant to move the two function calls to a separate function, which couldn't hurt but isn't necessary.

    >> I couldn't figure out how to get the returned values from the first two functions...
    Your code already does it, you store the values in the comp and user variables. You can just pass those variables to the printResults function (of course, you have to modify printResults to take two characters, one for the user answer and one for the computer answer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mouse to have human brain
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 03-10-2005, 05:39 PM
  2. upgrade computer ?
    By teeyester in forum Tech Board
    Replies: 4
    Last Post: 10-11-2003, 09:51 PM
  3. Regarding Undergraduate Computer Majors
    By UnregdRegd in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-04-2003, 11:55 AM
  4. Problems shutting off computer
    By frenchfry164 in forum Tech Board
    Replies: 9
    Last Post: 04-22-2003, 06:19 PM
  5. Computer engineering - programming.
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 07-15-2002, 02:37 PM