Yang.공부방

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
 
namespace Lol
{
    //싱글톤 (인스턴스를 하나만 생성하여 어떠한 곳에서도 접근이 가능하게끔 만든다)
    public class DataManager
    {
        public static DataManager instance;
        public Dictionary<int, CharacterData> dicCharacterData = new Dictionary<int, CharacterData>();
 
        private DataManager()
        {
        }
 
        public static DataManager GetInstance()
        {
            if (DataManager.instance == null)
            {
                DataManager.instance = new DataManager();
            }
            return DataManager.instance; 
        }
 
        //파일읽는 메소드(역직렬화) : CharacterData 즉, 변하면 안되는 기본 데이터를 불러온다
        public void LoadDatas()
        {
 
            var json = File.ReadAllText("./data/character_data.json");
 
            var arrCharacterData = JsonConvert.DeserializeObject<CharacterData[]>(json);
         
            foreach(var data in arrCharacterData)
            {
                dicCharacterData.Add(data.id, data);
            }
            Console.WriteLine("모든 데이터가 로드 되었습니다");
        }
    }
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Lol
{
    interface IMoveable //인터페이스
    {
        void Move(Vector2 position);
    }
}
 
cs

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Lol
{
    public class Vector2
    {
        public float x;
        public float y;
 
        public Vector2(float x, float y)
        {
            this.x = x;
            this.y = y;
        }
 
        public static Vector2 operator+  (Vector2 a, Vector2 b)
        {
            return new Vector2(a.x + b.x, a.y + b.y);
        }
 
        public static Vector2 operator -(Vector2 a, Vector2 b)
        {
            return new Vector2(a.x - b.x, a.y - b.y);
        }
 
        public static float GetDistance(Vector2 a, Vector2 b)
        {
            return (float)Math.Sqrt(Math.Pow(b.x - a.x, 2+ Math.Pow(b.y - a.y, 2));
        }
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Lol
{
    public class UserInfo //사용자의 데이터를 저장하는 클래스
    {
        public GameInfo gameInfo;
        public bool newBieCheck = true//신규유저인지 확인하는 변수
 
        public UserInfo()
        {
            this.gameInfo = new GameInfo();
        }
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Lol
{
    public class GameInfo
    {
        public List<CharacterInfo> characterInfos;
 
        public GameInfo()
        {
            this.characterInfos = new List<CharacterInfo>();
        }
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Lol
{
    public class CharacterInfo
    {
        public int id; //원래는 변하면 안되지만 Key로 사용한다.
        public int hp; //변할수 있는 값
 
        public CharacterInfo(int id, int hp)
        {
            this.id = id;
            this.hp = hp;
        }
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Lol
{
    public class CharacterData //변하면 안되는 기본 데이터
    {
        public int id;
        public int type;
        public string name;
        public int hp;
        public int damage;
        public float range;
    }
}
 
cs

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
 
namespace Lol
{
    //싱글톤 (인스턴스를 하나만 생성하여 어떠한 곳에서도 접근이 가능하게끔 만든다)
    public class DataManager
    {
        public static DataManager instance;
        public Dictionary<int, CharacterData> dicCharacterData = new Dictionary<int, CharacterData>();
 
        private DataManager()
        {
        }
 
        public static DataManager GetInstance()
        {
            if (DataManager.instance == null)
            {
                DataManager.instance = new DataManager();
            }
            return DataManager.instance; 
        }
 
        //파일읽는 메소드(역직렬화) : CharacterData 즉, 변하면 안되는 기본 데이터를 불러온다
        public void LoadDatas()
        {
 
            var json = File.ReadAllText("./data/character_data.json");
 
            var arrCharacterData = JsonConvert.DeserializeObject<CharacterData[]>(json);
         
            foreach(var data in arrCharacterData)
            {
                dicCharacterData.Add(data.id, data);
            }
            Console.WriteLine("모든 데이터가 로드 되었습니다");
        }
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Lol
{
    public class Anivia : Character ,IMoveable
    {
        public Anivia() : base()
        {
        }
 
        public virtual void Move(Vector2 position)
        {
        }
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Lol
{
    interface IMoveable //인터페이스
    {
        void Move(Vector2 position);
    }
}
 
cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
 
namespace Lol
{
    public class GameLauncher
    {
        private List<Character> characters = new List<Character>();
        private UserInfo userInfo;
        private Baron baron;
        private Anivia anivia;
 
        public GameLauncher()
        {
            DataManager.GetInstance().LoadDatas(); //파일불러오기
            this.GameStart();
            this.Save();
 
        }
 
        public void GameStart()
        {
            Console.WriteLine("게임이 시작되었습니다");
            
            if (File.Exists("./info/user_info.json"))
            {
                //this.userInfo.newBie = false;
                Console.WriteLine("기존유저입니다");
                var json = File.ReadAllText("./info/user_info.json"); //저장되어있는 info 파일 읽기
                var info = JsonConvert.DeserializeObject<UserInfo>(json); //역직렬화
                this.CreateUserInfo(info);//userinfo클래스의 인스턴스 생성
 
            }
            else
            {
                Console.WriteLine("신규유저입니다");
                Directory.CreateDirectory("./info");
 
                this.CreateUserInfo();
            }
 
 
 
            Console.WriteLine("게임준비완료");
 
            //캐릭터 생성
 
            if (this.userInfo.newBieCheck == true// 신규유저일 경우
            { 
                this.userInfo.newBieCheck = false;
                this.baron = this.CreateCharacterInfo<Baron>(0);
                //this.anivia = this.CreateCharacterInfo<Anivia>(1);
                baron.TakeDamage(1);
                characters.Add(baron);
            }
            else//기존유저일경우
            {
                //this.baron = this.CreateCharacterInfo<Baron>(this.userInfo.gameInfo.characterInfos[0]);
                //this.anivia = this.CreateCharacterInfo<Anivia>(this.userInfo.gameInfo.characterInfos[1]);
                //인덱스 오류
            }
        }
 
        //UserInfo 생성
        public void CreateUserInfo(UserInfo info = null//선택적 매개변수 (아무 값이 들어오지 않으면 null로 전달)
        {
            if (userInfo == null)
            {
                if (info == null//아무값이 안들어오면 UserInfo클래스의 인스턴스를 생성후 userInfo에 넣어준다
                {
                    this.userInfo = new UserInfo();
                }
                else
                {
                    this.userInfo = info; //값이 있을경우 userInfo에 넣어준다
                }
            }
            else
            {
                Console.WriteLine("이미 UserInfo객체가 있습니다");
            }
        }
 
        //저장메서드
        public void Save()
        {
            var characterInfo = new List<CharacterInfo>(); //새로운 List인스턴스생성
            foreach (var character in this.characters)
            {
                characterInfo.Add(character.Info); //값을 복사한다.
            }
            this.userInfo.gameInfo.characterInfos = characterInfo; //List 인스턴스 characterInfos 에 복사한 값을 넣어준다.
 
            //직렬화
            var json = JsonConvert.SerializeObject(this.userInfo);
            File.WriteAllText("./info/user_info.json", json, Encoding.UTF8);
            Console.WriteLine("저장완료");
        }
 
        private T CreateCharacterInfo<T>(CharacterInfo info) where T : Character, new() //기존유저일경우
        {
            var data = DataManager.GetInstance().dicCharacterData[info.id];
 
            var character = new T();
            character.Info = info;
 
 
            return character;
        }
 
        //오버로드
        private T CreateCharacterInfo<T>(int id) where T : Character, new() //신규유저일경우
        {
            var data = DataManager.GetInstance().dicCharacterData[id];
            var info = new CharacterInfo(data.id, data.hp);
 
            var character = new T();
            character.Info = info;
 
            return character;
        }
    }
}
 
cs


'C# > Problems' 카테고리의 다른 글

Array 인벤토리 잘모르겠는것  (0) 2019.04.03

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _0410Test
{
    public class CharacterInfo
    {
        public int id;
        public int hp;
 
        public CharacterInfo(int id , int hp)
        {
            this.id = id;
            this.hp = hp;
        }
    }
}
 
cs

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _0410Test
{
    public class Character
    {
        private CharacterInfo characterinfo;
        private int v;
 
        public CharacterInfo info { get; set; }
 
        public Character()
        {
        }
        
        public void TakeDamage(int damage)
        {
            this.info.hp -= damage;
            var data = new CharacterData();
 
            if (this.info.hp <= 0)
            {
                this.info.hp = 0;
                Console.WriteLine("{0}이(가) 사망하였습니다. ({1}/{2})", data.name, info.hp, data.hp);
            }
            else
            {
                Console.WriteLine("{0}이(가) 피해 (-{1})받았습니다 ({2}/{3})", data.name, damage, info.hp, data.hp);
            }
        }
 
        public void AttackDamage(Character Target)
        {
            
        }
 
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _0410Test
{
    public class GameInfo
    {
        public List<CharacterInfo> characterInfo;
 
        public GameInfo()
        {
            this.characterInfo = new List<CharacterInfo>();
        }
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _0410Test
{
    public class UserInfo
    {
        public GameInfo gameInfo;
        
        public UserInfo()
        {
            this.gameInfo = new GameInfo();
        }
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _0410Test
{
    public class CharacterData
    {
        public int id;
        public int type;
        public string name;
        public int hp;
        public int damage;
        public float range;
 
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _0410Test
{
    public class Monster : Character
    {
        public Monster() : base()
        {
        }
 
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _0410Test
{
    public class Champion : Character
    {
        public Champion() :base()
        {
        }
    }
}
 
cs
\
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _0410Test
{
    public class Anivia : Champion
    {
        public Anivia() : base()
        {
        }
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _0410Test
{
    public class Baron : Monster
    {
        public Baron() : base()
        {
        }
 
    }
}
 
cs
\
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
 
namespace _0410Test
{
    public class GameLauncher
    {
        private List<Character> characters = new List<Character>();
        private UserInfo userInfo;
 
        private const string USER_INFO_FILE_PATH = "./info/user_info.json";
        Dictionary<int, CharacterData> dicCharacterData = new Dictionary<int, CharacterData>();
        Dictionary<int, CharacterInfo> dicCharacterInfo = new Dictionary<int, CharacterInfo>();
 
        public GameLauncher()
        {
        }
 
        public void gameStart()
        {
 
            //this.LoadData();
            //Console.WriteLine("게임준비완료");
 
            if (Directory.Exists("./info"))
            {
                Console.WriteLine("기존유저입니다");
                //CharacterInfo characterinfo = null;
 
                var json = File.ReadAllText(USER_INFO_FILE_PATH);
                var arrcharacterinfo = JsonConvert.DeserializeObject<CharacterInfo[]>(json);
                //this.characterinfo = characterinfo;
 
                foreach (var data in arrcharacterinfo)
                {
                    this.dicCharacterInfo.Add(data.id, data);
                }
 
                
 
                //this.CreateUserInfo(arrcharacterinfo);
            }
            else
            {
                Console.WriteLine("신규유저입니다");
 
                Directory.CreateDirectory("./info");//새로운 폴더를 생성합니다.
                
                this.CreateUserInfo();
            }
 
 
 
            this.Save();
        }
 
        public void ReadFileAndToDictionary<T>(string path, Dictionary<int,T>dic) where T : Character
        {
            var json = File.ReadAllText(path);
            var arrDatas = JsonConvert.DeserializeObject<T[]>(json);
 
            foreach(var data in arrDatas)
            {
                dic.Add(data.info.id, data);
            }
 
        }
 
        public T CreateCharacter<T>(int id) where T : Character,new()
        {
            return null;
        }
 
        public void CreateUserInfo(UserInfo info = null)//선택적매개변수 (값을 넣지 않으면 info 의 값은 null로 들어간다)
        {
            if (info == null//info의 값이 null 즉,아무 값 도 들어오지 않을 시
            {
                this.userInfo = new UserInfo(); //새로운 UserInfo 인스턴스를 생성한다.
            }
            else
            {
                this.userInfo = info; // 어떠한 값이 들어올 시 
            }
 
        }
 
        public void LoadData()
        {
            var json = File.ReadAllText("./data/user_data.json");
 
            var arrCharacterDatas = JsonConvert.DeserializeObject<CharacterData[]>(json);
 
            foreach (var data in arrCharacterDatas)
            {
                this.dicCharacterData.Add(data.id, data);
            }
        }
 
        public void Save()
        {
            var characterInfo = new List<CharacterInfo>();
            foreach (var character in this.characters)
            {
                characterInfo.Add(character.info);
            }
 
            this.userInfo.gameInfo.characterInfo = characterInfo;
            var json = JsonConvert.SerializeObject(this.userInfo);
 
            File.WriteAllText(USER_INFO_FILE_PATH, json);
            Console.WriteLine("게임이 저장되었습니다");
        }
 
    }
}
 
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _0410Test
{
    public class App
    {
        private GameLauncher gamelauncher;
        public App()
        {
            gamelauncher = new GameLauncher();
        }
 
        public void Start()
        {
            Console.WriteLine("게임을 시작합니다");
            gamelauncher.gameStart();
        }
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _0410Test
{
    class Program
    {
        static void Main(string[] args)
        {
            var app = new App();
            app.Start();
        }
    }
}
 
cs


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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
 
namespace ConsoleApp3
{
    public class WeaponInfo : ItemInfo
    {
        public int max_level;
        public string name;
        public int type;
        public float dps_min;
        public float dps_max;
        public float attack_speed;
 
        public WeaponInfo(int id) : base(id)
        {
        }
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApp3
{
    public class Weapon : Item
    {
        new public WeaponInfo info;
 
        public Weapon(ItemInfo info) : base(info)
        {
            this.info = (WeaponInfo)info;
        }
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApp3
{
    public class ItemInfo
    {
        public int id;
 
        public ItemInfo(int id)
        {
            this.id = id;
        }
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApp3
{
    public class Item
    {
        public ItemInfo itemInfo;
 
        public Item(ItemInfo itemInfo)
        {
            this.itemInfo = itemInfo;
        }
    }
}
 
cs

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;
 
namespace ConsoleApp3
{
    public class GameLauncher
    {
        private InventoryInfo inventoryInfo;
        private Inventory inventory;
        private Dictionary<int, Weapon> dicWeaponData = new Dictionary<int, Weapon>();
        public GameLauncher()
        {
 
        }
 
        public void CreateInventory()
        {
            this.inventoryInfo = new InventoryInfo();
        }
 
        public void StartGame()
        {
            while (true)
            {
                Console.Clear();
                Console.WriteLine("[1:아이템생성 과 인벤추가 ,2:인벤목록출력 ,3: 아이템꺼내기 ,4:저장후 종료]");
                var inputKey = Console.ReadLine();
                Console.Write(" ");
 
                switch (inputKey)
                {
                    case "1":
                        Console.WriteLine("아이템 추가");
                        inventory.AddItem(dicWeaponData);
                        //this.inventoryInfo = inventory.AddItem(dicWeaponData);
                        break;
                    case "2":
                        Console.WriteLine("[인벤토리]");
                        break;
                    case "3":
                        Console.WriteLine("아이템꺼내기");
                        Console.WriteLine("꺼내실 아이템을 입력해주세요.");
                        var inputStr = Console.ReadLine(); 
                        
                        break;
                    case "4":
                        Console.WriteLine("아이템검색");
                        Console.WriteLine("검색하실 아이템명을 입력해주세요.");
                        var foundItem = Console.ReadLine();
                        var item = inventory.FindItemByName(foundItem);
                        if (item != null)
                        {
                            Console.WriteLine("해당아이템이 존재합니다.");
                        }
                        else
                        {
                            Console.WriteLine("해당아이템이 존재하지않습니다.");
                        }
                        Console.WriteLine("전 단계로 돌아가시려면 아무키나 입력하세요");
                        Console.ReadKey();
                        break;
                    case "5":
                        Console.WriteLine("\t\t저장 후 게임종료");
                        //this.SaveAndExit();
                        break;
                }
            }
        }
 
        public void SaveAndExit(InventoryInfo inventoryInfo)
        {
            //File.WriteAllText("./info/user_info.json", json, Encoding.UTF8);
            Console.WriteLine("데이터가 저장되었습니다.");
            //Thread.Sleep(1000);
            Console.WriteLine("End");
            Console.ReadKey();
            System.Environment.Exit(0);
        }
    }
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApp3
{
    public class InventoryInfo
    {
        public List<ItemInfo> itemInfos;
        public int itemCount;
        private List<Item> itemList;
 
        public InventoryInfo()
        {
            this.itemList = new List<Item>();
        }
    }
}
 
cs

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApp3
{
    public class Inventory
    {
        public InventoryInfo info;
 
        public Inventory(Dictionary<int, WeaponInfo> dicWeaponData)
        {
        }
 
        public void AddItem(Dictionary<int, Weapon> dic)
        {
            Console.WriteLine("추가하실 아이템을 선택해주세요");
            Console.WriteLine("1.왕시해자  2.고딕검  3.큰날  4.신검  5.군주의검");
            var inputkey = Console.ReadLine();
            if (inputkey == "1" || inputkey == "왕시해자")
            {
 
            }
            else if(inputkey == "2" || inputkey == "고딕검")
            {
 
            }
            else if (inputkey == "3" || inputkey == "큰날")
            {
 
            }
            else if (inputkey == "4" || inputkey == "신검")
            {
 
            }
            else if (inputkey == "5" || inputkey == "군주의검")
            {
 
            }
            else if (inputkey == "0" || inputkey == "뒤로가기")
            {
 
            }
            else
            {
                Console.WriteLine("잘못입력하셨습니다.");
            }
        }
 
        public Item FindItemByName(string name)
        {
            return null;
        }
 
        public void RemoveItem(string name)
        {
 
        }
    }
}
 
 
cs


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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
 
namespace ConsoleApp3
{
    public class App
    {
        private Inventory inventory;
        //private InventoryInfo InventoryInfo;
        private GameLauncher gameLauncher;
 
        Dictionary<int, WeaponInfo> dicWeaponData;
 
        public App()
        {
            this.dicWeaponData = new Dictionary<int, WeaponInfo>();
            this.gameLauncher = new GameLauncher();   
        }
 
        public void Start()
        {   
            this.Init();
 
            //gameLauncher.SaveAndExit(new InventoryInfo());
        }
 
        public void Init()
        {
            if (Directory.Exists("./data"))
            {
                Console.WriteLine("기존유저입니다");
                var weaponJson = File.ReadAllText("./data/weapon_data.json");
                var arrWeaponData = JsonConvert.DeserializeObject<WeaponInfo[]>(weaponJson);
 
                //foreach(var data in arrWeaponData)
                //{
                //    dicWeaponData.Add(data.id, data);
                //}
                this.inventory = new Inventory(dicWeaponData);
            }
            else
            {
                Console.WriteLine("신규유저입니다");
                Directory.CreateDirectory("./data");
                var json = JsonConvert.SerializeObject(this.inventory);
                File.WriteAllText("./data/weapon_data.json", json, Encoding.UTF8);
 
                var weaponJson = File.ReadAllText("./data/weapon_data.json");
                var arrWeaponData = JsonConvert.DeserializeObject<WeaponInfo>(weaponJson);
            }
        }
        
        public void GameStart()
        {
            this.gameLauncher.StartGame();
        }
 
    }
}
 
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            //var app = new App();
            //app.Start();
 
            new App().Start();
        }
    }
}
 

cs




인벤토리와 json파일을 연결하는 것

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_09
{
    class Inventory
    {
        public Item[] items = new Item[10];
 
        public Inventory()
        {
        }
 
        //추가
        public void AddItem(Item item)
        {
            for(int idx =0; idx <items.Length; idx++)
            {
                if(items[idx] == null)
                {
                    items[idx] = item;
                    Console.WriteLine("{0}이 생성되었습니다", items[idx].name);
                    break;
                }
            }
        }
 
        //삭제
        public void RemoveItem(string name)
        {
            Item removeitem = null;
            for (int i=0; i<items.Length; i++)
            { 
                if(items[i].name == name)
                {
                    removeitem = items[i];
                    items[i] = null;
                    break;
                }
            }
            Console.WriteLine("{0}을 버렸습니다",removeitem.name);
        }
        /*
        //검색
        public Item FindItem(string name)
        {
        }
        */
        //추가
        public void UpdateItem(ref Item item, string name)
        {
 
        }
 
        //출력
        public void DisplayItems()
        {
            for (int i = 0; i < items.Length; i++)
            {
                if (items[i] != null)
                {
                    Console.WriteLine("--> {0}",items[i].name);
                }
            }
        }
    }
}
 
cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_09
{
    class App
    {
        public App()
        {
 
        }
 
        public void Start()
        {
            Inventory inventory = new Inventory();
 
            inventory.AddItem(new Item("두건"));
            //inventory.AddItem(null);
            inventory.AddItem(new Item("조종복"));
            inventory.AddItem(new Item("장갑"));
            inventory.AddItem(new Item("null"));
 
            inventory.RemoveItem("두건");
 
            inventory.DisplayItems();
 
            
            
            
        }
    }
}
 
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_09
{
    public class Item
    {
        public string name;
 
        public Item(string name)
        {
            this.name = name;
        }
    }
}
 
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_09
{
    class Program
    {
        static void Main(string[] args)
        {
            App app = new App();
            app.Start();
        }
    }
}
 
cs


break;문의 습관화

ref

아이템 개수 추가 삭제

'C# > Problems' 카테고리의 다른 글

롤 애니비아 바론 싸움 (캐릭터생성오류)  (0) 2019.04.11

Dictionary<TKey,TValue>Class (네임스페이스 : System.Collections.Generic)

: 값을 키와 함께 받아 HashTable에 저장한다. 

: 키 값을 통해 데이터를 빠르게 검색 가능(키 값에 따라 검색 속도는 다르다)

: Add를 사용하여 키와 값을 저장


TKey : 사전에 있는 키의 형식

TValue : 사전에 있는 값의 형식


상속 : Object -> Dictionary<TKey,TValue>

파생 : System.ServiceModel.MessageQuerySet


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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Test_01
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<stringstring> Test = new Dictionary<stringstring>();
 
            Test.Add("key""value");
            Test.Add("양재""공부좀해");
            Test.Add("코딩""잘하고싶다");
            //요소 추가
 
            try
            {
                Test.Add("txt""winword.exe");
            }
            catch(ArgumentException)
            {
                Console.WriteLine("an element with Key = \"txt\" already exists.");
            }
            // 중복키일 경우 ArgumentException 발생
 
            Console.WriteLine("for key = \"코딩\", value = {0}.", Test["코딩"]);
            //출력 : for key = 코딩, value = 잘하고싶다
            
        }
    }
}
 
cs


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

속성  (0) 2019.04.03
[Collection]ArrayList  (0) 2019.04.01
//미완// Collection[컬렉션]  (0) 2019.03.31
Boxing 및 Unboxing  (0) 2019.03.29
(3.25) 몬스터 공격하기 ver.수정  (0) 2019.03.26