C#/수업내용

2020.04.08. 수업내용 - 유닛 좌우 움직이기 (특정위치 아이템)

dev_sr 2020. 4. 8. 22:35

1. 유닛은 left, right로 최대 3번만 움직인다. (x좌표)

2. left 공백 3 (left 3) 또는 right 공백 3 (right 3) 으로 입력받는다. (split 필요)

3. (-4,0)위치에 도달하면 '장검' 아이템을 먹는데 

4. 나중에 다시 가면 없어져서 못 먹는다.

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
            //유닛 왼쪽 오른쪽 이동 , (-4,0)지점에서 아이템을 한번만 먹는다
 
 
            int positionX = 0;
            int positionY = 0;
            bool getItem = false;
 
            Console.WriteLine("유닛명 : SCV");
            Console.WriteLine("이동 명령어 : left 횟수 또는 right 횟수 (최대 3번이동 가능)");
            Console.WriteLine("유닛의 초기 위치 : ({0},{1})", positionX,positionY);
            Console.WriteLine(); //한줄 공백
 
            while(true)
            {
                Console.Write("유닛을 이동하려면 명령어를 입력하세요. ");
 
                string input=Console.ReadLine();                //문자열 값 입력받기 left 3
                string[] strMoveStep = input.Split(' ');        //공백을 기준으로 나누기 left 와 3
 
                string direction = strMoveStep[0];              //direction에는 방향(left) 할당
                int moveStep = Int32.Parse(strMoveStep[1]);     //MoveStep에는 숫자(3) 할당
 
                Console.WriteLine(); //한줄 공백
 
                if (moveStep>3)
                {
                    Console.WriteLine("범위를 초과했습니다.");
                    continue;
                }
 
                if(direction == "left")
                {
                    Console.WriteLine("이전 위치 : ({0},{1})", positionX, positionY);
 
                    for (int i = 0; i < moveStep; i++)
                    {
                        positionX--;
                        Console.WriteLine("({0},{1})",positionX,positionY);
 
                    }
                    Console.WriteLine(); //한줄 공백
                }
 
                if (direction == "right")
                {
                    Console.WriteLine("이전 위치 : ({0},{1})", positionX, positionY);
 
                    for (int i = 0; i < moveStep; i++)
                    {
                        positionX++;
                        Console.WriteLine("({0},{1})", positionX, positionY);
 
                    }
                    Console.WriteLine(); //한줄 공백
                }
 
                if(positionX==-4 && getItem==false)
                {
                    Console.WriteLine("장검을 주었습니다.");
                    getItem = true;
                    Console.WriteLine(); //한줄 공백
                }
 
 
 
            }