본문 바로가기
c#/수업내용

2020.05.01 이차원 배열 움직이기

by Luna_O 2020. 5. 1.

실행 코드

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
enum eMoveType
    {
        Up, Down, Right, Left
    }
    class App
    {
        public int[] xy;
        public int[,] arr;
 
        public App()
        {
            //int[,] arr = new int[6, 3]
            //{ { 0, 0, 0 }, { 0, 0, 0 }, { 0 ,0, 0 }, { 0, 0, 0 },{ -1, -1, 0 }, { -1, -1, -1 } };
            arr = new int[63];
            arr[40= -1;
            arr[41= -1;
            arr[50= -1;
            arr[51= -1;
            arr[52= -1;
            
            for (int i = 0; i < 6; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    Console.Write(arr[i, j]);
                }
                Console.WriteLine("");
            }
 
            int tileval = GetTileVal(1-1);
 
            Console.WriteLine(tileval);
            var position = this.GetHeroPos(); //1, -1
            Console.WriteLine("초기 현재 위치 : {0}, {1}", position[0], position[1]);
            this.Move(eMoveType.Up);
            var pos = this.GetHeroPos();
            Console.WriteLine("Up 이동 위치 : {0}, {1}", pos[0], pos[1]);
            this.Move(eMoveType.Down);
            pos = this.GetHeroPos();
            Console.WriteLine("Down 이동 위치 : {0}, {1}", pos[0], pos[1]);
            this.Move(eMoveType.Right);
            pos = this.GetHeroPos();
            Console.WriteLine("Right 이동 위치 : {0}, {1}", pos[0], pos[1]);
            this.Move(eMoveType.Left);
            pos = this.GetHeroPos();
            Console.WriteLine("Left 이동 위치 : {0}, {1}", pos[0], pos[1]);
        }
        public int GetTileVal(int x, int y)
        {
            if (x == 1 && y == -1)
            {
                arr[-y, x] = 100;
            }
            return arr[-y, x];
        }
 
        public int[] GetHeroPos()
        {
            for (int i = 0; i < 6; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if(arr[i, j] == 100)
                    {
                        xy = new int[2];
                        xy[0= i;
                        xy[1= -j;
                        break;
                    }
                }
            }
            return this.xy;
        }
        public void Move(eMoveType moveType)
        {
            int x = this.xy[0];
            int y = this.xy[1];
            this.arr[x, -y] = 0;
            switch (moveType) 
            {
                case eMoveType.Down:
                    y--;
                    break;
                case eMoveType.Up:
                    y++;
                    break;
                case eMoveType.Right:
                    x++;
                    break;
                case eMoveType.Left:
                    x--;
                    break;
            }
            this.arr[x, -y] = 100;
        }
 
    }
 
cs

실행 창