Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.30 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. #define SIX 6
  7. #define FOUR 4
  8. #define TEN 10
  9. #define HUNDRED 100
  10. #define THOUSAND 1000
  11. #define TWENTY 20
  12. #define TWO 2
  13. #define FIFTEEN 15
  14. #define FIVE 5
  15. #define THREE 3
  16. #define TWENTYONE 21
  17.  
  18. void welcomeFunc();
  19. int difficultyFunc();
  20. int codeGen();
  21. void codeChecker(int code,int difficulty,int rounds);
  22. void hitMiss(int code , int uCode ,int rounds , int difficulty);
  23. int roundsFunc(int difficulty);
  24. void winLoseFunc(int winLose , int rounds , int orgRounds);
  25.  
  26. int main(void)
  27. {
  28.     int difficulty = 0;
  29.     int code = 0;
  30.     int rounds = 0;
  31.     int flag = 1;
  32.     char choice = 'a';
  33.     int flag2 = 1;
  34.     //declaring vars
  35.     srand (time(NULL));
  36.     //resetting seed every time we generate a number
  37.     while(flag)
  38.     {
  39.         flag =1;
  40.         flag2 = 1;
  41.         welcomeFunc();
  42.         //calling welcome function to print the welcoe page
  43.         difficulty = difficultyFunc();
  44.         //calling difficulty function to get difficulty from user
  45.         rounds = roundsFunc(difficulty);
  46.         //calling rounds function to calculate how many rounds the user deserves using the difficulty he entered
  47.         code = codeGen();
  48.         //calling code function to generate a code
  49.         codeChecker(code,difficulty,rounds);
  50.         //calling codeChecker function to take a code from the user,and call a function that checks how many hits and misses the user has
  51.         while(flag2)
  52.         {
  53.             printf("Would you like to play again? (y/n): ");
  54.             choice = getch();
  55.             printf("%c\n",choice);
  56.             if(choice != 'y' && choice != 'n')
  57.             {
  58.                 flag2 = 1;
  59.             }
  60.             else
  61.             {
  62.                 flag2 = 0;
  63.             }
  64.         }
  65.         if(choice=='y')
  66.         {
  67.             flag = 1;
  68.         }
  69.         else if(choice=='n')
  70.         {
  71.             flag = 0;
  72.             printf("\nBye Bye!");
  73.         }
  74.     }
  75.     return 0;
  76. }
  77.  
  78. /*
  79.     This function prints the welcome page
  80.     Input:
  81.         None
  82.     Output:
  83.         None
  84. */
  85. void welcomeFunc()
  86. {
  87.     printf("Welcome to MAGSHIMIM CODE-BREAKER!!!\n\n");
  88.     printf("A secret password was chosen to protect the credit card of Pancratius,\n");
  89.     printf("the descendant of Antiochus.\n");
  90.     printf("Your mission is to stop Pancratius by revealing his secret password.\n\n");
  91.     printf("The rules are as follows:\n");
  92.     printf("1. In each round you try to guess the secret password (4 distinct digits)\n");
  93.     printf("2. After every guess you'll receive two hints about the password\n");
  94.     printf("HITS:   The number of digits in your guess which were exactly right.\n");
  95.     printf("MISSES: The number of digits in your guess which belongs to\n");
  96.     printf("the password but were miss-placed.\n");
  97.     printf("3. If you'll fail to guess the password after a certain number of rounds\n");
  98.     printf("Pancratius will buy all the gifts for Hanukkah!!!\n\n");
  99.     printf("Please choose the game level:\n");
  100.     printf("1 - Easy (20 rounds)\n");
  101.     printf("2 - Moderate (15 rounds)\n");
  102.     printf("3 - Hard (10 rounds)\n");
  103.     printf("4 - Crazy (random number of rounds 5-25)\n");
  104. }
  105.  
  106. /*
  107.     This function takes difficultyLevel from user , calculate how many rounds he will have and return it to main function
  108.     Input:
  109.         None
  110.     Output:
  111.         difficulty level
  112. */
  113. int difficultyFunc()
  114. {
  115.     enum difficulty{Easy=1,Moderate,Hard,Crazy};
  116.     enum difficulty difficultyLevel;
  117.     int flag = 1;
  118.     int rounds = 0;
  119.     //declaring vars
  120.     while(flag)
  121.     {
  122.         printf("Make a choice: "); 
  123.         scanf("%d",&difficultyLevel);
  124.         getchar();
  125.         if(difficultyLevel<=FOUR && difficultyLevel>0)
  126.         {
  127.             flag = 0;
  128.         }
  129.         //if choice is invalid , asking to enter difficulty again
  130.     }
  131.     //getting difficulty from user
  132.     return difficultyLevel;
  133.     //returning difficulty to main function
  134. }
  135.  
  136. /*
  137.     Function checks how many rounds the user deserves using the difficulty he chose
  138.     Input:
  139.         difficulty
  140.     Output:
  141.         amonut of rounds
  142. */
  143. int roundsFunc(int difficulty)
  144. {
  145.     int rounds = 0;
  146.     //declaring vars
  147.     if(difficulty == 1)
  148.     {
  149.         rounds = TWENTY;
  150.     }
  151.     else if(difficulty == TWO)
  152.     {
  153.         rounds = FIFTEEN;
  154.     }
  155.     else if(difficulty == THREE)
  156.     {
  157.         rounds = TEN;
  158.     }
  159.     else if(difficulty == FOUR)
  160.     {
  161.         rounds = rand() % TWENTYONE + FIVE;
  162.     }
  163.     return rounds;
  164.     //returning amount of rounds to main function
  165. }
  166. /*
  167.     This function gets 3 random numbers from 1 to 6,puts them together(creates code) and return it to main function
  168.     Input:
  169.         None
  170.     Output:
  171.         Code
  172. */
  173. int codeGen()
  174. {
  175.     int firstNum = rand() %SIX + 1;
  176.     int secondNum = rand() %SIX + 1;
  177.     int thirdNum = rand() %SIX + 1;
  178.     int fourthNum = rand() %SIX + 1;
  179.     int code = 0;
  180.     //declaring vars
  181.     while(firstNum==secondNum || firstNum==thirdNum || firstNum==fourthNum)
  182.     {
  183.         firstNum = rand() %SIX + 1;
  184.     }
  185.     while(firstNum==secondNum || secondNum==thirdNum || secondNum==fourthNum)
  186.     {
  187.         secondNum = rand() % SIX + 1;
  188.     }
  189.     while(thirdNum==secondNum || firstNum==thirdNum || thirdNum==fourthNum)
  190.     {
  191.         thirdNum = rand() % SIX + 1;
  192.     }
  193.     while(fourthNum==secondNum || fourthNum==thirdNum || firstNum==fourthNum)
  194.     {
  195.         fourthNum = rand() % SIX + 1;
  196.     }
  197.     //making sure no numbers are repeating themselves in the code
  198.     code = firstNum + secondNum*TEN + thirdNum*HUNDRED + fourthNum*THOUSAND;
  199.     //making the code 1 number
  200.     return code;
  201.     //returnng code to main function
  202. }
  203.  
  204. /*
  205.     Function will get 4 digit code from user and check if its valid
  206.     Input:
  207.         Original generated code(our code)
  208.     Output:
  209.         the code that the user has entered
  210. */
  211. void codeChecker(int code,int difficulty,int rounds)
  212. {
  213.     char firstDigit = 'a';
  214.     char secondDigit = 'a';
  215.     char thirdDigit = 'a';
  216.     char fourthDigit = 'a';
  217.     int fDigit = 0;
  218.     int sDigit = 0;
  219.     int tDigit = 0;
  220.     int foDigit = 0;
  221.     int flag = 0;
  222.     int uCode = 0;
  223.     int winLose = 0;
  224.     //declaring vars
  225.     do{
  226.         printf("\nWrite your guess (only 1-6, no ENTER is needed)\n");     
  227.         if(difficulty!=FOUR)
  228.         {
  229.             printf("%d guesses left\n",rounds);
  230.         }
  231.         else
  232.         {
  233.             printf("CRAZY MODE!!!\n");
  234.         }
  235.         firstDigit = getch();
  236.         printf("%c",firstDigit);
  237.         secondDigit = getch();
  238.         printf("%c",secondDigit);
  239.         thirdDigit = getch();
  240.         printf("%c",thirdDigit);
  241.         fourthDigit = getch();
  242.         printf("%c",fourthDigit);
  243.         //getting code from user
  244.         fDigit = firstDigit - '0';         
  245.         sDigit = secondDigit - '0';
  246.         tDigit = thirdDigit - '0';
  247.         foDigit = fourthDigit - '0';   
  248.         //making the code an int(was char)
  249.         if(fDigit>SIX || fDigit<1)
  250.         {
  251.             printf("\nOnly 1-6 are allowed, try again!\n");
  252.             flag=1;
  253.         }
  254.         else if(sDigit>SIX || sDigit<1)
  255.         {
  256.             printf("\nOnly 1-6 are allowed, try again!\n");
  257.             flag=1;
  258.         }
  259.         else if(tDigit>SIX || tDigit<1)
  260.         {
  261.             printf("\nOnly 1-6 are allowed, try again!\n");
  262.             flag=1;
  263.         }
  264.         else if(foDigit>SIX || foDigit<1)
  265.         {
  266.             printf("\nOnly 1-6 are allowed, try again!\n");
  267.             flag=1;
  268.         }
  269.         //checking if numbers are smaller than 1 or bigger than six , if they are , loop will run again
  270.         else if(firstDigit==secondDigit || firstDigit==thirdDigit|| firstDigit==fourthDigit)
  271.         {
  272.             flag=1;
  273.         }
  274.         else if(secondDigit==thirdDigit || secondDigit==fourthDigit)
  275.         {
  276.             flag=1;
  277.         }
  278.         else if(thirdDigit==fourthDigit)
  279.         {
  280.             flag=1;
  281.         }
  282.         //checking if numbers are the same , if they are , loop will run again
  283.         else
  284.         {
  285.             flag=0;
  286.         }
  287.     }while(flag);
  288.     //loop will run once , then run again if flag = 1
  289.     uCode = fDigit*THOUSAND + sDigit*HUNDRED + tDigit*TEN + foDigit;
  290.     //putting the 4 digits together to 1 number
  291.     hitMiss(code,uCode,rounds,difficulty);
  292.     //calling hitMiss function to check how many hits and misses the user has
  293. }
  294.  
  295. /*
  296.     This function gets the original code we generated and the code the user entered and shows how many hits and misses he has
  297.     Input:
  298.         Original generated code(our code) , user code , amount of rounds , difficulty
  299.     Output:
  300.         None
  301. */
  302. void hitMiss(int code , int uCode ,int rounds , int difficulty)
  303. {
  304.     int hit = 0;
  305.     int miss = 0;
  306.     int foCode = code%TEN;
  307.     int tCode = (code%HUNDRED)/TEN;
  308.     int sCode = (code%THOUSAND)/HUNDRED;
  309.     int fCode = code/THOUSAND;
  310.     int foUCode = uCode%TEN;
  311.     int tUCode = (uCode%HUNDRED)/TEN;
  312.     int sUCode = (uCode%THOUSAND)/HUNDRED;
  313.     int fUCode = uCode/THOUSAND;
  314.     int winLose = 0;
  315.     int orgRounds = rounds;
  316.     //declaring vars
  317.     if(rounds!=0 && hit!=FOUR)
  318.     {
  319.         if(foUCode==foCode)
  320.         {
  321.             hit++;
  322.         }
  323.         if(foUCode==tCode || foUCode==sCode || foUCode == fCode)
  324.         {
  325.             miss++;
  326.         }
  327.         if(tUCode==tCode)
  328.         {
  329.             hit++;
  330.         }
  331.         if(tUCode==foCode || tUCode==sCode || tUCode == fCode)
  332.         {
  333.             miss++;
  334.         }
  335.         if(sUCode==sCode)
  336.         {
  337.             hit++;
  338.         }
  339.         if(sUCode==tCode || sUCode==foCode || sUCode == fCode)
  340.         {
  341.             miss++;
  342.         }
  343.         if(fUCode==fCode)
  344.         {
  345.             hit++;
  346.         }
  347.         if(fUCode==tCode || fUCode==sCode || fUCode == foCode)
  348.         {
  349.             miss++;
  350.         }
  351.         //checking how many misses and hits the user has
  352.         rounds--;
  353.         printf("\n\nYou got    %d HITS    %d MISSES.\n",hit,miss);
  354.         if(rounds!=0 && hit!=FOUR)
  355.         {
  356.             codeChecker(code,difficulty,rounds);
  357.         }
  358.     }
  359.     if(hit==FOUR || rounds == 0)
  360.     {
  361.         if(hit==FOUR)
  362.         {
  363.             winLose = 1;
  364.         }
  365.         else if(rounds==0 && hit!=FOUR)
  366.         {
  367.             winLose = 0;
  368.         }
  369.         winLoseFunc(winLose,rounds,orgRounds);
  370.         //calling winLose function to print a message if the user won/lost
  371.     }
  372. }
  373.  
  374. /*
  375.     Function prints a winning/losing message
  376.     Input:
  377.         if player won or lost
  378.     Output:
  379.         None
  380. */
  381. void winLoseFunc(int winLose , int rounds , int orgRounds)
  382. {
  383.     rounds--;
  384.     if(winLose == 1)
  385.     {
  386.         printf("\nYOU WON!\n");
  387.         printf("It took you only %d guesses , you are a professional code breaker!\n",orgRounds-rounds);
  388.         printf("  ________                        .___        ____.          ___.     ._. ._.       ___    \n");
  389.         printf(" /  _____/    ____     ____     __| _/       |    |   ____   \_ |__   | | | |   /\  \  \   \n");
  390.         printf("/   \  ___   /  _ \   /  _ \   / __ |        |    |  /  _ \   | __ \  | | | |   \/   \  \  \n");
  391.         printf("\    \_\  \ (  <_> ) (  <_> ) / /_/ |    /\__|    | (  <_> )  | \_\ \  \|  \|   /\    )  ) \n");
  392.         printf(" \______  /  \____/   \____/  \____ |    \________|  \____/   |___  /  __  __   \/   /  /  \n");
  393.         printf("        \/                         \/                             \/   \/  \/       /__/   \n");
  394.  
  395.     }
  396.     else if(winLose == 0)
  397.     {
  398.         printf("\nYOU LOST!\n");
  399.         printf("__________       .___       __________           ___ \n");
  400.         printf("\______   \      |   |      \______   \   /\    /  / \n");
  401.         printf(" |       _/      |   |       |     ___/   \/   /  /  \n");
  402.         printf(" |    |   \      |   |       |    |       /\  (  (   \n");
  403.         printf(" |____|_  /  /\  |___|  /\   |____|       \/   \  \  \n");
  404.         printf("        \/   \/         \/                      \__\ \n");
  405.     }
  406. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement