C语言游戏大富豪(30天赚钱)
大富豪
根据每天的商品价格选择卖出买入!
给你1万元,30天后你会成为最富有的人吗?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#include <Windows.h>
#include <time.h>
typedef struct {char name[20];//名称int count;//数量float price;//单价
}Goods;//商品typedef struct {Goods g[10];int gnum;//商品数量char name[20];//玩家姓名 float money;//余额
}Player;void initAll(Player* player);
int menu();
void help();
void saveRecord(Player* player);
void printRecord();
void sortRecord(Player p[],int n);
void swap(Player p[],int i,int j);
void outputRecord(Player p[],int n);
void startGame(Player* player,Goods goods[],int n);
void PrintNowInfo(Player* player,Goods goods[],int n,int day);//输出当天玩家及商品信息
void BuyGoods(Player* player,Goods goods[],int n);
void SaleGoods(Player* player,Goods goods[],int n);
void setGoods(Goods goods[],int n);
void totalMoney(Player* player,Goods goods[],int n); void totalMoney(Player* player,Goods goods[],int n)
{int no,count,i;while(player->gnum!=0){for(i=0;i<n;i++){if(!strcmp(player->g[player->gnum-1].name,goods[i].name)){break;} } player->money+=player->g[player->gnum-1].count*goods[i].price;player->gnum--;}printf("您的总资产为:%.2f\n",player->money);system("pause");
}void setGoods(Goods goods[],int n)
{int i,j,rate;for(i=0;i<n;i++){j=rand()%3;//0-1涨价 2降价if(j==2){rate = rand()%10+1;goods[i].price*=(1-rate/100.0);//减少在1/10 }else{rate = rand()%20;goods[i].price*=(1+rate/100.0);//涨幅在1/5 } }
}void SaleGoods(Player* player,Goods goods[],int n)
{int no,count,i;printf("请选择要售卖的商品编号:");scanf("%d",&no);if(no<1||no>player->gnum){printf("无该编号!\n");system("pause");return;}printf("请输入要售卖的商品数量:");scanf("%d",&count);if(player->g[no-1].count<count){printf("商品余量不足!\n");system("pause");return;}for(i=0;i<n;i++){if(!strcmp(player->g[no-1].name,goods[i].name)){break;} } player->money+=count*goods[i].price;player->g[no-1].count-=count;goods[i].count+=count;if(player->g[no-1].count==0)//库存为0 需要删除 {for(i=no-1;i<player->gnum;i++){player->g[i]=player->g[i+1];}player->gnum--;}printf("售卖成功!\n");system("pause");
}void BuyGoods(Player* player,Goods goods[],int n)
{int no,count,i,flag;printf("请选择要购买的商品编号:");scanf("%d",&no);if(no<1||no>n){printf("无该编号!\n");system("pause");return;}printf("请输入要购买的商品数量:");scanf("%d",&count);if(goods[no-1].count<count){printf("商品余量不足!\n");system("pause");return;}if(goods[no-1].price*count>player->money){printf("余额不足!\n");system("pause");return;}player->money-=goods[no-1].price*count;flag=0;for(i=0;i<player->gnum;i++){if(!strcmp(player->g[i].name,goods[no-1].name)){player->g[i].count+=count;flag=1;break;}}if(flag==0){i=player->gnum;player->g[i].count=count;goods[no-1].count-=count;strcpy(player->g[i].name,goods[no-1].name);player->gnum++;}printf("购买成功!\n");system("pause");
}void startGame(Player* player,Goods goods[],int n)
{int day=1;int choice;do{PrintNowInfo(player,goods,n,day);printf("1:购买商品\t2:售卖商品\t3:下一天\n");printf("请选择:");scanf("%d",&choice);if(choice==3){day++;if(day<30)setGoods(goods,n);//更新商品信息 }else if(choice==1){BuyGoods(player,goods,n);}else if(choice==2){SaleGoods(player,goods,n);}}while(day<=30);totalMoney(player,goods,n);
}void PrintNowInfo(Player* player,Goods goods[],int n,int day)
{int i,j;system("cls");printf("今天是第%d天\n",day);printf("%-10s%-10s%-10s%-10s\n","编号","商品","数量","单价");for(i=0;i<n;i++){printf("%-10d%-10s%-10d%-10.2f\n",i+1,goods[i].name,goods[i].count,goods[i].price);}printf("玩家剩余金钱:%.2f\n",player->money);printf("玩家拥有货物:\n"); printf("%-10s%-10s%-10s\n","编号","商品","数量");for(i=0;i<player->gnum;i++){printf("%-10d%-10s%-10d\n",i+1,player->g[i].name,player->g[i].count);}
}int menu()
{int choice;system("cls");printf("1:开始游戏\n");printf("2:游戏说明\n");printf("3:富豪排行榜\n");printf("4:退出游戏\n");printf("请输入选择:");scanf("%d",&choice);while(choice<1||choice>4){printf("请重新选择:");scanf("%d",&choice);}return choice;
}int main()
{int choice;Player player;Goods goods[10]={{"土豆",1000,5},{"地瓜",800,8},{"龙虾",100,12},{"鱼",80,15},{"大米",1000,3},{"西瓜",600,2},{"猪肉",200,15},{"电脑",10,8000},{"洗衣机",20,2000}};int goodsnum=9;do{choice=menu();switch(choice){case 1:srand(time(NULL)); initAll(&player);startGame(&player,goods,goodsnum);saveRecord(&player);break;case 2:help();break;case 3:printRecord();break; }}while(choice!=4); return 0;
}void outputRecord(Player p[],int n)
{int i;system("cls");printf("%-6s%-10s%-10s\n","名次","姓名","财富");for(i=0;i<n;i++){printf("%-6d%-10s%-10.2f\n",i+1,p[i].name,p[i].money);}system("pause");
}void swap(Player p[],int i,int j)
{Player t;t=p[i];p[i]=p[j];p[j]=t;
}void sortRecord(Player p[],int n)
{int i,j;for(i=0;i<n;i++){for(j=i+1;j<n;j++){if(p[i].money<p[j].money){swap(p,i,j);}}}
}void printRecord()
{Player p[100];int n=0;FILE* fp=fopen("record.txt","r+");if(fp==NULL){printf("无历史记录!\n");system("pause");return;}fscanf(fp,"%s%f",p[n].name,&p[n].money);while(!feof(fp)){n++;fscanf(fp,"%s%f",p[n].name,&p[n].money);}sortRecord(p,n);outputRecord(p,n);fclose(fp);
}void saveRecord(Player* player)
{FILE* fp=fopen("record.txt","a+");if(fp==NULL)return;fprintf(fp,"%s %f\n",player->name,player->money);fclose(fp);
}void help()
{system("cls");printf("大富豪:\n");printf("根据每天的商品价格选择卖出买入!\n");printf("给你1万元,30天后你会成为最富有的人吗?\n");system("pause");
}void initAll(Player* player)
{system("cls");printf("请输入玩家姓名:");scanf("%s",player->name);printf("游戏即将开始!\n");player->gnum=0;player->money=10000;system("pause");
}
C语言游戏大富豪(30天赚钱)相关推荐
- 设计解谜游戏的30堂课
设计解谜游戏的30堂课 文章目录 1.什么是Eureka Moment? 2.谜题与幽默是同构的 3.最大限度提高Sparkle 4.避免无意义的谜题 5.惊喜是Sparkle的重要源泉 6.有趣的事 ...
- c语言双缓冲怎么用,C语言游戏编程:GDI怎么实现双缓冲绘图去掉闪烁
在上篇文章中将我要用 C语言重新写一个俄罗斯方块 ,使用的是GDI的绘图模式(目前正在移植到DX上去,想添加一些更好友好的动画).数据与动画分离,动画的帧率保持在30左右.但是绘图的时候画面出现了强烈 ...
- c语言换装游戏源代码,C语言游戏源代码
C语言游戏源代码 C语言游戏源代码 1. 简单的开机密码程序 #include "conio.h" #include "string.h" #include & ...
- 【C语言游戏】微信飞机大战 | PlaneFight(EasyX,drawAlpha绘制透明贴图,计时器,计帧器,游戏难度自动调整,接受鼠标消息,源码素材免费分享)
一.数据结构介绍 struct aircraft //所有飞机的结构体 typedef struct aircraft{ int type;//飞机类型 int HP;//剩余血量 int bomb_ ...
- C语言游戏开发——打飞机游戏2.0
C语言游戏开发--打飞机游戏2.0 本次打飞机游戏对上次的打飞机游戏2.0做了代码重构和升级 通过定义函数来实现多个功能 以下为代码主体 通过w a s d来控制飞机的移动 通过空格来发射子弹 #in ...
- 直播 | 清华大学博士生姚远:对抗语言游戏
「AI Drive」是由 PaperWeekly 和 biendata 共同发起的学术直播间,旨在帮助更多的青年学者宣传其最新科研成果.我们一直认为,单向地输出知识并不是一个最好的方式,而有效地反馈和 ...
- c语言游戏编程网盘下载,C语言游戏编程 计算器(5分下载)
C语言游戏编程 #include /*DOS接口函数*/ #include /*数学函数的定义*/ #include /*屏幕操作函数*/ #include /*I/O函数*/ #include /* ...
- Go语言游戏服务器思维导图
大家好,今天整理下了游戏服务器开发的知识,不一定限定Go语言:由于本身本人在从事Go语言游戏服务器开发,所以可以认为是以Go语言为例来分析的, 高清图片在: https://github.com/Go ...
- 后期维特根斯坦的语境观“:语言游戏”与“生活形式”
[作者单位:福建师范大学] 摘 要:后期维特根斯坦批判了自己前期的意义图象论,提出了语言的意义在于对其的使用这一观点.将意义与语言使用联系在一起必然牵扯到语境这一要素,而维氏将语言游戏看作是语言与活动 ...
最新文章
- 对数函数定义域和值域_呆哥数学每日一题 —— 复合函数值域
- Linux系统支持的目录文件有,【技术支持】linux操作系统有哪些文件和目录操作相关命令?...
- 程序员应知——团队精神(转)
- 【VBA研究】查找目录以下全部文件的名称
- (读) 周鸿祎重新思考360(有感)
- Java学习小程序(4)数列求和
- 仪征市第二中学计算机老师,静心倾听花自开 ——仪征市第二中学徐丞老师
- GoLand中的指针操作 * 和
- 惹毛了老婆后,老王居然本能地想按Ctrl+Z...
- linux 内存使用很大,在32位和64位Linux上,为什么同一进程的pmap的内存使用量会有很大差异?...
- Csico CCNA学习笔记1_cdp telnet
- 最短路POJ 1062 	昂贵的聘礼
- python之路 jQuery学习笔记
- 百度谷歌雅虎搜狗提交链接入口
- svn update中断,报cleanup错误
- Windows 序列号查看
- java base64 包_Java实现BASE64编解码
- 把数字翻译成英文声明.C语言,如何把数字翻译成英文
- codeforces-379C. New Year Ratings Change
- MUSCI算法估计空间方位角