• º» °Ô½ÃÆÇÀº ¼ö¾÷½Ã°£¿¡ Çлýµé ½Ç½ÀÀ» À§ÇÑ °Ô½ÃÆÇÀÔ´Ï´Ù.
  • º» °Ô½ÃÆÇ¿¡ ¿Ã¶ó¿Í ÀÖ´Â ÇÁ·Î±×·¥Àº ´ëºÎºÐ ¿Ã¹Ù¸£Áö ¾ÊÀº ÇÁ·Î±×·¥ÀÔ´Ï´Ù.
        À±Çü½Ä
        ¾ß±¸°ÔÀÓ



    #include <stdio.h>

    #include <stdlib.h>

    #include <time.h>

     

    #define DIGIT_COUNT 4

    #define MAX_DIGIT 10

     

    void shuffleArray(int *arr, int length) {

        for (int i = 0; i < length; ++i) {

            int randomIndex = rand() % length;

            // Swap elements at i and randomIndex

            int temp = arr[i];

            arr[i] = arr[randomIndex];

            arr[randomIndex] = temp;

        }

    }

     

    void generateRandomNumbers(int *numbers) {

        int comp[MAX_DIGIT] = {0}; // Initialize array with all zeros

     

        // Fill comp array with 1 at randomly selected indices

        for (int i = 0; i < DIGIT_COUNT; ++i) {

            int randomIndex;

            do {

                randomIndex = rand() % MAX_DIGIT;

            } while (comp[randomIndex] == 1);

     

            comp[randomIndex] = 1;

        }

     

        // Select numbers from comp array (indices with value 1)

        int index = 0;

        for (int i = 0; i < MAX_DIGIT; ++i) {

            if (comp[i] == 1) {

                numbers[index++] = i;

            }

        }

     

        // Shuffle the array to randomize the order

        shuffleArray(numbers, DIGIT_COUNT);

    }

     

    int contains(int *arr, int length, int num) {

        for (int i = 0; i < length; ++i) {

            if (arr[i] == num) {

                return 1; // True

            }

        }

        return 0; // False

    }

     

    void getPlayerInput(int *numbers) {

        printf("Enter %d single-digit numbers separated by space: ", DIGIT_COUNT);

        for (int i = 0; i < DIGIT_COUNT; ++i) {

            int input;

            while (1) {

                if (scanf("%d", &input) == 1 && input >= 0 && input < MAX_DIGIT && !contains(numbers, i, input)) {

                    numbers[i] = input;

                    break;

                } else {

                    printf("Invalid input. Please enter single-digit numbers that are not repeated.\n");

                    while (getchar() != '\n');  // Clear the input buffer

                }

            }

        }

    }

     

    void evaluateGuess(int *computerNumbers, int *playerNumbers, int *strikes, int *balls) {

        *strikes = 0;

        *balls = 0;

     

        for (int i = 0; i < DIGIT_COUNT; ++i) {

            if (computerNumbers[i] == playerNumbers[i]) {

                (*strikes)++;

            } else if (contains(computerNumbers, DIGIT_COUNT, playerNumbers[i])) {

                (*balls)++;

            }

        }

    }

     

    int main(void) {

        int computerNumbers[DIGIT_COUNT];

        int playerNumbers[DIGIT_COUNT];

        int strikes, balls;

     

        srand((unsigned int)time(NULL));  // Move srand to the beginning of the main function

        generateRandomNumbers(computerNumbers);

        for(int i=0;i<4;i++){

            printf("%d ",computerNumbers[i]);

        }

        

        while (1) {

            getPlayerInput(playerNumbers);

            evaluateGuess(computerNumbers, playerNumbers, &strikes, &balls);

            

            printf("Strikes: %d, Balls: %d\n", strikes, balls);

     

            if (strikes == DIGIT_COUNT) {

                printf("Congratulations! You've guessed the numbers.\n");

                printf("Starting a new game.\n");

            } else {

                printf("Try again.\n");

            }

     

            printf("Enter -1 to exit or any other number to continue: ");

            int choice;

            scanf("%d", &choice);

     

            if (choice == -1) {

                printf("Exiting the game.\n");

                break;

            }

        }

     

        return 0;

    }

      µî·ÏÀÏ : 2023-11-28 [14:46] Á¶È¸ : 53 ´Ù¿î : 0   
     
    ¡â ÀÌÀü±ÛÁ¶°Ç¹® µµÀü°úÁ¦1
    ¡ä ´ÙÀ½±ÛµµÀü¹®Á¦
    Çлý½Ç½À °Ô½ÃÆÇ
    ¹øÈ£ ¨Ï Á¦ ¸ñ À̸§ Á¶È¸ µî·ÏÀÏ
    95 Á¶°Ç¹® µµÀü°úÁ¦1 ÇÁ·Î±×·¡¹Ö¾ð¾î ÃÖÇö¿ì 18 04-12
    94 4¿ù12ÀÏ ¼¼¼ö ºñ±³ ½Ç½À ÇÁ·Î±×·¡¹Ö¾ð¾î À±¿µ¹Î 19 04-12
    93 µµÀü¹®Á¦ ÇÁ·Î±×·¡¹Ö¾ð¾î ÀÌÀ±È¯ 10 04-12
    92 asdff ºñ°ø°³ ÇÁ·Î±×·¡¹Ö¾ð¾î À±Çü½Ä 57 11-28
    91 ¾ß±¸°ÔÀÓ ÇÁ·Î±×·¡¹Ö¾ð¾î À±Çü½Ä 53 11-28
    90 ddd ÇÁ·Î±×·¡¹Ö¾ð¾î À±Çü½Ä 115 10-31
    89 ½Ç½À2 ÇÁ·Î±×·¡¹Ö¾ð¾î ¼­¼ºÀº 426 03-16
    88 ¦¦❶ ½Ç½À2 ÇÁ·Î±×·¡¹Ö¾ð¾î Á¤¼ºÈÆ 365 03-16
    87 ¾ß±¸ ÇÁ·Î±×·¡¹Ö¾ð¾î ¹éÇö¹Î 608 10-31
    86 °Å²Ù·Î ÇÁ·Î±×·¡¹Ö¾ð¾î ±è°æȸ 613 09-26
    85 ¤±¤¤¤· ÇÁ·Î±×·¡¹Ö¾ð¾î Á¶¹®±Ù 728 06-13
    84 ·Î¶Ç ÇÁ·Î±×·¡¹Ö¾ð¾î ±è»ó¿ì 758 05-30
    83 ÇÔ¼ö ÇÁ·Î±×·¡¹Ö¾ð¾î ±è°æȸ 832 05-23
    82 proto ÇÁ·Î±×·¡¹Ö¾ð¾î ÀÌÇö½Ä 712 05-23
    81 ÇÔ¼ö ÇÁ·Î±×·¡¹Ö¾ð¾î ¹Úº´¹Î 697 05-23

    [1][2][3][4][5][6][7]