미완성 LOL캐릭터 생성 후 데미지 입기(데이터 저장 및 불러오기)
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 |
'C# > 미완성' 카테고리의 다른 글
(미완성)인벤토리 만들기 (0) | 2019.04.08 |
---|---|
#미완성#인벤토리 (0) | 2019.03.30 |
Array[배열] #미완성 : 메소드예시 (0) | 2019.03.27 |
와우 캐릭터생성(미완성) (0) | 2019.03.27 |
미완성(3.25 과제) new, class, dot(.), 형식변환, 멤버변수, 지역변수 (0) | 2019.03.26 |
(미완성)인벤토리 만들기
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(); } } } |
인벤토리와 json파일을 연결하는 것
'C# > 미완성' 카테고리의 다른 글
미완성 LOL캐릭터 생성 후 데미지 입기(데이터 저장 및 불러오기) (0) | 2019.04.10 |
---|---|
#미완성#인벤토리 (0) | 2019.03.30 |
Array[배열] #미완성 : 메소드예시 (0) | 2019.03.27 |
와우 캐릭터생성(미완성) (0) | 2019.03.27 |
미완성(3.25 과제) new, class, dot(.), 형식변환, 멤버변수, 지역변수 (0) | 2019.03.26 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Inventory
{
class Item
{
public string name;
//public string type;
public int count;
public int itemMincount;
public int itemMaxcount;
public Item(string name, int count = 0, int itemMincount=0 , int maxcount = 10)
{
this.name = name;
this.count = count;
this.itemMincount = itemMincount;
this.itemMaxcount = maxcount;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Inventory
{
class Function
{
List<Item> items = new List<Item>();
public Function() //아이템 기본 목록
{
items.Add(new Item("초보자용 몽둥이"));
items.Add(new Item("초보자용 방패"));
items.Add(new Item("체력포션"));
items.Add(new Item("마나포션"));
}
public void inventory()
{
while (true)
{
Console.WriteLine("\t\t\t[인벤토리]\n");
Console.WriteLine("1.아이템추가 2.아이템버리기 3.아이템목록 4.새로운아이템");
string input = Console.ReadLine();
Console.Clear();
if (input == "아이템추가" || input == "1" || input == "추가")
{
Add();
}
else if (input == "아이템삭제" || input == "2" || input == "삭제")
{
Delete();
}
else if (input == "아이템목록" || input == "3" || input == "검색")
{
itemlist();
}
else if (input == "새로운아이템" || input == "4" || input == "목록")
{
newitem();
}
else
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("잘못입력하셨습니다.");
Console.ResetColor();
}
}
}
public void Add()//아이템 추가
{
while (true)
{
Console.WriteLine("어떤 아이템을 추가하시겠습니까? \n");
Console.WriteLine("1.검 2.방패 3.체력포션 4.마나포션 0.뒤로가기");
string input = Console.ReadLine();
Console.Clear();
if (input == "검" || input == "1")
{
if (items[0].count >= items[0].itemMaxcount)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("검을 더이상 습득할 수 없습니다. \n");
Console.ResetColor();
}
else
{
items[0].count++;
Console.WriteLine("검이 추가되었습니다");
}
}
else if (input == "방패" || input == "2")
{
if (items[1].count >= items[1].itemMaxcount)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("방패를 더이상 습득할 수 없습니다. \n");
Console.ResetColor();
}
else
{
items[1].count++;
Console.WriteLine("방패가 추가되었습니다");
}
}
else if (input == "체력포션" || input == "3")
{
if (items[2].count >= items[2].itemMaxcount)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("체력포션을 더이상 습득할 수 없습니다. \n");
Console.ResetColor();
}
else
{
items[2].count++;
Console.WriteLine("체력포션이 추가되었습니다");
}
}
else if (input == "마나포션" || input == "4")
{
if (items[3].count >= items[3].itemMaxcount)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("마나포션을 더이상 습득할 수 없습니다. \n");
Console.ResetColor();
}
else
{
items[3].count++;
Console.WriteLine("마나포션이 추가되었습니다");
}
}
else if (input == "뒤로가기" || input == "0")
{
inventory();
}
else
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("잘못입력하셨습니다.");
Console.ResetColor();
}
}
}
public void newitem()
{
Console.WriteLine("추가하실 아이템을 입력하여주세요.");
if (items.Count > 10)
{
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine("더이상 추가하실 수 없습니다. [{0}/10]", items.Count);
Console.ResetColor();
}
else if (items.Count < 10)
{
items.Add(new Item(Console.ReadLine()));
int i = 4;
Console.WriteLine("{0}을 추가하셨습니다", items[i].name);
i++;
}
}
public void itemlist()
{
for (int i = 0; i < items.Count; i++)
{
if (items[i].count == 0)
{
Console.WriteLine("아이템이 없습니다.");
}
else if (items[i] != null)
{
Console.WriteLine("이름 : {0}\t [{1}/10] ", items[i].name, items[i].count);
}
}
}
public void Delete()
{
while (true)
{
Console.WriteLine("버리실 아이템을 선택해주세요 \n");
Console.WriteLine("1.검 2.방패 3.체력포션 4.마나포션 0.뒤로가기");
string input = Console.ReadLine();
Console.Clear();
if (input == "검" || input == "1")
{
if (items[0].count <= items[0].itemMincount)
{
items[0].count--;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("아이템이 더이상 없습니다");
Console.ResetColor();
}
}
else if (input == "방패" || input == "2")
{
if (items[0].count <= items[0].itemMincount)
{
items[0].count--;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("아이템이 더이상 없습니다");
Console.ResetColor();
}
}
else if (input == "체력포션" || input == "3")
{
if (items[0].count <= items[0].itemMincount)
{
items[0].count--;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("아이템이 더이상 없습니다");
Console.ResetColor();
}
}
else if (input == "마나포션" || input == "4")
{
if (items[0].count <= items[0].itemMincount)
{
items[0].count--;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("아이템이 더이상 없습니다");
Console.ResetColor();
}
}
else if (input == "뒤로가기" || input == "0")
{
inventory();
}
else
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("잘못입력하셨습니다.");
Console.ResetColor();
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Inventory
{
class Program
{
static void Main(string[] args)
{
var Funtion = new Function();
Funtion.inventory();
Console.ReadKey();
}
}
}
아이템 추가 시 중복 제거 및 인벤토리공간 추가 기능 미완성
'C# > 미완성' 카테고리의 다른 글
미완성 LOL캐릭터 생성 후 데미지 입기(데이터 저장 및 불러오기) (0) | 2019.04.10 |
---|---|
(미완성)인벤토리 만들기 (0) | 2019.04.08 |
Array[배열] #미완성 : 메소드예시 (0) | 2019.03.27 |
와우 캐릭터생성(미완성) (0) | 2019.03.27 |
미완성(3.25 과제) new, class, dot(.), 형식변환, 멤버변수, 지역변수 (0) | 2019.03.26 |
Array[배열] #미완성 : 메소드예시
Array Class (배열클래스)
배열생성 및 조작, 검색, 정렬하여 모든배열의 기본클래스 역할을 수행하도록 메서드를 제공
Array [배열]
: 동일한 유형의 여러 변수를 배열 데이터구조에 저장가능
: 기본값은 참조타입 : null, 숫자타입 : 0 이다
: 배열의 형식은 추상기본유형 Array에서 파생된 참조형식이다. (IEnumerable 및 lEbumerable <T> 를 구현하므로 foreach반복문을 사용할수있다)
상속 : Object -> Array
[1차원 배열 예시 int형]
1 2 | int[] arrayName = new int[5]; //int[] arrayName(배열클래스 인스턴스를 담는다) = int형 배열클래스의 인스턴스를 생성 (기본값 : 0) | cs |
[1차원 배열 예시 string형]
1 2 | string[] arrayName5 = new string[5]; //string형 배열클래스 인스턴스를 생성 (기본값 : null) | cs |
[1차원 배열 또다른 사용법]
1 2 | int[] arrayName = { 1, 2, 3, 4, 5 }; // ** 배열클래스의 인스턴스를 arrayName3 에 담는다. | cs |
[1차원 배열 초기화]
1 2 | int[] arrayName2 = new int[5] { 1, 2, 3, 4, 5 }; // 출력 값 : 1,2,3,4,5 (여기서 배열의 인덱스는 0,1,2,3,4 이다. | cs |
[배열특성의 메소드]
(이미지 참조 : https://m.blog.naver.com/sonicheroes1/220947806856)
<T> 가 붙은 메소드는 형식매개변수(Type Parameter)라고 부르며, 호출 시 T의 위치에 배열기반자료형을 매개변수로 입력하면 컴파일러가 알아서 처리해준다.
1 2 3 4 5 | int[] sortEx = { 1,2,3,4,5,6,7,8,9,10 }; foreach(int foreachEx in sortEx) { Console.WriteLine(foreachEx); } //출력값 1 2 3 4 5 6 7 8 9 10 | cs |
인자로 들어온 ArrayEx 내부의 인덱스 끝까지 순환해주는 반복문, 배열에서 많이 사용
사용법 : foreach(자료형 element in 배열명)
1 2 3 4 5 6 7 | int[] sortEx = new int[5] { 5, 4, 10, 2, 1 }; Array.Sort(sortEx); for(int i=0; i<5; i++) { Console.WriteLine(sortEx[i]); }//출력값 : 1 2 4 5 10 | cs |
[BinarySearch<T> 예시]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | namespace test { class Program { static void Main(string[] args) { int[] ArrayEx = { 12,23,34,45,56,67,78,89,91,10 }; string[] ArrayEx2 = { "가", "나", "다", "라", "마" }; Console.WriteLine(Array.BinarySearch<int>(ArrayEx, 89)); Console.WriteLine(Array.BinarySearch<string>(ArrayEx2, "라")); } } }결과 값 : 7 , 3 | cs |
정렬된 Array에서 값을 검색
참조 : https://docs.microsoft.com/ko-kr/dotnet/api/system.array.binarysearch?view=netframework-4.7.2
1차원배열 doc : https://docs.microsoft.com/ko-kr/dotnet/csharp/programming-guide/arrays/single-dimensional-arrays
Array Method 종류 : https://m.blog.naver.com/sonicheroes1/220947806856
GetValue Method doc : https://docs.microsoft.com/ko-kr/dotnet/api/system.reflection.propertyinfo.getvalue?view=netframework-4.7.2
string.IndexOf Method doc : https://docs.microsoft.com/ko-kr/dotnet/api/system.string.indexof?view=netframework-4.7.2
'C# > 미완성' 카테고리의 다른 글
미완성 LOL캐릭터 생성 후 데미지 입기(데이터 저장 및 불러오기) (0) | 2019.04.10 |
---|---|
(미완성)인벤토리 만들기 (0) | 2019.04.08 |
#미완성#인벤토리 (0) | 2019.03.30 |
와우 캐릭터생성(미완성) (0) | 2019.03.27 |
미완성(3.25 과제) new, class, dot(.), 형식변환, 멤버변수, 지역변수 (0) | 2019.03.26 |
와우 캐릭터생성(미완성)
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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GameEnums { class GameEnum { public enum eCamp //진영 { None, Alliance, Hord } public enum eTribe //(얼라이언스)부족 { None,//0 Human,//1 Dwarf,//2 Nightelf,//3 Gnomes,//4 Draenei,//5 Werewolf//6 } public enum eTribe2 //(호드)부족2 { None,//0 Oak,//1 Undead,//2 Tauren,//3 Troll,//4 Bloodelves,//5 Goblin//6 } public enum job { DeathKnight, //죽음의기사 Thief,//도적 Hunter,//사냥꾼 Monk,//수도사 Wizard,//마법사 Warlock,//흑마법사 Priests,//사제 Paladin,//성기사 Warrior,//전사 Powwow,//주술사 } } } | 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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GameEnums { class Character { public string name; private float x; private float y; public Character() { } public Character(string name) { this.name = name; } public void Move(float x, float y) { Console.WriteLine($"{name}님이 (x : {x} , y : {y}) 위치로 이동하였습니다."); } } } | 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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; namespace GameEnums { class App { public string AllianceList = "1.인간 2.드워프 3.나이트엘프 4.노움 5.드레나이 6.늑대인간"; public string HordList = "1.오크 2.언데드 3.타우렌 4.트롤 5.블러드엘프 6.고블린"; public App() { Console.WriteLine("진영을 선택해 주세요"); while (true) { string input = Console.ReadLine(); if (input == "얼라이언스") { Console.WriteLine("얼라이언스를 선택하셨습니다"); //Thread.Sleep(500); Console.WriteLine("종족을 선택해주세요"); Console.WriteLine(AllianceList); //foreach (var Values in Enum.GetValues(typeof(GameEnums.eTribe))){} int intinput = Convert.ToInt16(Console.ReadLine()); if (intinput == 1) { Console.WriteLine($"고르신 종족은 {GameEnum.eTribe.Human}입니다."); Console.WriteLine("직업을 선택해주세요"); } else if (intinput == 2) { Console.WriteLine($"고르신 종족은 {GameEnum.eTribe.Dwarf}입니다."); } else if (intinput == 3) { Console.WriteLine($"고르신 종족은 {GameEnum.eTribe.Nightelf}입니다."); } else if (intinput == 4) { Console.WriteLine($"고르신 종족은 {GameEnum.eTribe.Gnomes}입니다."); } else if (intinput == 5) { Console.WriteLine($"고르신 종족은 {GameEnum.eTribe.Draenei}입니다."); } else if (intinput == 6) { Console.WriteLine($"고르신 종족은 {GameEnum.eTribe.Werewolf}입니다."); } else if (intinput == 0) { break; } } else if (input == "호드") { Console.WriteLine("호드를 선택하셨습니다"); //Thread.Sleep(500); Console.WriteLine("종족을 선택해주세요"); int intinput = Convert.ToInt16(Console.ReadLine()); if (intinput == 1) { Console.WriteLine($"고르신 종족은 {GameEnum.eTribe2.Oak}입니다."); } else if (intinput == 2) { Console.WriteLine($"고르신 종족은 {GameEnum.eTribe2.Undead}입니다."); } else if (intinput == 3) { Console.WriteLine($"고르신 종족은 {GameEnum.eTribe2.Tauren}입니다."); } else if (intinput == 4) { Console.WriteLine($"고르신 종족은 {GameEnum.eTribe2.Troll}입니다."); } else if (intinput == 5) { Console.WriteLine($"고르신 종족은 {GameEnum.eTribe2.Bloodelves}입니다."); } else if (intinput == 6) { Console.WriteLine($"고르신 종족은 {GameEnum.eTribe2.Goblin}입니다."); } else if (intinput == 0) { break; } } else if (input != "얼라이언스" || input == "호드") { Console.WriteLine("잘못 입력하셨습니다"); Thread.Sleep(1000); Console.WriteLine("프로그램을 종료합니다"); break; } } /* Console.WriteLine("캐릭터의 이름을 선택하여 주세요"); var character = new Character(Console.ReadLine()); Console.WriteLine("캐릭터의 이름은 {0}입니다", character.name); character.Move(3f , 5f); */ } } } | 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 GameEnums { class Program { static void Main(string[] args) { new App(); } } } | cs |
'C# > 미완성' 카테고리의 다른 글
미완성 LOL캐릭터 생성 후 데미지 입기(데이터 저장 및 불러오기) (0) | 2019.04.10 |
---|---|
(미완성)인벤토리 만들기 (0) | 2019.04.08 |
#미완성#인벤토리 (0) | 2019.03.30 |
Array[배열] #미완성 : 메소드예시 (0) | 2019.03.27 |
미완성(3.25 과제) new, class, dot(.), 형식변환, 멤버변수, 지역변수 (0) | 2019.03.26 |