ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 2차원 배열의 늑대와 영웅
    C#/수업과제 2022. 6. 15. 18:01
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Study05
    {
        public class App
        {
            //생산자
            public App()
            {
                new Game();
    
    
            }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Study05
    {
        public class Game
        {
            public Game()
            {
                Map map = new Map();
                map.ReadMap();
    
                Hero hero = new Hero("홍길동", map);
                map.Index[0, 0] = hero.HeroId;
                map.ReadMap();
    
                Wolf wolf = new Wolf(map);
                map.Index[3, 1] = wolf.WolfId;
                map.ReadMap();
    
                hero.HeroMoveRight();
                map.ReadMap();
                hero.HeroMoveRight();
                map.ReadMap();
    
                hero.HeroMoveDown();
                map.ReadMap();
                hero.HeroMoveDown();
                map.ReadMap();
                hero.HeroMoveDown();
                map.ReadMap();
    
                hero.HeroMoveRight();
                map.ReadMap();
    
                hero.HeroMoveLeft();
                map.ReadMap();
    
                hero.HeroMoveUp();
                map.ReadMap();
    
                hero.HeroMoveLeft();
                map.ReadMap();
    
                hero.HeroMoveUp();
                map.ReadMap();
                hero.HeroMoveLeft();
                map.ReadMap();
                hero.HeroMoveDown();
                map.ReadMap();
    
            }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Study05
    {
        public class Hero
        {
            public int HeroId = 10;
            private int HeroDamage = 5;
            public string heroName = null;
            public Map map;
            public int[,] HeroLocation;
            public Hero(string name, Map map)
            {
                this.heroName = name;
                this.map = map;
                this.HeroLocation = map.Index;
                Console.WriteLine("영웅\"{0}\"이 생성되었습니다.", this.heroName);
            }
    
            public void HeroMoveRight()
            {
                for (int i = 0; i < this.map.Index.GetLength(0); i++)
                {
                    for (int j = 0; j < this.map.Index.GetLength(1); j++)
                    {
    
                        if ((this.map.Index[i, j] == 10) && j == 3)
                        {
                            Console.WriteLine("오른쪽으로 더는 이동할 수 없습니다.");
                            return;
                        }
                        if ((this.map.Index[i, j] == 10) && (this.map.Index[i, j + 1] != 0))
                        {
                            Console.WriteLine("방해물로 인해 이동할 수 없습니다.");
                            return;
                        }
                        else if ((this.map.Index[i, j] == 10) && (j != 3))
                        {
                            this.HeroLocation[i, j] = 0;
                            this.HeroLocation[i, j + 1] = 10;
                            this.map.Index[i, j] = 0;
                            this.map.Index[i, j + 1] = 10;
                            return;
                        }
    
                    }
                }
            }
            public void HeroMoveLeft()
            {
                for (int i = 0; i < this.map.Index.GetLength(0); i++)
                {
                    for (int j = 0; j < this.map.Index.GetLength(1); j++)
                    {
    
                        if ((this.map.Index[i, j] == 10) && (j == 0))
                        {
                            Console.WriteLine("왼쪽으로 더는 이동할 수 없습니다.");
                            return;
                        }
                        if ((this.map.Index[i, j] == 10) && (this.map.Index[i, j - 1] != 0))
                        {
                            Console.WriteLine("방해물로 인해 이동할 수 없습니다.");
                            return;
                        }
                        else if ((this.map.Index[i, j] == 10) && (j != 0))
                        {
                            this.HeroLocation[i, j] = 0;
                            this.HeroLocation[i, j - 1] = 10;
                            this.map.Index[i, j] = 0;
                            this.map.Index[i, j - 1] = 10;
                            return;
                        }
    
                    }
                }
            }
            public void HeroMoveDown()
            {
                for (int i = 0; i < this.map.Index.GetLength(0); i++)
                {
                    for (int j = 0; j < this.map.Index.GetLength(1); j++)
                    {
    
                        if ((this.map.Index[i, j] == 10) && i == 3)
                        {
                            Console.WriteLine("아래쪽으로 더는 이동할 수 없습니다.");
                            return;
                        }
                        if ((this.map.Index[i, j] == 10) && (this.map.Index[i + 1, j] != 0))
                        {
                            Console.WriteLine("방해물로 인해 이동할 수 없습니다.");
                            return;
                        }
                        else if ((this.map.Index[i, j] == 10) && (i != 3))
                        {
                            this.HeroLocation[i, j] = 0;
                            this.HeroLocation[i + 1, j] = 10;
                            this.map.Index[i, j] = 0;
                            this.map.Index[i + 1, j] = 10;
                            return;
                        }
    
                    }
                }
            }
            public void HeroMoveUp()
            {
                for (int i = 0; i < this.map.Index.GetLength(0); i++)
                {
                    for (int j = 0; j < this.map.Index.GetLength(1); j++)
                    {
    
                        if ((this.map.Index[i, j] == 10) && i == 0)
                        {
                            Console.WriteLine("위으로 더는 이동할 수 없습니다.");
                            return;
                        }
                        if ((this.map.Index[i, j] == 10) && (this.map.Index[i - 1, j] != 0))
                        {
                            Console.WriteLine("방해물로 인해 이동할 수 없습니다.");
                            return;
                        }
                        else if ((this.map.Index[i, j] == 10) && (i != 0))
                        {
                            this.HeroLocation[i, j] = 0;
                            this.HeroLocation[i - 1, j] = 10;
                            this.map.Index[i, j] = 0;
                            this.map.Index[i - 1, j] = 10;
                            return;
                        }
    
                    }
                }
            }
            public void HeroAttack(Wolf target)
            {
                for (int i = 0; i < this.map.Index.GetLength(0); i++)
                {
                    for (int j = 0; j < this.map.Index.GetLength(1); j++)
                    {
                        if ((this.map.Index[i, j] == 10) && (this.map.Index[i + 1, j] == 100))
                        {
                            Console.WriteLine("{0}을 공격합니다", target.WolfName);
                            if (target.WolfHp > 0)
                            {
                                target.WolfHp -= this.HeroDamage;
                                Console.WriteLine("{0}이 {1}만큼 데미지를 입었습니다. \t {0}/{2}", target.WolfName, this.HeroDamage, target.WolfFullHp);
                            }
                            else
                            {
                                Console.WriteLine("늑대가 사망하였습니다.");
                                this.map.Index[3, 1] = 0;
                            }
                        }
    
                    }
    
                }
            }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Study05
    {
        public class Map
        {
    
            public int[,] Index = new int[4, 4];
            public Map()
            {
                Index[2, 0] = -1;
                Index[2, 1] = -1;
                Index[3, 3] = -1;
            }
    
            public void ReadMap()
            {
    
                for (int i = 0; i < this.Index.GetLength(0); i++)
                {
                    for (int j = 0; j < this.Index.GetLength(1); j++)
                    {
                        Console.Write("{0}\t", this.Index[i, j]);
                    }
                    Console.WriteLine();
                }
                Console.WriteLine("-------------------------------------");
    
            }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Study05
    {
        public class Wolf
        {
            public string WolfName = "늑대";
            public int WolfId = 100;
            public int WolfHp = 20;
            public int WolfFullHp = 20;
            public Map map;
            public int[,] WolfLocation;
            public Wolf(Map map)
            {
                this.map = map;
                this.WolfLocation = map.Index;
                Console.WriteLine("늑대가 생성되었습니다");
            }
    
            public void WolfTakeHit()
            { 
            
            }
        }
    }

    'C# > 수업과제' 카테고리의 다른 글

    텍스트 Rpg만들기  (0) 2022.06.21
    데이터 저장 연습과제  (0) 2022.06.20
    2048게임  (0) 2022.06.15
    for문 연습 문제  (0) 2022.06.10
    디아블로 아이템 출력하기  (0) 2022.06.09
Designed by Tistory.