Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- #include <stdlib.h>
- #include <time.h>
- #define SIX 6
- #define FOUR 4
- #define TEN 10
- #define HUNDRED 100
- #define THOUSAND 1000
- #define TWENTY 20
- #define TWO 2
- #define FIFTEEN 15
- #define FIVE 5
- #define THREE 3
- #define TWENTYONE 21
- void welcomeFunc();
- int difficultyFunc();
- int codeGen();
- void codeChecker(int code,int difficulty,int rounds);
- void hitMiss(int code , int uCode ,int rounds , int difficulty);
- int roundsFunc(int difficulty);
- void winLoseFunc(int winLose , int rounds , int orgRounds);
- int main(void)
- {
- int difficulty = 0;
- int code = 0;
- int rounds = 0;
- int flag = 1;
- char choice = 'a';
- int flag2 = 1;
- //declaring vars
- srand (time(NULL));
- //resetting seed every time we generate a number
- while(flag)
- {
- flag =1;
- flag2 = 1;
- welcomeFunc();
- //calling welcome function to print the welcoe page
- difficulty = difficultyFunc();
- //calling difficulty function to get difficulty from user
- rounds = roundsFunc(difficulty);
- //calling rounds function to calculate how many rounds the user deserves using the difficulty he entered
- code = codeGen();
- //calling code function to generate a code
- codeChecker(code,difficulty,rounds);
- //calling codeChecker function to take a code from the user,and call a function that checks how many hits and misses the user has
- while(flag2)
- {
- printf("Would you like to play again? (y/n): ");
- choice = getch();
- printf("%c\n",choice);
- if(choice != 'y' && choice != 'n')
- {
- flag2 = 1;
- }
- else
- {
- flag2 = 0;
- }
- }
- if(choice=='y')
- {
- flag = 1;
- }
- else if(choice=='n')
- {
- flag = 0;
- printf("\nBye Bye!");
- }
- }
- return 0;
- }
- /*
- This function prints the welcome page
- Input:
- None
- Output:
- None
- */
- void welcomeFunc()
- {
- printf("Welcome to MAGSHIMIM CODE-BREAKER!!!\n\n");
- printf("A secret password was chosen to protect the credit card of Pancratius,\n");
- printf("the descendant of Antiochus.\n");
- printf("Your mission is to stop Pancratius by revealing his secret password.\n\n");
- printf("The rules are as follows:\n");
- printf("1. In each round you try to guess the secret password (4 distinct digits)\n");
- printf("2. After every guess you'll receive two hints about the password\n");
- printf("HITS: The number of digits in your guess which were exactly right.\n");
- printf("MISSES: The number of digits in your guess which belongs to\n");
- printf("the password but were miss-placed.\n");
- printf("3. If you'll fail to guess the password after a certain number of rounds\n");
- printf("Pancratius will buy all the gifts for Hanukkah!!!\n\n");
- printf("Please choose the game level:\n");
- printf("1 - Easy (20 rounds)\n");
- printf("2 - Moderate (15 rounds)\n");
- printf("3 - Hard (10 rounds)\n");
- printf("4 - Crazy (random number of rounds 5-25)\n");
- }
- /*
- This function takes difficultyLevel from user , calculate how many rounds he will have and return it to main function
- Input:
- None
- Output:
- difficulty level
- */
- int difficultyFunc()
- {
- enum difficulty{Easy=1,Moderate,Hard,Crazy};
- enum difficulty difficultyLevel;
- int flag = 1;
- int rounds = 0;
- //declaring vars
- while(flag)
- {
- printf("Make a choice: ");
- scanf("%d",&difficultyLevel);
- getchar();
- if(difficultyLevel<=FOUR && difficultyLevel>0)
- {
- flag = 0;
- }
- //if choice is invalid , asking to enter difficulty again
- }
- //getting difficulty from user
- return difficultyLevel;
- //returning difficulty to main function
- }
- /*
- Function checks how many rounds the user deserves using the difficulty he chose
- Input:
- difficulty
- Output:
- amonut of rounds
- */
- int roundsFunc(int difficulty)
- {
- int rounds = 0;
- //declaring vars
- if(difficulty == 1)
- {
- rounds = TWENTY;
- }
- else if(difficulty == TWO)
- {
- rounds = FIFTEEN;
- }
- else if(difficulty == THREE)
- {
- rounds = TEN;
- }
- else if(difficulty == FOUR)
- {
- rounds = rand() % TWENTYONE + FIVE;
- }
- return rounds;
- //returning amount of rounds to main function
- }
- /*
- This function gets 3 random numbers from 1 to 6,puts them together(creates code) and return it to main function
- Input:
- None
- Output:
- Code
- */
- int codeGen()
- {
- int firstNum = rand() %SIX + 1;
- int secondNum = rand() %SIX + 1;
- int thirdNum = rand() %SIX + 1;
- int fourthNum = rand() %SIX + 1;
- int code = 0;
- //declaring vars
- while(firstNum==secondNum || firstNum==thirdNum || firstNum==fourthNum)
- {
- firstNum = rand() %SIX + 1;
- }
- while(firstNum==secondNum || secondNum==thirdNum || secondNum==fourthNum)
- {
- secondNum = rand() % SIX + 1;
- }
- while(thirdNum==secondNum || firstNum==thirdNum || thirdNum==fourthNum)
- {
- thirdNum = rand() % SIX + 1;
- }
- while(fourthNum==secondNum || fourthNum==thirdNum || firstNum==fourthNum)
- {
- fourthNum = rand() % SIX + 1;
- }
- //making sure no numbers are repeating themselves in the code
- code = firstNum + secondNum*TEN + thirdNum*HUNDRED + fourthNum*THOUSAND;
- //making the code 1 number
- return code;
- //returnng code to main function
- }
- /*
- Function will get 4 digit code from user and check if its valid
- Input:
- Original generated code(our code)
- Output:
- the code that the user has entered
- */
- void codeChecker(int code,int difficulty,int rounds)
- {
- char firstDigit = 'a';
- char secondDigit = 'a';
- char thirdDigit = 'a';
- char fourthDigit = 'a';
- int fDigit = 0;
- int sDigit = 0;
- int tDigit = 0;
- int foDigit = 0;
- int flag = 0;
- int uCode = 0;
- int winLose = 0;
- //declaring vars
- do{
- printf("\nWrite your guess (only 1-6, no ENTER is needed)\n");
- if(difficulty!=FOUR)
- {
- printf("%d guesses left\n",rounds);
- }
- else
- {
- printf("CRAZY MODE!!!\n");
- }
- firstDigit = getch();
- printf("%c",firstDigit);
- secondDigit = getch();
- printf("%c",secondDigit);
- thirdDigit = getch();
- printf("%c",thirdDigit);
- fourthDigit = getch();
- printf("%c",fourthDigit);
- //getting code from user
- fDigit = firstDigit - '0';
- sDigit = secondDigit - '0';
- tDigit = thirdDigit - '0';
- foDigit = fourthDigit - '0';
- //making the code an int(was char)
- if(fDigit>SIX || fDigit<1)
- {
- printf("\nOnly 1-6 are allowed, try again!\n");
- flag=1;
- }
- else if(sDigit>SIX || sDigit<1)
- {
- printf("\nOnly 1-6 are allowed, try again!\n");
- flag=1;
- }
- else if(tDigit>SIX || tDigit<1)
- {
- printf("\nOnly 1-6 are allowed, try again!\n");
- flag=1;
- }
- else if(foDigit>SIX || foDigit<1)
- {
- printf("\nOnly 1-6 are allowed, try again!\n");
- flag=1;
- }
- //checking if numbers are smaller than 1 or bigger than six , if they are , loop will run again
- else if(firstDigit==secondDigit || firstDigit==thirdDigit|| firstDigit==fourthDigit)
- {
- flag=1;
- }
- else if(secondDigit==thirdDigit || secondDigit==fourthDigit)
- {
- flag=1;
- }
- else if(thirdDigit==fourthDigit)
- {
- flag=1;
- }
- //checking if numbers are the same , if they are , loop will run again
- else
- {
- flag=0;
- }
- }while(flag);
- //loop will run once , then run again if flag = 1
- uCode = fDigit*THOUSAND + sDigit*HUNDRED + tDigit*TEN + foDigit;
- //putting the 4 digits together to 1 number
- hitMiss(code,uCode,rounds,difficulty);
- //calling hitMiss function to check how many hits and misses the user has
- }
- /*
- This function gets the original code we generated and the code the user entered and shows how many hits and misses he has
- Input:
- Original generated code(our code) , user code , amount of rounds , difficulty
- Output:
- None
- */
- void hitMiss(int code , int uCode ,int rounds , int difficulty)
- {
- int hit = 0;
- int miss = 0;
- int foCode = code%TEN;
- int tCode = (code%HUNDRED)/TEN;
- int sCode = (code%THOUSAND)/HUNDRED;
- int fCode = code/THOUSAND;
- int foUCode = uCode%TEN;
- int tUCode = (uCode%HUNDRED)/TEN;
- int sUCode = (uCode%THOUSAND)/HUNDRED;
- int fUCode = uCode/THOUSAND;
- int winLose = 0;
- int orgRounds = rounds;
- //declaring vars
- if(rounds!=0 && hit!=FOUR)
- {
- if(foUCode==foCode)
- {
- hit++;
- }
- if(foUCode==tCode || foUCode==sCode || foUCode == fCode)
- {
- miss++;
- }
- if(tUCode==tCode)
- {
- hit++;
- }
- if(tUCode==foCode || tUCode==sCode || tUCode == fCode)
- {
- miss++;
- }
- if(sUCode==sCode)
- {
- hit++;
- }
- if(sUCode==tCode || sUCode==foCode || sUCode == fCode)
- {
- miss++;
- }
- if(fUCode==fCode)
- {
- hit++;
- }
- if(fUCode==tCode || fUCode==sCode || fUCode == foCode)
- {
- miss++;
- }
- //checking how many misses and hits the user has
- rounds--;
- printf("\n\nYou got %d HITS %d MISSES.\n",hit,miss);
- if(rounds!=0 && hit!=FOUR)
- {
- codeChecker(code,difficulty,rounds);
- }
- }
- if(hit==FOUR || rounds == 0)
- {
- if(hit==FOUR)
- {
- winLose = 1;
- }
- else if(rounds==0 && hit!=FOUR)
- {
- winLose = 0;
- }
- winLoseFunc(winLose,rounds,orgRounds);
- //calling winLose function to print a message if the user won/lost
- }
- }
- /*
- Function prints a winning/losing message
- Input:
- if player won or lost
- Output:
- None
- */
- void winLoseFunc(int winLose , int rounds , int orgRounds)
- {
- rounds--;
- if(winLose == 1)
- {
- printf("\nYOU WON!\n");
- printf("It took you only %d guesses , you are a professional code breaker!\n",orgRounds-rounds);
- printf(" ________ .___ ____. ___. ._. ._. ___ \n");
- printf(" / _____/ ____ ____ __| _/ | | ____ \_ |__ | | | | /\ \ \ \n");
- printf("/ \ ___ / _ \ / _ \ / __ | | | / _ \ | __ \ | | | | \/ \ \ \n");
- printf("\ \_\ \ ( <_> ) ( <_> ) / /_/ | /\__| | ( <_> ) | \_\ \ \| \| /\ ) ) \n");
- printf(" \______ / \____/ \____/ \____ | \________| \____/ |___ / __ __ \/ / / \n");
- printf(" \/ \/ \/ \/ \/ /__/ \n");
- }
- else if(winLose == 0)
- {
- printf("\nYOU LOST!\n");
- printf("__________ .___ __________ ___ \n");
- printf("\______ \ | | \______ \ /\ / / \n");
- printf(" | _/ | | | ___/ \/ / / \n");
- printf(" | | \ | | | | /\ ( ( \n");
- printf(" |____|_ / /\ |___| /\ |____| \/ \ \ \n");
- printf(" \/ \/ \/ \__\ \n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement