whazzulookinat
@yhy12540722
Reviews Written
-
Average Rating
-
Posts
Q&A
while(1)
์ฝ๋๊ฐ ์ด์ํ๊ฒ ๋ณต๋ถ์ด ๋๋ค์...ใ ใ ใ ใ
- 0
- 3
- 285
Q&A
while(1)
์ด์ด์ ์ง๋ฌธ๋๋ฆฌ๋ ค๊ณ ํฉ๋๋ค. ๊ฐ์๋ฅผ ๋ค์ผ๋ฉด์ ์ฝ๋๋ฅผ ๋ค ์ง๋ณด์๋๋ฐ, ์์ธ์ ์์ ์์ง๋ง ์คํ์ด ๋์ง ์์ต๋๋ค.. ๋๋ฆ๋๋ก ๋๋ฒ๊น ์ ํด๋ณธ ๊ฒฐ๊ณผ, shuffleAnimal ํจ์์์ ์ด์์ด ๋ฐ์ํ ๋ฏ ์ถ์ต๋๋ค. while(1)์ด ๋น๋น ๋์์ ๊ฒฐ๊ณผ๊ฐ ์๋์ค๋๊ฑด๊ฐ ์ถ๊ธฐ๋ ํฉ๋๋ค. ์ฝ๋๋ฅผ ํ๋ฒ ๋ด์ฃผ์๋ฉด ๊ฐ์ฌํ ๊ฒ ๊ฐ์ต๋๋ค. include include //10๋ง๋ฆฌ์ ์๋ก ๋ค๋ฅธ ๋๋ฌผ(๊ฐ ์นด๋ 2์ฅ์ฉ) //์ฌ์ฉ์๋ก๋ถํฐ 2๊ฐ์ ์ ๋ ฅ๊ฐ์ ๋ฐ์์ -> ๊ฐ์ ๋๋ฌผ ์ฐพ์ผ๋ฉด ์นด๋ ๋ค์ง๊ธฐ //์ด ์คํจ ํ์ ์๋ ค์ฃผ๊ธฐ int arrayAnimal[4][5];//20์ฅ์ ์นด๋ int checkAnimal[4][5];//๋ค์งํ๋์ง ์ฌ๋ถ ํ์ธ char *strAnimal[10]; //character pointer ํ void initAnimalArray(); void initAnimalName(); void shuffleAnimal(); int getEmptyPosition(); void printAnimals(); void printQuestion(); int conv_pos_x(int x); int conv_pos_y(int y); int foundAllAnimals(); 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;//๊ฐ์ ์นด๋ ์ ํ์ ๋ฌดํจ } //์ขํ์ ํด๋นํ๋ ์นด๋๋ฅผ ๋ค์ง์ด๋ณด๊ณ ๊ฐ์์ง ์๊ฐ์์ง ํ์ธ //์ ์ ์ขํ๋ฅผ (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", arrayAnimal[firstSelect_x][firstSelect_y]); checkAnimal[firstSelect_x][firstSelect_y] = 1; checkAnimal[secondSelect_x][secondSelect_y] = 1;//์ ํ๋ ์นด๋๋ผ๊ณ ๋ฐ๊ฟ. } else { printf("\n\n ๋ก!!(ํ๋ ธ๊ฑฐ๋, ์ด๋ฏธ ๋ค์งํ ์นด๋์ ๋๋ค!!)\n"); printf("%d : %d\n", select1, strAnimal[arrayAnimal[firstSelect_x][firstSelect_y]]); printf("%d : %d\n", select1, strAnimal[arrayAnimal[secondSelect_x][secondSelect_y]]); printf("\n\n"); failCount++; } //๋ชจ๋ ๋๋ฌผ์ ์ฐพ์๋์ง ์ฌ๋ถ 1:์ฐธ,0:๊ฑฐ์ง if (foundAllAnimals() == 1) { printf("\n\n===์ถํํฉ๋๋ค!! ๋ชจ๋ ๋๋ฌผ์ ๋ค ์ฐพ์๋ค์!!==\n\n"); printf("์ง๊ธ๊น์ง ์ด %d๋ฒ ์ค์ํ์ จ์ต๋๋ค.", failCount); break; } } return 0; } void initAnimalArray() { for (int i = 1; i for (int j = 1; 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 for (int j = 0; 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;//0~19์ฌ์ด์ ์ 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;//x๋ฅผ 5๋ก ๋๋ ๋ชซ } int conv_pos_y(int y){ return y % 5;//y๋ฅผ 5๋ก ๋๋ ๋๋จธ์ง } void printAnimals() { for (int i = 0; i for (int j = 0; j printf("%8s", strAnimal[arrayAnimal[i][j]]); } printf("n"); } printf("n==================================n"); } void printQuestion() { printf("nn(๋ฌธ์ )nn"); int seq = 0; for (int i = 0; i } int foundAllAnimals() { for (int i = 0; i for (int j = 0; j if (checkAnimal[i][j] == 0) {//๋ชป์ฐพ์ ๋๋ฌผ์ด ์์ return 0; } } } return 1; //๋ชจ๋ ๋ค ์ฐพ์ }
- 0
- 3
- 285




