《猜拳小游戏》--一个c语言写的小项目

很久以前写的一个小项目,有兴趣的可以进来看看。

这个小项目主要包含以下信息和功能:

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();


}

    

运行结果截图:



  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: #include<stdio.h> #include<stdlib.h> #include<time.h> int main() { int n,i; char a[3][10]={"石头","剪刀","布"}; //将石头、剪刀和布分别存储在二维字符数组a中 srand(time(NULL)); //srand()函数设置随机数种子 for(i=1;i<=3;i++) //循环3次让玩家猜拳 { printf("请猜拳(1. 石头 2. 剪刀 3. 布):"); scanf("%d",&n); if(n<1 || n>3) //输入不合法判断 { printf("您输入的数字不合法,请重新输入!\n"); i--; //重新输入 continue; } printf("您出了%s, 电脑出了%s, ",a[n-1],a[rand()%3]); if(n==1 && rand()%3==1 || n==2 && rand()%3==2 || n==3 && rand()%3==0) printf("平局!\n"); else if(n==1 && rand()%3==2 || n==2 && rand()%3==0 || n==3 && rand()%3==1) printf("您赢了!\n"); else printf("您输了!\n"); } return 0; } ### 回答2: 下面是一个用C语言的简单猜拳游戏示例: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int playerChoice, computerChoice; printf("猜拳游戏\n"); printf("请输入数字选择:\n"); printf("1. 石头\n"); printf("2. 剪刀\n"); printf("3. 布\n"); // 获取玩家选择 printf("你的选择:"); scanf("%d", &playerChoice); // 随机生成电脑选择 srand(time(NULL)); computerChoice = rand() % 3 + 1; // 打印玩家和电脑选择 printf("玩家选择:%d\n", playerChoice); printf("电脑选择:%d\n", computerChoice); // 判断胜负 if (playerChoice == computerChoice) { printf("平局!\n"); } else if ((playerChoice == 1 && computerChoice == 2) || (playerChoice == 2 && computerChoice == 3) || (playerChoice == 3 && computerChoice == 1)) { printf("玩家胜利!\n"); } else { printf("电脑胜利!\n"); } return 0; } ``` 这个程序首先会让玩家输入数字选择石头、剪刀或布,然后通过随机数生成电脑的选择。然后程序比较玩家和电脑的选择,根据规则判断胜负,并输出结果。如果玩家和电脑选择相同,则为平局;如果玩家选择石头,电脑选择剪刀,或者玩家选择剪刀,电脑选择布,或者玩家选择布,电脑选择石头,则玩家胜利;否则电脑胜利。 ### 回答3: 猜拳游戏是一个非常简单且有趣的游戏。我们可以使用C语言来编一个猜拳游戏,以下是程序的示例代码: ``` #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int player, computer; int player_score = 0, computer_score = 0; int rounds; printf("欢迎来到猜拳游戏!\n"); printf("请输入游戏回合数: "); scanf("%d", &rounds); for (int i = 0; i < rounds; i++) { printf("\n第 %d 回合\n", i+1); printf("请玩家选择出拳(1 - 石头,2 - 剪刀,3 - 布): "); scanf("%d", &player); srand(time(NULL)); computer = rand() % 3 + 1; printf("电脑选择: %d\n", computer); if (player == 1 && computer == 2 || player == 2 && computer == 3 || player == 3 && computer == 1) { printf("玩家胜利!\n"); player_score++; } else if (player == computer) { printf("平局!\n"); } else { printf("电脑胜利!\n"); computer_score++; } } printf("\n======= 游戏结束 =======\n"); printf("玩家得分: %d\n", player_score); printf("电脑得分: %d\n", computer_score); if (player_score > computer_score) { printf("玩家获胜!\n"); } else if (computer_score > player_score) { printf("电脑获胜!\n"); } else { printf("比赛结果为平局!\n"); } return 0; } ``` 以上代码实现了一个猜拳游戏游戏开始时,通过`scanf`函数获取玩家想要进行的回合数。然后,使用一个`for`循环进行每一回合的游戏。 在每一回合中,玩家需要输入出拳的数字(1代表石头,2代表剪刀,3代表布),电脑会随机生成一个数字作为出拳选择。然后程序会判断胜负,并计算玩家和电脑的得分。 最后,程序会统计游戏结束后的得分情况,并输出获胜的一方或平局的结果。 这个简单的猜拳游戏仅供参考,可以根据实际需求对程序进行修改或扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值