Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <math.h>
- #include <time.h>
- #include <conio.h>
- #define ROUNDS_EASY 20
- #define ROUNDS_MODERATE 15
- #define ROUNDS_HARD 10
- enum Difficulty
- {
- Easy = 1,
- Moderate,
- Hard,
- Crazy
- };
- void printInfo();
- int selectDifficulty();
- int getInput(int guessesLeft);
- bool containsDigit(int number, int digit);
- bool checkValid(int number);
- bool hasDistinctDigits(int number);
- int guessInfo(int inputNumber, int genNumber, char ret);
- //global variables
- bool isCrazy = false;
- int main()
- {
- srand(time(NULL));
- int inputNumber = 0, genNumber = 0, miss = 0, hit = 0, rounds = 0, guessesLeft = 0;
- char replayInput = ' ';
- while (1)
- {
- printInfo();
- rounds = selectDifficulty();
- guessesLeft = rounds;
- genNumber = generateNumber();
- printf("[INFO] Generated Number: %d\n", genNumber);
- while (guessesLeft > 0)
- {
- inputNumber = getInput(guessesLeft);
- guessesLeft--;
- hit = guessInfo(inputNumber, genNumber, 'h');
- miss = guessInfo(inputNumber, genNumber, 'm');
- if (hit == 4)
- {
- printf(" 4 HITS YOU WON!!!\n\n"
- "It took you only %d guesses, you are a professional code breaker!\n", rounds - guessesLeft);
- guessesLeft = -1;
- }
- else
- {
- printf("You got %d HITS %d MISSES.\n", hit, miss);
- if (guessesLeft == 0)
- {
- printf("\nOOOOHHHH!!! Pancratius won and bought all of Hanukkah's gifts.\n"
- "Nothing left for you...\n"
- "The secret password was %d\n", genNumber);
- }
- }
- }
- do {
- printf("\nWould you like to play again? (y/n): ");
- replayInput = getch();
- printf("%c\n", replayInput);
- if (replayInput == 'n' || replayInput == 'N')
- {
- printf("\nBye Bye!\n");
- system("pause");
- return 0;
- }
- } while (replayInput != 'y' && replayInput != 'Y');
- }
- }
- /*
- Returns the misses/hits according the two given numbers
- input: first number, seconds number, info setting
- output: (if<info> equals to 'h' this function will return HITS, otherwise, if<info> equals to 'm' this function will return MISSES)
- */
- int guessInfo(int inputNumber, int genNumber, char info)
- {
- int miss = 0, hit = 0, i = 0;
- for (i = 1; i < 5; i++)
- {
- if (containsDigit(genNumber, digitFromNumber(inputNumber, i)))
- {
- if (digitFromNumber(genNumber, i) == digitFromNumber(inputNumber, i))
- {
- hit++;
- }
- else
- {
- miss++;
- }
- }
- }
- switch (info)
- {
- case 'm':
- return miss;
- case 'h':
- return hit;
- default:
- return -1;
- }
- }
- /*
- Checks if the given digit contains in the given number
- input: number and one digit
- output: true if the given digit contains in the given number, otherwise, false
- */
- bool containsDigit(int number, int digit)
- {
- int curr_digit = 0;
- while (number != 0)
- {
- curr_digit = number % 10;
- if (curr_digit == digit)
- {
- return true;
- }
- number /= 10;
- }
- return false;
- }
- /*
- Printing difficulty selection, and return the rounds number according the selection
- input: -
- output: number of rounds
- */
- int selectDifficulty()
- {
- int diff = 0;
- printf("Please choose the game level:\n"
- "1 - Easy (20 rounds)\n"
- "2 - Moderate (15 rounds)\n"
- "3 - Hard (10 rounds)\n"
- "4 - Crazy (random number of rounds 5-25)\n");
- do {
- printf("Make a choice: ");
- scanf("%d", &diff);
- switch (diff)
- {
- case Easy:
- return ROUNDS_EASY;
- case Moderate:
- return ROUNDS_MODERATE;
- case Hard:
- return ROUNDS_HARD;
- case Crazy:
- isCrazy = true;
- return rand() % (25 - 5 + 1) + 5;
- default:
- diff = -1;
- break;
- }
- } while (diff == -1);
- }
- /*
- Asking for input from the user (input to guess the generated number)
- input: guesses left number (to print them before the input)
- output: number which user inputed
- */
- int getInput(int guessesLeft)
- {
- int i = 0, numInput = 0, digit = 0;
- bool validNum = false;
- do {
- numInput = 0;
- printf("Write your guess (only 1-6, no ENTER is needed)\n");
- if (isCrazy)
- {
- printf("CRAZY MODE!!!\n");
- }
- else
- {
- printf("%d guesses left\n", guessesLeft);
- }
- for (i = 0; i < 4; i++)
- {
- digit = getch();
- printf("%d", digit - '0');
- numInput = combine(numInput, digit - '0');
- }
- printf("\n");
- validNum = checkValid(numInput);
- if (!validNum)
- {
- printf("Only 1-6 and distinct digits are allowed, try again!\n");
- }
- } while (!validNum);
- return numInput;
- }
- /*
- Returns selected digit from the given number
- input: number and digitNumber (place of digit, for example: 5314 - (digitNumber 3 = digit 1))
- 5 3 1 4 - Digits
- 1 2 3 4 - Digits Number (place)
- */
- int digitFromNumber(int number, int digitNum)
- {
- int i = 0;
- for (i = 0; i < 4 - digitNum; i++)
- {
- number /= 10;
- }
- return number % 10;
- }
- /*
- Checks if in the given number each digit between 1-6 and doesn't repeating her self
- input: number
- output: true if all is ok, false, if something wrong
- */
- bool checkValid(int number)
- {
- bool rangeOK = false;
- bool distinctDigits = hasDistinctDigits(number);
- //extract number into digits
- int digit1 = digitFromNumber(number, 1);
- int digit2 = digitFromNumber(number, 2);
- int digit3 = digitFromNumber(number, 3);
- int digit4 = digitFromNumber(number, 4);
- //check if one of the digits doesn't between the allowed range (1-6)
- rangeOK = ((digit1 >= 1 && digit1 <= 6) && (digit2 >= 1 && digit2 <= 6) && (digit3 >= 1 && digit3 <= 6) && (digit4 >= 1 && digit4 <= 6));
- return rangeOK && distinctDigits;
- }
- /*
- Checks if the digits in the given number are distinct
- input: number
- output: true if disticnt, false if not
- */
- bool hasDistinctDigits(int number)
- {
- int numMask = 0, digitIdx = 0, curDigit = 0, digitMask = 0;
- int numDigits = (int) ceil(log10(number + 1));
- for (digitIdx = 0; digitIdx < numDigits; digitIdx++)
- {
- curDigit = (int)(number / pow(10, digitIdx)) % 10;
- digitMask = (int) pow(2, curDigit);
- if ((numMask & digitMask) > 0)
- {
- return false;
- }
- numMask = numMask | digitMask;
- }
- return true;
- }
- /*
- Generates random number according the rules (each digit is between 1 -> 6, each digit must be distinct)
- input: -
- output: randomized number
- */
- int generateNumber()
- {
- int fullNum = 0;
- do {
- fullNum = combine(combine(rand() % (6 - 1 + 1) + 1, rand() % (6 - 1 + 1) + 1), combine(rand() % (6 - 1 + 1) + 1, rand() % (6 - 1 + 1) + 1));
- } while (!hasDistinctDigits(fullNum));
- return fullNum;
- }
- /*
- Combines two numbers into one (12 &41 -> 1241)
- input: two numbers
- output: these two numbers together (as one number)
- */
- int combine(int a, int b)
- {
- int times = 1;
- while (times <= b)
- {
- times *= 10;
- }
- return a *times + b;
- }
- /*
- Prints program info, such as rules and tutorial
- input: -
- output: -
- */
- void printInfo()
- {
- printf("Welcome to \"MAGSHIMIM CODE-BREAKER\"!!!\n\n"
- "A secret password was chosen to protect the credit card of Pancratius,\n"
- "the descendant of Antiochus.\n"
- "Your mission is to stop Pancratius by revealing his secret password.\n\n"
- "The rules are as follows:\n"
- "1. In each round you try to guess the secret password (4 distinct digits)\n"
- "2. After every guess you'll receive two hints about the password\n"
- " HITS: The number of digits in your guess which were exactly right.\n"
- " MISSES: The number of digits in your guess which belongs to\n"
- " the password but were miss-placed.\n"
- "3. If you'll fail to guess the password after a certain number of rounds\n"
- " Pancratius will buy all the gifts for Hanukkah!!!\n\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement