📄 tiger.cpp
字号:
/*
12月上机完成小程序:老虎机增强版 (在已有程序上,增加以下功能):
1. 为每位游戏者增加帐号管理功能(参考复旦BBS)
a. 游戏者每次需要登陆帐号和密码才可以玩,第一次游戏者系统应提供帐号注册
b. 游戏者登陆后显示参看玩耍的历史纪录,包括输赢统计和金额,以及剩下金额
c. 设立一个管理帐号,可以为其他帐号充值,并可以查阅其他帐号的输赢情况,充值情况
2. 增加游戏过程信息
a. 输家的钱要累积起来,第一个赢的人将获得前面积累下来的所有钱
b. 每次开始玩,及玩耍过程中,系统要统计并显示积累的钱数,及前面已玩的次数(即最近一次赢后到这次玩耍时共玩了多少次)
3. 每次游玩结束后,应统计并排序显示出赢钱金额前10位的游戏者,以及积累钱数
4. 以上提及信息(游戏者相关信息,输赢信息、统计等)应存放在同目录下的一个文件里,以便程序下次调用。
注意:老虎机图案总共只需要有三幅不同图案即可,不然胜率太低
上交方式:发邮件致助教邮箱,12月27日凌晨0点截止上交
上交方式:C语言源程序,exe, 以及说明文件(如果有的话),在源码或说明文件里列出学号和姓名,方便统分。
建议:不超过2人一组,超出的人分数为0
*/
#include <conio.h>
#include <process.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Player
{
char user[20];
char password[20];
int money,loss;
int win,lost;
int rank;
};
void sort(struct Player *p,int n)
{
int i,j;
for(i=1;i<=n;i++)
p[i-1].rank=1;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(i!=j){ if(p[i-1].money<p[j-1].money) p[i-1].rank++; }
}
void TopTen(struct Player *p,int n)
{
int i,j;
int k;
if(n>=10)
k=10;
else
k=n;
for(i=1;i<=k;i++)
for(j=1;j<=n;j++)
if(i==p[j-1].rank) printf("Player:%10s , Money:%10d, Rank:%10d\n",p[j-1].user,p[j-1].money,p[j-1].rank);
}
int Read(struct Player *p,int *pool_bet,int *time) //read data
{
FILE *fp;
int i;
int n;
if((fp=fopen("data.txt","rt"))==NULL)
{
printf("error on open data file!");
exit(1);
}
printf("Reading data...\n");
fscanf(fp,"%d %d %d",&n,pool_bet,time);
if(n>=50)
{
printf("the number of player is full...");
fclose(fp);
exit(1);
}
for(i=1;i<=n;i++)
fscanf(fp,"%s %s %d %d %d %d %d",p[i-1].user,p[i-1].password,&p[i-1].money,&p[i-1].loss,&p[i-1].win,&p[i-1].lost,&p[i-1].rank);
fclose(fp);
return n;
}
void Write(struct Player *p,int n,int pool_bet,int time)
{
FILE *fp;
int i;
if((fp=fopen("data.txt","wt"))==NULL)
{
printf("error on open data file!");
exit(1);
}
fprintf(fp,"%d %d %d\n",n,pool_bet,time);
for(i=1;i<=n;i++)
fprintf(fp,"%s %s %d %d %d %d %d\n",p[i-1].user,p[i-1].password,p[i-1].money,p[i-1].loss,p[i-1].win,p[i-1].lost,p[i-1].rank);
printf("Writing over..\n");
fclose(fp);
}
int Create(struct Player *p,int n,int pool_bet,int time)
{
char temp_user[20];
char ok='n'; //password
int i;
if(n>50 || n<0)
{
printf("the number of player is full or wrong...\nExit the Game\n");
exit(1);
}
else
{
UserName1:
printf("Please input user name: ");
scanf("%s",temp_user);
for(i=1;i<=n;i++)
if(!strcmp(temp_user,p[i-1].user))
{
printf("sorry the user has existed!\n");
goto UserName1;
}
strcpy(p[n++].user,temp_user);
//while( ok !='y' )
//{
printf("Please input password: ");
scanf("%s",p[n-1].password);
// printf("Are you sure?(y/n) ");
// scanf("%c", &ok);
//}
p[n-1].money=1000;
p[n-1].loss=0;
p[n-1].lost=0;
p[n-1].win=0;
p[n-1].rank=1;
sort(p,n);
Write(p,n,pool_bet,time);
return n;
}
}
void Game(struct Player *p,int n,int m,int *pool_bet,int *time)
{
unsigned int a=3,b=3,c=3;
int i=0;
int bet;
char ans='y';
printf("press any button to play.^_^\n");
getchar();
getchar();
system("cls");
while(ans=='y'||ans=='Y')
{
printf("Welcome %s! :)\nThe money in the pool:%5d Time:%5d\n",p[n].user,*pool_bet,*time);
printf("money: %5d , win:%3d , lose:%3d, the money you win:%5d \n",p[n].money,p[n].win,p[n].lost,p[n].loss);
printf("* %d * %d * %d *\n",a,b,c);
if(p[n].money<=0)
{
printf("Sorry, you are out of money :(\nlost the game!\n");
return;
}
do{
printf("How much do you want to chip in?");
getchar();
scanf("%d",&bet);
if(bet<0 || bet>p[n].money)
printf("ERROR!Please Input Valid Money\n");
}while(bet<0 || bet>p[n].money);
*pool_bet +=bet;
while(++i)
{
a=(a+12132412*i)%3;
b=(b+22334325*i)%3;
c=(c+32355467*i)%3;
printf("\r* %d * %d * %d *",a,b,c);
if(kbhit())
{
getche();
printf("\r* %d * %d * %d *\n",a,b,c);
break;
}
}
if(a==b&&b==c)
{
p[n].money += *pool_bet;
p[n].win++;
*time=0;
p[n].loss+=*pool_bet;
printf("You win all the money in the pool:%d!!!\n",*pool_bet);
printf("Congratulations! @@ money:%d\n",p[n].money);
*pool_bet=0;
Write(p,m,*pool_bet,*time);
}
else
{
p[n].money -= bet;
p[n].lost++;
(*time)++;
p[n].loss-=bet;
printf("Sorry you lose :( money: %d\n",p[n].money);
Write(p,m,*pool_bet,*time);
}
do{
printf("Do you want to end game(y/n)\n");
getchar();
scanf("%c",&ans);
}while(ans!='y'&& ans!='Y'&& ans!='n'&& ans!='N');
system("cls");
}
sort(p,m);
TopTen(p,m);
system("pause");
}
void LogUser(struct Player *p,int n,int *pool_bet,int *time)
{
char temp_user[20],temp_psd[20];
int i;
int cur_n;
int flag=0;
char ans='y';
UserName2:
printf("Please input username: ");
scanf("%s",temp_user);
printf("Please input password: ");
scanf("%s",temp_psd);
for(i=1;i<=n;i++)
if(!strcmp(temp_user,p[i-1].user) && !strcmp(temp_psd,p[i-1].password))
{
flag=1;
cur_n=i-1;
}
if(flag==0)
{
printf("Sorry the user or the password is wrong!\n");
goto UserName2;
}
if(flag==1)
Game(p,cur_n,n,pool_bet,time);
}
void Show(struct Player *p,int n)
{
int i;
for(i=1;i<=n;i++)
printf("player:%10s money: %5d win:%3d lose:%3d the money he/she win:%5d \n",p[i-1].user,p[i-1].money,p[i-1].win,p[i-1].lost,p[i-1].loss);
}
void AddMoney(struct Player *p,int n,int pool_bet,int time)
{
char temp_user[20];
int temp_money=0;
int i;
int flag=0;
int cur_n;
UserName3:
printf("Please input username: ");
scanf("%s",temp_user);
for(i=1;i<=n;i++)
if(!strcmp(temp_user,p[i-1].user))
{
flag=1;
cur_n=i-1;
}
if(flag==0)
{
printf("Sorry the user or the password is wrong!\n");
goto UserName3;
}
if(flag==1)
{
printf("player:%10s money: %5d win:%3d lose:%3d the money he/she win:%5d \n",p[cur_n].user,p[cur_n].money,p[cur_n].win,p[cur_n].lost,p[cur_n].loss);
printf("How much do you want to add?");
scanf("%d",&temp_money);
p[cur_n].money+=temp_money;
if(p[cur_n].money <=0 )
{
printf("Sorry you cannot do this!!\n");
p[cur_n].money-=temp_money;
}
printf("player:%10s money: %5d win:%3d lose:%3d the money he/she win:%5d \n",p[cur_n].user,p[cur_n].money,p[cur_n].win,p[cur_n].lost,p[cur_n].loss);
sort(p,n);
Write(p,n,pool_bet,time);
}
}
void Admin(struct Player *p,int n,int pool_bet,int time)
{
char temp_psd[20];
int choice;
do{
printf("Please input password:");
scanf("%s",temp_psd);
}while(strcmp(temp_psd,"mandy"));
do{
system("cls");
printf(" Menu \n");
printf("1.Show User Data\n");
printf("2.Add Money\n");
printf("3.Rank\n");
printf("4.Exit\n");
scanf("%d",&choice);
if(choice>=1 && choice<=3)
switch(choice)
{
case 1:if(n==0)
printf("There is not any player.\n");
Show(p,n);system("pause");
;break;
case 2:AddMoney(p,n,pool_bet,time);system("pause");break;
case 3:TopTen(p,n);system("pause");break;
case 4:return;
}
system("cls");
}while(choice!=4);
}
main()
{
int n,pool_bet,time;
int choice;
struct Player player[50];
n=Read(player,&pool_bet,&time);
do{
printf("\tMenu \n");
printf("1.Create New User\n");
printf("2.Log on the Game\n");
printf("3.Administrators\n");
printf("4.Exit\n");
scanf("%d",&choice);
if(choice>=1 && choice<=4)
switch(choice)
{
case 1:n=Create(player,n,pool_bet,time);break;
case 2:if(n==0)
{
printf("There is not any player.Please create a new one.\n");
system("pause");
break;
}
LogUser(player,n,&pool_bet,&time);
break;
case 3:Admin(player,n,pool_bet,time);break;
case 4:break;
}
system("cls");
}while(choice!=4);
Write(player,n,pool_bet,time);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -