본문 바로가기
프로그래밍/C 언어(코딩)

C언어] 숫자 베이스볼 게임 (함수화)

by 곰나나 2023. 12. 18.

[숫자 베이스볼 게임 (함수화)]

툴 버전 : Visual Studio 2022

 

 

숫자 베이스볼 게임을 반복문과 연산자, 비교연산, 논리연산자를 활용하여 만들고 함수화 하기

 

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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* 숫자 야구 게임 만들기
* 반복문
* 함수화
*/
#include <stdio.h>
#include <stdlib.h> 
#include <time.h> 
#include <Windows.h> 
 
//& 함수 선언
int Main_Game();
int Computer_Random_Number();
int Player_numbers();
int Ball_Strike_Judgment(int com_num, int player_num);
 
void main()
{
    Main_Game();
}// main
 
int Main_Game()
{
    //& 변수 선언(컴퓨터의 숫자)
    int com_num = Computer_Random_Number();
 
    //& Test용 컴퓨터가 뽑은수 
    //printf("컴퓨터가 뽑은 수 : %d\n", com_num);
 
    while (1)
    {
        //& 플레이어의 숫자를 받아온다.
        int player_num = Player_numbers();
 
        //& 플레이어 승패 판단 (리턴 받은 같이 9이면 게임 종료)
        if (Ball_Strike_Judgment(com_num, player_num) == 9)
        {
            printf("----------------------------------------------\n");
            printf("\t 승리하셨습니다! 축하합니다.\n");
            printf("----------------------------------------------\n");
 
            break;
        }// if
    }// while
    return 0;
}//Main_Game
 
//& 컴퓨터 3자리수 랜덤하게 설정 하는 함수 
int Computer_Random_Number() 
    //& 변수 선언 (숫자 3개)
    int com_num_1;
    int com_num_2;
    int com_num_3;
 
    //& 랜덤 난수 생성 하기
    srand(time(NULL)); 
 
    //& 컴퓨터 숫자 랜덤 숫자 받기
    com_num_1 = rand() % 10;
    com_num_2 = rand() % 10
    com_num_3 = rand() % 10;
    /*
    *  중복 숫자가 없게 예외 처리
    *  1과2와 같다면 2번에 수를 다시 받는다.
    *  1과3, 2와3의 수 중 중복이 있다면 3번에 수를 다시 받는다.
    *  컴퓨터의 수를 받고 리턴 하기전 첫번쨰는 100의 자리 두번째는 10의 자리로 만들어주고 수를 더한뒤 리턴 한다.
    */
    while (com_num_1 == com_num_2) 
    { 
        com_num_2 = rand() & 10;
    }
    while (com_num_1 == com_num_3 || com_num_2 == com_num_3) 
    { 
        com_num_3 = rand() % 10;
    }
    return com_num_1 * 100 + com_num_2 * 10 + com_num_3; 
}
 
//& 플레이어의 숫자 고르기 함수
int Player_numbers() 
    //& 변수 선언
    int player_num_1=0;
    int player_num_2=0;
    int player_num_3=0;
 
    printf("----------------------------------------------\n");
    
    printf("백의 자리 숫자 입력 : ");
    scanf_s("%d"&player_num_1);
    printf("십의 자리 숫자 입력 : ");
    scanf_s("%d"&player_num_2);
    printf("일의 자리 숫자 입력 : ");
    scanf_s("%d"&player_num_3);
 
    printf("\n입력된 숫자는 %d %d %d 입니다.\n", player_num_1, player_num_2, player_num_3);
    printf("컴퓨터 숫자는 * * * 입니다.\n");
 
    //& 문자열이 들어갈때 예외 처리 하기
    while (getchar() != '\n');
    //& 리턴
    return player_num_1 * 100 + player_num_2 * 10 + player_num_3; 
}//Player_numbers
 
//& 볼 스트라이크 판정 함수
int Ball_Strike_Judgment(int com_num, int player_num) 
{
    //& 변수 선언 (볼,스트라이크, 유저의 숫자 받을 변수3개, 컴퓨터의 숫자 받을 변수3개)
    int strike = 0;
    int ball = 0;
 
    int player_num_1 = player_num / 100;
    int player_num_2 = (player_num / 10) % 10;
    int player_num_3 = player_num % 10;
 
    int com_num_1 = com_num / 100
    int com_num_2 = com_num / 10 % 10
    int com_num_3 = com_num % 10
 
    //& 백의자리, 십의자리, 일의 자리가 각각 같을때 1스트라이크 
    if (player_num_1 == com_num_1)
    { 
        strike++;
    }
    if (player_num_2 == com_num_2)
    { 
        strike++;
    }
    if (player_num_3 == com_num_3)
    { 
        strike++;
    }
 
    //& 백의자리, 십의자리, 일의 자리가 같을때 모두 같을때 3스트라이크
    if (player_num_1 == com_num_1 && player_num_2 == com_num_2 && player_num_3 == com_num_3)
    { 
        strike = 3//스트라이크에 3더해줌
    }
 
    /*
    * 유저의 백의자리가 컴퓨터의 십의자리나 일의 자리와 같을때
    * 유저의 십의자리가 컴퓨터의 백의자리나 일의 자리와 같을때
    * 유저의 일의자리가 컴퓨터의 백의자리나 십의 자리와 같을때
    * 볼 카운트
    */
    if (player_num_1 == com_num_2 || player_num_1 == com_num_3)
    { 
        ball++;
    }
    if (player_num_2 == com_num_1 || player_num_2 == com_num_3)
    {
        ball++;
    }
    if (player_num_3 == com_num_1 || player_num_3 == com_num_2)
    {
        ball++;
    }
 
    //& 볼과 스트라이크가 하나도 없을때. 
    if (ball == 0 && strike == 0)
    {
        printf("\t!!아웃!!\n");
    }
 
    //& 볼 갯수 알려주기
    if (ball > 0 && strike == 0)
    {
        printf("%d볼 입니다.\n", ball);
    }
 
    //& 볼과 스트리아크 갯수 알려주기
    if (ball > 0 && strike > 0)
    { 
        printf("%d볼, %d스트라이크 입니다.\n",ball, strike);
    }
 
    //& 볼이 없고 스트라이크만 존재할때 스트라이크 갯수 알려주기 하지만 3스트라이크 일때 9를 반환 하여 게임 끝내기
    if (ball == 0 && strike > 0)
    { 
        printf("%d스트라이크 입니다.\n", strike);
 
        if (strike == 3
        { 
            return 9;
        }
    }
 
}// Ball_Strike_Judgment
cs

 

결과 화면)

728x90