Thread: Computer guess the number ...

  1. #1
    Registered User
    Join Date
    Mar 2021
    Posts
    9

    Computer guess the number ...

    It is up to the computer to guess in seven tries the number the user is thinking of. On each move, the user must indicate whether the number tried is higher or lower than the number he/she thought of. Build your program so that the computer always wins.
    My code:
    Code:
    #include <time.h>
    #include<stdio.h>
    #include <stdlib.h>
    
    
    int main(void) {
    
    
    int numberGuess = 0;
    int low = 1;
    int high = 100;
    int computerGuess = 0;
    
    
    printf("Enter a number, 1 - 100: ");
    scanf("%d", &numberGuess);
    
    
    while (computerGuess != numberGuess) 
    {
    
    
      computerGuess = ((high - low)/2 + low);
      printf("%d ", computerGuess);
    
    
      if (numberGuess > computerGuess)
        {
        printf("Your guess was to low \n");
        low = computerGuess+1;
        }
      else if (numberGuess < computerGuess)
        {
        printf("Your guess was to high \n");
        high = computerGuess-1;
    }
      else if (numberGuess == computerGuess)
    {
    printf("Yess!! you got it!\n");
        }
     }
    
    
    return 0; 
    }
    Last edited by puctw; 03-04-2021 at 05:00 AM.

  2. #2
    Registered User
    Join Date
    Mar 2021
    Posts
    9
    I need help to fix the code.

  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    What is the problem?
    Does it crash?
    Does it allow more than 7 tries?
    Doesn't it guess it at all?

    One problem I see is that you set high to 100, but the user can enter sth. like 55.

  4. #4
    Registered User
    Join Date
    Mar 2021
    Posts
    9
    I can't tell the computer the number. I have to tell the computer low or high. And the program always has to win.

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    One observation:

    Computer guess the number ...-untitled-png

  6. #6
    Registered User
    Join Date
    Mar 2021
    Posts
    9
    Statement:


    1. write a program that implements a game in which the objective of the player is to guess, in 7 tries, a random integer number between 1 and 100, generated by the computer. In each move the computer must inform if the number entered is below or above the number that you want to guess. Is there a winning strategy?


    2. Let's reverse the roles: the computer must guess in seven tries the number the user is thinking of. In each move, the user must indicate if the number tried is higher or lower than the number he thought of. Build your program in such a way that the computer always wins.

  7. #7
    Registered User
    Join Date
    Mar 2021
    Posts
    9
    The 1 is done! Now i need to fix the code for the statement number 2, and i can't.

  8. #8
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Notice for the rage from 1 to 100, 7 tries will always "win", since
    Computer guess the number ...-untitled-png

  9. #9
    Registered User
    Join Date
    Mar 2021
    Posts
    9
    So now how can i give the calls to the program to go high or low?

  10. #10
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Almost the same code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
      int numberGuess;
      int computerGuess;
      int count;
      int low = 1;
      int high = 100;
    
      do
      {
        printf ( "Enter a number, 1 - 100: " );
    
        if ( ( count = scanf ( "%d", &numberGuess ) ) == 1 )
          if ( numberGuess >= 1 && numberGuess <= 100 )
            break;
    
        puts ( "\nValue out of range. Try Again." );
      } while ( 1 );
    
      count = 0;
    
      // NOTE: For the range 1..100 this loop will iterate
      //       7 times or less, always.
      while ( 1 )
      {
        computerGuess = ( high + low ) / 2;
    
        printf ( "Guess #%d: % 3d ", ++count, computerGuess );
    
        // Found! exit loop.
        if ( numberGuess == computerGuess )
        {
          puts ( "OK" );
          break;
        }
    
        if ( numberGuess > computerGuess )
        {
          puts ( "MORE!" );
          low = computerGuess;
        }
        else
        {
          puts ( "LESS!" );
          high = computerGuess;
        }
      }
    
      return EXIT_SUCCESS;
    }
    Last edited by flp1969; 03-04-2021 at 06:43 AM.

  11. #11
    Registered User
    Join Date
    Mar 2021
    Posts
    9
    The problem is: I can't choose the number, the number is on my mind! I only can write LESS or MORE. Screenshot by Lightshot

  12. #12
    Registered User
    Join Date
    Mar 2021
    Posts
    9
    "Me" need to be me writing.

    For Example:

    Computer: 50
    Me: LESS
    Computer: 54
    Me: OK

    You win ...

  13. #13
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by puctw View Post
    The problem is: I can't choose the number, the number is on my mind! I only can write LESS or MORE. Screenshot by Lightshot
    Well... As far as I know, the telepathic interface was not invented yet.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game: Computer Guess My Number
    By Laythe in forum C++ Programming
    Replies: 1
    Last Post: 03-31-2012, 11:25 PM
  2. Replies: 17
    Last Post: 10-20-2011, 06:32 PM
  3. Replies: 7
    Last Post: 10-19-2011, 08:45 AM
  4. Guess My Number (Need help)
    By dhardin in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2009, 12:59 PM
  5. can u guess my number?
    By hotsox in forum C Programming
    Replies: 2
    Last Post: 01-07-2006, 01:40 PM

Tags for this Thread