C语言-面向接口编程-甲乙双方

假如框架接口设计 由甲方设计,具体功能由乙方去完成,最后完成一个对接。

例如:

  1. 框架接口设计:
  2.  初始化游戏
  3. 核心功能战斗
  4. 查看玩家信息
  5. 结束游戏

甲方编程:

 

#include


//初始化游戏
typedef void(* INIT_GAME)(void ** gameHandle,char* name);  //函数指针

//游戏战斗  返回游戏战斗结果 ,1代表胜利  0 失败

typedef int(* FIGHT_GAME)(void* gameHandle,int gameLevel);

//查看玩家信息
typedef void(*PRINT_GAME)(void* gameHandle);


//离开游戏

typedef void(*CLOSE_GAME)(void* gameHandle);


void playGmae(INIT_GAME init, FIGHT_GAME fight,PRINT_GAME printGame,CLOSE_GAME closeGame)
{
	char name[64];
	int ret;
	int level = -1;
	//初始化游戏
	void* gameHandle = NULL;
	printf("请输入玩家姓名:\n");
	//char name[64];
	scanf("%s",name);

	init(&gameHandle,name);

	//游戏战斗
	//int level = -1;

	printf("请选择游戏难度:\n");
	printf("1、普通\n");
	printf("2、中等\n");
	printf("3、困难\n");

	scanf("%d",&level);

	ret = fight(gameHandle,level);
	if(ret == 0)
	{
		printf("挑战失败\n");

	}
	else
	{
		printf("挑战成功\n");
		printGame(gameHandle);
	}

	//离开游戏

	closeGame(gameHandle);

}

int main()
{
	return 0;
}

乙方实现具体功能:

#pragma once
#include
#include
#include
struct Player
{
	char name[64]; //玩家姓名
	int level;		//玩家等级
	int exp;		//玩家经验
};

//初始化游戏
void INIT_GAME_Company(void ** gameHandle,char* name);  //函数指针

//游戏战斗  返回游戏战斗结果 ,1代表胜利  0 失败

int FIGHT_GAME_Company(void* gameHandle,int gameLevel);

//查看玩家信息
void PRINT_GAME_Company(void* gameHandle);


//离开游戏
void CLOSE_GAME_Company(void* gameHandle);

//判断游戏是否胜利  返回经验值
//我们输入1等级的话,就让它90%可能性赢
//我们输入2等级的话,就让它50%可能性赢
//我们输入3等级的话,就让它10%可能性赢
int isWin(int winRate,int level);
#include"GameCompany.h"


//初始化游戏
void INIT_GAME_Company(void ** gameHandle,char* name) //函数指针
{
	struct Player* player = (struct 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)
{
	int addExp = 0;
	struct Player* player = (struct Player*)gameHandle;

	switch(gameLevel)
	{
	case 1:
		addExp = isWin(90,1);
		break;
	case 2:
		addExp = isWin(50,2);
		break;
	case 3:
		addExp = isWin(10,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 = (struct Player*) gameHandle;
	printf("玩家 <%s>  当前等级<%d>   当前经验 <%d>",player->name,player->level ,player->exp );
}


//离开游戏
void CLOSE_GAME_Company(void* gameHandle)
{
	if(gameHandle != NULL)
	{
		free(gameHandle);
		gameHandle == NULL;
	}
}

int isWin(int winRate,int level)
{
	int score = rand()% 100 + 1;
	if(score <= winRate)
	{
		return 10*level;
	}
	else //战斗失败
		return 0;
}

接口对接

注意的点:1.在甲方这添加上乙方的头文件  2.甲方mian函数的书写

#include
#include"GameCompany.h"


//初始化游戏
typedef void(* INIT_GAME)(void ** gameHandle,char* name);  //函数指针

//游戏战斗  返回游戏战斗结果 ,1代表胜利  0 失败

typedef int(* FIGHT_GAME)(void* gameHandle,int gameLevel);

//查看玩家信息
typedef void(*PRINT_GAME)(void* gameHandle);


//离开游戏

typedef void(*CLOSE_GAME)(void* gameHandle);


void playGame(INIT_GAME init, FIGHT_GAME fight,PRINT_GAME printGame,CLOSE_GAME closeGame)
{
	char name[64];
	int ret;
	int level = -1;
	//初始化游戏
	void* gameHandle = NULL;
	printf("请输入玩家姓名:\n");
	//char name[64];
	scanf("%s",name);

	init(&gameHandle,name);

	//游戏战斗
	//int level = -1;

	printf("请选择游戏难度:\n");
	printf("1、普通\n");
	printf("2、中等\n");
	printf("3、困难\n");

	scanf("%d",&level);

	ret = fight(gameHandle,level);
	if(ret == 0)
	{
		printf("挑战失败\n");

	}
	else
	{
		printf("挑战成功\n");
		printGame(gameHandle);
	}

	//离开游戏

	closeGame(gameHandle);

}

int main()
{
	playGame(INIT_GAME_Company,FIGHT_GAME_Company,PRINT_GAME_Company,CLOSE_GAME_Company);
	return 0;
}

 

 

你可能感兴趣的:(C语言,C语言-面向接口编程)