언어/C

[C] 구조체 - 고양이 뽑기 게임

돌멩이수프 2022. 4. 25. 14:21
728x90
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

typedef struct {
	char* name;
	int age;
	char* character;
	int level;
} CAT;

int collection[5] = { 0,0,0,0,0 };
CAT cats[5];

void initCats();
void printCat(int collect);
int checkCollection();

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

	initCats();
	while (1)
	{
		printf("엔터를 치고 당신의 고양이를 만나보세요.\n");
		getchar();

		int collect = rand() % 5;
		printCat(collect);
		collection[collect] = 1;
		int collectAll = checkCollection();
		
		if (collectAll == 1)
		{
			printf("모든 고양이를 모으셨네요.\n\n");
			break;
		}
	}

	return 0;
}

void initCats()
{
	cats[0].name = "깜냥이";
	cats[0].age = 5;
	cats[0].character = "온순";
	cats[0].level = 1;

	cats[1].name = "귀요미";
	cats[1].age = 3;
	cats[1].character = "날카로움";
	cats[1].level = 2;

	cats[2].name = "수줍이";
	cats[2].age = 7;
	cats[2].character = "늘 잠만 잠";
	cats[2].level = 3;

	cats[3].name = "까꿍이";
	cats[3].age = 2;
	cats[3].character = "시끄러움";
	cats[3].level = 4;

	cats[4].name = "돼냥이";
	cats[4].age = 1;
	cats[4].character = "배고픔";
	cats[4].level = 5;
}

void printCat(int collect)
{
	printf("  이름:  %s\n", cats[collect].name);
	printf("  나이:  %d\n", cats[collect].age);
	printf("  특징:  %s\n", cats[collect].character);
	printf("  레벨:  ");

	for (int i = 0; i < cats[collect].level; i++)
	{
		printf("★");
	}
	printf("\n");
}

int checkCollection()
{
	int collectAll = 1;

	for (int i = 0; i < 5; i++)
	{
		if (collection[i] == 1)
		{
			printf("%10s", cats[i].name);
		}
		else
		{
			printf("%10s", "(빈자리)");
			collectAll = 0;
		}
	}
	printf("\n\n====================================================\n\n");

	return collectAll;
}

↓↓ 구조체 프로젝트의 핵심 부분이다. ↓↓

#include <stdio.h>

typedef struct {
	char* name;
	int age;
	char* character;
	int level;
} CAT;

int collection[2] = { 0,0 };
CAT cats[2];

int main (void)
{
    initData();
    return 0;
}

void initData()
{
	cats[0].name = "깜냥이";
	cats[0].age = 5;
	cats[0].character = "온순";
	cats[0].level = 1;

	cats[1].name = "귀요미";
	cats[1].age = 3;
	cats[1].character = "날카로움";
	cats[1].level = 2;
}

깜냥이라는 고양이의 이름을 꺼내고 싶으면 우리는 cats[0].name; 을 사용하면 된다. 귀요미라는 고양이의 나이를 꺼내고 싶으면 cats[1].age; 를 사용하면 된다. 구조체를 사용하면 배열 각각마다 다양한 정보를 입력할 수 있다. 사용도 매우 간편하다.

여러 프로젝트를 하나씩 완성해나아가면서 재미가 생긴다. 다행이다.

728x90