ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 데이터 저장 연습과제
    C#/수업과제 2022. 6. 20. 02:38
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    using Newtonsoft.Json;
    
    namespace Homework
    {
        public class App
        {
            public Dictionary<int, ItemData> DicItem;
            //생성자
            public App()
            {
                string item_json = File.ReadAllText("./item_data.json");
                ItemData[] arrItemDatas = JsonConvert.DeserializeObject<ItemData[]>(item_json);
                Dictionary<int, ItemData> dicItem = arrItemDatas.ToDictionary(x => x.id);
                this.DicItem = dicItem;
    
                Console.WriteLine("닉네임을 입력하세요");
                string userId = Console.ReadLine();
                string saveHeroPath = string.Format("./{0}_info.json", userId);
    
                bool exists = File.Exists(saveHeroPath);
                if (exists)
                {
                    Console.WriteLine("{0}님 반갑습니다. 돌아오셨군요.", userId);
                    Character hero = JsonConvert.DeserializeObject<Character>(saveHeroPath);
                    foreach (ItemInfo ownItems in hero.Inventory)
                    {
                        Item ownItem = CreateItem(ownItems);
                        Console.WriteLine("현재 {0}을 소유하고 있습니다", ownItem.name);
                    }
                    Console.WriteLine("복귀선물로 \"천 갑옷\"을 선물합니다");
                    Item present = new Item(DicItem[103]);
                    hero.GetItem(present);
    
                        string serializedjson = JsonConvert.SerializeObject(hero);
                    File.WriteAllText(saveHeroPath, serializedjson);
                }
                else
                {
                    Console.WriteLine("새로운 용사 {0}님 반갑습니다.", userId);
                    Character hero = new Character(userId);
    
                    Item startItem = new Item(dicItem[100]);
                    hero.GetItem(startItem);
                    Console.WriteLine("시작아이템 {0}을 받았습니다.", startItem.name);
    
                    string serializedjson = JsonConvert.SerializeObject(hero);
                    File.WriteAllText(saveHeroPath, serializedjson);
                }
            }
            private Item CreateItem(ItemInfo itemInfo)
            {
                Item item = new Item(itemInfo);
                item = (Item)DicItem[itemInfo.id];
                item.enforce = itemInfo.enforce;
                return item;
            }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Homework
    {
        public class ItemData
        {
            public int id;
            public string name;
            public int attack;
            public int defense;
            public int item_part;
    
        }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Homework
    {
        public class Character
        {
    
            public string Name { get; }
            public List<ItemInfo> Inventory;
            public ItemInfo[] EquipmetPart;
            public int AttackStat = 0;
            public int DefenseStat = 0;
            //생성자
            public Character(string name)
            {
                Name = name;
                EquipmetPart = new ItemInfo[2];
                this.Inventory = new List<ItemInfo>();
            }
    
            public void GetItem(Item item)
            {
                ItemInfo getItem = new ItemInfo(item);
                this.Inventory.Add(getItem);
            }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Homework
    {
        public class ItemInfo
        {
            public int id;
            public int enforce = 0;
            public ItemInfo(Item item)
            {
                this.id = item.id;
                this.enforce = item.enforce;
            }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Homework
    {
    
        public class Item : ItemData
        {
    
            public int enforce = 0;
            public Item(ItemInfo info)
            {
                this.id = info.id;
                this.enforce = info.enforce;
            }
            public Item(ItemData item)
            {
                this.id = item.id;
                this.name = item.name;
                this.attack = item.attack;
                this.defense = item.defense;
                this.item_part = item.item_part;
            }
        }
    }

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

    텍스트 Rpg만들기  (0) 2022.06.21
    2차원 배열의 늑대와 영웅  (0) 2022.06.15
    2048게임  (0) 2022.06.15
    for문 연습 문제  (0) 2022.06.10
    디아블로 아이템 출력하기  (0) 2022.06.09
Designed by Tistory.