#include#include #include //商品结构-名称、单价、库存量、描述 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(); //交易函数 void Trade(Player *player, int propId); int i,j; //循环变量 int main() { printf("n* * * * * * * * * * * * * * * * 交 易 前 * * * * * * * * * * * * * * * * * * * * * *nn"); //1,初始化数据 Init(); //2,打印这些初始化数据 ShowProps(); ShowPlayers(); printf("n* * * * * * * * * * * * * * * * 交 易 后 * * * * * * * * * * * * * * * * * * * * * *nn"); 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("*tttt商 城 *n"); printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *n"); printf("* %st 名称tt 单价tt库存tt 商品介绍 *n","编号"); printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *n"); for(int i = 0;i < propsCount;i++) { printf("* %dt%st%.2lftt %dtt%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("*ttt 玩 家 信 息 *n"); printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *n"); printf("* 编号t 用户名tt 密码ttt 金币ttt元宝 *n"); printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *n"); for(int i = 0;i < playerCount;i++) { printf("* %dt %st%stt %.2lftt%.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%st%dtn",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++; } }
最终效果



