Number Guessing Game

Java Program

import java.util.Scanner;
import java.util.Random;

public class GuessingGame {
    public static void guessPracticeMode(int upper) {
        int lower = 1;
        Random rand = new Random();
        int num = rand.nextInt(upper - lower + 1) + lower;
        System.out.println("The Random Number is " + num);

        int guess;
        int tries = 0;
        System.out.println("*********************************************");
        System.out.println("*********************************************");
        System.out.println("There is a Hidden Number present in the game ...");
        System.out.println("Do you have what it takes to guess the hidden number ?");

        while (true) {
            System.out.println("Guess the Number !");
            Scanner scanner = new Scanner(System.in);
            guess = scanner.nextInt();
            tries++;

            if (guess == num) {
                System.out.println("Congratulations !!");
                System.out.println("**You've found the secret number !**");
                System.out.println("You took " + tries + " tries to guess the number");

                if (tries <= 5)
                    System.out.println("You're a detective !\n*********************************************");
                else if (tries > 5 && tries <= 15) {
                    System.out.println("You can surely work it down !");
                    System.out.println("Keep playing and Keep practicing !");
                    System.out.println("*********************************************");
                } else {
                    System.out.println("You took too many tries !");
                    System.out.println("Keep playing and Keep practicing !");
                    System.out.println("*********************************************");
                }
                break;
            } else if (guess < num) {
                System.out.println("!! INCORRECT CHOICE !!");
                System.out.println("You guessed lower.");
                System.out.println("TRY AGAIN !");
            } else {
                System.out.println("!! INCORRECT CHOICE !!");
                System.out.println("You guessed higher.");
                System.out.println("TRY AGAIN !");
            }
        }
    }

    public static void guessDetectiveMode(int upper) {
        int lower = 1;
        Random rand = new Random();
        int num = rand.nextInt(upper - lower + 1) + lower;
        System.out.println("The Random Number is " + num);
        
        Scanner scanner = new Scanner(System.in);
        int guess;
        int tries = 0;
        System.out.println("*********************************************");
        System.out.println("*********************************************");
        System.out.println("There is a Hidden Number present in the game ...");
        System.out.println("Do you have what it takes to guess the hidden number ?");

        int i = 6;
        while (i > 0) {
            System.out.println("Guess the Number !");
            guess = scanner.nextInt();
            tries++;
            if (guess == num) {
                System.out.println("Congratulations !!");
                System.out.println("**You've found the secret number !**");
                System.out.println("You took " + tries + " tries to guess the number");
                System.out.println("You're a detective !");
                System.out.println("*********************************************");
                break;
            } else if (guess < num) {
                System.out.println("!! INCORRECT CHOICE !!");
                System.out.println("You guessed lower.");
                System.out.println("You have " + --i + " attempts");
            } else {
                System.out.println("!! INCORRECT CHOICE !!");
                System.out.println("You guessed higher.");
                System.out.println("You have " + --i + " attempts");
            }
        }

        /*if (guess != num) {
            System.out.println("\nNot Bad");
            System.out.println("Don't be Sad");
            System.out.println("You can always come back for more.");
            System.out.println("The Hidden Number was " + num);
            System.out.println("*********************************************");
        }*/
    }

    public static void main(String[] args) {
        int upper, mode;
        int retry;

        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("*****************************");
            System.out.println(" DETECTIVE MODE  - Press 1");
            System.out.println(" PRACTICE MODE   - Press 2");
            System.out.println("*****************************");
            mode = scanner.nextInt();

            switch (mode) {
                case 1:
                    System.out.println(" **************************** ");
                    System.out.println(" Welcome to Detective Mode");
                    System.out.println("Remember to tread Cautiously");
                    System.out.println("Your tries are limited !");
                    System.out.println("Enter the maximum number of guessing :: ");
                    upper = scanner.nextInt();
                    Random rand = new Random();
                    rand.setSeed(System.currentTimeMillis());
                    guessDetectiveMode(upper);
                    System.out.println("\n\n\n\n\n");
                    System.out.println("Do You Want To Try Again ?");
                    System.out.println("Press 1 for YES      Press 2 for NO");
                    retry = scanner.nextInt();
                    if (retry != 1)
                        System.exit(0);
                    break;

                case 2:
                    System.out.println(" ********************************");
                    System.out.println("Welcome to Practice Mode");
                    System.out.println("You can practice as much as you want here");
                    System.out.println("You have unlimited tries");
                    System.out.println("Practice to be the next Sherlock Holmes !");
                    System.out.println("Enter the maximum number of guessing :: ");
                    upper = scanner.nextInt();
                    Random rand2 = new Random();
                    rand2.setSeed(System.currentTimeMillis());
                    guessPracticeMode(upper);
                    System.out.println("\n\n\n\n\n");
                    System.out.println("Do You Want To Try Again ?");
                    System.out.println("Press 1 for YES      Press 2 for NO");
                    retry = scanner.nextInt();
                    if (retry != 1)
                        System.exit(0);
                    break;

                default:
                    System.out.println("That is a WRONG CHOICE !");
                    System.out.println("Type the correct choice !");
            }
        }
    }
}

C Program

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void guessPracticeMode(int upper){

   int lower=1;
    int num = (rand() % (upper - lower + 1)) + lower;
    printf("The Random Number is %d \n", num); // This statement is meant for debugging purpose only !

   int guess,i;
   int tries=0;
      printf("*********************************************\n");
      printf("*********************************************\n");
      printf("There is a Hidden Number present in the game ...\n");
      printf("Do you have what it takes to guess the hidden number ?\n");

 guessingLabel:

   printf("Guess the Number !\n");
   scanf("%d",&guess);
   tries++;
    if(guess==num){
         printf("Congratulations !! \n**You've found the secret number !**\n");
         printf("You took %d tries to guess the number\n",tries);
            if(tries<=5)
                 printf("You're a detective !\n*********************************************\n");
            else if(tries>5 && tries <=15){
                 printf("You can surely work it down !\nKeep playing and Keep practicing !");
                 printf("*********************************************\n"); }
            else{
                 printf("You took too many tries !\nKeep playing and Keep practicing !");
                 printf("*********************************************\n");}
    }
    else if(guess<num){
        printf("!! INCORRECT CHOICE !!\nYou guessed lower.\nTRY AGAIN !\n");

        goto guessingLabel;
    }
    else{
        printf("!! INCORRECT CHOICE !!\nYou guessed higher.\nTRY AGAIN !\n");
        goto guessingLabel;
    }
 }

 void guessDetectiveMode(int upper){

   int lower=1,i=6;
    int num = (rand() % (upper - lower + 1)) + lower;
    printf("The Random Number is %d \n", num); // This statement is meant for debugging purpose only !

   int guess;
   int tries=0;
      printf("*********************************************\n");
      printf("*********************************************\n");
      printf("There is a Hidden Number present in the game ...\n");
      printf("Do you have what it takes to guess the hidden number ?\n");

 guessingLabel:
  while(i>0){
   printf("Guess the Number !\n");
   scanf("%d",&guess);
   tries++;
    if(guess==num){
         printf("Congratulations !! \n**You've found the secret number !**\n");
         printf("You took %d tries to guess the number\n",tries);
         printf("You're a detective !");
         printf("*********************************************\n");
         break;
     }
      else if(guess<num){
          printf("!! INCORRECT CHOICE !!\nYou guessed lower.\nYou have %d attempts\nTRY AGAIN !\n",--i);
          goto guessingLabel;
      }
     else{
         printf("!! INCORRECT CHOICE !!\nYou guessed higher.\nYou have %d attempts\nTRY AGAIN !\n",--i);
         goto guessingLabel;

      }
     i--;
     }
        if(guess!=num){
        printf("\nNot Bad\nDon't be Sad\nYou can always come back for more. \n");
        printf("\nThe Hidden Number was %d\n",num);
        printf("*********************************************\n");}
    }

int main()
{
 int upper,mode;
 int retry;

label:
 printf("*****************************\n");
 printf(" DETECTIVE MODE - Press 1\n");
 printf(" PRACTICE MODE - Press 2\n");
 printf("*****************************\n");
 scanf("%d",&mode);
 switch(mode){
    case 1:
        printf(" **************************** \n");
        printf(" Welcome to Detective Mode\nRemember to tread Cautiously\nYour tries are limited !\n");
        printf("Enter the maximum number of guessing :: \n");
     scanf("%d",&upper);
  srand(time(0));
    guessDetectiveMode(upper);
      printf("\n\n\n\n\n");
      printf("Do You Want To Try Again ? \n");
      printf("Press 1 for YES Press 2 for NO\n");

       scanf("%d",&retry);
        if(retry==1)
          goto label;
        else
          exit(0);
       break;

    case 2:
 printf(" ********************************\n");
 printf("Welcome to Practice Mode\nYou can practice as much as you want here\n");
 printf("You have unlimited tries\nPractice to be the next Sherlock Holmes\n!");
   printf("Enter the maximum number of guessing :: \n");
     scanf("%d",&upper);
  srand(time(0));
    guessPracticeMode(upper);
      printf("\n\n\n\n\n");
      printf("Do You Want To Try Again ? \n");
      printf("Press 1 for YES Press 2 for NO\n");
       scanf("%d",&retry);
        if(retry==1)
          goto label;
        else
          exit(0);
       break;

    default:
        printf("That is a WRONG CHOICE !\nType the correct choice !\n");
        goto label;
      }
    return 0;
  }

Related Posts

CSS Cheatsheet

Transform your web design game with my CSS cheatsheet! Master the art of styling, dive into essential properties and selectors, and create visually stunning websites effortlessly. Whether…

HTML Cheatsheet

Unleash your web development potential with my HTML cheatsheet! Elevate your coding skills, master essential tags and attributes, and build stunning websites with ease. Whether you’re a…

Git&GitHub Cheatsheet

Elevate your Git and GitHub game with my ultimate cheatsheet! Unlock the power of version control, streamline collaboration, and boost productivity. Navigate the complexities of Git commands…

Command Line Cheatsheet

Master the Command Line effortlessly with my comprehensive cheatsheet! Streamline your workflow, boost productivity, and become a command line guru. Unlock essential commands, shortcuts, and tips to…

OpenAI’s APIs Notes

Unlock the potential of OpenAI’s APIs with my comprehensive notes! Dive into the world of cutting-edge artificial intelligence, explore the capabilities of GPT-4 and beyond, and learn…

SQL Notes

Discover the essence of SQL with my comprehensive notes! Dive into the world of Structured Query Language, unraveling the complexities, and mastering database management. Elevate your SQL…

This Post Has 2 Comments

  1. Usually I do not read article on blogs however I would like to say that this writeup very compelled me to take a look at and do so Your writing taste has been amazed me Thanks quite nice post

    1. Hello! Thank you for taking the time to read the article, even though you don’t typically read articles on blogs. I’m delighted to hear that it compelled you to take a closer look. Your kind words about my writing mean a lot to me, and I’m thrilled that you found the post quite nice. If there are any topics you’d like to see covered in the future, please feel free to let me know. I appreciate your support!

Leave a Reply

Your email address will not be published. Required fields are marked *