유니티3D 프로그래밍

C# 3주차 5일 수업 내용 : File, Thread (21.03.26) 본문

C#/수업내용

C# 3주차 5일 수업 내용 : File, Thread (21.03.26)

tjdgus9955 2021. 3. 26. 11:40

쿠키런 게임 캐릭터 선택

using System;
using System.IO;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace Study01
{
   
    public class App
    {

        public App()
        {
            Console.WriteLine("App");
            GameInfo gameInfo = null;
            string dataPath = "./cookie_data.json";
            string infoPath = "./game_info.json";

            //json파일 읽기
            string json = File.ReadAllText(dataPath);
            Console.WriteLine(json);

            //역직렬화 
            CookieData[] cookies = JsonConvert.DeserializeObject<CookieData[]>(json);

            //사전에 넣기 
            Dictionary<int, CookieData> dicCookies = cookies.ToDictionary(x => x.id);

            //파일있는지 검사 (신규유저 판별)
            if (File.Exists(infoPath))
            {
                Console.WriteLine("기존유저");
                //파일 로드
                var loadedGameInfoJson = File.ReadAllText(infoPath);
                //역직렬화 
                gameInfo = JsonConvert.DeserializeObject<GameInfo>(loadedGameInfoJson);
                //출력 
                CookieInfo cookieInfo = gameInfo.cookieInfos.Find(x => x.isSelected);
                Console.WriteLine("cookieInfo: {0}", cookieInfo);
            }
            else
            {
                Console.WriteLine("신규유저");
                gameInfo = new GameInfo();
                //Data를 기반으로 Info객체 생성
                foreach (var pair in dicCookies)
                {
                    var cookieData = pair.Value;
                    if (cookieData.is_default)
                    {
                        var cookieInfo = new CookieInfo(cookieData.id, cookieData.level, true);
                        gameInfo.cookieInfos.Add(cookieInfo);
                    }
                }
                //직렬화후 저장 
                File.WriteAllText(infoPath, JsonConvert.SerializeObject(gameInfo));
                Console.WriteLine("game_info저장 완료");
            }
        }

    }
}

 

 

using System;


namespace Study01
{
    public class CookieData
    {
        public int id;
        public string name;
        public int grade;
        public int level;
        public int max_hp;
        public string skill_name;
        public bool is_default;
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study01
{
    public class CookieInfo
    {
        public int id;
        public int level;
        public bool isSelected;

        public CookieInfo(int id, int level, bool isSelected = false)
        {
            this.id = id;
            this.level = level;
            this.isSelected = isSelected;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study01
{
    public class GameInfo
    {
        public List<CookieInfo> cookieInfos;

        public GameInfo()
        {
            this.cookieInfos = new List<CookieInfo>();
        }
    }
}

Thread

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Study00
{
    public class App
    {
        //생성자
        public App()
        {
            Console.WriteLine("App");

            Console.WriteLine("메인 스레드 시작");
            //스레드 변수 선언
            Thread thread;

            //스레드 인스턴스화
            thread = new Thread(new ThreadStart(Work));
            thread.Start();

            Console.WriteLine("메인 스레드 종료");
        }
        public void Work()
        {
            Console.WriteLine("일 시작");
            Thread.Sleep(1000); //1초 지연
            Console.WriteLine("일 끝");
        }
    }
}

using System;
using System.Diagnostics;
using System.Threading;

namespace Study00
{
    public class App
    {
        //생성자 
        public App()
        {
            Console.WriteLine("App");

            //스레드 생성 
            Thread t1 = new Thread(this.Idle);
            Thread t2 = new Thread(this.Sql);
            Thread t3 = new Thread(this.Win)
            {
                Priority = ThreadPriority.Highest
            };

            //스레드 실행 
            t1.Start();
            t2.Start();
            t3.Start();

            Process.Start("chrome.exe");
            Process.Start("notepad.exe");
        }

        private void Idle()
        {
            Thread.Sleep(3000);
            Console.WriteLine("비주얼 스튜디오를 실행합니다.");
        }

        private void Win()
        {
            Thread.Sleep(3000);
            Console.WriteLine("Window Server에 접속 합니다.");
        }

        private void Sql()
        {
            Thread.Sleep(3000);
            Console.WriteLine("DB서버에 접속 합니다.");
        }

    }
}

using System;
using System.Diagnostics;
using System.Threading;

namespace Study00
{
    public class App
    {
        //생성자 
        public App()
        {
            Console.WriteLine("App");

            Thread t1 = new Thread(new ThreadStart(this.Work));
            t1.Name = "sub";
            t1.Start();

            Console.WriteLine("t1 state : {0}", t1.ThreadState);

            for(int i = 0; i < 10; i++)
            {
                Console.WriteLine("main : {0}", i);
            }
        }

        private void Work()
        {
            for(int i = 0; i < 10; i++)
            {
                Console.WriteLine("sub : {0}", i);
            }
        }

    }
}