C언어 동물카드뒤집기 오류 해결해주세요ㅠㅠ
-
게시물 수정 , 삭제는 로그인 필요
동물 카드 뒤집기인데 자꾸 사진처럼 몇몇 동물이 누락 되네요 왜 그럴까요??
=================================================================
#include <stdio.h>
#include <time.h>
//서로 다른 동물 10마리(각 카드 2장씩)
//사용자로부터 2개의 입력값을 받음
//카드 뒤집어서 맞추기
//총 실패 횟수 알려줌
int arrayAnimal[4][5]; //카드 지도(20장의 카드)
int checkAnimal[4][5]; //뒤집혔는지 여부 확인
char * strAnimal[10];
void initAnimalArray();
void initAnimalName();
void suffleAnimal();
int getEmptyPostion();
int conv_pos_x(int x);
int conv_pos_y(int y);
void printAnimals();
void printQuestion();
int FoundAnimals();
int main(void) {
srand(time(NULL));
initAnimalArray();
initAnimalName();
suffleAnimal();
int failCount = 0; //실패횟수
while (1) {
int select1 = 0; //사용자가 선택한 1번째 수
int select2 = 0; //사용자가 선택한 2번째 수
printAnimals(); //동물 위치 출력
printQuestion(); //문제 출력 카드 지도
printf("뒤집을 카드 두 개를 고르시오 : ");
scanf_s("%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", select2, strAnimal[arrayAnimal[SecondSelect_x][SecondSelect_y]]);
printf("\n\n");
failCount++;
}
//모든 동물을 찾았는지 여부 1 : 참 0 : 거짓
if(FoundAnimals()==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 suffleAnimal() {
//동물이 4*5
//ㅁㅁㅁㅁㅁ
//ㅁㅁㅁㅁㅁ
//ㅁㅁㅁㅁㅁ
//ㅁㅁㅁㅁㅁ
//4:53:00
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 2; j++) {
int pos = getEmptyPostion(); //비어있는 위치를 반환해줘
int x = conv_pos_x(pos);
int y = conv_pos_y(pos);
arrayAnimal[x][y] = i;
}
}
}
//좌표에서 빈공간 찾기
int getEmptyPostion() {
//ㅁㅁㅁㅁㅁ 0 1 2 3 4
//ㅁㅁㅁㅁㅁ 5 6 7 8 9
//ㅁㅁㅁㅁㅁ 10 11 12 13 14
//ㅁㅁㅁㅁㅁ 15 16 17 18 19
while (1) {
int randPos = rand() % 20; //0~19에서 숫자 반환
// EX)19 =>(3,4)
int x = conv_pos_x(randPos);
int y = conv_pos_y(randPos);
if (arrayAnimal[x][y] = -1) {
return randPos;
}
}
return 0;
}
//4:59:00
int conv_pos_x(int x) {
// 19 => (3,4)
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\n");
int seq = 0;
//ㅁㅁㅁㅁㅁ 0 1 2 3 4
//ㅁㅁㅁㅁㅁ 5 6 7 8 9
//ㅁㅁㅁㅁㅁ 10 11 12 13 14
//ㅁㅁㅁㅁㅁ 15 16 17 18 19
//5:11:00
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 FoundAnimals() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 5; j++) {
if (checkAnimal[i][j] == 0) {
return 0;
}
}
}
return 1; //모든 동물을 다 찾음
}
=================================================================
#include <stdio.h>
#include <time.h>
//서로 다른 동물 10마리(각 카드 2장씩)
//사용자로부터 2개의 입력값을 받음
//카드 뒤집어서 맞추기
//총 실패 횟수 알려줌
int arrayAnimal[4][5]; //카드 지도(20장의 카드)
int checkAnimal[4][5]; //뒤집혔는지 여부 확인
char * strAnimal[10];
void initAnimalArray();
void initAnimalName();
void suffleAnimal();
int getEmptyPostion();
int conv_pos_x(int x);
int conv_pos_y(int y);
void printAnimals();
void printQuestion();
int FoundAnimals();
int main(void) {
srand(time(NULL));
initAnimalArray();
initAnimalName();
suffleAnimal();
int failCount = 0; //실패횟수
while (1) {
int select1 = 0; //사용자가 선택한 1번째 수
int select2 = 0; //사용자가 선택한 2번째 수
printAnimals(); //동물 위치 출력
printQuestion(); //문제 출력 카드 지도
printf("뒤집을 카드 두 개를 고르시오 : ");
scanf_s("%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", select2, strAnimal[arrayAnimal[SecondSelect_x][SecondSelect_y]]);
printf("\n\n");
failCount++;
}
//모든 동물을 찾았는지 여부 1 : 참 0 : 거짓
if(FoundAnimals()==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 suffleAnimal() {
//동물이 4*5
//ㅁㅁㅁㅁㅁ
//ㅁㅁㅁㅁㅁ
//ㅁㅁㅁㅁㅁ
//ㅁㅁㅁㅁㅁ
//4:53:00
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 2; j++) {
int pos = getEmptyPostion(); //비어있는 위치를 반환해줘
int x = conv_pos_x(pos);
int y = conv_pos_y(pos);
arrayAnimal[x][y] = i;
}
}
}
//좌표에서 빈공간 찾기
int getEmptyPostion() {
//ㅁㅁㅁㅁㅁ 0 1 2 3 4
//ㅁㅁㅁㅁㅁ 5 6 7 8 9
//ㅁㅁㅁㅁㅁ 10 11 12 13 14
//ㅁㅁㅁㅁㅁ 15 16 17 18 19
while (1) {
int randPos = rand() % 20; //0~19에서 숫자 반환
// EX)19 =>(3,4)
int x = conv_pos_x(randPos);
int y = conv_pos_y(randPos);
if (arrayAnimal[x][y] = -1) {
return randPos;
}
}
return 0;
}
//4:59:00
int conv_pos_x(int x) {
// 19 => (3,4)
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\n");
int seq = 0;
//ㅁㅁㅁㅁㅁ 0 1 2 3 4
//ㅁㅁㅁㅁㅁ 5 6 7 8 9
//ㅁㅁㅁㅁㅁ 10 11 12 13 14
//ㅁㅁㅁㅁㅁ 15 16 17 18 19
//5:11:00
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 FoundAnimals() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 5; j++) {
if (checkAnimal[i][j] == 0) {
return 0;
}
}
}
return 1; //모든 동물을 다 찾음
}