#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
//宏定义
#define NAME_SIZE 20
#define PASSWD_SIZE 20
#define NAME "zhangsan"
#define PASSWD "123456"
#define TRUE 1
/*行缓冲*/
#define MFLUSH {int ch = 0; while((ch = getchar()) != '\n' && ch != EOF) ;}
#define CONTINUE(X) {printf("%s",X); getchar();}
//玩家结构体
typedef struct player
{
char name[NAME_SIZE];
char passwd[PASSWD_SIZE];
int total;
int victory;
} player_t;
player_t *player;
/*********/
/* */
/*********/
player_t *creat_player(void)
{
player = (player_t *)malloc(sizeof (player_t)*1);
if (NULL == player)
{
return NULL;
}
memset(player,0,sizeof (player_t));
strcpy(player->name,NAME );
strcpy(player->passwd,PASSWD);
player->total = 0;
player->victory = 0;
}
//销毁玩家
void destory_player()
{
if (NULL != player)
{
free(player);
player = NULL;//如果没置空,别人直接用就崩溃
}
}
/*
创建菜单
*/
void meue()
{
system("cls");//windows
printf("welcome \n");
printf("**********\n");
printf("1. 石头 2. 剪刀 3. 布 0.退出\n");
printf("请出拳: ");
}
int myrand()
{
int chose = 0;
srand(time(NULL));
chose = rand()%3 + 1;
return chose;
}
/*打印出拳详情*/
void out_win(int playerChose,int computerChose)
{
if (1 == playerChose)
{
printf("你出了石头!\n");
}
else if (2 == playerChose)
{
printf("你出了剪刀!\n");
}
else if (3 == playerChose)
{
printf("你出了布!\n");
}
if (1 == computerChose)
{
printf("电脑出了石头!\n");
}
else if (2 == computerChose)
{
printf("电脑出了剪刀!\n");
}
else if (3 == computerChose)
{
printf("电脑出了布!\n");
}
}
void load(void)
{
int i = 0;
for ( i = 2; i >=0 ;i--)
{
system("cls");
printf("电脑出拳中:%d",i);
fflush(stdout);
Sleep(1000);
}
printf("\n");
}
/*
控制菜单
*/
void meue_ctr()
{
int player_chose = 0;
int computer_chose = 0;
int win = 0;
while (TRUE)
{
do
{
meue();
scanf("%d", &player_chose);
MFLUSH;
} while (player_chose > 3 || player_chose < 0);
if (0 == player_chose)
{
return;
}
computer_chose = myrand();
printf("******出拳详情******\n");
out_win(player_chose,computer_chose);
win = player_chose - computer_chose;
player->total++;
switch (win)
{
case -1:
case 2 :
printf("恭喜!\n");
player->victory++;
CONTINUE("按回车继续…");
break;
case 0:
printf("平局!\n");
CONTINUE("按回车继续…");
break;
default :
printf("很遗憾!\n");
CONTINUE("按回车继续…");
break;
}
}
}
void victory_display()
{
double win = 0.0;
printf("\n\n\t\t 排行榜\n");
printf("\t\t*********\n");
printf("%10s %10s %10s %10s\n","姓名","总局数","胜利数","胜率\n");
if (0 != player->total)
{
win = (double)player->victory/player->total * 100;
}
printf("%10s %d %10d %5.12f\n ",player->name,player->total,player->victory,win);
}
int main(void)
{
player = creat_player();
if (NULL == player)
{
return 1;
}
meue_ctr();
victory_display();
destory_player();
return 0;
}
猜拳游戏
最新推荐文章于 2024-08-05 11:19:45 发布