阶段小功能—>游戏商城(C语言)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
模拟实现道具店购物功能(商店暂时只支持一种类型的商品)
商品具备名称、价格、库存等属性
模拟玩家购买游戏道具
1,玩家选择要购买的道具
2,玩家同意交易后扣除相应的游戏币
3,对应商品库存-1
4,玩家背包中增加商品或该商品的数量+1
*/
/*商品结构*/
//商品结构-名称、单价、库存量、描述
typedef struct _prop
{
int id; //道具的唯一编号
char name[50]; //道具名称
double price; //道具单价
int stock; //库存量,如果在背包中代表此道具的叠加数量
char desc[200]; //道具描述
}Prop;
/*背包结构*/
//背包结构-玩家编号、游戏道具[商品数组]、道具数量、最大道具数
typedef struct _bag
{
int playerId; //所属玩家的编号
int count; //当前背包中,道具的数量
int max; //当前背包的插槽总数-插槽数量可以让用户用rmb购买
Prop props[18]; //当前背包中的道具数组
}Bag;
/*玩家结构*/
//玩家结构-编号、名称、密码、金钱【背包】
typedef struct _player
{
int id; //玩家编号
char name[50]; //用户名
char pass[50]; //密码
Bag bag; //玩家的背包
double gold; //玩家金币-人性化显示:100000转换成银币、铜币显示
double sysee; //元宝数量
}Player;
Prop *props;
Player *players;
int propsCount = 0;
int playerCount = 0;
void Init(); //用来初始化游戏数据
void ShowProps();
void ShowPlayers();
//交易函数
/*参数1:参与交易的玩家指针-为了方便修改玩家交易后的金币数*/
/*参数2:交易玩家的id*/
void Trade(Player *player, int propId);
int i,j; //循环变量
int main()
{
printf("\n* * * * * * * * * * * * * * * * 交 易 前 * * * * * * * * * * * * * * * * * * * * * *\n\n");
//1,初始化数据
Init();
//2,打印这些初始化数据
ShowProps();
ShowPlayers();
printf("\n* * * * * * * * * * * * * * * * 交 易 后 * * * * * * * * * * * * * * * * * * * * * *\n\n");
Trade(&players[2],2);
Trade(&players[2],3);
Trade(&players[1],3);
Trade(&players[2],1);
ShowProps();
ShowPlayers();
system("color 2");
return 0;
}
void Init()
{
static Prop propArray[] = {
{1, "无尽之刃", 3000, 10, "增加暴击8000!"},
{2, "泣血之刀", 2800, 10, "增加吸血600!"},
{3, "抵抗之靴", 200, 10, "增加移速53%,魔抗24%!"},
{4, "贤者之书", 3200, 10, "增加法力2000!"},
{5, "破军之刀", 3500, 10, "增加物理攻击50%!"},
//{6, "黎明破晓", 1800, 10, "增加射速25%!"},
};
propsCount = sizeof(propArray) / sizeof(Prop);
props = propArray; //设定指针的指向
static Player playArray[] = {
{1, "laity ", "sucessful", .gold = 50000, .bag.max=8},
{2, "dull ", "dull2022", .gold = 150000, .bag.max=8},
{3, "emperor", "emperor22", .gold = 173000, .bag.max=8},
{4, "fantasy", "fatasy1983", .gold = 15900, .bag.max=8}
};
players = playArray;
playerCount = sizeof(playArray) / sizeof(Player);
}
void ShowProps()
{
if(props == NULL)
return;
printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf("*\t\t\t\t商 城 *\n");
printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf("* %s\t 名称\t\t 单价\t\t库存\t\t 商品介绍 *\n","编号");
printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
for(int i = 0;i < propsCount;i++)
{
printf("* %d\t%s\t%.2lf\t\t %d\t\t%s*\n",props[i].id,props[i].name,props[i].price,props[i].stock,props[i].desc);
printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
}
}
void ShowPlayers()
{
if(players == NULL)
return;
printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf("*\t\t\t 玩 家 信 息 *\n");
printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf("* 编号\t 用户名\t\t 密码\t\t\t 金币\t\t\t元宝 *\n");
printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
for(int i = 0;i < playerCount;i++)
{
printf("* %d\t %s\t%s\t\t %.2lf\t\t%.2lf*\n",players[i].id,players[i].name,players[i].pass,players[i].gold,players[i].sysee);
printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
for(j = 0;j < players[i].bag.count;j++)
{
printf("\t%s\t%d\t\n",players[i].bag.props[j].name, players[i].bag.props[j].stock);
}
printf("\n");
}
}
void Trade(Player *player, int propId)
{
//商品库存,玩家余额,玩家背包能否装下
Prop *tradeProp = NULL; //要购买的商品指针
for(i = 0;i < propsCount;i++)
{
if(propId == props[i].id)
{
tradeProp = &props[i]; //数组中第i个元素的地址
break;
}
}
if(tradeProp->stock <= 0)
{
printf("地主家都没有余粮,商品已售磐!\n");
return;
}
if(player->gold < tradeProp->price)
{
printf("你个穷鬼,连钱都没有还来购买商品!\n");
return;
}
if(player->bag.count >= player->bag.max && player->bag.count != 0)
{
printf("背包已满,交易失败!\n");
return;
}
//满足交易条件,执行交易操作
//1,商品库存-1
tradeProp->stock--;
//2,玩家金币-商品单价
player->gold -= tradeProp->price;
//3,玩家背包道具增加
//判断玩家背包中是否已经有该商品
//如果有,背包中的该商品总数+1
for(i = 0;i < player->bag.count;i++)
{
//如果要购买的商品id与背包中的某个商品id相同
if(propId == player->bag.props[i].id)
{
player->bag.props[i].stock++;
break;
}
}
//如果没有该商品,添加该商品
if(i == player->bag.count)
{
//向背包中创建一个商品 -> 复制一份要交易的商品信息到背包中
player->bag.props[player->bag.count].id = tradeProp->id;
player->bag.props[player->bag.count].price = tradeProp->price;
player->bag.props[player->bag.count].stock = 1;
strcpy(player->bag.props[player->bag.count].name,tradeProp->name);
strcpy(player->bag.props[player->bag.count].desc,tradeProp->desc);
player->bag.count++;
}
}
最终效果