본문 바로가기
프로그램/C언어 1000제

C언어 78제] C언어 콘서트 CHAPTER 10 p385 도전문제 2 벽에 닿으면 실패하는 게임으로 바꿔보자

by 건티 2021. 12. 28.
728x90

출처 : 반크_독도포스터

 

참고풀이]

//Dev-C++ 5.11로 작업함.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

int main(void) {
   char board[10][10] = { {'#', '#', '#', '#', '#', '.', '.', '.', '.', '.' },
      {'.', '.', '.', '.', '.', '#', '.', '.', '.', '.' },
      {'#', '#', '#', '.', '#', '.', '.', '.', '.', '.' },
      {'.', '.', '#', '.', '.', '#', '.', '.', '.', '.' },
      {'.', '.', '#', '.', '.', '#', '.', '.', '.', '.' },
      {'.', '.', '#', '.', '.', '#', '.', '.', '.', '.' },
      {'.', '.', '.', '#', '.', '.', '#', '#', '.', '.' },
      {'.', '.', '.', '.', '#', '.', '.', '.', '#', '#' },
      {'.', '.', '.', '.', '.', '#', '.', '.', '.', '.' },
      {'.', '.', '.', '.', '.', '#', '#', '#', '#', '#' } };
   int xpos = 0, ypos = 1;
   int x, y;
   int ch, ch2;
   int chk;
   board[ypos][xpos] = '@';

   // 사용자로부터 위치를 받아서 보드에 표시한다. 
   chk=1;
   while (1) {
      system("cls");
      printf("왼쪽이동: →, 오른쪽 이동: ←, 위쪽 이동: ↑, 아래쪽 이동: ↓\n");
      for (y = 0; y < 10; y++) {
         for (x = 0; x < 10; x++) printf("%c", board[y][x]);
         printf("\n");
      }
      board[ypos][xpos] = '.';
      ch = getch();
      if (ch == 224) {
         ch2 = getch();
         if (ch2 == 75) xpos--; // ←
         else if (ch2 == 80) ypos++; // ↑
         else if (ch2 == 72) ypos--; // ↓
         else if (ch2 == 77) xpos++; // →
      }
      if(ypos==10 || xpos==10) break;
      if(board[ypos][xpos]=='#')
      {
         chk=0;
         break;
      }
      else board[ypos][xpos] = '@';
   }

   if(chk) printf("\n통과를 축하합니다!!!\n");
   else
   {
      printf("\n벽에 닿아 미션을 통과하지 못하였습니다.\n");
      printf("다음에는 꼭 통과하세요.\n");
   }

   return 0;
}

 

참고풀이 결과]

 

 

 

대한민국의 아름다운 영토, 독도의 봄

 

반응형

댓글