C 购买商品的游戏
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 /* 5 *模拟实现道具店购物功能(商店暂时只支持一种类型的商品) 6 *商品具备名称,价格,库存等属性 7 *模拟玩家购买游戏道具 8 *1.玩家选择要购买的道具 9 *2.玩家同意交易后扣除相应的游戏币 10 *3.对应商品库存-1 11 *4.玩家背包中增加商品或该商品数量+1 12 */ 13 14 15 //定义结构 16 //1.商品结构-名称,单价,库存量,描述 17 //2.背包结构-玩家编号,名称,商品[10],道具数量,最大道具数 18 //3.玩家结构-编号,名称,密码,金钱,【背包】 19 20 typedef struct _props //商品结构 21 { 22 int id; //道具编号 23 char name[50]; //道具名称 24 double price; //道具单价 25 int stock; //库存量:如果再背包中,表示次道具的叠加数量 26 char desc[200]; //道具的功能描述 27 28 }Props; 29 30 31 32 typedef struct _bag //背包结构 33 { 34 35 int playerId; //所属玩家的编号 36 int count ; //当前背包中,道具的数量 37 int max; //当前背包的插槽总数 38 Props props[8]; //当前背包中的道具数组,可购买 39 40 }Bag; 41 42 typedef struct _players //玩家结构 43 { 44 int id; //玩家编号 45 char name[50]; //用户名/昵称 46 char pass[50]; //密码 47 Bag bag; //玩家的背包 48 double gold; //玩家金币-人性显示:可以将100000铜币转化成银币,金币显示 49 double sysee; //元宝数量 50 51 52 }Players; 53 54 Props *props; 55 Players *players; 56 57 void Init(); //用来初始化游戏数据 58 void ShowProps(); 59 void ShowPlayers(); 60 //交易函数 参数1.参与交易玩家指针-为了方便修改玩家交易后的金币数 61 //参数2.玩家购买商品的ID 62 void Trade(Players *players,int propsId); 63 64 //全局变量 默认值= 0 65 int propsCount = 0; 66 int playersCount = 0; 67 68 69 70 int main() 71 { 72 73 //初始化游戏数据 74 Init(); 75 printf("\n****************交易前******************\n"); 76 77 //打印这些初始化数据 78 ShowProps(); 79 80 ShowPlayers(); 81 82 printf("\n\n****************交易结果******************\n\n"); 83 Trade(&players[0],3); Trade(&players[0],4); 84 Trade(&players[0],3); 85 Trade(&players[0],5); 86 87 88 Trade(&players[2],5); 89 90 91 printf("\n\n****************交易后******************\n"); 92 ShowProps(); 93 94 ShowPlayers(); 95 return 0; 96 } 97 98 //交易函数 参数1.参与交易玩家指针-为了方便修改玩家交易后的金币数 99 //参数2.玩家购买商品的ID 100 void Trade(Players *players,int propsId) 101 { 102 int i ; 103 //需要判断:1.玩家商品的库存,2.玩家的余额3.玩家背包的空间够不够 104 Props *tradeProps = NULL; //需要购买的商品指针 105 for(i = 0; i <propsCount;i++) 106 { 107 if(propsId ==props[i].id) 108 { 109 tradeProps = &props[i]; //tradeProps = props + i; 110 break; 111 } 112 } 113 114 if(tradeProps->stock <= 0 ) //指针访问结构体 -> 115 { 116 printf("商店都被买空了!!\n"); 117 return; 118 119 } 120 if(players->gold <tradeProps ->price) 121 { 122 printf("钱包里的钱是空的呦!!!交易失败!!!\n "); 123 return; 124 } 125 126 if(players->bag.count>= players->bag.max && players->bag.count != 0) 127 { 128 printf("背包已满,交易失败!!!!\n"); 129 return; 130 131 } 132 else 133 { 134 printf("恭喜您,交易成功!!!\n"); 135 } 136 137 //满足交易条件,执行交易的业务操作 138 //.商品库存-1 139 tradeProps->stock--; 140 //玩家金币-商品单价 141 players ->gold -= tradeProps->price; 142 //玩家背包道具增加 143 144 //判断玩家背包中是否已有该商品 145 //如果没有该商品,该商品添加背包中 146 //如果有该商品,背包中的该商品数+1 147 148 for(i = 0;i<players->bag.count;i++) 149 { 150 //如果购买的商品ID 跟 背包中的某个商品ID相同 151 if(propsId == players->bag.props[i].id) 152 { 153 players->bag.props[i].stock++; 154 break; 155 156 } 157 158 } 159 //如果没有该商品,该商品添加背包中 160 161 if(i == players->bag.count ) 162 { 163 //向背包中创建一个商品-复制一份交易的商品信息到背包中 164 int newIndex = players -> bag.count; 165 players -> bag.props[newIndex].id = tradeProps->id; 166 players -> bag.props[newIndex].price = tradeProps->price; 167 players -> bag.props[newIndex].stock = 1; 168 strcpy(players -> bag.props[newIndex].name,tradeProps->name); 169 strcpy(players -> bag.props[newIndex].desc,tradeProps->desc); 170 players->bag.count++; 171 172 173 174 175 } 176 177 178 179 } 180 189 void Init() 190 { 191 //初始化数据 192 static Props propsArray[] = { 193 194 {1,"双倍经验卡",3000,10,"双击666"}, 195 {2,"无尽之刃",5000,8,"双倍暴击"}, 196 {3,"皮甲战衣",8000,10,"穿上很哇塞!"}, 197 {4,"禁锢鲁棒",13000,10,"变幻莫测的大棒子"}, 198 {5,"升级卡",83000,10,"吃了以后还想吃吃吃吃,再吃吃吃吃吃吃。。。"}, 199 200 }; 201 // propsCount = 5; 202 propsCount = sizeof(propsArray) / sizeof(Props); 203 props = propsArray; //设定指针的指向 204 static Players playersArray[] = { 205 {1,"德玛西亚之力","123456",.gold = 50000,.bag.max = 8}, 206 {2,"诺克萨斯之手","123456",.gold = 150000,.bag.max = 8}, 207 {3,"亚索哈塞给","123456",.gold = 500000,.bag.max = 8}, 208 {4,"永烈双子","123456",.gold = 1150000,.bag.max = 8}, 209 // {5,"马可波罗","123456",.gold = 50000,}, 210 211 }; 212 playersCount = 4; 213 players = playersArray; 214 215 } 216 217 218 219 void ShowProps() 220 { 221 int i ; 222 if((props == NULL)) 223 return; 224 printf("%-15s%-20s%-17s%-12s%-12s\n","商品编号","商品名称","商品单价","商品库存","商品介绍"); 225 for(i = 0;i < propsCount;i++) 226 { 227 printf("%-15d%-20s%-17.0lf%-12d%-12s\n",props[i].id,props[i].name,props[i].price,props[i].stock,props[i].desc); 228 229 } 230 231 232 233 } 234 void ShowPlayers() 235 { 236 int i,j ; 237 if(players == NULL) 238 return; 239 printf("%-15s%-20s%-17s\n","玩家编号","玩家名称","玩家金币"); 240 for(i = 0; i < playersCount; i++) 241 { 242 printf("%-15d%-20s%-17.01f\n",players[i].id,players[i].name,players[i].gold); 243 for(j = 0; j <players[i].bag.count;j++) 244 { 245 246 247 printf("\t%s\t%d\t",players[i].bag.props[j].name,players[i].bag.props[j].stock); 248 } 249 printf("\n"); 250 251 } 252 253 254 255 }
本文来自博客园,作者:Bytezero!,转载请注明原文链接:https://www.cnblogs.com/Bytezero/p/15079126.html