//初始化游戏
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;//战斗失败
}