版权声明:本文为博主原创文章,未经许可,不得转载! https://blog.csdn.net/bin_ge_love/article/details/51626398
很久以前写的一个小项目,有兴趣的可以进来看看。
这个小项目主要包含以下信息和功能:
1、玩家信息--->(结构体)包含名字,密码,赢的场数, 总的场数,胜利比率
2、创建玩家
3、销毁玩家
4、创建游戏菜单
5、电脑载入(出拳时动态显示)
6、显示出拳的信息
7、电脑出拳的核心函数(随机函数)
8、退出时显示排行榜
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
//宏定义
//#define 宏名 宏定义;
#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); }
//{
// char ch = 0;
// ch = getchar();
// while ( '\n' != ch && ch != EOF)
// {
// ch = getchar();
// }
//}
#define CONTINUE(X) {printf("%s", X); getchar();}
//{
// char str = "按回车键继续";
// printf("%s", "按回车键继续"/*tr*/);
// getchar();
//}
#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;
/************************************************************************/
/* 函数功能:创建玩家
/* 函数参数:无
/* 返 回 值:成功-返回堆内存首地址
/* 失败-返回NULL
/************************************************************************/
player_t * ceart_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;
return player;
}
/************************************************************************/
/* 函数功能:销毁玩家
/* 函数参数:无
/* 返 回 值:无
/************************************************************************/
void destory_player()
{
if (NULL != player)
{
free(player);
player = NULL;
}
}
/************************************************************************/
/* 函数功能:创建菜单
/* 函数参数:无
/* 返 回 值:无
/************************************************************************/
void meue()
{
system("cls");
printf("欢迎来到猜拳游戏\n");
printf("==================\n\n");
printf("1. 石头 2. 剪刀 3. 布 0. 退出\n");
printf("请您出拳:");
}
int myrand()
{
int chose = 0;
srand(time(NULL));
chose = rand() % 3 + 1; //[0 maxnum][1-3]
return chose;
}
/************************************************************************/
/* 函数功能:打印详细信息
/* 函数参数:choseOfPlayer 玩家出拳
/* choseOfComputer电脑出拳
/* 返 回 值:无
/************************************************************************/
void out_win(int choseOfplayer, int choseOfComputer)
{
if(1 == choseOfplayer)
{
printf("您出了石头\n");
}
else if (2 == choseOfplayer)
{
printf("您出了剪刀\n");
}
else if(3 == choseOfplayer)
{
printf("您出了布\n");
}
if(1 == choseOfComputer)
{
printf("电脑出了石头\n");
}
else if (2 == choseOfComputer)
{
printf("电脑出了剪刀\n");
}
else if(3 == choseOfComputer)
{
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 compuer_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;
}
load();
compuer_chose = myrand();
printf("==================出拳详情==================\n");
out_win(player_chose, compuer_chose);
win = player_chose - compuer_chose;
//总局数
player->total++;
switch (win)
{
case -1:
case 2:
{
printf("\n\n恭喜您,赢了\n");
player->victory++;
CONTINUE("按回车继续");
break;
}
case 0:
{
printf("\n\n和了\n");
CONTINUE("按回车继续");
break;
}
default:
{
printf("\n\n很遗憾您,输了\n");
CONTINUE("按回车继续");
break;
}
}
}
}
/************************************************************************/
/* 函数功能:排行榜
/* 函数参数:无
/* 返 回 值:无
/************************************************************************/
void victory_display()
{
double win = 0.0;
printf("\n\n\t\t 排行榜\n");
printf("\t\t==========\n\n");
printf("%10s %10s %10s %10s\n", "姓名", "总局数", "胜利数", "胜率\n");
printf("=============================================\n\n");
if (0 != player->total)
{
win = (double)player->victory / player->total * 100;
}
printf("%10s %10d %10d %5.2lf%% \n", player->name, player->total, player->victory, win);
}
int main(void)
{
player = ceart_player();
if (NULL == player)
{
return 1;
}
meue_ctr();
victory_display();
destory_player();
}
运行结果截图: