养猪游戏C++

#include<iostream>
#include<fstream>
#include<string>
#include<time.h>
#include<stdlib.h>
using namespace std;

double money;
int month;
int day;
int sellcount;
int allpig;

struct pig{
	int species;
	int growday;
	int growmonth;
	double weight;
	int number;
	pig*next;
};

class pigjuan{
	private:
		int cntnum;
		pig*head;
		int isblackspecies;
	public:
		pigjuan();
		void print();
		void addpig(pig*p);
		void searchpig(int number);
		void sellpig();
		void insert(int species);
		double getprice();
		int getspecies(int i);
		int getgrowmonth(int i);
		int getgrowday(int i);
		double getweight(int i);
		void next(int nexttime);
	    bool search(int number);
	    void clearpigjuan();
	    int getallpig(pigjuan pjuan[]);
	    void setisBlackPig(int i){
		isblackspecies=i;
	}
	    int getspecies1(){
	    	int count=0;
	    	for(int i=0;i<cntnum;i++){
	    		if (getspecies(i)==1)
	    		count++;
			}
			return count;
		}
		int getspecies2(){
	    	int count=0;
	    	for(int i=0;i<cntnum;i++){
	    		if (getspecies(i)==2)
	    		count++;
			}
			return count;
		}
		int getspecies3(){
	    	int count=0;
	    	for(int i=0;i<cntnum;i++){
	    		if (getspecies(i)==3)
	    		count++;
			}
			return count;
		}
	int getcount(){
		return cntnum;
	}
	int getisBlackPig(){
		return isblackspecies;
	}
	void save(ofstream&savefile);                
	pig*gethead(){
		return head;
	}
};
pigjuan::pigjuan(){
	int cntnum=0;
	int isblackspecies=0;
	head=NULL;
} 
void pigjuan::addpig(pig*p){
	cntnum++;
	p->next=NULL;
	if(head==NULL){
		head=p;
	}
	else {
	pig*p1=head;
	while(p1->next){
		p1=p1->next;
	}
	p1->next=p;
}
}
int pigjuan::getspecies(int i){
	pig*p=head;
	for(int j=0;j<i&&p;j++){
		p=p->next;
	}
	return p->species;
}
double pigjuan::getweight(int i){
	pig*p=head;
	for(int j=0;j<i&&p;j++){
		p=p->next;
	}
	return p->weight;
}
int pigjuan::getgrowmonth(int i){
	pig*p=head;
	for(int j=0;j<i&&p;j++){
		p=p->next;
	}
	return p->growmonth;
}
int pigjuan::getgrowday(int i){
	pig*p=head;
	for(int j=0;j<i&&p;j++){
		p=p->next;
	}
	return p->growday;
}
double pigjuan::getprice(){
	double sellprice=0;
	if(head==0)
	return 0;
	int month,day,flag=0;
	pig*p=head,*p1=p;
	while(p){
		month=p->growmonth;
		day=p->growday;
		while(day>=30){
			day-=30;
			month++;
		}
	
	if(month>=12||p->weight>75){
		if(cntnum==1){
		setisBlackPig(0);
		head=NULL;
		delete p;
		cntnum--;
		break;
	
	}
	if(p->species==1){
	sellprice+=30*p->weight;
	}
	if(p->species==2){
	sellprice+=14*p->weight;
	}
	if(p->species==3){
	sellprice+=12*p->weight;
	}
	p1->next=p->next;
	delete p;
	p=p1->next;
	cntnum--;
	continue;
}
	else if(flag==0){
		flag=1;
		head=p;
	}
	p1=p;
	p=p1->next;
}
return sellprice;
}
void pigjuan::save(ofstream&savefile){

	if(head==NULL){
		savefile<<'$'<<endl;
		return;
	}
	else {
	pig*p=head;
	int i=0;
	savefile<<cntnum<<endl;
	while(p){
		savefile<<getspecies(i)<<"   "<<getweight(i)<<"   "<<getgrowmonth(i)<<"   "<<getgrowday(i)<<endl;
		i++;
		p=p->next;
	}
}
}
void pigjuan::print(){
	if(cntnum==0){
		cout<<"空猪圈"<<endl;
		return ;
	}
	if(isblackspecies==1){
		cout<<"是黑猪圈,"<<endl;
	}
	else
	cout<<"不是黑猪圈,"<<endl;
	pig*p=head;
		while(p){
		int month=p->growmonth;
		int day=p->growday;
		while(day>=30){
			day-=30;
			month++;
		}
	cout<<"共有"<<cntnum<<"头猪"<<endl;
	int i=p->number;
	cout<<i<<"号猪养了"<<month<<"个月"<<day<<"天"<<p->weight<<"Kg,";
	if(p->species==1)cout<<"种类为黑猪"<<endl;
	if(p->species==2)cout<<"种类为小花猪"<<endl; 
	if(p->species==3)cout<<"种类为大白花猪"<<endl;
	p=p->next; 
}
}

void pigjuan::next(int nexttime){
	srand((unsigned)time(NULL));
	pig*p=head;
	while(p){
		p->weight+=(double)(rand()%12)*nexttime/10;//(直接取余1.2不行啊 
		if(nexttime==1)p->growday++;
		else p->growmonth++;
		p=p->next;
	}
}
bool pigjuan::search(int number){
	pig*p=head;
	while(p){
		if(p->number==number)
		return true;
		else
		p=p->next;
	}
	return false;
}
void pigjuan::clearpigjuan(){
	if(head==NULL){
	return;
}
else {
	pig*p=head;
	pig*p1=p;
	cntnum=0;
	setisBlackPig(0);
	head=NULL;
	while(p){
		p1=p;
	p=p->next;
	delete p1;
}
delete p;
}
}
int getallpig(pigjuan pjuan[]){
	int allcount=0;
	for(int i=0;i<100;i++){
		allcount+=pjuan[i].getcount();
	}
	return allcount;
}
void Menu(){
	cout<<"\n\n\n\n\n\n\n";
	cout<<"           ======================================================\n";
	cout<<'\n';
	cout<<"                                   养猪游戏                        \n";
	cout<<'\n';
	cout<<"                       请选择游戏操作:\n";
	cout<<'\n';
	cout<<"                                1.开始游戏\n";
	cout<<'\n';
	cout<<"                                2.帮助\n";
	cout<<'\n';
	cout<<"                                3.退出\n";
	cout<<'\n';
	cout<<"           ======================================================\n";
}
void Menu_initial(){
	system("cls");
	cout<<"\n\n\n\n\n\n\n";
	cout<<"            ======================================================\n";
	cout<<"                                          我的猪场                        \n";
	cout<<'\n';
	cout<<"                       请选择操作:\n";
	cout<<'\n';
	cout<<"                                 1.查询\n";
	cout<<"                                 2.出圈并购猪\n";
	cout<<"                                 3.重新游戏\n";
	cout<<"                                 4.保存游戏\n";
	cout<<"                    5.下一天                      6.下个月        \n";
	cout<<'\n';
	cout<<"               当前金币数:"<<money<<"    总猪数:"<<allpig<<"   饲养时间:";if(month>0)cout<<month<<"个月";if(day>=0)cout<<day<<"天";
	cout<<'\n';
	cout<<"           ======================================================\n";
}
void check(){
	system("cls");
	cout<<"\n\n\n\n\n\n\n";
	cout<<"            ======================================================\n";
	cout<<"                                         查询                          \n";
	cout<<'\n';
	cout<<"                       请选择操作:\n";
	cout<<'\n';
	cout<<"                            1.查询某猪圈猪的数量和种类\n";
	cout<<"                            2.查询某猪圈某头猪的状态信息\n";
	cout<<"                            3.统计每种猪的数量、体重、饲养时间分布\n";
	cout<<"                            4.查询近5年猪的销售和购入记录\n";
	cout<<"                            5.打印当前猪场猪的所有信息\n";
	cout<<"                            6.返回\n";
	cout<<'\n';
	cout<<"            ======================================================\n";
}
void savefile(pigjuan pjuan[]){
	ofstream savefile;       
	savefile.open("123.txt");
	if(!savefile){           
		cout<<"打开保存文件失败!";
		exit(0);
	} 
	savefile<<1<<endl;
	for(int i=0;i<100;i++){
		pjuan[i].save(savefile);
	}
	savefile.close();
}
void Menu_pigjuan(pigjuan pjuan[]){
	allpig=500;
	money=20000;
	month=0;
	day=0;
	sellcount=0; 				
	ofstream f;
	f.open("123.txt");
	f<<0;
	f.close(); 
	f.open("456.txt");
	f<<'#';
	f.close(); 
	pig*p;
	srand((unsigned)time(NULL));
	int count=500;
	while(count--){
		p=new pig;
		p->species=rand()%3+1;
		p->weight=rand()%30+20;
		p->growmonth=0;
		p->growday=0;
		for(int i=0;i<100;i++){
			if(p->species==1&&(pjuan[i].getisBlackPig()==1||pjuan[i].gethead()==NULL)&&pjuan[i].getcount()<10){
				p->number=pjuan[i].getcount();
				pjuan[i].addpig(p);
				pjuan[i].setisBlackPig(1);
				break;
			}
			if(p->species!=1&&pjuan[i].getisBlackPig()==0&&pjuan[i].getcount()<5){//<10???
				p->number=pjuan[i].getcount();
				pjuan[i].addpig(p);
				break;
			}
		}
		
	}
}
void Menu_pigjuan_byfile(pigjuan pjuan[]){
	ifstream getinfo;
	getinfo.open("789.txt");
	getinfo>>allpig>>money>>month>>day>>sellcount; 
	getinfo.close();
	ifstream read;
	read.open("123.txt");
	if(!read){
		cout<<"读取保存的数据失败!";
		exit(0);
	} 
	int t,cntnum;
	pig*p;
	read>>t;
	for(int i=0;i<100;i++){
		read>>cntnum;
		for(int j=0;j<cntnum;j++){
			p=new pig;
			p->number=j;
			read>>p->species;
			if(p->species=='$'){
				delete p;
				break;
			}
			else read>>p->weight>>p->growmonth>>p->growday;
			pjuan[i].addpig(p);
		}	
	}
	read.close();
}
void pigjuan::insert(int species){
	pig*p=new pig;
	srand((unsigned)time(NULL));
	p->species=species;		
	p->weight=rand()%30+20;
	p->growmonth=0;	
	p->growday=0;
	if(head==NULL){
		p->number=0;
		p->next=NULL;
		head=p;
		cntnum++;
		return;
	}
	if(head->number!=0){
		p->number=0;
		p->next=head;
		head=p;
		cntnum++;
		return;
	}
	else {
	pig*p1=head,*p2=p1;
	int k=0;
	while(p1->number==k&&p1->next){
		k++;
		p2=p1;
		p1=p1->next;
	}
	if(p1->next==NULL){
		p->number=cntnum;
		p->next=NULL;
		p1->next=p;
	}
	else{
		p->number=k;
		p2->next=p;
		p->next=p1;
	}
	cntnum++;
} 
}
void buypig(int bpig,int lpig,int wpig,pigjuan pjuan[]){
	while(bpig--){
		for(int i=0;i<100;i++){
			if(pjuan[i].getisBlackPig()==1&&pjuan[i].getcount()<10){
				pjuan[i].insert(1);
				break; 
			}
			if(pjuan[i].gethead()==NULL){
				pjuan[i].insert(1);
				pjuan[i].setisBlackPig(1);
				break;
			}
		}		
	}	
	int average=allpig/100+1;
	while(1){
		for(int i=0;i<100;i++){
			if(pjuan[i].getisBlackPig()==0){
				if(lpig>0&&pjuan[i].getcount()<=average){
					pjuan[i].insert(2);
					lpig--;
				}
				if(wpig>0&&pjuan[i].getcount()<=average){
					pjuan[i].insert(3);
					wpig--;
				}
			}
		}
		if(lpig==0&&wpig==0)break;
	}
}
void save_456(int outpignum,double sellprice,int bpig,int lpig,int wpig){
	ifstream testin;
	testin.open("456.txt");
	char flag;
	testin>>flag;
	if(flag=='#'){
		testin.close();
		ofstream testout;
		testout.open("456.txt");
		testout<<'$';
		testout.close();
	} 
	else testin.close();
	ofstream save("456.txt",ios::app);
	save<<sellcount<<endl;                																		
	save<<outpignum<<"   "<<sellprice<<"    "<<bpig<<"   "<<lpig<<"   "<<wpig<<"   "<<2000*bpig+1500*lpig+300*wpig<<endl;
	save.close(); 
}

void save_789(){
	ofstream save;
	save.open("789.txt");
	if(!save){
		cout<<"无法打开文件,保存全局变量失败!";exit(0);
	}
	else {
	save<<allpig<<"   "<<money<<"   "<<month<<"   "<<day<<"   "<<sellcount<<endl;
	cout<<"保存成功!"<<endl;
	save.close();
}
}

void read456(){
	ifstream read;
	read.open("456.txt");
	if(!read){
		cout<<"打开出圈信息文件失败";
		exit(0);
	} 
	char flag;
	read>>flag;
	if(flag=='#'){
		cout<<"无记录"<<endl;
		return;
	}
	int times,outpigs,sellprice,bpig,lpig,wpig,cost,memlast;
	while(!read.eof()){
		read>>times;
		if(memlast==times){
			flag=1;
			continue;
		}
		memlast=times;
		read>>outpigs>>sellprice>>bpig>>lpig>>wpig>>cost;
		cout<<"第"<<times/4+1<<"年";
		cout<<"第"<<(times%4+1)<<"个3月:\n";
		cout<<"出圈猪数:"<<outpigs<<"     获得收益:"<<sellprice<<"   购进黑猪数:"<<bpig<<"   小花猪数:"<<lpig<<"   大白花猪数:"<<wpig<<"   花费:"<<cost<<endl;
	}
	read.close();
}
int main(){
	pigjuan pjuan[100];
	Menu();
	int flag=98;
	ifstream infile;
	infile.open("123.txt");
    infile>>flag; 
	if(flag==0){
	Menu_pigjuan(pjuan);
	}
else {
Menu_pigjuan_byfile(pjuan);
}
    infile.close();
	allpig=getallpig(pjuan); 
	int game;
    cout<<"请输入你要的操作:"<<endl; 
	cin>>game;
	do{
	switch(game){
		case 1:{
			Menu_initial();
			int option;
			cin>>option;
			do{
			switch(option){
				case 1:{
						check();
				       int choice;
				       cin>>choice;
				       do{
				       switch(choice){
				       		case 1:{
							   int num;
							      cout<<"请输入猪圈编号:";
				       		      cin>>num;
				       		      cout<<"此猪圈有"<<pjuan[num].getcount()<<"头猪,其中有黑猪"<<pjuan[num].getspecies1()<<"头,小花猪"<<pjuan[num].getspecies2()<<"头,大白花猪"<<pjuan[num].getspecies3()<<"头\n\n";
						    	  break;
								  }
							case 2:{
									int num1,num2;
									cout<<"请输入猪圈号:";
									cin>>num1;
									while(1){
										if(num1>=0&&num1<100&&pjuan[num1].getcount()!=0)break;
										else cout<<"此猪圈为空,请重新输入";cin>>num1;
									} 
									cout<<"此猪圈共有"<<pjuan[num1].getcount()<<"头猪,请输入猪的编号:";
									cin>>num2;
									while(1){
										if(!pjuan[num1].search(num2)){
											cout<<"此猪不存在,可能已出圈,可能编号过大,请重新输入:";
											cin>>num2;
										}
										else break;
									}
									cout<<"此猪为";
									if(pjuan[num1].getspecies(num2)==1)cout<<"黑猪,体重为";	
									if(pjuan[num1].getspecies(num2)==2)cout<<"小花猪,体重为";
									if(pjuan[num1].getspecies(num2)==3)cout<<"大白花猪,体重为";
									cout<<pjuan[num1].getweight(num2)<<"kg,饲养时间为";
									int month=pjuan[num1].getgrowmonth(num2),day=pjuan[num1].getgrowday(num2);
									while(day>=30){
										day-=30;
										month++;
									}
									if(month>0)cout<<month<<"个月";cout<<day<<"天\n\n";  
									break;
									} 
							case 3:{
								
								break;
							}
							case 4:{
								read456();
								break;
							}
							case 5:{
								for(int i=0;i<100;i++){
									cout<<i<<"号圈,";
									pjuan[i].print();
								}
								break;
							}
							default:break;
					   }
					   if(choice==6)break;
					   cin>>choice;
					   }while(1);
					   Menu_initial();
				break;
				}
				case 2:{
						if(month+day/30<3){
							cout<<"距离上次出圈后的饲养时间不足3月,请再饲养一段时间!"<<endl;
							break;
						}
						double sellprice=0;
						for(int i=0;i<100;i++){
							sellprice+=pjuan[i].getprice();
						} 
						int temp=getallpig(pjuan),outpignum=allpig-temp;
						allpig=temp;
						cout<<"此次圈出猪"<<outpignum<<"头,总体售价为:"<<sellprice<<"元\n";
						money+=sellprice;
						int blackpig=0,blankpigjuan=0,count=0;
						for(i=0;i<100;i++){
							if(pjuan[i].getcount()==0){blankpigjuan++;continue;}
							if(pjuan[i].getspecies1())blackpig+=10-pjuan[i].getspecies1();
							else count+=10-pjuan[i].getcount();
						}
						cout<<"未满的黑猪圈可购入"<<blackpig<<"头黑猪,剩余空圈数:"<<blankpigjuan<<endl;
						int bpig,lpig,wpig;
						cout<<"提示:每头黑猪2000元,每头小花猪1500元,每头大白花猪3000元,当前余额:"<<money<<endl;
						cout<<"如果您有足够多的金币,建议购买"<<blackpig<<"头黑猪,"<<blankpigjuan*10+count<<"头大白花猪\n"; 
						cout<<"请依次输入本次购入的黑猪,小花猪,大花白猪数:"; 
						cin>>bpig>>lpig>>wpig;
						while(1){
							if(bpig>blackpig+blankpigjuan*10||lpig>blankpigjuan*10+count||wpig>blankpigjuan*10+count||lpig+wpig>blankpigjuan*10+count)cout<<"猪数超过猪场容纳量,请重新输入:\n";
							else if(money<2000*bpig+1500*lpig+3000*wpig)cout<<"余额不足,请重新输入\n";
								else if(((blankpigjuan-(bpig-blackpig)/10)*10<=lpig+wpig)&&bpig>blackpig)cout<<"购猪组合不合理,黑猪与其他猪不能混养,请重新输入:\n";
									else if(allpig+bpig+lpig+wpig>=1000){
											cout<<"养满猪场,win"<<endl;
											exit(0);
									}else break; 
							cin>>bpig>>lpig>>wpig;
						}
						money-=2000*bpig+1500*lpig+3000*wpig;
						allpig+=bpig+lpig+wpig;
						cout<<allpig<<endl;	
						buypig(bpig,lpig,wpig,pjuan);
						save_456(outpignum,sellprice,bpig,lpig,wpig);
						sellcount++;
						while(day>=30){
							day-=30;
							month++;
						}
						month-=3;
						cin.get(); 
						cin.get(); 
						Menu_initial();
						break;
						}
				case 3: {
						for(int i=0;i<100;i++)pjuan[i].clearpigjuan(); 
						Menu_pigjuan(pjuan);
						Menu_initial();
						break;
						}
				case 4:{ savefile(pjuan);
						save_789(); 
					break;
					}
				case 5:{
						for(int i=0;i<100;i++){
							pjuan[i].next(1);
						}
						day++;
						Menu_initial();
					break;
				}
				case 6:{
						for(int i=0;i<100;i++){
							pjuan[i].next(30);
						}
						month++;
						Menu_initial();
					break;
				}
				default:break;
			}
			cin>>option;
			}while(1);
		break;	
		}
		case 2:{
			cout<<"输赢条件:\n";
			cout<<"养满猪场胜利\n";
			break;
		}
	}
	if(game==3)exit(0);
	else cin>>game;
	}while(1);
} 



  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值