菜鸟学C++的首作!!!(猜拳游戏)
xniao 2002-03-19 08:46:01 下载地址:http://user1.7host.com/may8866/test.exe
源代码:
#include<iostream>
#include<fstream>
#include<string>
#include<time.h>
#include<cstdlib>
#include<conio.h>
#include<stdlib.h>
using namespace std;
void write_log(int suc_fai)
{
ofstream outfile ("log.txt",ios_base::app);
if (!outfile) ofstream outfile ("log.txt");
if (!outfile) cout <<"\n无法存储挡案记录!!!\n";
if (suc_fai==1)
outfile <<"赢\n";
if (suc_fai==2)
outfile <<"输\n";
}
void doing_game(int hand_number,int guess_number)
{
int random_number;
srand( (unsigned)time( NULL ) );
for (int x=1;x<5;++x)
random_number=int(rand())/10000;
int suc_fai=1;
cout <<"\n我出个"<<random_number<<"只手。"<<"你出了"<<hand_number<<"只手,一共有"<<(random_number+hand_number)*5<<"只手指头,所以......";
if (guess_number==hand_number*5+random_number*5)
{
suc_fai=1;
cout <<"\n恭喜,猜对了!!";
}
else
{
suc_fai=2;
cout <<"\n你猜错了!";
}
write_log(suc_fai);
}
void start_game()
{
int hand_number,guess_number;
cout <<"\n开始啦~~~~~~~~~~!!!";
while(1)
{
cout <<"\n你要伸出几个手?(0-2)";
cin >>hand_number;
if (hand_number>2||hand_number<0)
{
cout<<"faint...指定动作无法完成!\n";
continue;}
cout <<"你猜测的手指头数目(0,5,10,15,20):";
cin >>guess_number;
if (!(guess_number==0)&&!(guess_number==5)&&!(guess_number==15)&&!(guess_number==20)&&!(guess_number==10))
{
cout<<"faint......你白痴啊~~~~~!重来!!!\n";
continue;}
doing_game(hand_number,guess_number);
cout <<"\n继续玩吗?(任意键继续,ESC键退出)";
if (getch()== 27) exit(0);
else continue;
}
}
void read_log()
{
ifstream infile ("log.txt");
if (!infile)
cout <<"未发现历史记录!!!\n";
else
{
cout <<"\n历史记录:\n";
int count=1;
string name;
while (infile >> name)
{
cout <<count<<":"<<name<<" ";
if (count<10) cout<<" ";
if (count%10==0) cout <<"\n";
count++;
}
}
}
int main()
{
int choice;
cout <<"\n###############################################"
<<"\n# #"
<<"\n# 菜鸟猜拳(15/20版) #"
<<"\n# #"
<<"\n###############################################";
while(1)
{
cout <<"\n1:开始游戏 2:退出游戏 3:查看记录 选择:";
cin >>choice;
if (choice<1||choice>3) continue;
if (choice==1) start_game();
if (choice==2) return 0;
if (choice==3) read_log();
}
}
###########################################
哈哈,够幼稚吧~~~~~~~~~~:)