#pragma once
#include
#include
#include
#include
#include


#define NANE_SIZE 10
#define PASSWD_SIZE 10 			
#define PASSWD    getchar();


typedef struct player
{
	char name[NANE_SIZE];
	char passwd[PASSWD_SIZE];
	int total;
	int victory;
}Player,*Pplayer;

Player *player = NULL;

//创建玩家
Pplayer Creat_Player()
{
	Pplayer player_tmp = (Pplayer)malloc(sizeof(Player));
	if(NULL == player_tmp)
	{
		return NULL;
	}
	strcpy(player_tmp->name ,"zhangsan");
	strcpy(player_tmp->passwd ,"123456");
	player_tmp->total = 0;
	player_tmp->victory = 0;
	
	return player_tmp;
}

//销毁玩家
void Destroy_Player()
{
	if(player != NULL)
	{
		free(player);
		player = NULL;
	}
}

//界面的实现
void Menu()
{
	printf("欢迎来到猜拳游戏\n");
	printf("=============================================\n");
	printf("1.石头 2.剪刀 3.布 0.退出\n");
	printf("玩家请出拳 ");
}

//电脑出拳的实现
void Compter()
{
	int i = 0;
	for(i = 3;i >= 0; --i)
	{
		system("cls");
		printf("电脑正在出拳%d\n",i);
		fflush(stdout);
		Sleep(1000);
	}
	printf("\n===============最终结果================\n\n");
}

//电脑随机值
int My_rand()
{
	system("cls");
	srand((unsigned int)(time(NULL)));
	return rand()%3+1;
}

//排名
void Ranking(char *name,int total,int victory)
{
	double ratio = 0.0;
	if(total!= 0)
	{
		ratio = (victory * 1.0)/total;
	}
	printf("\n\n===================排行榜==================\n\n");
	printf("姓名      总局数     胜利局数        胜利率\n");
	printf("%s %5d%10d%18.2lf\n",name,total,victory,ratio);
}


//结果显示
void Show(int player_choose,int computer_choose)
{
	if(player_choose == 1)
	{
		printf("您出了石头\n");
	}
	else if(player_choose == 2)
	{
		printf("您出了剪刀\n");
	}
	else
	{
		printf("您出了布\n");
	}

	if(computer_choose == 1)
	{
		printf("电脑出了石头\n");
	}
	else if(computer_choose == 1)
	{
		printf("电脑出了剪刀\n");
	}
	else
	{
		printf("电脑出了布\n");
	}
}
//函数主体
void Menu_str()
{
	int player_choose = 0;
	
	int sub = 0;
	
	while(1)
	{
		int computer_choose = My_rand();
		system("cls");
		Menu();
		scanf("%d",&player_choose);
		
		if(0 == player_choose)
		{
			Ranking(player->name,player->total,player->victory);
			return;
		}
		player->total++;
		Compter();
		Show(player_choose,computer_choose);
		sub = player_choose - computer_choose;
		switch(sub)
		{
		case 0:
			{
				char ch;
				printf("平局\n");
				printf("按任意键继续\n");
				do{
				 ch = getchar();
				}while(ch != '\n' && ch != EOF);
                PASSWD;
				break;
			}
		case -1:
		case 2:
			{
				char ch;
				printf("恭喜你,你赢了\n\n");
				printf("按任意键继续\n");
				do{
				 ch = getchar();
				}while(ch != '\n' && ch != EOF);
                PASSWD;
				player->victory++;
				break;
			}
		default:
			{
				char ch;
				printf("很遗憾,你输了\n\n");
				printf("按任意键继续\n");
				do{
				 ch = getchar();
				}while(ch != '\n' && ch != EOF);
                PASSWD;
				break;
			}
		}
	}
}

int main()

{

player = Creat_Player();

Menu_str();

system("pause");

return 0;

}