SHARE
TWEET

Bulls n Cows

FouhChainz Nov 19th, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. #define TEST true     // If true run tests (only), else run program
  7. #define DEBUG true    // If true, print number to guess to make debugging easier
  8.  
  9. // ----------- Declare functions used in main ----------
  10.  
  11. // Generates a 4-digit random number with no repeated digits
  12. // (digits in range 1-9)
  13. int get_random_4digit();
  14.  
  15. // Returns number of bulls in guessed number
  16. int count_bulls(int guess, int answer);
  17.  
  18. // Returns number of bulls and cows in guessed number
  19. int count_cows_and_bulls(int guess, int answer);
  20.  
  21. // Get input from player
  22. int get_player_guess();
  23.  
  24. // Testing of functions, see far below
  25. void test();
  26.  
  27. int n_digits(int number);
  28.  
  29. bool digit_is_in(int number, int digit);
  30.  
  31. int rand_nr1;
  32. int rand_nr2;
  33. int rand_nr3;
  34. int rand_nr4;
  35. int answer = 0;
  36. int total= 0;
  37.  
  38. /*
  39.  *  Program starts here
  40.  */
  41. int main(void) {
  42.  
  43.     srand((int) time(NULL));     // Init random number generator
  44.     answer = get_random_4digit;
  45.     printf("Answer is %d\n", total);
  46.     /*if (TEST) {
  47.         test();
  48.         return 0;
  49.     }
  50.  
  51.     int answer = get_random_4digit();
  52.     if (DEBUG) {
  53.         printf("Answer is %d\n", answer);
  54.     }
  55.  
  56.     printf("Welcome to Bulls and Cows\n");
  57.     printf("Try to guess a 4 digit number with digits 1-9\n");
  58.     printf("and no repeating digits (-1 to abort).\n\n");
  59.     printf("Bulls = correct digit(s) in correct positions.\n");
  60.     printf("Cows = correct digit(s).\n\n");
  61.  
  62.     bool aborted = false;
  63.     int n_guess = 0;
  64.     int guess = 0;
  65.     int bulls = 0;
  66.     int cows = 0;
  67.  
  68.     // ----- The game loop ------
  69.  
  70.     // TODO Use functions (and more) to implement the game
  71.     // TODO Try to sketch one round, then surround with a loop.
  72.  
  73.     // --- End game loop --------
  74.  
  75.     if (aborted) {
  76.         printf("Game aborted\n");
  77.     } else {
  78.         printf("Done, number was %d and you needed %d guesses\n", answer, n_guess);
  79.     }
  80. */
  81.     return 0;
  82. }
  83.  
  84. // ------- Functions definitions --------------------------------
  85.  
  86. // TODO All the function definitions here
  87.  
  88. //rand_nr = sträng? köra index(i) på string.at?, använda string.find(number)
  89.  
  90. // This one's for free...
  91. int get_player_guess() {
  92.     int guess;
  93.     printf("Guess > ");
  94.     scanf("%d", &guess);
  95.     return guess;
  96. }
  97.  
  98. int n_digits(int number){
  99.     int count = 0;
  100.     if (number > 0)
  101.     {
  102.         number = number %10;
  103.         count ++;
  104.     }
  105.     return count;
  106. }
  107.  
  108. bool digit_is_in(int number, int digit){
  109.  
  110.  
  111. }
  112.  
  113. int get_random_4digit(){
  114.     rand_nr1 = rand() % 9 + 1;
  115.     rand_nr2 = rand() % 9 + 1;
  116.     rand_nr3 = rand() % 9 + 1;
  117.     rand_nr4 = rand() % 9 + 1;
  118.     return total = rand_nr1*1000 + rand_nr2*100 + rand_nr3*10 + rand_nr4*1;
  119.  
  120. }
  121.  
  122. // Returns number of bulls in guessed number
  123. int count_bulls(int guess, int answer){
  124.  
  125.     //if guess at [x] == rand_nr then bull++
  126.  
  127. }
  128.  
  129. // Returns number of bulls and cows in guessed number
  130. int count_cows_and_bulls(int guess, int answer){
  131.  
  132.     //
  133.     // cows = found -- bulls;
  134.  
  135. }
  136.  
  137. // ---------------- The function to do all tests --------------------
  138.  
  139. #define EQUALS(v1, v2) printf((v1) == (v2) ? "true\n" : "false\n")
  140.  
  141. void test() {
  142.  
  143.     // TODO Uncomment on at the time and test
  144.     /*
  145.     EQUALS(n_digits(123), 3);
  146.     EQUALS(n_digits(12345), 5);
  147.     EQUALS(n_digits(1023945), 7);
  148.  
  149.     EQUALS(get_digit_at_index(12345, 0), 1);
  150.     EQUALS(get_digit_at_index(12345, 2), 3);
  151.     EQUALS(get_digit_at_index(12345, 4), 5);
  152.  
  153.     printf("Random answer %d\n", get_random_4digit());
  154.     printf("Random answer %d\n", get_random_4digit());
  155.     printf("Random answer %d\n", get_random_4digit());
  156.  
  157.     EQUALS(digit_is_in(2637, 2), true);
  158.     EQUALS(!digit_is_in(2637, 4), true);
  159.  
  160.     EQUALS(count_bulls(1807, 7810), 1);
  161.     EQUALS(count_bulls(2647, 2837), 2);
  162.  
  163.     EQUALS(count_cows_and_bulls(1807, 7810), 4);
  164.     EQUALS(count_cows_and_bulls(2647, 2837), 2);
  165.     */
  166. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top