본문 바로가기

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

실습_배열,맵이동 맵출력 프로그램

1.요구 사항

-배열 사용

-맵내에서 이동

-1번맵에서 2번맵 이동 포탈

-현재 맵에서의 위치 표현



2.결과 화면 및 샘플


배열_맵이동,위치출력,포탈.exe





3.소스 코드


PS. 동서남북 에서 쓰는  다차원배열 변수명.GetLength(1);  은 다중배열에서 각 차원의 개수를 출력해준다.

  예를 들어 GetLength(0) 을 입력하면 1차원의 배열의 개수를 뽑아준다.

 
       //글자색 넣어주는 함수 시작
        public static string ColoredConsoleWrite(ConsoleColor color, string text)
        {
            ConsoleColor originalColor = Console.ForegroundColor;
            Console.ForegroundColor = color;
            Console.Write(text);
            Console.ForegroundColor = originalColor;
            return text;
        }
        //글자색 넣어주는 함수 끝
        static void Main(string[] args)
        {
            Console.SetWindowSize(110, 30);//콘솔창 크기 (가로 세로)
            int checkMap = 0;//속해있는 맵 번호
            int userX = 5;//x축 좌표
            int userY = 5;//y축 좌표
            string command = "";//명령어 받는 변수
            int[,] array1 = //1번 맵
            {
                {1,1,1,1,1,1,1,1,1,1 },
                {1,0,0,0,0,0,0,0,0,1 },
                {1,0,0,0,0,0,0,0,0,1 },
                {1,0,0,0,0,0,0,0,0,1 },
                {1,0,0,0,0,0,0,0,0,1 },
                {1,0,0,0,0,0,0,0,0,1 },
                {1,0,0,0,0,0,0,0,0,1 },
                {1,0,0,0,0,0,0,0,0,1 },
                {1,3,0,0,0,0,0,0,0,1 },
                {1,1,1,1,1,1,1,1,1,1 }
            };
            int[,] array2 =//2번 맵 
            {
                {1,1,1,1,1,1,1,1,1,1,1,1,1 },
                {1,3,0,0,0,0,0,0,0,0,0,0,1 },
                {1,0,0,0,0,0,0,0,0,0,0,0,1 },
                {1,0,0,0,0,0,0,0,0,0,0,0,1 },
                {1,0,0,0,0,0,0,0,0,0,0,0,1 },
                {1,0,0,0,0,0,0,0,0,0,0,0,1 },
                {1,0,0,0,0,0,0,0,0,0,0,0,1 },
                {1,0,0,0,0,0,0,0,0,0,0,0,1 },
                {1,0,0,0,0,0,0,0,0,0,0,0,1 },
                {1,0,0,0,0,0,0,0,0,0,0,0,1 },
                {1,0,0,0,0,0,0,0,0,0,0,0,1 },
                {1,1,1,1,1,1,1,1,1,1,1,1,1 }
            };
            while (true)//반복 시작
            {
                Console.Clear();//화면 클리어
                Console.WriteLine(userX + "," + userY + ":" + checkMap);//유저의 x,y축:: 속해있는 맵번호
                if (checkMap == 0)//1번 맵에 있을 경우 맵출력
                {
                    for (int x = 0; x < 10; x++)
                    {
                        for (int y = 0; y < 10; y++)
                        {
                            if (userX == y && userY == x) ColoredConsoleWrite(ConsoleColor.Yellow, array1[x, y].ToString());
                            else Console.Write(array1[x, y]);
                            if (y < 9) Console.Write("\t");

                        }
                        Console.WriteLine();
                    }
                }
                else if (checkMap == 1)//2번 맵에 있을 경우 맵출력
                {
                    for (int x = 0; x < 12; x++)
                    {
                        for (int y = 0; y < 13; y++)
                        {
                            if (userX == y && userY == x) ColoredConsoleWrite(ConsoleColor.Yellow, array2[x, y].ToString());
                            else Console.Write(array2[x, y]);
                            if (y < 12) Console.Write("\t");

                        }
                        Console.WriteLine();
                    }
                }
                Console.WriteLine("원하는 명령어를 입력하시오.");
                Console.WriteLine("끝,동,서,남,북");
                Console.Write("::>");
                command = Console.ReadLine();//명령어 받기
                if (command == "끝") break;//나가기 구현
                if (checkMap == 0)//맵 1일때 이동 구현
                {
                    if (command == "동")//우로 가는거 구현
                    {
                        if ((userX + 1) < array1.GetLength(1) && array1[userY, userX + 1] != 1)
                        {
                            userX++;
                            if (array1[userY, userX ] == 3)//맵이동관련
                            {
                                checkMap++;
                                userX = 1;
                                userY = 1;
                            }
                        }

                        else Console.WriteLine("이동할수 없습니다.");
                    }
                    if (command == "서")//좌로 가는거 구현
                    {
                        if ((userX - 1) > -1 && array1[userY, userX - 1] != 1)
                        {
                            userX--;
                            if (array1[userY, userX ] == 3)
                            {
                                checkMap++;
                                userX = 1;
                                userY = 1;
                            }
                        }
                        else Console.WriteLine("이동할수 없습니다.");
                    }
                    if (command == "남")//밑으로 가는거 구현
                    {
                        if ((userY + 1) < array1.GetLength(0) && array1[userY + 1, userX] != 1)
                        {
                            userY++;
                            if (array1[userY, userX ] == 3)//맵이동관련
                            {
                                checkMap++;
                                userX = 1;
                                userY = 1;
                            }
                        }
                        else Console.WriteLine("이동할수 없습니다.");
                    }
                    if (command == "북")//위로 가는거 구현
                    {
                        if ((userY - 1) > -1 && array1[userY - 1, userX] != 1)
                        {
                            userY--;
                            if (array1[userY, userX ] == 3)//맵이동관련
                            {
                                checkMap++;
                                userX = 1;
                                userY = 1;
                            }
                        }
                        else Console.WriteLine("이동할수 없습니다.");
                    }
                }
                else if (checkMap == 1)//맵2 일때 이동 구현
                {
                    if (command == "동")
                    {
                        if ((userX + 1) < array2.GetLength(1) && array2[userY, userX + 1] != 1)
                        {
                            userX++;
                            if (array2[userY, userX ] == 3)//맵이동관련
                            {
                                checkMap--;
                                userX = 1;
                                userY = 8;
                            }
                        }
                        else Console.WriteLine("이동할수 없습니다.");
                    }
                    if (command == "서")
                    {
                        if ((userX - 1) > -1 && array2[userY, userX - 1] != 1)
                        {
                            userX--;
                            if (array2[userY, userX ] == 3)//맵이동관련
                            {
                                checkMap--;
                                userX = 1;
                                userY = 8;
                            }
                        }
                        else Console.WriteLine("이동할수 없습니다.");
                    }
                    if (command == "남")
                    {
                        if ((userY + 1) < array2.GetLength(0) && array2[userY + 1, userX] != 1)
                        {
                            userY++;
                            if (array2[userY, userX ] == 3)//맵이동관련
                            {
                                checkMap--;
                                userX = 1;
                                userY = 8;
                            }
                        }
                        else Console.WriteLine("이동할수 없습니다.");
                    }
                    if (command == "북")
                    {
                        if ((userY - 1) > -1 && array2[userY - 1, userX] != 1)
                        {
                            userY--;
                            if (array2[userY, userX ] == 3)//맵이동관련
                            {
                                checkMap--;
                                userX = 1;
                                userY = 8;
                            }
                        }
                        else Console.WriteLine("이동할수 없습니다.");
                    }
                    
                }
            }
            Console.ReadKey();
        }