ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [C] 숫자 야구 게임
    COMPUTER LANGUAGE/C 2019. 5. 28. 11:45

     

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    75

    76

    77

    78

    79

    80

    81

    82

    83

    84

    85

    86

    87

    88

    89

    90

    91

    92

    93

    94

    95

    96

    97

    98

    99

    100

    101

    102

    103

    104

    105

    106

    107

    108

    109

    110

    111

    112

    개발 환경 : VisualStudio 2017

    #include <stdio.h>

    #include <stdlib.h>

    #include <time.h>

    int main(void)

    {

        int input = 0// 사용자가 입력

        int com, num1, num2, num3; // 랜덤생성번호, 1000,100,1

        int input1,input2,input3; // 100,10,1 자릿수

        int turn; //기회

        int x,y,z;

        int i,j,k;

        int restart; // 재시작 변수

        

        

        srand((unsigned)time(NULL));

        while(1)

        {

            while(1)

            {

                com = rand() % 1000//0~999 랜덤 숫자 생성

                num1 = com /100//100의 자리 숫자

                num2 = (com % 100/ 10// 10의 자리 숫자

                num3 = com % 10// 1의 자리 숫자

                

                if(num1 == num2 || num2 == num3 || num3 == num1)

                    continue//중복된 숫자 재생성

                break;

            }

    //        printf("%d", com); //랜덤 숫자 확인

            printf("숫자 야구 게임입니다.\n");

            for(turn=9; turn>=0 ; turn--)

            {

                printf("총 %d번의 기회가 남았습니다.\n",turn);

                printf("세자리 숫자를 입력하세요 : ");

                scanf("%d",&input);

                

                input1=input/100//input1은 100의 자리수

                input2=(input % 100)/10//input2는 10의 자리수

                input3=input % 10//input3는 1의 자리수

                

                if(input1 == input2 || input2 == input3 || input3 == input1)

                {

                    printf("숫자 야구는 중복된 수가 없습니다.\n");

                    turn++;

                    continue;

                }

                else if (input>999)

                {

                    printf("숫자의 범위가 너무 큽니다.\n");

                    turn++;

                    continue;

                }

                // 3개의 숫자 전부 스트라이크면?

                x = num1 == input1;

                y = num2 == input2;

                z = num3 == input3;

                

                // 볼이면?

                i = (num1 == input2 || num1 == input3);

                j = (num2 == input1 || num2 == input3);

                k = (num3 == input1 || num3 == input2);

                

                if((x+y+z)==0)

                {

                    printf("[0]S [%d]B \n", i+j+k); // i+j+k는 볼의 숫자.

                }

                else if((x+y+z) >= 1)

                {

                    if((x+y+z) == 1)

                    {

                        printf("[1]S [%d]B \n", i+j+k);

                    }

                    else if((x+y+z) == 2)

                    {

                        printf("[2]S [%d]B \n", i+j+k);

                    }

                    else

                    {

                        printf("[3]Strike!!\n");

                        break;

                    }

                }

            }

            if(!(com == input)) //9번의 기회 전부 다 맞추지 못했을 경우

            {

                printf("--------------------\n");

                printf("안타깝습니다. OUT!!!!!!!\n");

                printf("--------------------\n");

                printf("맞춰야 할 숫자는 %d입니다.\n", com);

            }

            

            printf("--------------------\n");

            printf("게임을 다시 시작하겠습니까?\n");

            printf("--------------------\n");

            printf("1.YES , 2.NO 입력 : ");

            scanf("%d",&restart);

            printf("\n--------------------\n");

            

            switch(restart)

            {

                case 1:

                    continue;

                case 2:

                    printf("종료합니다.\n");

                    break;

                default:

                    printf("잘못된 값 입력. 종료합니다.\n");

                    break;

            }

            break;

        }

    }

Designed by Tistory.