8-8 연장전 / 소스코드 공유해드려요. 참고하세요!
64
이정완
작성한 질문수 8
0
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 10마리의 서로 다른 동물(각 카드 2장씩)
// 사용자로부터 2개의 입력값을 받아서 -> 같은 동물 찾으면 카드 뒤집기
// 모든 동물 쌍을 찾으면 게임 종료
// 총 실패 횟수 알려주기
int arrayAnimal[4][5]; // 카드 지도 (20장의 카드)
int checkAnimal[4][5]; // 뒤집혔는지 여부 확인
char *strAnimal[10];
void initAnimalArray();
void initAnimalName();
void shuffleAnimal();
int getEmptyPosition();
int conv_pos_x(int x);
int conv_pos_y(int y);
void printfAnimals();
void printQuestion();
int foundAllAnimals();
void printAlign(const char *str, int totalWidth);
int getVisibleWidth(const char *str);
int main(void)
{
srand(time(NULL));
initAnimalArray();
initAnimalName();
shuffleAnimal();
int failCount = 0; // 실패 횟수
while (1)
{
int select1 = 0; // 사용자가 선택한 처음 수
int select2 = 0; // 사용자가 선택한 두번째 수
printfAnimals(); // 동물 위치 출력
printQuestion(); // 문제 출력 (카드 지도)
printf("뒤집을 카드를 2개 고르세요 : ");
scanf("%d %d", &select1, &select2);
if (select1 == select2) // 같은 카드 선택 시 무효
continue;
// 좌표에 해당하는 카드를 뒤집어 보고 같은지 안같은지 확인
// 정수 좌표를 (x, y)로 변환
int firstSelect_x = conv_pos_x(select1);
int firstSelect_y = conv_pos_y(select1);
int secondSelect_x = conv_pos_x(select2);
int secondSelect_y = conv_pos_y(select2);
// 같은 동물인 경우
if ((checkAnimal[firstSelect_x][firstSelect_y] == 0 && checkAnimal[secondSelect_x][secondSelect_y] == 0) &&
(arrayAnimal[firstSelect_x][firstSelect_y] == arrayAnimal[secondSelect_x][secondSelect_y]))
{
printf("\n\n빙고! : %s 발견 \n\n", strAnimal[arrayAnimal[firstSelect_x][firstSelect_y]]);
checkAnimal[firstSelect_x][firstSelect_y] = 1;
checkAnimal[secondSelect_x][secondSelect_y] = 1;
}
// 다른 동물인 경우
else
{
printf("\n\n 땡!! (틀렸거나, 이미 뒤집힌 카드입니다)\n");
printf("%d : %s\n", select1, strAnimal[arrayAnimal[firstSelect_x][firstSelect_y]]);
printf("%d : %s\n", select1, strAnimal[arrayAnimal[secondSelect_x][secondSelect_y]]);
printf("\n\n");
failCount++;
}
// 모든 동물을 찾았는지 여부, 1 : 참, 0 : 거짓
if (foundAllAnimals() == 1)
{
printf("\n\n 축하합니다 ! 모든 동물을 다 찾았네요 \n");
printf("지금까지 총 %d 번 실수했습니다\n", failCount);
break;
}
}
return 0;
}
void initAnimalArray()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 5; j++)
{
arrayAnimal[i][j] = -1;
}
}
}
void initAnimalName()
{
strAnimal[0] = "원숭이";
strAnimal[1] = "하마";
strAnimal[2] = "강아지";
strAnimal[3] = "고양이";
strAnimal[4] = "돼지";
strAnimal[5] = "코끼리";
strAnimal[6] = "기린";
strAnimal[7] = "낙타";
strAnimal[8] = "타조";
strAnimal[9] = "호랑이";
}
void shuffleAnimal()
{
// ☐ ☐ ☐ ☐ ☐
// ☐ ☐ ☐ ☐ ☐
// ☐ ☐ ☐ ☐ ☐
// ☐ ☐ ☐ ☐ ☐
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 2; j++)
{
int pos = getEmptyPosition();
int x = conv_pos_x(pos);
int y = conv_pos_y(pos);
arrayAnimal[x][y] = i;
}
}
}
// 좌표에서 빈 공간 찾기
int getEmptyPosition()
{
// ☐ ☐ ☐ ☐ ☐ 0 1 2 3 4 -> 0 0 0 0 0
// ☐ ☐ ☐ ☐ ☐ 5 6 7 8 9 -> 1 1 1 1 1
// ☐ ☐ ☐ ☐ ☐ 10 11 12 13 14 -> 2 2 2 2 2
// ☐ ☐ ☐ ☐ ☐ 15 16 17 18 19 -> 3 3 3 3 3
while (1)
{
int randPos = rand() % 20; // 0 - 19 사이의 숫자 반환
// 19 -> (3, 4)
int x = conv_pos_x(randPos);
int y = conv_pos_y(randPos);
if (arrayAnimal[x][y] == -1)
{
return randPos;
}
}
return 0;
}
int conv_pos_x(int x)
{
// 19 -> (3,4)
return x / 5;
}
int conv_pos_y(int y)
{
// 19 -> 19 / 5 ? 몫은 3, 나머지 4
return y % 5; // y를 5로 나눈 나머지 값
}
void printfAnimals()
{
printf("\n======== 이건 비밀인데 . . 몰래 보여줍니다 =======\n\n");
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 5; j++)
{
printAlign(strAnimal[arrayAnimal[i][j]], 8);
}
printf("\n");
}
printf("\n============================================\n\n");
}
void printQuestion()
{
printf("\n\n(문제)\n");
int seq = 0;
// seq // checkAnimal
// ☐ ☐ ☐ ☐ ☐ 0 1 2 3 4 0 0 0 0 0
// ☐ ☐ ☐ ☐ ☐ 5 6 7 8 9 1 0 0 0 0
// ☐ ☐ ☐ ☐ ☐ 10 11 12 13 14
// ☐ ☐ ☐ ☐ ☐ 15 16 17 18 19
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 5; j++)
{
// 카드를 뒤집어서 정답을 맞혔으면 ' 동물 이름'
if (checkAnimal[i][j] != 0)
{
printAlign(strAnimal[arrayAnimal[i][j]], 8);
}
else
{
char temp[16];
sprintf(temp, "%d", seq); // 숫자를 문자열로 바꿔서
printAlign(temp, 8); // 한글처럼 정렬
}
seq++;
// 아직 뒤집지 못했으면 (정답을 못맞혔으면) 뒷면 -> 위치를 나타내는 숫자
}
printf("\n");
}
}
int foundAllAnimals()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 5; j++)
{
if (checkAnimal[i][j] == 0)
{
return 0;
}
}
}
return 1; // 모두 다 찾음
}
int getVisibleWidth(const char *str)
{
int width = 0;
while (*str)
{
if ((*str & 0xF0) == 0xE0)
{
width += 2;
str += 3;
}
else
{
width += 1;
str += 1;
}
}
return width;
}
void printAlign(const char *str, int totalWidth)
{
int visibleWidth = getVisibleWidth(str);
printf("%s", str);
for (int i = 0; i < totalWidth - visibleWidth; i++)
{
printf(" ");
}
}
답변 0
안녕하세요 7장 포인터에 관해 질문드립니다.
0
63
1
섹션6. 프로젝트 소스코드 올려드려요.
0
93
1
영상속에서 배운 코드들을 Git Repository에 올려두될까요?
0
79
1
경찰서 조서 프로젝트 문의
0
104
1
경찰관 조서 프로젝트
0
142
1
scanf
0
92
1
제대로 작성한것 같은데 빌드가 않됩니다.,
0
170
1
구문 오류 C2059
0
397
1
컴파일 시 fatal error C1010 발생
0
284
1
반환값이 없는 함수
0
215
1
반올림 되는건가요?
0
252
1
맥으로수강
0
193
1
10-2
0
257
1
질문이 있습니다
0
341
1
3-3 for 반복문 ++i 일때 질문입니다.
0
244
1
6-4(배열 파트 '문자 vs 문자열') sizeof 예제 다르게 출력되는 분들 참고하세요.
1
411
0
함수 선언과 정의를 동시에 해도 되나요?
0
1076
1
빌드오류가 있다고 나오고, 지정된 파일을 찾을 수 없다고 오류가 나옵니다..
0
403
1
안녕하세요 좋은 강의와 책 감사드립니다!! 다름이 아니라 명령어를 실행시켰을 때 저런식으로 나와서 질문드립니다
0
933
0
질문있습니다!
0
375
1
질문드립니다
0
384
1
scanf_s(" %d", &answer); 이후 바로 종료
0
308
0
질문 드립니다.
0
428
1
sizeof에서 오류가 나는 것 같아서 물어보고 싶습니다.
0
562
0





