一个简单的猜拳游戏的实现

来源:互联网 发布:动态课件制作软件 编辑:程序博客网 时间:2024/04/29 05:17
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>


#define NAME_SIZE 20
#define PASSWD_SIZE 20
#define TURE 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 *create_player(void)
{
player = (player_t *)malloc(sizeof(player_t));
if(NULL == player)
{
return NULL;
}
memset(player,0,sizeof(player_t));
strcpy(player->name,"zhangsan");
    strcpy(player->passwd,"123456");
player->total = 0;
player->victory = 0;
}


void destroy_player()
{
if(NULL != player)
{
free(player);
player = NULL;    //
}
}


void menu()
{
system("cls");
printf("欢迎来到猜拳游戏\n");
printf("======================\n");
printf("1.石头  2.剪刀  3.布  0.退出\n\n");
printf("请您出拳: ");
}


int myrand()
{
int choose = 0;
srand(time(NULL));
choose = rand() % 3 +1;
return choose;
}


void out_win(int player_choose,int computer_choose)
{
if(1 == player_choose)
{
printf("\n您出了石头!\n");
}
else if(2 == player_choose)
    {
printf("\n您出了剪刀!\n");
}
else if(3 == player_choose)
    {
printf("\n您出了布!\n");
}
if(1 == computer_choose)
{
printf("\n电脑出了石头!\n\n");
}
else if(2 == computer_choose)
    {
printf("\n电脑出了剪刀!\n\n");
}
else if(3 == computer_choose)
    {
printf("\n电脑出了布!\n\n");
}
}
void menu_ctr()
{
int player_choose = 0;
int computer_choose = 0;
int win;
while(TURE)
{
do
{
menu();
scanf("%d",&player_choose);
MFLUSH
}while(player_choose > 3);
if(player_choose == 0)
{
return 0;
}
        computer_choose = myrand();
out_win(player_choose,computer_choose);
win = player_choose - computer_choose;
player->total++;
switch(win)
{
case -1:
case 2:
{
printf("恭喜您,赢了!\n\n");
        player->victory++;
CONTINUE("按回车键继续");
break;
}
case 0:
{
printf("平局!\n\n");
                               CONTINUE("按回车键继续");
break;
}
        default:
{
printf("很遗憾,您输了\n\n");
CONTINUE("按回车键继续");
break;
}


}
}
}
void victory_display()
{
printf("\n\n\t\t  排行榜\n");
printf("\n\n");
printf("========================================\n");
printf("%10s %10s %10s %10s\n","姓名","总局数","胜利数","胜率");
if(0 != player->total)
{
printf("%10s %10d %10d %10lf\n",player->name,player->total,player->victory,(double)(player->victory)/(player->total)*100);
}
}


int main()
{
player = create_player();
if(NULL == player)
{
return 1;
}
menu_ctr();
        victory_display();
system("pause");
return 0;
}
0 0
原创粉丝点击