유니티3D 프로그래밍
C# 2주차 1일 수업 과제 (21.03.15) 본문
인벤토리와 아이템
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");
Inventory inven = new Inventory(10);
Item item = new Item("활", Item.eItemType.WEAPON, 1000);
Item item1 = new Item("흉갑", Item.eItemType.ARMOR, 2000);
Item item2 = new Item("쌍검", Item.eItemType.WEAPON, 1500);
Item item3 = new Item("체력포션", Item.eItemType.POTION, 100);
Item item4 = new Item("마력포션", Item.eItemType.POTION, 200);
inven.AddItem(item);
inven.AddItem(item1);
inven.AddItem(item2);
inven.AddItem(item3);
inven.AddItem(item4);
inven.Print();
Console.WriteLine();
item2 = inven.GetItem("흉갑");
inven.Print();
Console.WriteLine();
inven.FillInventory();
inven.Print();
Console.WriteLine();
inven.AddItem(item2);
inven.Print();
Console.WriteLine();
Console.Write("찾을 아이템을 입력하시오 : ");
string input = Console.ReadLine();
Console.WriteLine(inven.FindItem(input));
Console.Write("판매할 아이템을 입력하시오 : ");
input = Console.ReadLine();
inven.SellItem(input);
Console.WriteLine("현재 보유 골드 :{0} ", inven.GetGold());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study00
{
class Inventory
{
Item[] items;
int userGold = 0;
//생성자
public Inventory(int capacity)
{
Console.WriteLine("Inventory");
this.items = new Item[capacity];
}
public void AddItem(Item item)
{
for(int i = 0; i < items.Length; i++)
{
if(items[i] == null)
{
items[i] = item;
Console.WriteLine("{0} 아이템을 넣었습니다.", item.name);
break;
}
}
}
public Item GetItem(string itemName)
{
for(int i = 0; i < items.Length; i++)
{
if (items[i] != null)
{
if (items[i].name == itemName)
{
Item item = items[i];
items[i] = null;
return item;
}
}
}
Console.WriteLine("없는 아이템 입니다.");
return null;
}
public bool FindItem(string itemName)
{
for (int i = 0; i < items.Length; i++)
{
if(items[i] != null)
{
if (items[i].name == itemName)
{
Console.WriteLine("{0} 아이템이 존재합니다.", itemName);
return true;
}
}
}
Console.WriteLine("{0} 아이템이 존재하지 않습니다.", itemName);
return false;
}
public void FillInventory()
{
for (int i = 0; i < items.Length; i++)
{
if (items[i] == null && i + 1 < items.Length)
{
if (items[i + 1] != null)
{
items[i] = items[i + 1];
items[i + 1] = null;
}
}
}
}
public void Print()
{
int count = 1;
foreach(Item item in items)
{
if(item != null)
{
Console.WriteLine("{0}. {1}({2})", count, item.GetName(), item.GetItemType());
}
else
{
Console.WriteLine("{0}. [Empty]", count);
}
count++;
}
}
public void SellItem(string itemName)
{
Item item = GetItem(itemName);
if (item != null)
{
this.userGold += item.sellGold;
}
}
public int GetGold()
{
return userGold;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study00
{
public class Item
{
public enum eItemType
{
WEAPON,
ARMOR,
ACCESSORY,
POTION
}
public string name;
public eItemType itemType;
public int sellGold;
public int duplicateCount;
//생성자
public Item(string name, eItemType itemType, int sellGold)
{
this.name = name;
this.itemType = itemType;
this.sellGold = sellGold;
this.duplicateCount = 0;
}
public string GetName()
{
return this.name;
}
public eItemType GetItemType()
{
return this.itemType;
}
}
}
'C# > 수업과제' 카테고리의 다른 글
C# 2주차 3일 수업 과제 : Stack (21.03.17) (0) | 2021.03.17 |
---|---|
C# 2주차 2일 수업 과제 (21.03.16) (0) | 2021.03.16 |
C# 1주차 5일 연습 과제 (21.03.12) (0) | 2021.03.12 |
C# 1주차 4일 연습 과제 (21.03.11) (0) | 2021.03.11 |
C# 1주차 3일 연습 과제 (21.03.10) (0) | 2021.03.10 |