본문 바로가기

개발자 모드/C언어

C언어 카드 뒤집기 게임

728x90

 

 

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

// 10마리의 서로 다른 동물 (각 카드 2장)
// 사용자로부터 2개의 입력값을 받아서 -> 같은 동물 찾으면 카드 뒤집기
// 모든 동물 쌍을 찾으면 게임 종료
// 총 실패 횟수 표시

int arrayAnimal[4][5];  // 카드 지도 20장
int checkAnimal[4][5];  // 뒤집힌 카드 여부 확인
void initAnimalArray();
char* strAnimal[10];
void initAnimalName();
void shuffleAnimal();
int getEmptyPosition();
int conv_pos_x(int x);
int conv_pos_y(int y);

void printfAnimals();
void printfQuestion();

int foundAllAnimals();

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

	initAnimalArray();
	initAnimalName();
	shuffleAnimal();
	
	
	int failCount = 0; // 총 실패 횟수


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


		printfAnimals();
		printfQuestion();

		printf(" 뒤집을 카드를 2개 고르세요 : ");

		scanf_s("%d %d", &select1, &select2);

		if (select1 == select2)
			continue;
		

		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");
			printf("%d : %s \n", select1, strAnimal[arrayAnimal[firstSelect_x][firstSelect_y]]);
			printf("%d : %s \n", select1, strAnimal[arrayAnimal[secondSelect_x][secondSelect_y]]);

			failCount++;
		}

		if (foundAllAnimals() == 1)
		{
			printf("\n축하합니다. \n");
			printf("\n지금까지 총 %d 번의 실수를 하셨습니다.", 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 
	// ㅁㅁㅁㅁㅁ 5 6 7 8 9 
	// ㅁㅁㅁㅁㅁ 10 11 12 13 14  
	// ㅁㅁㅁㅁㅁ 15 16 17 18 19 
	
	while (1)
	{
		int ranPos = rand() % 20; 
		// 19 -> (3,4)

		int x = conv_pos_x(ranPos);
		int y = conv_pos_y(ranPos);

		if (arrayAnimal[x][y] == -1)
		{
			return ranPos;
		}
	}

	return 0;
}

int conv_pos_x(int x)
{
	// 19 -> (3,4)

	return x/5;
}

int conv_pos_y(int y)
{
	// 19 -> (3,4)

	return y%5;
}


void printfAnimals()
{
	// ㅁㅁㅁㅁㅁ 0 1 2 3 4 
	// ㅁㅁㅁㅁㅁ 5 6 7 8 9 
	// ㅁㅁㅁㅁㅁ 10 11 12 13 14  
	// ㅁㅁㅁㅁㅁ 15 16 17 18 19 

	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 printfQuestion() 
{

	// ㅁㅁㅁㅁㅁ 0 1 2 3 4 
	// ㅁㅁㅁㅁㅁ 5 6 7 8 9 
	// ㅁㅁㅁㅁㅁ 10 11 12 13 14  
	// ㅁㅁㅁㅁㅁ 15 16 17 18 19 

	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;
}

 

 

결과

 

728x90