#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <Windows.h>
 
#define MAX 14
#define GOAL 8
 
 
enum colorName
{
        BLACK,
        D_BLUE,
        D_GREEN,
        D_SKYBLUE,
        D_RED,
        D_VIOLET,
        D_YELLOW,
        GRAY,
        D_GRAY,
        BLUE,
        GREEN,
        SKYBLUE,
        RED,
        VIOLET,
        YELLOW,
        WHITE,
};
 
void color(int bgColor, int textColor)
{
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), bgColor*16+textColor);
}
 
 
int main(void)
{
        int map[15][15]=
        {
                {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                {1,0,1,0,0,0,0,0,0,0,0,0,0,0,1},
                {1,0,1,0,1,1,1,1,1,1,1,1,1,0,1},
                {1,0,1,0,1,0,0,0,0,0,0,0,1,0,1},
                {1,0,1,0,1,0,1,1,1,1,1,0,1,0,1},
                {1,0,1,0,1,0,1,0,0,0,0,0,1,0,1},
                {1,0,1,0,1,0,1,1,1,1,1,0,1,0,1},
                {1,0,1,0,1,0,1,3,0,0,1,0,1,0,1},
                {1,0,1,0,1,0,1,1,1,0,1,0,1,0,1},
                {1,0,1,0,1,0,1,0,0,0,1,0,1,0,1},
                {1,0,1,0,1,0,1,0,1,1,1,0,1,0,1},
                {1,0,1,0,1,0,1,0,0,0,0,0,1,0,1},
                {1,0,1,0,1,0,1,1,1,1,1,1,1,0,1},
                {1,0,0,0,1,0,0,0,0,0,0,0,0,0,1},
                {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        };
 
        int myPosition_X=1, myPosition_Y=1;
        int i, j, k;
        int wallDetected=0;
        int moveCount=0;
        int loop=1;
 
        char input = 0;
 
        do
        {
                system("cls");
 
                map[myPosition_Y][myPosition_X]=2;
 
                for (i=0;i<=MAX;i++)
                {
                        for (j=0;j<=MAX;j++)
                        {
                                if (map[i][j]==1)
                                {
                                        color(BLACK, WHITE);
                                        printf("■");
                                }
 
                                else if (map[i][j]==0)
                                {
                                        color(BLACK, WHITE);
                                        printf("　");
                                }
 
                                else if (map[i][j]==2)
                                {
                                        color(BLACK, RED);
                                        printf("♂");
                                }
 
                                else if (map[i][j]==3)
                                {
                                        color(BLACK, RED);
                                        printf("♀");
                                }
                        }
 
                        printf("\t\t");
 
                        for (k=0;k<=MAX;k++)
                        {
                                printf("%d ",map[i][k]);
                        }
 
                        printf("\n");
                }
 
                printf("\n");
 
                color(BLACK,YELLOW);
                printf("x : ");
 
                color(BLACK,WHITE);
                printf("%d\t", myPosition_X+1);
 
                color(BLACK,YELLOW);
                printf("y : ");
 
                color(BLACK,WHITE);
                printf("%d\n\n", -(myPosition_Y+1));
 
                color(BLACK,YELLOW);
                printf("움직인 횟수 : ");
 
                color(BLACK,WHITE);
                printf("%d\n\n", moveCount);
 
                if (wallDetected==1)
                {
                        color(BLACK,YELLOW);
                        printf("SYSTEM : ");
 
                        color(BLACK,WHITE);
                        printf("으악! 벽이다. \n\n");
                }
                input=_getch();
 
                switch (input)
                {
 
                case 72 :
                       
                        moveCount++;
                        wallDetected=0;
 
                        map[myPosition_Y][myPosition_X]=0;
                        myPosition_Y--;
                       
                        if (map[myPosition_Y][myPosition_X]==1)
                        {
                                moveCount--;
                                wallDetected=1;
 
                                myPosition_Y++;
                        }
 
                        else if (map[myPosition_Y][myPosition_X]==3)
                                loop=0;
 
                        break;
 
                case 75 :
                       
                        moveCount++;
                        wallDetected=0;
 
                        map[myPosition_Y][myPosition_X]=0;
                        myPosition_X--;
 
                        if (map[myPosition_Y][myPosition_X]==1)
                        {
                                moveCount--;
                                wallDetected=1;
 
                                myPosition_X++;
                        }
 
                        else if (map[myPosition_Y][myPosition_X]==3)
                                loop=0;
 
                        break;
 
                case 77 :
 
                        moveCount++;
                        wallDetected=0;
 
                        map[myPosition_Y][myPosition_X]=0;
                        myPosition_X++;
 
                        if (map[myPosition_Y][myPosition_X]==1)
                        {
                                moveCount--;
                                wallDetected=1;
 
                                myPosition_X--;
                        }
 
                        else if (map[myPosition_Y][myPosition_X]==3)
                                loop=0;
 
                        break;
 
                case 80 :
 
                        moveCount++;
                        wallDetected=0;
 
                        map[myPosition_Y][myPosition_X]=0;
                        myPosition_Y++;
 
                        if (map[myPosition_Y][myPosition_X]==1)
                        {
                                moveCount--;
                                wallDetected=1;
 
                                myPosition_Y--;
                        }
 
                        else if (map[myPosition_Y][myPosition_X]==3)
                                loop=0;
 
                        break;
 
                case 27 :
 
                        loop=0;
 
                        break;
                }
 
        } while(loop);
 
        return 0;
}