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

C언어 77제] C언어 콘서트 CHAPTER 10 p385 도전문제1 화살표 키 입력받기

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;
   board[ypos][xpos] = '@';

   // 사용자로부터 위치를 받아서 보드에 표시한다. 
   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]=='#')
      {
         if (ch2 == 75) xpos++; // ←
         else if (ch2 == 80) ypos--; // ↑
         else if (ch2 == 72) ypos++; // ↓
         else if (ch2 == 77) xpos--; // →
      }
      board[ypos][xpos] = '@';
   }
   printf("\n통과를 축하합니다!!!\n");

   return 0;
}

 

참고풀이 결과]

 

 

 

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

 

반응형

댓글