GameShop

王波
2023-12-01

## GameShop

```c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct _prop
{
	int id;         //道具的  id
	char names[50]; //道具的名称
	double price;   //道具的单价
	int stock;      //道具的库存
	char desc[100]; //道具的描述
}Prop;
typedef struct _bag
{
	int id;     //玩家背包的编号
	int count;  //背包中道具的数量
	int max;    //当前背包的插槽总数
	int place;         //背包剩余位置总数
	Prop props[6]; //当前背包中的道具数组
}Bag;
typedef struct _player
{
	int id;            //玩家的编号
	char name[50];     //用户名
	char password[50]; //密码
	Bag bag;           //玩家的背包
	double gold;       //玩家的金币
	double sysee;      //元宝的数量
}Player;
int showpropcount;     //统计商店道具个数
int playerscount;      //统计玩家个数
Prop* props;           //指向商店的结构指针
Player*player;         //指向玩家的指针
void InputShop();      //引入商店内容
void ShowShop();       //打印商店内容
void InputPlayers();   //引入玩家内容
void ShowPlayers();    //打印玩家内容
void Trade(Player *player, int propid);




int main()
{
	system("color 74");
	InputShop();
	InputPlayers();
	printf("\n***********交易前*************");
	ShowShop();
	ShowPlayers();
	printf("\n***********交易后*************");
	printf("\n\n");
	Trade(&player[0], 3);
	Trade(&player[0], 2);
	Trade(&player[0], 1);
	Trade(&player[1], 3);
	Trade(&player[0], 3);
	Trade(&player[2], 4);
	Trade(&player[3], 1);
	Trade(&player[3], 5);
	Trade(&player[2], 3);
	Trade(&player[1], 5);
	ShowShop();
	ShowPlayers();
}


void InputShop()
{
	static  Prop showprop[] = {
		{1,"饮血剑",2300,10,"25%的物理吸血"},
		{2,"无尽之刃",3200,10,"20%的物理暴击,暴击伤害翻倍"},
		{3,"疾步之靴",700,15,"增加25%的移动速度"},
		{4,"卢登的回声",2500,7,"给予法术涌动效果,增加法术伤害"},
		{5,"亡者的板甲",3000,5,"增加移动速度生命值,5秒内没有攻击则下次攻击额外伤害"}
	};
	props = showprop;
	showpropcount = 5;
}
void ShowShop()
{
	int i;
	if (props == NULL)
		return;
	printf("%-5s%-11s%-5s%-6s%-60s\n","编号","名称","单价","库存","装备描述");
	for (i = 0; i < showpropcount; i++)
	{
		printf("%-5d%-11s%-5.0lf%-6d%-60s\n", props[i].id,props[i].names,props[i].price,props[i].stock,props[i].desc);
	}
}
void InputPlayers()
{
	static Player players[] = {
		{1,"Fiker","123456789",.bag.id = 1,.bag.max = 6, .gold = 7000,.bag.count = 0,.bag.place = 6},
		{2,"PDD","135790",.bag.id = 1,.bag.max = 6, .gold = 6000,.bag.count = 0,.bag.place = 6},
		{3,"Doinb","246810",.bag.id = 1,.bag.max = 6, .gold=6000,.bag.count = 0,.bag.place = 6},
		{4,"Uzi","987654321",.bag.id = 1,.bag.max = 6, .gold = 7000,.bag.count = 0,.bag.place = 6}
	};
	player = players;
	playerscount = sizeof(players) / sizeof(Player);
}

void ShowPlayers()
{
	int i,j;
	if (player == NULL)
		return;
	printf("%-10s%-8s%-13s%-10s%-20s%-14s%-14s%-10s%-10s\n","玩家编号", "用户名", "密码", "背包编号", "背包剩余道具数量", "背包位置总数","背包剩余位置","金币数量","点券数量");
	for (i = 0; i < playerscount; i++)
	{
		printf("%-10d%-8s%-13s%-10d%-20d%-14d%-14d%-10.0lf%-10.0lf\n", player[i].id, player[i].name, player[i].password, player[i].bag.id, player[i].bag.count,
			player[i].bag.max,player[i].bag.place,player[i].gold,player[i].sysee);
		for (j = 0; j < player[i].bag.max - player[i].bag.place; j++)
		{
			printf("%s\t%d\n", player[i].bag.props[j].names, player[i].bag.props[j].stock);
		}
		printf("\n");
	}
}

void Trade(Player* player, int propid)
{
	int i;
	Prop* tradeprop;
	tradeprop = NULL;
	if (propid > 5 || propid < 1)
	{
		printf("你输入的装备有误.\n");
		exit(0);
	}
	for (i = 0; i < showpropcount; i++)
	{
		if (propid == props[i].id)
		{
			tradeprop = &props[i];
			break;
		}
	}
	//库存,背包位置,金币, 
	if (tradeprop->stock <= 0)
	{
		printf("该道具库存不足.\n\n");
		return;
	}
	if ((player->bag.max - player->bag.count) < 1)
	{
		printf("%s你的背包已满,请清理后再买.\n\n",player->name);
		return;
	}
	if (player->gold < tradeprop->price)
	{
		printf("你的金币不足.\n\n");
		return;
	}
	tradeprop->stock--;//商店库存-1`在这里插入代码片`
	player->gold -= tradeprop->price;//玩家金币减少

	for (i = 0; i < player->bag.max; i++)
	{
		//如果购买的道具的id和背包中已经有的道具id相同
		if (propid == player->bag.props[i].id)
		{
			player->bag.props[i].stock++;//找到之后背包中该商品个库存加1
			player->bag.count++;
			break;
		}

		if (i == player->bag.count)
		{ 
			int index = player->bag.count;
			player->bag.props[index].id = tradeprop->id;
			strcpy(player->bag.props[index].names, tradeprop->names);
			player->bag.props[index].stock = 1;
			strcpy(player->bag.props[index].desc, tradeprop->desc);
            player->bag.count++;
			player->bag.place--;
			break;

		}
    }

}
Trade(&player[0], 3);
Trade(&player[0], 2);
Trade(&player[0], 1);
Trade(&player[1], 3);
Trade(&player[0], 3);
Trade(&player[2], 4);
Trade(&player[3], 1);
Trade(&player[3], 5);
Trade(&player[2], 3);
Trade(&player[1], 5);
以上main以内为购买装备部分,暂时只可程序内购买。。。

 类似资料:

相关阅读

相关文章

相关问答