面向接口---函数实现

//初始化游戏
void INIT_GAME_COMPANY(void ** gameHandle, char * name)
{
	struct Player * player = malloc(sizeof(struct Player));
	if (player == NULL)
			return;


	//玩家进行初始化操作
	strcpy(player->name, name);
	player->level = 0;
	player->exp = 0;

	*gameHandle = player;


}

//游戏战斗 返回游戏战斗结果 如果是1 代表游戏胜利 返回0 代表战斗失败
int FIGHT_GAME_COMPANY(void * gameHandle, int gameLevel)
{
	struct Player * player = gameHandle;

	int addExp = 0;//累积的经验值

	switch (gameLevel)
	{
	case 1:
		//内部提供一个函数 判断游戏是否胜利
		addExp = isWin(90, 1);
		break;
	case 2:
		addExp = isWin(50, 2);
		break;
	case 3:
		addExp = isWin(30, 3);
		break;

	default:
		break;
	}
	//将获取的经验 给人物身上
	player->exp += addExp;
	player->level = player->exp / 10;

	if (addExp == 0)
		return 0;//失败
	else
		return 1;//胜利

 }

//查看玩家信息
void PRINT_GAME_COMPANY(void * gameHandle)
{
	struct Player * player = gameHandle;
	printf("玩家<%s>-----当前等级<%d>-----当前经验<%d>\n", player->name, player->level, player->exp);

 }

//离开游戏
void CLOSE_GAME_COMPANY(void * gameHandle)
{
	if (gameHandle == NULL)
		return;
	free(gameHandle);
	gameHandle = NULL;

 }

 //判断游戏是否胜利 参数一 胜率 参数二 难度  返回值  胜利后的经验,如果返回的是0 代表战斗失败
 int isWin(int winRate, int diff)
 {
	 int random = rand() % 100 + 1;
	 if (random <= winRate)
	 {
		 //代表胜利
		 return 10 * diff;
	 }
	 else
		 return 0;//战斗失败
 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值