inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

C 프로그래밍 - 입문부터 게임 개발까지

프로젝트(연장선)

질문있습니다

해결된 질문

257

단팔

작성한 질문수 1

0

아무리 영상을 다시 재생하면서 봐도 똑같이 적었는데 실행시키면 문제 번호가 짝수만 나오게 됩니다 왜 그러는걸까요

#include <stdio.h>
#include <time.h>

int arrayAnimal[4][5];
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 printAnimals();
void printQuestion();

int main(void)
{
	srand(time(NULL));

	initAnimalArray();
	initAnimalName();

	shuffleAnimal();

	int failCount = 0;

	while (1)
	{
		int select1 = 0;
		int select2 = 0;

		printAnimals();
		printQuestion();
		printf("뒤집을 카드를 2개 고르세요 : ");
		scanf_s("%d %d", &select1, &select2);

		if (select1 == select2)
			continue;

		int firstSelect_x = conv_pos_x(select1);
		int firstSelect_y = conv_pos_x(select1);

		int secondSelect_x = conv_pos_x(select2);
		int secondSelect_y = conv_pos_x(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", select2, strAnimal[arrayAnimal[secondSelect_x][secondSelect_y]]);
			printf("\n\n");

			failCount++;
		}

		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()
{
	while (1)
	{
		int randPos = rand() % 20;
		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)
{
	return x / 5;
}

int conv_pos_y(int y)
{
	return y % 5;
}

void printAnimals()
{
	printf("\n=====이건 비밀인데.. 몰래 보여줍니다=====\n\n");
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 5; j++)
		{
			printf("%8s", strAnimal[arrayAnimal[i][j]]);
		}
		printf("\n");
	}
	printf("\n===================================\n\n");
}
void printQuestion()
{
	printf("\n\n(문제)\n");
	int seq = 0;

	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 5; j++)
		{
			if (checkAnimal[i][j] != 0)
			{
				printf("%8s", strAnimal[arrayAnimal[i][j]]);
			}
			else
			{
				printf("%8d", seq++);
			}
			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;
}

c

답변 3

1

나도코딩

안녕하세요^^

강의 12분 58초 부분을 보시면 되겠네요.

printQuestion함수에 seq++ 부분이 2번 있어서 2씩 증가한답니다. else 문 안에 seq++ 을 seq 로만 바꿔주시면 됩니다.

감사합니다.

0

나도코딩

안녕하세요?

^^ 잘못된 코드가 있는데요, 아래 부분을 주석 설명대로 고쳐보시겠어요?

감사합니다.

int firstSelect_x = conv_pos_x(select1);
int firstSelect_y = conv_pos_x(select1); // conv_pos_y 로 바뀌어야 합니다

int secondSelect_x = conv_pos_x(select2);
int secondSelect_y = conv_pos_x(select2); // conv_pos_y 로 바뀌어야 합니다

0

단팔

위와 같은 문제는 해결했습니다 근데 또 다른 문제가 발생하는데 이번에는 숫자 2개를 입력해도 카드가 1장 밖에 안뒤집어지는데 왜 그러는건가요?

안녕하세요 7장 포인터에 관해 질문드립니다.

0

63

1

8-8 연장전 / 소스코드 공유해드려요. 참고하세요!

0

64

0

섹션6. 프로젝트 소스코드 올려드려요.

0

94

1

영상속에서 배운 코드들을 Git Repository에 올려두될까요?

0

79

1

경찰서 조서 프로젝트 문의

0

104

1

경찰관 조서 프로젝트

0

142

1

scanf

0

92

1

제대로 작성한것 같은데 빌드가 않됩니다.,

0

170

1

구문 오류 C2059

0

400

1

컴파일 시 fatal error C1010 발생

0

284

1

반환값이 없는 함수

0

215

1

반올림 되는건가요?

0

254

1

맥으로수강

0

193

1

10-2

0

257

1

질문이 있습니다

0

342

1

3-3 for 반복문 ++i 일때 질문입니다.

0

244

1

6-4(배열 파트 '문자 vs 문자열') sizeof 예제 다르게 출력되는 분들 참고하세요.

1

411

0

함수 선언과 정의를 동시에 해도 되나요?

0

1076

1

빌드오류가 있다고 나오고, 지정된 파일을 찾을 수 없다고 오류가 나옵니다..

0

404

1

안녕하세요 좋은 강의와 책 감사드립니다!! 다름이 아니라 명령어를 실행시켰을 때 저런식으로 나와서 질문드립니다

0

935

0

질문있습니다!

0

378

1

질문드립니다

0

387

1

scanf_s(" %d", &answer); 이후 바로 종료

0

312

0

질문 드립니다.

0

431

1