유니티3D 프로그래밍
C# 게임 알고리즘 시험 본문
map_data.json
0.00MB
item_data.json
0.00MB
npc_data.json
0.00MB
App
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study00
{
public class App
{
public App()
{
Console.WriteLine("App");
string itemPath = "./item_data.json";
DataManager.Instance.LoadData<Item>(itemPath);
Login login = new Login();
Tutorial tutorial = new Tutorial();
}
}
}
Login
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Study00
{
public class Login
{
public int CharacterCount = 0;
public string id;
public Login()
{
while (true)
{
Console.WriteLine("아이디를 입력하세요. 처음이시라면 회원가입을 입력하세요");
id = Console.ReadLine();
if (id == "회원가입")
{
this.SignUp();
continue;
}
if (File.Exists("./profile.json"))
{
DataManager.Instance.LoadData<Profile>("./profile.json");
var profile = DataManager.Instance.GetData<Profile>(id);
Console.Write("비밀번호를 입력하세요. : ");
string checkPassword = Console.ReadLine();
int password = Convert.ToInt32(checkPassword);
if (profile.name == id && profile.password == password)
{
Console.WriteLine("로그인에 성공하셨습니다.");
Job job = new Job();
return;
}
Console.WriteLine("없는 아이디 또는 비밀번호가 틀렸습니다.. 다시 입력하세요.");
}
else
{
Console.WriteLine("없는 아이디 입니다. 다시 입력하세요.");
}
}
}
public void SignUp()
{
string name;
int password;
while (true)
{
Console.WriteLine("아이디는 하나만 가질 수 있습니다.");
Console.Write("아이디를 입력하세요. 이름은 최대 10글자 입니다. : ");
name = Console.ReadLine();
if (name.Length > 10)
{
Console.WriteLine("아이디가 10글자가 넘어갔습니다. 다시 입력하세요");
Thread.Sleep(500);
continue;
}
break;
}
while (true)
{
Console.Write("비밀번호를 입력하세요 : ");
string firstPassword = Console.ReadLine();
Console.Write("비밀번호를 다시 입력하세요. : ");
string secondtPassword = Console.ReadLine();
if (firstPassword.Equals(secondtPassword))
{
password = Convert.ToInt32(secondtPassword);
Console.WriteLine("회원가입이 완료되었습니다.");
}
else
{
Console.WriteLine("두번째 비밀번호가 틀렸습니다. 다시 입력하세요.");
continue;
}
break;
}
List<ID> list = new List<ID>();
ID id = new ID(CharacterCount, name, password);
list.Add(id);
CharacterCount++;
string json = JsonConvert.SerializeObject(list);
File.WriteAllText("./profile.json", json);
}
}
}
ID
using System;
namespace Study00
{
public class ID
{
public int id;
public string name;
public int password;
public ID(int id, string name, int password)
{
this.id = id;
this.name = name;
this.password = password;
}
}
}
Profile
using System;
namespace Study00
{
public class Profile : RawData
{
public string name;
public int password;
}
}
DataManager
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study00
{
public class DataManager
{
public static readonly DataManager Instance = new DataManager();
private List<RawData> list = new List<RawData>();
public DataManager()
{
}
public void LoadData<T>(string path) where T : RawData
{
var json = this.ReadFile(path);
var arr = JsonConvert.DeserializeObject<T[]>(json);
this.list.AddRange(arr);
}
public IEnumerable<T> GetDatas<T>(string name)
{
return this.list.OfType<T>();
}
public T GetData<T>(string name) where T : RawData
{
var data = this.list.FindAll(x => x.GetType().Equals(typeof(T)));
foreach (RawData rawData in data)
{
if (rawData.name == name)
{
return (T)rawData;
}
}
return null;
}
public T GetRawData<T>(string name) where T : RawData
{
Instance.LoadData<T>("./item_data.json");
return null;
}
private string ReadFile(string path)
{
string json = File.ReadAllText(path);
return json;
}
public string GetList()
{
return list.First().name;
}
}
}
Job
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Study00
{
public class Job
{
public MyCharacter myCharacter = null;
string infoPath = "./character.json";
public Job()
{
while (true)
{
if (File.Exists(infoPath))
{
Console.WriteLine("캐릭터를 불러옵니다.");
DataManager.Instance.LoadData<MyCharacter>(infoPath);
var loadedCharacterInfo = DataManager.Instance.GetData<MyCharacter>("tjdgus9955");
loadedCharacterInfo.CharacterStat();
break;
}
else
{
Console.WriteLine("새로 아이디를 만드셨군요?");
Console.WriteLine("직업을 선택하세요.");
string jobName = Console.ReadLine();
if (jobName != "전사" && jobName != "도적" && jobName != "궁수" && jobName != "마법사")
{
Console.WriteLine("없는 직업입니다. 다시 선택하세요");
continue;
}
if (ChoiceJob(jobName))
{
DataManager.Instance.LoadData<Profile>("./profile.json");
var myName = DataManager.Instance.GetData<Profile>("tjdgus9955");
List<NewCharacter> character = new List<NewCharacter>();
character.Add(new NewCharacter(myName.name, jobName));
string json = JsonConvert.SerializeObject(character);
File.WriteAllText(infoPath, json);
Console.WriteLine("축하합니다! 직업을 선택하셨습니다.");
Thread.Sleep(1000);
Console.WriteLine("이제 튜토리얼을 시작합니다.");
Tutorial tutorial = new Tutorial();
break;
}
}
}
}
public bool ChoiceJob(string jobName)
{
bool yesOrNoBool = false;
while (true)
{
Console.Write("직업을 선택하셨습니다. {0} 맞습니까? Y/N : ", jobName);
string yesOrNo = Console.ReadLine();
if (yesOrNo == "Y")
{
Console.WriteLine("직업을 고르셨습니다.");
yesOrNoBool = true;
break;
}
else if (yesOrNo == "N")
{
Console.WriteLine("N을 누르셨습니다. 다시 직업을 선택해 주세요.");
break;
}
else
{
Console.WriteLine("잘못 입력하셨습니다. 다시 입력하세요.");
continue;
}
}
return yesOrNoBool;
}
}
}
NewCharacter
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study00
{
public class NewCharacter
{
public string name;
public string job;
public int level;
public int exp;
public int hp;
public int mp;
public int stat_str;
public int stat_dex;
public int stat_int;
public int stat_wis;
public int substat_melee;
public int substat_magic;
public int substat_meleeArmor;
public int substat_magicArmor;
public NewCharacter(string name, string job)
{
this.name = name;
this.job = job;
this.level = 0;
this.exp = 0;
if (job == "전사")
{
this.stat_str = 10;
this.stat_dex = 7;
this.stat_int = 5;
this.stat_wis = 5;
this.hp = 100 + stat_str * 10 + stat_dex * 5;
this.mp = 50 + stat_wis * 10 + stat_int * 5;
this.substat_melee = stat_str * 2 + stat_dex;
this.substat_meleeArmor = stat_str * 2 + stat_dex;
this.substat_magic = stat_int * 2 + stat_wis;
this.substat_magicArmor = stat_int * 2 + stat_wis;
}
else if (job == "도적")
{
this.stat_str = 7;
this.stat_dex = 10;
this.stat_int = 5;
this.stat_wis = 5;
this.hp = 100 + stat_str * 10 + stat_dex * 5;
this.mp = 50 + stat_wis * 10 + stat_int * 5;
this.substat_melee = stat_str * 2 + stat_dex;
this.substat_meleeArmor = stat_str * 2 + stat_dex;
this.substat_magic = stat_int * 2 + stat_wis;
this.substat_magicArmor = stat_int * 2 + stat_wis;
}
else if (job == "궁수")
{
this.stat_str = 7;
this.stat_dex = 10;
this.stat_int = 5;
this.stat_wis = 5;
this.hp = 100 + stat_str * 10 + stat_dex * 5;
this.mp = 50 + stat_wis * 10 + stat_int * 5;
this.substat_melee = stat_str * 2 + stat_dex;
this.substat_meleeArmor = stat_str * 2 + stat_dex;
this.substat_magic = stat_int * 2 + stat_wis;
this.substat_magicArmor = stat_int * 2 + stat_wis;
}
else
{
this.stat_str = 5;
this.stat_dex = 5;
this.stat_int = 10;
this.stat_wis = 7;
this.hp = 100 + stat_str * 10 + stat_dex * 5;
this.mp = 50 + stat_wis * 10 + stat_int * 5;
this.substat_melee = stat_str * 2 + stat_dex;
this.substat_meleeArmor = stat_str * 2 + stat_dex;
this.substat_magic = stat_int * 2 + stat_wis;
this.substat_magicArmor = stat_int * 2 + stat_wis;
}
}
}
}
MyCharacter
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study00
{
public class MyCharacter : RawData
{
public string name;
public string job;
public int level;
public int exp;
public int hp;
public int mp;
public int stat_str;
public int stat_dex;
public int stat_int;
public int stat_wis;
public int substat_melee;
public int substat_magic;
public int substat_meleeArmor;
public int substat_magicArmor;
public void CharacterStat()
{
Console.WriteLine();
Console.WriteLine("이름 : {0, 10} | 직업 : {1, 2} | 레벨 : {2, 5} | 경험치 : {3}", name, job, level, exp);
Console.WriteLine("HP : {0, 5}", hp);
Console.WriteLine("MP : {0, 5}", mp);
Console.WriteLine("힘 : {0, 5}", stat_str);
Console.WriteLine("민첩 : {0, 5}", stat_dex);
Console.WriteLine("지능 : {0, 5}", stat_int);
Console.WriteLine("지혜 : {0, 5}", stat_wis);
Console.WriteLine("물리공격력 : {0, 5}", substat_melee);
Console.WriteLine("물리방어력 : {0, 5}", substat_meleeArmor);
Console.WriteLine("마법공격력 : {0, 5}", substat_magic);
Console.WriteLine("마법방어력 : {0, 5}", substat_magicArmor);
Console.WriteLine();
}
}
}
Tutorial
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Study00
{
public class Tutorial
{
Command command = new Command();
public Tutorial()
{
Thread.Sleep(1000);
Console.WriteLine("먼저, 본인의 상태를 확인하기 위한 명령어 입니다.");
Thread.Sleep(1000);
while (true)
{
Console.Write("'상태'라는 명령어를 입력해주시기 바랍니다. : ");
string state = Console.ReadLine();
if (state != "상태")
{
Console.WriteLine();
Console.WriteLine("잘못 입력하셨습니다. 다시 입력해 주시기 바랍니다.");
continue;
}
else
{
command.CallCommand(state);
Console.WriteLine();
Thread.Sleep(1000);
Console.WriteLine("잘 하셨습니다! 이제 다음은 자신의 인벤토리 창을 확인하는 것입니다."); ;
Thread.Sleep(1000);
break;
}
}
Console.Write("인벤토리를 입력해 주시기 바랍니다. : ");
while (true)
{
string inventory = Console.ReadLine();
if (inventory != "인벤토리")
{
Console.WriteLine();
Console.WriteLine("잘못 입력하셨습니다. 다시 입력해 주시기 바랍니다. : ");
continue;
}
else
{
command.CallCommand(inventory);
Console.WriteLine("인벤토리를 여셨습니다. 지금은 아무 아이템도 없군요");
Console.WriteLine("목검 아이템을 지급해 드리겠습니다.");
var woodSword = DataManager.Instance.GetData<Item>("수련용목검");
command.CallCommand("수련용목검 줍는다");
Console.Write("다시 인벤토리를 입력해 보세요. : ");
while(true)
{
string secondInventory = Console.ReadLine();
if (secondInventory == "인벤토리")
{
command.CallCommand(inventory);
break;
}
Console.WriteLine("인벤토리를 입력하세요. 다시 입력해 주시기 바랍니다.");
}
}
Console.WriteLine();
break;
}
Console.WriteLine("이번엔 아이템을 주워보겠습니다.");
Console.WriteLine("마침 바닥에 최하급체력포션이 떨어져 있네요!");
Console.WriteLine("(아이템이름) 줍는다를 입력하세요");
while (true)
{
string pickUp = Console.ReadLine();
string[] pickUpArr = pickUp.Split(' ');
foreach (string pickTemp in pickUpArr)
{
if (pickTemp.Equals("최하급체력포션"))
{
command.CallCommand(pickUp);
break;
}
}
break;
}
Console.WriteLine("아이템이 주워졌는지 확인하기 위해 인벤토리를 불러오세요.");
while (true)
{
string inventory = Console.ReadLine();
if (inventory != "인벤토리")
{
Console.WriteLine();
Console.WriteLine("잘못 입력하셨습니다. 다시 입력해 주시기 바랍니다. : ");
continue;
}
else
{
command.CallCommand(inventory);
}
Console.WriteLine();
break;
}
}
}
}
Command
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study00
{
public class Command
{
public string characterPath = "./character.json";
public string command;
public Inventory inventory = new Inventory();
public Command()
{
}
public void CallCommand(string command)
{
string[] commandArr = command.Split(' ');
foreach (string arr in commandArr)
{
if (arr == "상태")
{
this.StatCommand();
}
else if (arr == "인벤토리")
{
this.InvenInfoConmmand();
}
else if (arr == "줍는다")
{
foreach (string firstElement in commandArr)
{
this.ItemAddCommand(firstElement);
break;
}
}
}
}
public void StatCommand()
{
DataManager.Instance.LoadData<MyCharacter>(characterPath);
MyCharacter myCharacter = DataManager.Instance.GetData<MyCharacter>("tjdgus9955");
myCharacter.CharacterStat();
}
public virtual void InvenInfoConmmand()
{
inventory.InvenList();
}
public void ItemAddCommand(string name)
{
var json = File.ReadAllText("./item_data.json");
var itemInfos = DataManager.Instance.GetData<Item>(name);
inventory.ItemAdd(itemInfos);
Console.WriteLine("{0} 아이템을 얻었습니다.", name);
}
public Inventory ReturnInventory()
{
return inventory;
}
}
}
Inventory
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study00
{
public class Inventory
{
private int weight;
public Item[] itemLists = new Item[30];
public Inventory()
{
}
public void ItemAdd(Item item)
{
for (int i = 0; i < itemLists.Length; i++)
{
if (itemLists[i] == null)
{
itemLists[i] = item;
break; ;
}
}
}
public void ItemRemove(string itemName)
{
for (int i = 0; i < itemLists.Length; i++)
{
if (itemLists[i].name == itemName)
{
itemLists[i] = null;
break;
}
}
}
public Item ItemUse(Item item)
{
return null;
}
public void InvenList()
{
Console.WriteLine();
Console.WriteLine("--------- 보유한 아이템 목록 ---------");
for (int i = 0; i < itemLists.Length; i++)
{
if (itemLists[i] != null)
{
Console.WriteLine(" [{0}]", itemLists[i].name);
}
}
Console.WriteLine("--------------------------------------");
Console.WriteLine();
}
}
}
Item
using System;
namespace Study00
{
public class Item : RawData
{
public string name;
public int damage;
public int armor;
public bool use;
public int weight;
public string type;
public int hp;
public int mp;
}
}
RawData
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study00
{
public class RawData
{
public string name;
}
}
Map
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study00
{
public class Map : RawData
{
public int map_id;
public string map_desc;
}
}
MapArr
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study00
{
public class MapArr
{
public int userPositionX = 4;
public int userPositionY = 4;
public string mapDataPath = "./map_data.json";
public string wall = "벽";
public MapArr()
{
var data = DataManager.Instance.GetDatas<Map>("중앙광장").GetEnumerator();
Map[] mapArr = new Map[10];
int count = 0;
while (data.MoveNext())
{
mapArr[count] = DataManager.Instance.GetData<Map>(data.Current.name);
Console.WriteLine(mapArr[count].name);
count++;
}
//맵 세팅
Map[][] map = new Map[][]{
new Map[] {null, mapArr[9], mapArr[9], mapArr[9], null, null, mapArr[1], null, null },
new Map[] {null, mapArr[9], mapArr[9], mapArr[9], null, null, mapArr[1], null, null },
new Map[] {null, null, mapArr[1], null, null, null, mapArr[1], mapArr[1], mapArr[1]},
new Map[] {mapArr[1], mapArr[1], mapArr[1], mapArr[1], mapArr[1], mapArr[1], mapArr[1], null, null},
new Map[] {null, null, mapArr[1], mapArr[3], mapArr[2], mapArr[4], mapArr[1], null, null},
new Map[] {null, null, mapArr[1], mapArr[1], mapArr[0], mapArr[1], mapArr[1], null, null},
new Map[] {mapArr[1], mapArr[1], mapArr[1], mapArr[5], mapArr[6], mapArr[7], mapArr[1], null, null},
new Map[] {null, null, mapArr[1], mapArr[1], mapArr[8], mapArr[1], mapArr[1], mapArr[1], mapArr[1]},
new Map[] {null, null, null, mapArr[1], null, null, null, null, null},
new Map[] {null, null, null, mapArr[1], null, null, null, null, null}
};
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < 9; j++)
{
if (map[i][j] == null)
{
Console.Write(" {0} ", wall);
}
else
{
if (map[i][j].name == "무기상점" || map[i][j].name == "장비상점" || map[i][j].name == "잡화상점" || map[i][j].name == "힘교환소" || map[i][j].name == "중앙광장")
{
Console.Write(" {0} ", map[i][j].name);
}
else if (map[i][j].name == "민첩교환소" || map[i][j].name == "지능교환소" || map[i][j].name == "지혜교환소")
{
Console.Write(" {0} ", map[i][j].name);
}
else if (map[i][j].name == "사냥터1")
{
Console.Write(" {0} ", map[i][j].name);
}
else
{
Console.Write(" [{0}] ", map[i][j].name);
}
}
}
Console.WriteLine();
}
}
public Map GetMapInfo()
{
return null;
}
}
}
NPC
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study00
{
public class NPC : RawData
{
public string npc_name;
public int npc_id;
}
}
'C# > 수업과제' 카테고리의 다른 글
C# 3주차 3일 수업 과제 : File (21.03.24) (0) | 2021.03.24 |
---|---|
C# 3주차 1일 수업 과제 : Delegate, Action, Callback (21.03.22) (0) | 2021.03.22 |
C# 2주차 5일 수업 내용 및 과제 : 2차원 배열 및 2048 프로그램(21.03.19) (0) | 2021.03.19 |
C# 2주차 3일 수업 과제 : Stack (21.03.17) (0) | 2021.03.17 |
C# 2주차 2일 수업 과제 (21.03.16) (0) | 2021.03.16 |