Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdbool.h>
- #include <stdlib.h>
- #include <time.h>
- #define TEST true // If true run tests (only), else run program
- #define DEBUG true // If true, print number to guess to make debugging easier
- // ----------- Declare functions used in main ----------
- // Generates a 4-digit random number with no repeated digits
- // (digits in range 1-9)
- int get_random_4digit();
- // Returns number of bulls in guessed number
- int count_bulls(int guess, int answer);
- // Returns number of bulls and cows in guessed number
- int count_cows_and_bulls(int guess, int answer);
- // Get input from player
- int get_player_guess();
- // Testing of functions, see far below
- void test();
- int n_digits(int number);
- bool digit_is_in(int number, int digit);
- int rand_nr1;
- int rand_nr2;
- int rand_nr3;
- int rand_nr4;
- int answer = 0;
- int total= 0;
- /*
- * Program starts here
- */
- int main(void) {
- srand((int) time(NULL)); // Init random number generator
- answer = get_random_4digit;
- printf("Answer is %d\n", total);
- /*if (TEST) {
- test();
- return 0;
- }
- int answer = get_random_4digit();
- if (DEBUG) {
- printf("Answer is %d\n", answer);
- }
- printf("Welcome to Bulls and Cows\n");
- printf("Try to guess a 4 digit number with digits 1-9\n");
- printf("and no repeating digits (-1 to abort).\n\n");
- printf("Bulls = correct digit(s) in correct positions.\n");
- printf("Cows = correct digit(s).\n\n");
- bool aborted = false;
- int n_guess = 0;
- int guess = 0;
- int bulls = 0;
- int cows = 0;
- // ----- The game loop ------
- // TODO Use functions (and more) to implement the game
- // TODO Try to sketch one round, then surround with a loop.
- // --- End game loop --------
- if (aborted) {
- printf("Game aborted\n");
- } else {
- printf("Done, number was %d and you needed %d guesses\n", answer, n_guess);
- }
- */
- return 0;
- }
- // ------- Functions definitions --------------------------------
- // TODO All the function definitions here
- //rand_nr = sträng? köra index(i) på string.at?, använda string.find(number)
- // This one's for free...
- int get_player_guess() {
- int guess;
- printf("Guess > ");
- scanf("%d", &guess);
- return guess;
- }
- int n_digits(int number){
- int count = 0;
- if (number > 0)
- {
- number = number %10;
- count ++;
- }
- return count;
- }
- bool digit_is_in(int number, int digit){
- }
- int get_random_4digit(){
- rand_nr1 = rand() % 9 + 1;
- rand_nr2 = rand() % 9 + 1;
- rand_nr3 = rand() % 9 + 1;
- rand_nr4 = rand() % 9 + 1;
- return total = rand_nr1*1000 + rand_nr2*100 + rand_nr3*10 + rand_nr4*1;
- }
- // Returns number of bulls in guessed number
- int count_bulls(int guess, int answer){
- //if guess at [x] == rand_nr then bull++
- }
- // Returns number of bulls and cows in guessed number
- int count_cows_and_bulls(int guess, int answer){
- //
- // cows = found -- bulls;
- }
- // ---------------- The function to do all tests --------------------
- #define EQUALS(v1, v2) printf((v1) == (v2) ? "true\n" : "false\n")
- void test() {
- // TODO Uncomment on at the time and test
- /*
- EQUALS(n_digits(123), 3);
- EQUALS(n_digits(12345), 5);
- EQUALS(n_digits(1023945), 7);
- EQUALS(get_digit_at_index(12345, 0), 1);
- EQUALS(get_digit_at_index(12345, 2), 3);
- EQUALS(get_digit_at_index(12345, 4), 5);
- printf("Random answer %d\n", get_random_4digit());
- printf("Random answer %d\n", get_random_4digit());
- printf("Random answer %d\n", get_random_4digit());
- EQUALS(digit_is_in(2637, 2), true);
- EQUALS(!digit_is_in(2637, 4), true);
- EQUALS(count_bulls(1807, 7810), 1);
- EQUALS(count_bulls(2647, 2837), 2);
- EQUALS(count_cows_and_bulls(1807, 7810), 4);
- EQUALS(count_cows_and_bulls(2647, 2837), 2);
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement