Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //guessing.c
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- int main(void)
- {
- printf("Hello, welcome to the guessing game\n");
- char mode;
- srand(time(NULL));
- int NumToGuess; //random number
- int TotalGuesses;
- int found;
- int guess;
- int CharChecker = 0;
- int top;
- int bottom;
- char hly; //testing for high/low/correct
- int Quit = 0; //testing for trying a game again
- char QuitChecker;
- //((top# - bottom) / 2) + bottom = guess
- printf("Do you want to guess, or should I? (Type C for computer, U for you)\n");
- while (Quit == 0){ //Unlimited loop. Progam will keep looping until closed
- while (CharChecker == 0){ //Checking for whether or not the character entered is valid. If not, it asks you again
- scanf("%c",&mode);
- if (mode == 'u' || mode == 'U'){ //testing for User
- CharChecker=1;
- printf("\nOk, I'm thinking of a number\n"); //testing input
- NumToGuess = (rand() % 1000)+1;
- TotalGuesses = 0;
- found = 0;
- while(found==0){
- //printf("\nNumber was %d\n",NumToGuess); //troubleshooting
- printf("\nWhat is your guess?");
- scanf("%d",&guess);
- //printf("\nYour guess is %d\n",guess);//checking guess
- TotalGuesses++;
- if (guess == NumToGuess){
- found=1;
- printf("You win! You got it in %d guess(es). ", TotalGuesses);
- if (TotalGuesses >10){
- printf("You could do better.");
- }else {
- printf("You know the secret to a binary search!");}
- }
- else if (guess >=1000||guess <=0){
- printf("Out of Range");
- }
- else if (guess < NumToGuess){
- printf("Too low!");
- }
- else if (guess > NumToGuess){
- printf("\nToo high!");}
- }
- }
- else if (mode == 'c'|| mode == 'C'){ //testing for Computer
- CharChecker=1;
- TotalGuesses=0;
- printf("Okay, is your number 500? (H for too high,L for too low,Y for yes) ->");
- found=0;
- guess=500;
- top=1000;
- bottom=0;
- while(found==0 && TotalGuesses<=10){
- scanf("%c",&hly);
- if (hly == 'h'|| hly == 'H'){
- top=guess;
- guess = (((top-bottom)/2)+bottom);
- TotalGuesses++;
- printf("Okay, is your number %d? (H,L,Y) ->", guess);
- }else if (hly == 'l'|| hly == 'L'){
- bottom=guess;
- guess = (((top-bottom)/2)+bottom);
- TotalGuesses++;
- printf("Okay, is your number %d? (H,L,Y) ->", guess);
- }else if (hly == 'y'|| hly =='Y'){
- printf("dang, I'm good\n");
- found=1;
- }else if (hly == ' '|| hly == '\n'){
- }
- else {
- printf("\nInvalid input\n");
- printf("Type H for too high, L for too low or Y for yes\n");
- }
- if (TotalGuesses >10){
- printf("There is no other possibility!");
- }
- }
- }
- else if (mode == ' '|| mode == '\n'){
- }
- else {
- printf("\nInvalid input\n");
- printf("Type C for computer, U for you\n");
- }}
- if (found == 1){ //setting up for whether or not you want to play again
- //Using Repeat because it would repeat "Do you want to play again" line
- printf ("\nDo you want to play again? (Y/N) \n");
- scanf("%c",&QuitChecker);
- scanf("%c",&QuitChecker); //had a bug apparently where it would repeat the "Do you want to
- //play again" unless I put this scan line twice
- if ((QuitChecker == 'y'|| QuitChecker == 'Y')&& QuitChecker != '\n'){
- printf("Do you want to guess, or should I? (Type C for computer, U for you)\n");
- CharChecker=0;
- } //taking input
- else if (QuitChecker == 'n'|| QuitChecker =='N'){
- exit(0);} //makes the game close, exiting the loop
- else if (QuitChecker == ' '|| '\n'){}
- else{
- printf("\nInvalid input. Y or N");
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement