학원/경일게임아카데미

13. 열번째 수업

셩잇님 2022. 12. 14. 19:41
반응형

경일게임아카데미 프로그래밍반 28기 10일차 수업 (2021. 04. 21)

 


 

리스트 구현

리스트는 일반적으로 데이터가 1만개 이상일 때 빠르다.

리스트의 종류로는 순차리스트, 연결리스트가 있으며 연결 리스트에는 단일 연결리스트, 원형 연결리스트, 이중 연결리스트 등이 있다.

 

List.h

#pragma once
#include <iostream>
#include <list>
using namespace std;

class ListTest
{
private:
	list<int> _lNum;
	list<int>::iterator _liNum;

public:
	ListTest();
	~ListTest();
};

List.cpp

#include "210421 1. ListTest.h"

ListTest::ListTest()
{
	_lNum.push_back(100);
	_lNum.push_front(50);

	for (int i = 0; i < 3; i++)
	{
		_lNum.push_back(i + 1);
	}

	/* 
	for (int i = 0; i < _lNum.size(); i++)
	{
		cout << _lNum[i] << endl;
		// 왜 안될까? 벡터는 배열이므로 쓸 수 있다.
		// 그렇지만 리스트는 배열이 아니므로 쓸수 없다
	}
	*/

	for (_liNum = _lNum.begin(); _liNum != _lNum.end(); ++_liNum)
	{
		cout << *_liNum << endl;
	}
	// ++_liNum 이터레이터는 무겁기 때문에 전치를 사용한다

	_lNum.insert(_lNum.begin(), 500);

	// pop_front(), pop_back()
	// 와 같은 함수들도 당연히 존재한다.
}

ListTest::~ListTest()
{

}

 


 

벡터를 이용한 상점 및 인벤토리 구현

MainGame.h

#pragma once
#include "210421 3. Shop.h"
#include "210421 4. Inventory.h"
using namespace std;

enum LOCATION
{
	LOCATION_SHOP = 1,
	LOCATION_INVEN
};

class MainGame
{
private:
	Shop* _shop;
	Inventory* _inven;
	LOCATION _location;

	int _money;			// 소지금
	int _selectNum;		// 선택변수

public :
	MainGame();
	~MainGame();

	void setLocation(int number);
	void setMainPage();
	void setShopPage();
	void setInvenPage();
	void buy_item();
};

 

MainGame.cpp 

#include "210421 2. MainGame.h"

MainGame::MainGame()
{
	_shop = new Shop;
	_shop->itemSetup();
	
	_inven = new Inventory;

	_money = 500000;
	_selectNum = 0;

	// 처음에는 메인 화면을 보여준다
	setMainPage();
}

MainGame::~MainGame()
{
	delete _shop;
	delete _inven;
}

void MainGame::setLocation(int number)
{
	switch (number)
	{
	case LOCATION_SHOP:
		setShopPage();
		break;

	case LOCATION_INVEN:
		setInvenPage();
		break;

	default:
		setMainPage();
		break;
	}
}

void MainGame::setMainPage()
{
	cout << "=============== 28기 용사 상점 ==============" << endl;
	cout << " 1. 상점          2. 인벤토리" << endl;
	cout << "=============================================" << endl;
	cin >> _selectNum;
	
	system("cls");
	setLocation(_selectNum);
}

void MainGame::setShopPage()
{
	while (true)
	{
		cout << "=============== 28기 용사 상점 ==============" << endl;
		cout << " 1. 무기구 2. 방어구 3. 악세사리 4. 포션" << endl;
		cout << "=============================================" << endl;
		cout << "메인화면으로 가려면 0번" << endl;
		cin >> _selectNum;

		system("cls");
		if (_selectNum == 0)
		{
			setMainPage();
			break;
		}
		_shop->itemOutput(_selectNum);
		cout << "구매는 1, 뒤로가려면 아무키나 누르세요" << endl;
		cin >> _selectNum;
		if (_selectNum == 1)
		{
			buy_item();
		}
		else { }
		system("cls");
	}
}

void MainGame::setInvenPage()
{
	while (true)
	{
		cout << "=============== 용사 인벤토리 ===============" << endl;
		// TODO - 인벤토리에 있는 아이템 출력
		_inven->InvenOutput();
		cout << "=============================================" << endl;
		cout << "메인화면으로 가려면 0번" << endl;
		cin >> _selectNum;

		system("cls");
		if (_selectNum == 0)
		{
			setMainPage();
			break;
		}
	}
}

void MainGame::buy_item()
{
	cout << "test" << endl;
}

 

Shop.h

#pragma once
#include <iostream>
#include <vector>
#include <String>
using namespace std;

enum ITEM
{
	ITEM_WEAPON = 1,
	ITEM_ARMOR,
	ITEM_ACCESORY,
	ITEM_POTION,
	ITEM_END
};

struct tagItemInfo
{
	ITEM itemKind;				// 아이템 종류
	string name;				// 아이템 이름
	string description;			// 아이템 설명
	int ability;				// 아이템 능력
	int price;					// 아이템 가격
};

class Shop
{
private:
	vector <tagItemInfo>			_vItem;
	vector <tagItemInfo>::iterator	_viItem;

public:
	Shop();
	~Shop();

	void itemSetup();				// 아이템 세팅
	void itemOutput(int itemNum);	// 아이템 출력

	vector<tagItemInfo> getVShopItem() { return _vItem; }
	vector<tagItemInfo>::iterator getVIShopItem() { return _viItem; }

	//vector<tagItemInfo> setVShopItem() { _vItem = _vItem; }
	//vector<tagItemInfo>::iterator getVIShopItem() { _viItem = _viItem; }
};

Shop.cpp

#include "210421 3. Shop.h"
#include <Windows.h>

Shop::Shop()
{

}

Shop::~Shop()
{

}

void Shop::itemSetup()
{
	tagItemInfo weapon1;
	weapon1.itemKind = ITEM_WEAPON;
	weapon1.name = "유정이의 듀란달";
	weapon1.description = "완전 쎄다. 자세히 들여다보면 made in china가 적혀있다";
	weapon1.ability = 999;
	weapon1.price = 10;
	_vItem.push_back(weapon1);

	tagItemInfo weapon2;
	weapon2.itemKind = ITEM_WEAPON;
	weapon2.name = "시영이의 낫";
	weapon2.description = "흔하디 흔한 낫, 소작농의 한이 담겨있다 두덕리 마을 기념품";
	weapon2.ability = 5;
	weapon2.price = 10;
	_vItem.push_back(weapon2);

	tagItemInfo weapon3;
	weapon3.itemKind = ITEM_WEAPON;
	weapon3.name = "광수의 K2";
	weapon3.description = "px에서 구매가능하다, 행군할때 무거워짐";
	weapon3.ability = 20;
	weapon3.price = 4000;
	_vItem.push_back(weapon3);

	tagItemInfo armor1;
	armor1.itemKind = ITEM_ARMOR;
	armor1.name = "영섭이의 스냅백";
	armor1.description = "나는 슬플 때 힙합을 춰 -네가-";
	armor1.ability = 0;
	armor1.price = 1000;
	_vItem.push_back(armor1);

	tagItemInfo armor2;
	armor2.itemKind = ITEM_ARMOR;
	armor2.name = "유림이의 글러브";
	armor2.description = "불주먹 스킬이 탑재되어 있다, 본인도 뜨겁다";
	armor2.ability = 30;
	armor2.price = 100000;
	_vItem.push_back(armor2);

	tagItemInfo armor3;
	armor3.itemKind = ITEM_ARMOR;
	armor3.name = "정훈이의 태양갑옷";
	armor3.description = "태양만세 포즈로만 다닐 수 있다";
	armor3.ability = 50;
	armor3.price = 5;
	_vItem.push_back(armor3);

	tagItemInfo acce1;
	acce1.itemKind = ITEM_ACCESORY;
	acce1.name = "찬미의 은 반지";
	acce1.description = "이상하다..늑대인간이 끼고있었다..hoxy?";
	acce1.ability = 2;
	acce1.price = 10000;
	_vItem.push_back(acce1);

	tagItemInfo acce2;
	acce2.itemKind = ITEM_ACCESORY;
	acce2.name = "주화의 시계";
	acce2.description = "마취침이 나간다. 물론 방향은 내 쪽이다";
	acce2.ability = 10;
	acce2.price = 5;
	_vItem.push_back(acce2);

	tagItemInfo potion1;
	potion1.itemKind = ITEM_POTION;
	potion1.name = "양쌤의 아아";
	potion1.description = "선동과 날조가 능수능란해진다 feat.10분";
	potion1.ability = 2000;
	potion1.price = 2000;
	_vItem.push_back(potion1);

	tagItemInfo potion2;
	potion2.itemKind = ITEM_POTION;
	potion2.name = "종익이의 몬스터";
	potion2.description = "심장아 나대지마 (그거 부정맥이야)";
	potion2.ability = 500;
	potion2.price = 1000;
	_vItem.push_back(potion2);
}


void Shop::itemOutput(int itemNum)
{
	for (_viItem = _vItem.begin(); _viItem != _vItem.end(); ++_viItem)
	{
		if (_viItem->itemKind != itemNum) continue;

		cout << "=============== 28기 용사 상점 ==============" << endl;
		cout << "이 름 : " << _viItem->name << endl;
		cout << "성 능 : " << _viItem->ability << endl;
		cout << "설 명 : " << _viItem->price << endl;
		cout << "가 격 : " << _viItem->description << endl;
		cout << "=============================================" << endl << endl;
	}	
}

 

Inventory.h

#pragma once
#include <vector>
#include <iostream>
#include "210421 3. Shop.h"
using namespace std;

class Inventory
{
public:
	Inventory();
	~Inventory();
 
	void InvenOutput();
};

 

Inventory.cpp

#include "210421 4. Inventory.h"

Inventory::Inventory()
{

}

Inventory::~Inventory()
{

}

void Inventory::InvenOutput()
{
	// TODO - 출력
}
반응형