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



    #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] Á¶È¸ : 195 ´Ù¿î : 0   
     
    ¡â ÀÌÀü±ÛÀüÈ­¹øÈ£ ¼öÁ¤ÇÏ´Â ÇÁ·Î±×·¥
    ¡ä ´ÙÀ½±Û°áÁ¤ Æ®¸®
    Çлý½Ç½À °Ô½ÃÆÇ
    ¹øÈ£ ¨Ï Á¦ ¸ñ À̸§ Á¶È¸ µî·ÏÀÏ
    103 ÀüÈ­¹øÈ£ ¼öÁ¤ÇÏ´Â ÇÁ·Î±×·¥ ÇÁ·Î±×·¡¹Ö¾ð¾î ÀÓÀç¸ð 7 12-03
    102 14ÁÖÂ÷ ½Ç½À ÇÁ·Î±×·¡¹Ö¾ð¾î Â÷»ó¹Î 8 12-03
    101 °áÁ¤ Æ®¸® ÀڷᱸÁ¶ À̱âÁ¤ 41 11-26
    100 ¾ß±¸½Ç½ÀN ÇÁ·Î±×·¡¹Ö¾ð¾î ÃÖÇö¿ì 72 11-19
    99 ¾ß±¸½Ç½À ÇÁ·Î±×·¡¹Ö¾ð¾î ¹éÀμ­ 68 11-19
    98 11ÁÖÂ÷ ½Ç½À ÇÁ·Î±×·¡¹Ö¾ð¾î ÀüÈ£¼º 72 11-12
    97 Å¥¸¦ ÀÌ¿ëÇÑ ³Êºñ ¿ì¼± Ž»ö ÇÁ·Î±×·¡¹Ö¾ð¾î ÀÓÀç¸ð 115 10-29
    96 Å¥¸¦ ÀÌ¿ëÇÑ ³Êºñ ¿ì¼± Ž»ö BFS ±¸Çö ÀڷᱸÁ¶ À̱âÁ¤ 111 10-29
    95 Á¶°Ç¹® µµÀü°úÁ¦1 ÇÁ·Î±×·¡¹Ö¾ð¾î ÃÖÇö¿ì 153 04-12
    94 4¿ù12ÀÏ ¼¼¼ö ºñ±³ ½Ç½À ÇÁ·Î±×·¡¹Ö¾ð¾î À±¿µ¹Î 172 04-12
    93 µµÀü¹®Á¦ ÇÁ·Î±×·¡¹Ö¾ð¾î ÀÌÀ±È¯ 161 04-12
    92 asdff ºñ°ø°³ ÇÁ·Î±×·¡¹Ö¾ð¾î À±Çü½Ä 235 11-28
    91 ¾ß±¸°ÔÀÓ ÇÁ·Î±×·¡¹Ö¾ð¾î À±Çü½Ä 195 11-28
    90 ddd ÇÁ·Î±×·¡¹Ö¾ð¾î À±Çü½Ä 274 10-31
    89 ½Ç½À2 ÇÁ·Î±×·¡¹Ö¾ð¾î ¼­¼ºÀº 632 03-16

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