<mosaic.cnfolio.com>
PROGRAM
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. /*
  4. This coursework demonstrates how a Pokemon Go player can gain the maximum XP during a Luck Egg bonus timer for different Pokemons
  5.  
  6. I did some research and built a formula, and this program assumes that the following formula is used for calculating this
  7.  
  8. Formula/Logic of calculations:
  9.     - A Lucky Egg bonus timer is valid for 30 minutes
  10.     - Experience is gained when a Pokemon evolves. One Pokemon can be evolved at one time
  11.     - One evolution takes 22 seconds
  12.     - Each Pokemon has a required number of candies that are used to evolve
  13.     - Every time a Pokemon is evolved, the user gains 1000XP
  14.  
  15. This logic has been made by analyzing the flow on this website, and other related articles: https://www.pidgeycalc.com/
  16. The number of required candies for each pokemon to evolve has been taken from this link: https://www.pidgeycalc.com/data/pokemon.json
  17.  
  18. Pseudo code start:
  19.    
  20.     Declare global variables to share information between multiple functions
  21.     int pokemonChosen;
  22.     int numPokemons;
  23.     int numCandies;
  24.    
  25.     Declare function names and types
  26.     1. function to collect use data
  27.     2. function to select a pokemon
  28.     3. function to calculate maximum XP that can be gained
  29.    
  30.     Declare a 2D array that store Pokemon names and the corresponding number of candies required to evolve
  31.    
  32.     int main() {
  33.         Print heading
  34.        
  35.         call the function to collect input values
  36.        
  37.         call the function to calculate the maximum XP that can be gained using the values collected above
  38.        
  39.         Print result
  40.     }
  41.    
  42.     void collectValues() {
  43.         call the function to get the pokemon selection
  44.        
  45.         Perform validation to check if the pokemon selection number is valid
  46.        
  47.         if (pokemonChosen < 1 || pokemonChosen > 4) {
  48.             print error that the number entered is invalid
  49.            
  50.             call the collectValues() function again to get the pokemonSelection again
  51.         }
  52.         else {
  53.             get integer inputs for the number of pokemons and number of candies
  54.            
  55.             show the pokemon name in each headings for bettwe user experience using the array declared at the beginning
  56.         }
  57.     }
  58.    
  59.     int pokemonSelection() {
  60.         Print heading for selection
  61.        
  62.         Declare local variable i and j
  63.         i is the index that will be used to iterate, and j is the number to display before each pokemon to get a selection
  64.         for ( i = 0; i < 4; i++ ) {
  65.             loop through the pokemonArray and print the name and number of each pokemon line by line
  66.         }
  67.         get integer input from the user and store it in the global variable for pokemonChosen
  68.     }
  69.    
  70.     float maxXp(int numPokemons, int numCandies, int pokemonChosen) {
  71.         initialize variables for totalXp and timesEvolved
  72.        
  73.         get the candiesRequiredToEvolve from the pokemon array, and convert it into a float since it is originally of type char
  74.        
  75.         if (number of candies entered by the user < candiesRequiredToEvolve) {
  76.             print error that candies are not enough, and break stop the function by returning 0
  77.         }
  78.         else if (numPokemons < 1) {
  79.             print error that the user has not entered any pokemons and stop the function by returning 0
  80.         }
  81.         else {
  82.             perform calculation here
  83.            
  84.             initialize a timer variable that will store the time after each evolution
  85.            
  86.             use a while loop to iterate till the timer can reach a maximum of 30 minutes (1800 seconds)
  87.             while (timeCounter <= 1800) {
  88.                
  89.                 on each iteration check if the pokemons available count has reached
  90.                 if (numPokemons == 0) {
  91.                     break the while loop
  92.                 }
  93.                 else {
  94.                     check if there are enough candies available in this iteration to perform an evolution
  95.                     if (numCandies < candiesRequiredToEvolve) {
  96.                         break the while loop
  97.                     }
  98.                     else {
  99.                         decrease pokemon value by one
  100.                         decrease the number of candies available by the amount of candies required to evolve
  101.                         increase the evolutions counter by 1
  102.                         increment the time counter by 22 seconds, and go to the next iteration of the while loop
  103.                     }
  104.                 }
  105.             }
  106.         }
  107.     }
  108.    
  109. Pseudo code end 
  110. */
  111.  
  112.  
  113. /*Declaring global variable so that they can be used througout the program lifecycle between different functions*/
  114. int pokemonChosen;
  115. int numPokemons;
  116. int numCandies;
  117.  
  118. /*Function declarations required by C*/
  119. void collectValues();
  120. int pokemonSelection();
  121. float maxXp(int numPokemons, int numCandies, int pokemonChosen);
  122.  
  123. /*Declaring a 2 Dimensional character array for generic use. This array contains the Pokemons we want to show. To add more Pokemons, simply add them in this array*/
  124. /*pokemonArray[] = {{"pokemonName, numberOfCandiesRequiredToEvolve"}}*/
  125. char *pokemonArray[4][2] = {{"Dragonair","100"}, {"Pidgey","12"}, {"Rattata","25"}, {"Zubat","50"}};
  126.  
  127. int main()
  128. {
  129.     /*Keeping the main function clean for better readability and using it mainly for logic creation and program flow control by splitting functionalities into smaller functions.*/
  130.    
  131.     /*Print the Heading*/
  132.     printf("Pokemon Bonus Calculator!\n");
  133.    
  134.     /*Collect input values from the user. See function: collectValues()*/
  135.     collectValues();
  136.    
  137.     /*Use the input values collected from the previous function to calculate the maximum XP results. See function "maxXp"*/
  138.     int gainedXp = maxXp(numPokemons, numCandies, pokemonChosen);
  139.    
  140.     /*Print the maximum XP that can be earner with the input values*/
  141.     printf("\nMaximum XP: %d\n", gainedXp);
  142.     return 0;
  143. }
  144.  
  145. void collectValues() {
  146.    
  147.     /*First step is the get the Pokemon the user wants to use. The following function gets that done. See function: pokemonSelection()*/
  148.     pokemonChosen = pokemonSelection();
  149.    
  150.    
  151.     /*After getting the pokemon from the function above, we perform a validation check to see if the number is between 1 and 4, since we only have 4 pokemons*/
  152.     if (pokemonChosen < 1 || pokemonChosen > 4) {
  153.        
  154.         /*If the number is not between 1 and 4, we give a validation error, and call this function again, since calling this function again will run the pokemonSelection() function again to retry getting the right pokemon*/
  155.         printf("\nSelect a number between 1 and 4\n\n");
  156.         collectValues();
  157.     }
  158.     else {
  159.         /*If the selected pokemon is between 1 and 4, we get the other values from the user*/
  160.        
  161.         /*We print the heading of the first input to get the number of the chosen pokemon. In pokemonArray[pokemonChosen-1][0], the [pokemonChosen-1] is used to get the chosen pokemon index. We have to subtract 1 from the chosen pokemon because array index starts from 0, not 1. And we get the 0 index from the second dimension to get the name, and include it in the heading */
  162.         printf("\nEnter number of %s Pokemons:", pokemonArray[pokemonChosen-1][0]);
  163.         /*We get an integer value for the number of pokemons, and assign the number to its corresponding global variable*/
  164.         scanf("%d", &numPokemons);
  165.        
  166.         /*The logic behind collection the number of candies is exacty the same as described above for collecting the number of pokemons logic*/
  167.         printf("\nEnter number of %s Candies:", pokemonArray[pokemonChosen-1][0]);
  168.         scanf("%d", &numCandies);
  169.     }
  170.    
  171.    
  172.     /*printf("\nvalues: %d %d\n", numPokemons, numCandies);*/
  173. }
  174.  
  175. int pokemonSelection() {
  176.     /*Get the Pokemon selection from the predefined array of pokemons using a for loop. Display the names of the Pokemons with a number*/
  177.    
  178.     /*Heading*/
  179.     printf("\nEnter the number of your Pokemon:\n");
  180.    
  181.     /*Local variables to display the list of pokemons*/
  182.     int i, j;
  183.    
  184.     /*for loop to iterate through the array of pokemons to display their names. Since we have 4 pokemons in the array, we give the "i < 4" argument to the loop*/
  185.     for ( i = 0; i < 4; i++ ) {
  186.        
  187.         /*Since i is initialized as 0 because the array starts from zero index, we need to add 1 to it to show it to the user for proper selection*/
  188.         j = i + 1;
  189.        
  190.         /*The following line gets the number to show, using the variable j, and gets the index of the array using the variable i. With the "i" index in the array, we get the first element from the second dimension of the array, which is the pokemon name to display*/
  191.         printf("%d. %s\n", j, pokemonArray[i][0]);
  192.     }
  193.    
  194.     /*The following line gets an integer input from the user, and assign the value to the global pokemonChose variable, so that it can be used for the next function*/
  195.     scanf("%d", &pokemonChosen);
  196.    
  197.     /*The selection above is the most crucial as this selection will be used for further calculations*/
  198.     return pokemonChosen;
  199. }
  200.  
  201. float maxXp(int numPokemons, int numCandies, int pokemonChosen) {
  202.     /*This function calculates the maximum experience that can be gained using the formula mentioned in the pseudo code at the top. The function declaration takes in the inputs that were collected*/
  203.    
  204.     /*Initialize the local variables required for calculation*/
  205.    
  206.     /*The total XP gained. This is the return value for this function at the end*/
  207.     float totalXp = 0;
  208.    
  209.     /*We keep a counter to not down the times pokemons are evolved to calculate the max XP*/
  210.     int timesEvolved = 0;
  211.    
  212.     /*We convert the candiesRequiredToEvolve from the pokemonArray from char type to a float so that it can be used in numeric calculations. It is being kept as a float so that even decimal values can be used in the future*/
  213.     float candiesRequiredToEvolve = atof(pokemonArray[pokemonChosen-1][1]);
  214.    
  215.     /*The following code is where the calculations happen, we also perform validation checks and show relevat error messages here*/
  216.     if (numCandies < candiesRequiredToEvolve) {
  217.         /*If the number of candies the user entered is less than the cadies required to evolve for the selected pokemon, print the following error*/
  218.         printf("\nNot enough candies to evolve\n");
  219.        
  220.         /*Since we don't want the function to perform any more calculation, we return the function with 0, and stop right here*/
  221.         return 0;
  222.     }
  223.     else if (numPokemons < 1) {
  224.         /*In the number of candies are valid, but the user did not enter any pokemon, we show them the following message and stop the function by returning 0*/
  225.         printf("\nNo Pokemons available\n");
  226.         return 0;
  227.     }
  228.     else {
  229.         /*If all validation has succeeded, we perform the calculation*/
  230.        
  231.         /*We initialize the timer that will be incremented for the while loop below*/
  232.         int timeCounter = 0;
  233.        
  234.         /*Since a Luck Egg bonus time is for 30 minutes, it means it is 1800 seconds. One evolution takes 22 seconds. So on each evolution, we increment the timner by 22, and tell the while loop to only run till a maximum of 1800 seconds, which is half an hour*/
  235.         while (timeCounter <= 1800) {
  236.             /*We start the iterations for counting evolutions*/
  237.            
  238.             if (numPokemons == 0) {
  239.                 /*If we have reached the end of the number of pokemons, break the while loop as no more evolutions can occur*/
  240.                 break;
  241.             }
  242.             else {
  243.                 /*Coming in the else statement means that there are still pokemons available that can be evolved*/
  244.                
  245.                 if (numCandies < candiesRequiredToEvolve) {
  246.                     /*Next we check if there are enough candies left to evolve an available pokemon. If not, then break the while loop here*/
  247.                     break;
  248.                 }
  249.                 else {
  250.                     /*If we have pokemons, and enough candies, perform an evolution*/
  251.                    
  252.                     /*Decrease the number of pokemons by 1*/
  253.                     numPokemons--;
  254.                    
  255.                     /*Decrease the number of candies available by the number of candies required to evolve */
  256.                     numCandies -= candiesRequiredToEvolve;
  257.                    
  258.                     /*Increase the timesEvolved counter by 1*/
  259.                     timesEvolved++;
  260.                    
  261.                     /*Increase the timer by 22 seconds, and go to the next iteration of the while loop*/
  262.                     timeCounter += 22;
  263.                 }
  264.             }
  265.         }
  266.     }
  267.    
  268.     /*Print the number of times pokemons have evolved accoring to the inputs provided*/
  269.     printf("\nTimes Evolved: %d\n", timesEvolved);
  270.    
  271.     /*During a Lucky Egg bonus timer, the user gets 1000 XP on each evolution, so multiply the times evolved by 1000*/
  272.     totalXp = timesEvolved * 1000;
  273.    
  274.     /*Return the XP gained*/
  275.     return totalXp;
  276.  }