반응형
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <Windows.h>
#include <time.h>
#include <algorithm>
using namespace std;
#pragma warning (disable : 4996)
//인벤토리 구현
//User 정보
struct UserInfo
{
char UserName[128];
int GameGold = 100000;
};
struct ItemInfo
{
char Name[128];
int Level; //1~30
int Grade; //등급 1~7
int Star; //성급 1~6성(업그레이드 확률은 성급이 올라갈 수록 점점 어려워짐)
int Cost = 1000;
};
void SaveFile(vector<ItemInfo>* ItemList)
{
FILE* wFP = fopen("ItemList.pnj", "wb");
int num = 0;
num = ItemList->size();
if (wFP != NULL)
{
fwrite(&num, sizeof(int), 1, wFP);//번호
for (int i = 0; i < ItemList->size(); i++)
{
int Strsize = strlen((*ItemList)[i].Name) + 1;
fwrite(&Strsize, sizeof(1), 1, wFP);//글자수 저장
fwrite((*ItemList)[i].Name, Strsize, 1, wFP);
fwrite(&(*ItemList)[i].Level, sizeof(int), 1, wFP);
fwrite(&(*ItemList)[i].Grade, sizeof(int), 1, wFP);
fwrite(&(*ItemList)[i].Star, sizeof(int), 1, wFP);
fwrite(&(*ItemList)[i].Cost, sizeof(int), 1, wFP);
}
}
fclose(wFP);
}
void LoadFile(vector<ItemInfo>* ItemList)
{
FILE* rFP = fopen("ItemList.pnj", "rb");
int num = 0;
int Strsize = 0;
if (rFP != NULL)
{
fread(&num, sizeof(int), 1, rFP);//번호
for (int i = 0; i < num; i++)
{
ItemInfo TempList;
fread(&Strsize, sizeof(1), 1, rFP);//글자수 저장
fread(TempList.Name, Strsize, 1, rFP);
fread(&TempList.Level, sizeof(int), 1, rFP);
fread(&TempList.Grade, sizeof(int), 1, rFP);
fread(&TempList.Star, sizeof(int), 1, rFP);
fread(&TempList.Cost, sizeof(int), 1, rFP);
ItemList->push_back(TempList);
}
fclose(rFP);
}
else
{
printf("Fail");
getchar();
system("cls");
}
}
void AddItem(vector<ItemInfo>* ItemList)
{
ItemInfo TempList;
char t_Name[5][128] = { "아기상어의반지","고양이의검","강아지의방패","싸이어인의장갑","사자의지팡이" };
int random = 0;
random = rand() % 5;
strcpy(TempList.Name, t_Name[random]);
random = (rand() % 3) + 1;//1~3
TempList.Level = random;
TempList.Grade = (rand() % 7) + 1;
TempList.Star = (rand() % 6) + 1;
TempList.Cost = 1000;
ItemList->push_back(TempList);
SaveFile(ItemList);
}
bool LevelSort(const ItemInfo& a, const ItemInfo& b)
{
return a.Level > b.Level;
}
bool GradeSort(const ItemInfo& a, const ItemInfo& b)
{
return a.Grade < b.Grade;//낮을수록 높은 등급
}
bool StarSort(const ItemInfo& a, const ItemInfo& b)
{
return a.Star > b.Star;
}
bool(*Sort)(const ItemInfo& a, const ItemInfo& b);//Sort포인터함수
void AddPrintList(vector<ItemInfo>* ItemList)
{
int count = ItemList->size();
printf("<보유중인 아이템 리스트>\n");
for (int i = 0; i < count; i++)
{
if (i == count - 1)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
printf("%d번 : 이름(%s) [%d레벨] [%d등급] [%d성] [%d원]\n",
i + 1, (*ItemList)[i].Name, (*ItemList)[i].Level, (*ItemList)[i].Grade, (*ItemList)[i].Star, (*ItemList)[i].Cost);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
else
{
printf("%d번 : 이름(%s) [%d레벨] [%d등급] [%d성] [%d원]\n",
i + 1, (*ItemList)[i].Name, (*ItemList)[i].Level, (*ItemList)[i].Grade, (*ItemList)[i].Star, (*ItemList)[i].Cost);
}
}
}
void PrintList(vector<ItemInfo>* ItemList)
{
int count = ItemList->size();
printf("<보유중인 아이템 리스트>\n");
for (int i = 0; i < count; i++)
{
printf("%d번 : 이름(%s) [%d레벨] [%d등급] [%d성] [%d원]\n",
i + 1, (*ItemList)[i].Name, (*ItemList)[i].Level, (*ItemList)[i].Grade, (*ItemList)[i].Star, (*ItemList)[i].Cost);
}
}
void LevelUpgrade(vector<ItemInfo>* ItemList, vector<UserInfo>* User)
{
int Sel = 0;
printf("레벨을 올리고 싶은 무기번호를 선택해 주세요(1000골드 감소) :");
scanf_s("%d", &Sel);
getchar();
Sel--;
int Level = (*ItemList)[Sel].Level;
float chance = (31 - Level) / 30.0f * 100.0f;
printf("레벨업 확율은 %d%% 입니다. 계속 하시겠습니까? 1,Yes 2,No :", (int)chance);
int Try = 0;
scanf_s("%d", &Try);
getchar();
if (Try == 2)
return;
if ((*User)[0].GameGold < 1000)
{
printf("\n보유머니가 부족합니다.");
return;
}
(*User)[0].GameGold = (*User)[0].GameGold - 1000;
for (int i = 0; i < 3; i++)
{
printf(".");
Sleep(300);
}
int luck = 0;
luck = rand() % 100;
if (luck < (int)chance)
{
(*ItemList)[Sel].Level++;
(*ItemList)[Sel].Cost = (*ItemList)[Sel].Cost + 100;
printf("\n레벨업에 성공했습니다! 1레벨 상승!\n");
printf("%d번 : 이름(%s) [%d레벨] [%d등급] [%d성] [%d원]\n",
Sel + 1, (*ItemList)[Sel].Name, (*ItemList)[Sel].Level, (*ItemList)[Sel].Grade, (*ItemList)[Sel].Star, (*ItemList)[Sel].Cost);
printf("남은 보유머니 : %d", (*User)[0].GameGold);
}
else
{
(*ItemList).erase((*ItemList).begin() + Sel);
printf("레벨업 실패...\n");
printf("해당 아이템을 잃었습니다...");
getchar();
}
}
void GradeUpgrade(vector<ItemInfo>* ItemList, vector<UserInfo>* User)
{
int Sel1 = 0;
int Sel2 = 0;
printf("등급을 올리고 싶은 무기번호를 선택해 주세요(500골드 감소) :");
scanf_s("%d", &Sel1);
getchar();
Sel1--;
printf("재료 아이템을 선택하세요 :");
scanf_s("%d", &Sel2);
getchar();
Sel2--;
if (Sel1 == Sel2)
{
printf("\n강화아이템과 재료아이템이 일치합니다.");
return;
}
int Grade = (*ItemList)[Sel1].Grade;
float chance = Grade / 7.0f * 100.0f;
printf("등급업 확율은 %d%% 입니다. 계속 하시겠습니까? 1,Yes 2,No :", (int)chance);
int Try = 0;
scanf_s("%d", &Try);
getchar();
if (Try == 2)
return;
if ((*User)[0].GameGold < 1000)
{
printf("\n보유머니가 부족합니다.");
return;
}
(*User)[0].GameGold = (*User)[0].GameGold - 500;
for (int i = 0; i < 3; i++)
{
printf(".");
Sleep(300);
}
int luck = 0;
luck = rand() % 100;
if (luck < (int)chance)
{
(*ItemList)[Sel1].Grade--;
(*ItemList)[Sel1].Cost = (*ItemList)[Sel1].Cost + 1000;
printf("\n등급업에 성공했습니다! 1등급 상승!\n");
printf("%d번 : 이름(%s) [%d레벨] [%d등급] [%d성] [%d원]\n",
Sel1 + 1, (*ItemList)[Sel1].Name, (*ItemList)[Sel1].Level, (*ItemList)[Sel1].Grade, (*ItemList)[Sel1].Star, (*ItemList)[Sel1].Cost);
printf("남은 보유머니 : %d", (*User)[0].GameGold);
(*ItemList).erase((*ItemList).begin() + Sel2);
}
else
{
(*ItemList).erase((*ItemList).begin() + Sel1);
(*ItemList).erase((*ItemList).begin() + Sel2);
printf("등급업 실패...\n");
printf("모든 아이템을 잃었습니다...");
getchar();
}
}
void StarUpgrade(vector<ItemInfo>* ItemList, vector<UserInfo>* User)
{
int Sel1 = 0;
int Sel2 = 0;
printf("성급을 올리고 싶은 무기번호를 선택해 주세요(1000골드 감소) :");
scanf_s("%d", &Sel1);
getchar();
Sel1--;
printf("재료 아이템을 선택하세요 :");
scanf_s("%d", &Sel2);
getchar();
Sel2--;
if (Sel1 == Sel2)
{
printf("\n강화아이템과 재료아이템이 일치합니다.");
return;
}
int Star = (*ItemList)[Sel1].Star;
float chance = (7 - Star) / 6.0f * 100;
printf("성급업 확율은 %d%% 입니다. 계속 하시겠습니까? 1,Yes 2,No :", (int)chance);
int Try = 0;
scanf_s("%d", &Try);
getchar();
if (Try == 2)
return;
for (int i = 0; i < 3; i++)
{
printf(".");
Sleep(300);
}
int luck = 0;
luck = rand() % 100;
if (luck < (int)chance)
{
(*ItemList)[Sel1].Star++;
(*ItemList)[Sel1].Cost = (*ItemList)[Sel1].Cost + 3000;
printf("\n성급업에 성공했습니다! 1성급 상승!\n");
printf("%d번 : 이름(%s) [%d레벨] [%d등급] [%d성] [%d원]\n",
Sel1 + 1, (*ItemList)[Sel1].Name, (*ItemList)[Sel1].Level, (*ItemList)[Sel1].Grade, (*ItemList)[Sel1].Star, (*ItemList)[Sel1].Cost);
(*ItemList).erase((*ItemList).begin() + Sel2);
}
else
{
(*ItemList).erase((*ItemList).begin() + Sel1);
(*ItemList).erase((*ItemList).begin() + Sel2);
printf("레벨업 실패...\n");
printf("모든 아이템을 잃었습니다...");
getchar();
}
}
void UpgradeItem(void(*Upgrade)(vector<ItemInfo>* ItemList, vector<UserInfo>* User), vector<ItemInfo>* ItemList, vector<UserInfo>* User)
{
Upgrade(ItemList, User);
SaveFile(ItemList);
}
void UserSave(vector<UserInfo>* User)
{
FILE* wFP = fopen("User.pnj", "wb");
if (wFP != NULL)
{
int Strsize = strlen((*User)[0].UserName) + 1;
fwrite(&Strsize, sizeof(1), 1, wFP);//글자수 저장
fwrite((*User)[0].UserName, Strsize, 1, wFP);
fwrite(&(*User)[0].GameGold, sizeof(int), 1, wFP);
}
fclose(wFP);
}
void UserLoad(vector<UserInfo>* User)
{
FILE* wFP = fopen("User.pnj", "rb");
if (wFP != NULL)
{
UserInfo TempUser;
int Strsize = 0;
fread(&Strsize, sizeof(1), 1, wFP);//글자수 저장
fread(TempUser.UserName, Strsize, 1, wFP);
fread(&TempUser.GameGold, sizeof(int), 1, wFP);
User->push_back(TempUser);
}
fclose(wFP);
}
void AddUser(vector<UserInfo>* User)
{
UserInfo TempUser;
FILE* rFP = fopen("User.pnj", "rb");
if (rFP == NULL)
{
printf("유저의 이름을 입력해 주세요 : ");
scanf_s("%s", TempUser.UserName, sizeof(TempUser.UserName));
User->push_back(TempUser);
UserSave(User);
}
system("cls");
}
void SellItem(vector<ItemInfo>* ItemList, vector<UserInfo>* User)
{
int Sel = 0;
PrintList(ItemList);
printf("\n판매할 아이템 번호를 선택해주세요(가격의 80%%를 받게 됩니다.) :");
scanf_s("%d", &Sel);
getchar();
Sel--;
if (Sel <0 || Sel>(*ItemList).size())
{
printf("잘못 입력하셨습니다.");
getchar();
return;
}
int price = (*ItemList)[Sel].Cost *0.8;
printf("%d원에 판매할 수 있습니다. \n정말 판매 하시겠습니까? 1.Yes 2.No :",price);
int Try = 0;
scanf_s("%d", &Try);
getchar();
if (Try == 2)
return;
(*ItemList).erase((*ItemList).begin() + Sel);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_BLUE);
printf("유저 보유 머니 [%d] + %d\n", (*User)[0].GameGold,price);
(*User)[0].GameGold = (*User)[0].GameGold + price;
printf("유저 보유 머니 [%d] ", (*User)[0].GameGold);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
SaveFile(ItemList);
UserSave(User);
}
void main()
{
srand((unsigned)time(NULL));
vector<ItemInfo> ItemList;
vector<UserInfo> User;
AddUser(&User);
UserLoad(&User);
LoadFile(&ItemList);
while (true)
{
printf("<무기 강화 프로그램>");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED);
printf("[%s] : Gold[%d]\n\n", User[0].UserName, User[0].GameGold);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
printf("1.아이템 추가 2.아이템 보기 3.아이템 업그레이드 4.아이템 판매 99.프로그램 종료 : ");
int Sel = 0;
scanf("%d", &Sel);
getchar();
printf("\n");
if (Sel < 1 || Sel > 4)
{
if (Sel == 99)
break;
continue;
system("cls");
}
if (Sel == 1)
{
AddItem(&ItemList);
AddPrintList(&ItemList);
}
else if (Sel == 2)
{
printf("1, 그냥보기 2, 레벨순 3, 등급순 4, 성급순 : ");
scanf_s("%d", &Sel);
getchar();
if (Sel < 1 || Sel > 4)
{
continue;
system("cls");
}
//bool (*Sort)(const ItemInfo & a, const ItemInfo & b);
if (Sel == 1)
Sort = LevelSort;
else if (Sel == 2)
Sort = GradeSort;
else if (Sel == 3)
Sort = StarSort;
if (Sel == 1)
{
PrintList(&ItemList);
}
else
{
if (Sel == 2)
Sort = LevelSort;
else if (Sel == 3)
Sort = GradeSort;
else if (Sel == 4)
Sort = StarSort;
sort(ItemList.begin(), ItemList.end(), Sort);
PrintList(&ItemList);
}
}//else if (Sel == 2)
else if (Sel == 3)
{
printf("무엇을 강화하실 건가요? \n1, 레벨 2, 등급 3, 성급 :");
scanf_s("%d", &Sel);
getchar();
printf("\n");
PrintList(&ItemList);
printf("\n");
if (Sel == 1)
{
UpgradeItem(LevelUpgrade, &ItemList, &User);
}
else if (Sel == 2)
{
UpgradeItem(GradeUpgrade, &ItemList, &User);
}
else if (Sel == 3)
{
UpgradeItem(StarUpgrade, &ItemList, &User);
}
}//else if (Sel == 3)
else if (Sel == 4)
{
SellItem(&ItemList, &User);
}
UserSave(&User);
getchar();
system("cls");
}//while
}
반응형
'C++' 카테고리의 다른 글
[c++] STL Container의 종류 (0) | 2021.08.18 |
---|---|
[c++] STL 이란? (0) | 2021.08.18 |
[c++] 스택(Stack)과 힙(Heap) (0) | 2021.08.16 |
[c++] 포인터와 레퍼런스 (0) | 2021.08.09 |
[c++]파일 로딩 프로그램 (0) | 2020.04.02 |