본문 바로가기

정리 카테고리/실습_과제

실습_특수문자로 맵 구현하기

1.요구 사항

-맵정보를 숫자가 아닌 텍스트로

-맵 정보는 파일에서 불러오기

-못가는 지역 적용하고 메시지 출력하기




2.결과 화면 및 필요 파일

//이번 실행영상의 실행을 위해서는 D드라이브에 map.txt와 mapinfo.txt를 넣으신후 진행하여야 됩니다.


map.txt

mapinfo.txt

텍스트 지도.exe






3.소스 코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
class Program
{
 
 
    public static string ColoredConsoleWrite(ConsoleColor color, string text)
    {
        ConsoleColor originalColor = Console.ForegroundColor;
        Console.ForegroundColor = color;
        Console.Write(text);
        Console.ForegroundColor = originalColor;
        return text;
    }
 
 
    string[,] map01 = new string[3238];//01번 맵 정보
    int[,] mapInfo01 = new int[3238];
    int[] userHere = new int[3];//x좌표,y좌표,맵번호(맵이 다중일때 활용)
    int[] userBeforeHere = new int[3];//유저의 이동전 좌표
//메인===================================================
    static void Main(string[] args)
    {
        Console.SetWindowSize(11040);
        Program p = new Program();
        p.MapMake();//맵 출력정보 초기화
        p.MapInfoMake();//맵 비교정보 초기화
        p.UserSet();//유저 초기화
        bool checkLoop = true;//프로그램 반복문 제어
        int checkEvent = 0;//
        string command = "";//명령어 받는 변수
        p.PrintMap();//맵 출력
        //반복문 시작
        while (checkLoop)
        {
 
            Console.WriteLine("명령어를 입력하시오.");
            Console.WriteLine("동,서,남,북,끝");
            Console.Write("::>");
            command = Console.ReadLine();
            Console.Clear();
            if (command == "끝"break;
            else if (command == "동") p.MoveEast();
            else if (command == "서") p.MoveWest();
            else if (command == "남") p.MoveSouth();
            else if (command == "북") p.MoveNorth();
            else
            {
                Console.WriteLine("잘못된 명령어 입니다.");
                p.PrintMap();//
            }
 
        }
 
 
        Console.WriteLine("Press any key to exit.");
 
        Console.ReadLine();
    }
//메인===================================================
 
//맵 초기화
    void MapMake()
    {
        //출력되는 맵 정보 불러오기(특수문자)
        string path = @"D:\map.txt";
        string[] lines = System.IO.File.ReadAllLines(path);
        int i = 0;
        foreach (string line in lines)
        {
            var arrX = line.Split(',');
            for (int j = 0; j < 38; j++)
            {
                map01[i, j] = arrX[j];
            }
            i++;
        }
    }
    //유저 정보 초기화
    void UserSet()
    {
        userHere[0= 4;
        userHere[1= 4;
        userHere[2= 1;
        userBeforeHere = userHere;
    }
    //맵 정보 초기화
    void MapInfoMake()
    {
        //맵 정보 불러오기(숫자 - 벽등 비교정보)
        string path = @"D:\mapinfo.txt";
        string[] lines = System.IO.File.ReadAllLines(path);
        int i = 0;
        foreach (string line in lines)
        {
            var arrX = line.Split(',');
            for (int j = 0; j < 38; j++)
            {
                mapInfo01[i, j] = int.Parse(arrX[j]);
            }
            i++;
        }
    }
    //동 이동
    void MoveEast()
    {
        if (mapInfo01[userHere[1], (userHere[0+ 1)] == 9) { ColoredConsoleWrite(ConsoleColor.Green, "나무로 막혀있습니다.\n"); PrintMap(); }
        else if (userHere[0+ 1 < mapInfo01.GetLength(1&& mapInfo01[userHere[1], (userHere[0+ 1)] != 1)
        {
            userBeforeHere = userHere;
            userHere[0+= 1;
            Console.WriteLine("정상적으로 이동했습니다.");
            PrintMap();
        }
        else { Console.WriteLine("이동할수 없는 공간입니다."); PrintMap(); }
    }
    //서 이동
    void MoveWest()
    {
        if (mapInfo01[userHere[1], (userHere[0- 1)] == 9) { ColoredConsoleWrite(ConsoleColor.Green, "나무로 막혀있습니다.\n"); PrintMap(); }
        else if (userHere[0- 1 > -1 && mapInfo01[userHere[1], (userHere[0- 1)] != 1)
        {
            userBeforeHere = userHere;
            userHere[0-= 1;
            Console.WriteLine("정상적으로 이동했습니다.");
            PrintMap();
        }
        else { Console.WriteLine("이동할수 없는 공간입니다."); PrintMap(); }
    }
    //남 이동
    void MoveSouth()
    {
        if (mapInfo01[(userHere[1+ 1), userHere[0]] == 9) { ColoredConsoleWrite(ConsoleColor.Green, "나무로 막혀있습니다.\n"); PrintMap(); }
        else if (userHere[1+ 1 < mapInfo01.GetLength(0&& mapInfo01[(userHere[1+ 1), userHere[0]] != 1)
        {
            userBeforeHere = userHere;
            userHere[1+= 1;
            Console.WriteLine("정상적으로 이동했습니다.");
            PrintMap();
        }
        else { Console.WriteLine("이동할수 없는 공간입니다."); PrintMap(); }
    }
    //북 이동
    void MoveNorth()
    {
        if (mapInfo01[(userHere[1- 1), userHere[0]] == 9) { ColoredConsoleWrite(ConsoleColor.Green, "나무로 막혀있습니다.\n"); PrintMap(); }
        else if (userHere[1- 1 > -1 && mapInfo01[(userHere[1- 1), userHere[0]] != 1)
        {
            userBeforeHere = userHere;
            userHere[1-= 1;
            Console.WriteLine("정상적으로 이동했습니다.");
            PrintMap();
        }
        else { Console.WriteLine("이동할수 없는 공간입니다."); PrintMap(); }
    }
    //맵 출력
    void PrintMap()
    {
        for (int i = 0; i < mapInfo01.GetLength(0); i++)
        {
            for (int j = 0; j < mapInfo01.GetLength(1); j++)
            {
                if (mapInfo01[i, j] == 1) { ColoredConsoleWrite(ConsoleColor.DarkYellow, map01[i, j]); }
                else if (i == userHere[1&& j == userHere[0]) { ColoredConsoleWrite(ConsoleColor.Cyan, "●"); }
                else if (mapInfo01[i, j] == 5) { ColoredConsoleWrite(ConsoleColor.Red, map01[i, j]); }//사냥터 정보
                else if (mapInfo01[i, j] == 9) { ColoredConsoleWrite(ConsoleColor.Green, map01[i, j]); }//나무정보
                else { ColoredConsoleWrite(ConsoleColor.White, map01[i, j]); }
            }
            Console.WriteLine();
        }
    }
}
 
 
출처: http://2god.tistory.com/57 [To.]
cs