/*模拟实现道具店购物功能
商店暂时只支持一种类型的商品
商品具备名称、价格、库存等属性
模拟玩家购买游戏道具
1.玩家选择要购买的道具
2.玩家同意交易后扣除相应游戏币
3.对应商品库存-1
*4.玩家背包中增加商品或该商品数量+1
商店暂时只支持一种类型的商品
商品具备名称、价格、库存等属性
模拟玩家购买游戏道具
1.玩家选择要购买的道具
2.玩家同意交易后扣除相应游戏币
3.对应商品库存-1
*4.玩家背包中增加商品或该商品数量+1
*/
我将其分为了三个文件,main.c、Shop.c和Shop.h。
Shop.h:
#ifndef SHOP_H_INCLUDED #define SHOP_H_INCLUDED /* 模拟实现道具店购物功能 商店暂时只支持一种类型的商品 商品具备名称、价格、库存等属性 模拟玩家购买游戏道具 1.玩家选择要购买的道具 2.玩家同意交易后扣除相应游戏币 3.对应商品库存-1 *4.玩家背包中增加商品或该商品数量+1 结构体:1.商店:道具编号、道具名称、库存、单价、 2.玩家:id、昵称、金钱、背包属性 3.背包:玩家id、背包最大空间、背包道具 */ typedef struct _shop//商店的结构体 { int Propid;//道具id char PropNames[50];//道具名称 int PropStock;//道具库存 int PropPrice;//道具单价 }Prop; typedef struct _bag//背包的机构体 { int playerid;//道具所属玩家的id int bagmax;//背包的最大空间 int count;//当前背包道具数量 Prop props[8];//这个数组存在的意义:背包里会调用道具的结构体 }Bag; typedef struct _player//玩家的结构体 { int id;//玩家的id char playernames[50];//玩家的昵称 int money;//玩家的金币数量 Bag bag;//玩家的背包(相当于结构体中嵌套结构体) }Player; void Init(); void showprops(); void showplayer(); void trade(Player *player,int tradeid); #endif // SHOP_H_INCLUDED
Shop.c:
#include "Shop.h" #include<string.h> /* 模拟实现道具店购物功能 商店暂时只支持一种类型的商品 商品具备名称、价格、库存等属性 模拟玩家购买游戏道具 1.玩家选择要购买的道具 2.玩家同意交易后扣除相应游戏币 3.对应商品库存-1 *4.玩家背包中增加商品或该商品数量+1 */ Prop *props;//定义了一个道具指针 Player *players;//定义一个玩家指针 int propscount=0;//定义道具数量 int playercount=0;//定义玩家数量 void Init()//初始化函数 { static Prop proparray[]={ //初始化商店数组 {1,"59坦克",4,100000,}, {2,"双倍经验卡",10,1000,}, {3,"WZ111",2,200000,}, {4,"高级账号3天",10,10000,}, {5,"中型坦克输弹机",10,100000,} }; propscount = sizeof(proparray)/sizeof(Prop);//商店里拥有的道具数量 props = proparray;//将指针指向数组,直接可以用props操作数据 static Player playerarray[]={//玩家初始化 {1,"进击的小黑狗",300000,.bag.bagmax = 10}, {2,"熊出没",200000,.bag.bagmax = 10}, {3,"半城抹茶",400000,.bag.bagmax = 10}, {4,"小毛丫",100000,.bag.bagmax = 10} }; playercount = sizeof(playerarray)/sizeof(Player);//拥有的玩家数量 players = playerarray;//将指针指向数组 } void showprops()//显示商店的情况 { int i; if(props == NULL)//如果该指针为空时 { return; } printf("%-5s%-14s%-9s库存\n","编号","名称","单价"); for(i=0; i<propscount; i++) { printf("%-5d%-14s%-9d%d\n",props[i].Propid,props[i].PropNames,props[i].PropPrice,props[i].PropStock); //因为前面已经将指针props指向了数组proparray,所以props可以看做时数组 } } void showplayer()//显示玩家的情况 { int i,j; if(players == NULL)//如果该指针为空时 { return; } printf("%-5s%-14s%-9s\n","编号","昵称","金钱"); for(i=0; i<playercount; i++) { printf("%-5d%-14s%-9d\n",players[i].id,players[i].playernames,players[i].money); printf("%-14s数量\n","道具名称");//因为前面已经将指针players指向了数组playerarray,所以players可以看做时数组 for(j=0; j<players[i].bag.count;j++)//嵌套,既要显示玩家信息,又要显示该玩家拥有道具信息 printf("%-14s%d\n",players[i].bag.props[j].PropNames,players[i].bag.props[j].PropStock); } } /** * 交易函数 * 参数1:参与交易的玩家指针-为了方便修改玩家交易后的金币数 * 参数2:玩家购买的商品id */ void trade(Player *player,int tradeid) { int i,j; //1.金钱是否够 2.玩家背包是否满了3.商品的库存 Prop *tradeprop =NULL;//要购买商品指针 for(i=0; i<propscount; i++)//记录下你要购买的商品id { if(tradeid == props[i].Propid) { tradeprop = &props[i]; break;//找到了就结束程序 } } if(tradeprop->PropStock < 1)//商城里是否还有库存 { printf("商城也没有了,期待下次刷新\n"); printf("交易失败\n"); } if(player->money <tradeprop->PropPrice)//金钱是否够 { printf("抱歉您的钱还不够\n"); printf("交易失败\n"); } if(player->bag.count >= player->bag.bagmax && player->bag.count != 0) { printf("抱歉您的背包已满\n"); printf("交易失败\n"); } //可以交易 1.玩家金币减少 2.商品库存 - 1 player->money =player->money -tradeprop->PropPrice; tradeprop->PropStock--; //*3、玩家背包道具增加 //判断玩家背包中是否已有该商品 //如果有该商品,该商品添加到背包中即可 //如果没有该商品,背包中的该商品总数+1 for(i=0; i<player->bag.count; i++) { if(tradeid == player->bag.props[i].Propid)//已经有了 { player->bag.props[i].PropStock++; } } if(i == player->bag.count)//如果没有该商品,该商品添加到背包中即可 { int index; index = player->bag.count;//存放数组的位置 player->bag.count++; player->bag.props[index].Propid =tradeid;//添加道具id strcpy(player->bag.props[index].PropNames,tradeprop->PropNames); player->bag.props[index].PropStock = 1; } /**printf("请输入想买道具的编号\n"); scanf("%d",&tradeid); //条件:1.编号在我们范围内2.金钱是否够 3.玩家背包是否满了4.商品的库存 while(tradeid<=0 && tradeid>5) { printf("输入的道具编号有误,请重新输入\n"); scanf("%d",&tradeid); }改进的地方 */ }main.c:
#include <stdio.h> #include <stdlib.h> #include "Shop.h" #include<string.h> Player *players;//定义一个玩家指针 int main() { Init(); printf("交易前*****************************\n"); showprops(); showplayer(); trade(&players[0],2); printf("交易后*****************************\n"); showprops(); showplayer(); return 0; }