Implementing Interface-Oriented Programming in C

Interface-Oriented Programming

  • Implementing programming methods in the company
  • The client and the contractor agree on the interface and implement their respective functionalities
  • Finally, successful integration to achieve game functionality

Code Example:

interface_oriented_programming.c

#define CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "GameCompany.h"
#include <time.h>
// Initialize game
typedef void (*INIT_GAME)(void **gameHandle, char *name);
// Game battle
typedef int (*FIGHT_GAME)(void *gameHandle, int gameLevel);
// View player information
typedef void (*PRINT_GAME)(void *gameHandle);
// Exit game
typedef void (*CLOSE_GAME)(void *gameHandle);
void playGame(INIT_GAME init, FIGHT_GAME fight, PRINT_GAME printGame, CLOSE_GAME closeGame)
{
    // Initialize game
    void *gameHandle = NULL;
    printf("Please enter the player's name:\n");
    char name[64];
    scanf("%s", name);
    init(&gameHandle, name);
    // Game battle
    int level = -1;
    while (1)
    {
        getchar();
        system("cls"); // Clear screen
        printf("Select game difficulty:\n");
        printf("1. Easy\n");
        printf("2. Medium\n");
        printf("3. Hard\n");
        scanf("%d", &level);
        getchar();
        int ret = fight(gameHandle, level);
        if (ret == 0)
        {
            printf("Challenge failed\n");
            break;
        }
        else
        {
            printf("Challenge succeeded\n");
            printGame(gameHandle);
        }
    }
    // Exit game
    closeGame(gameHandle);
}
int main()
{
    // Add random seed
    srand((unsigned int)time(NULL));
    playGame(INIT_GAME_COMPANY, FIGHT_GAME_COMPANY, PRINT_GAME_COMPANY, CLOSE_GAME_COMPANY);
    return EXIT_SUCCESS;
}

GameCompany.c

#include "GameCompany.h"
// Initialize game
void INIT_GAME_COMPANY(void **gameHandle, char *name)
{
    struct Player *player = malloc(sizeof(struct Player));
    if (player == NULL)
    {
        return;
    }
    // Player initialization
    strcpy(player->name, name);
    player->level = 0;
    player->exp = 0;
    *gameHandle = player;
}
// Game battle
int FIGHT_GAME_COMPANY(void *gameHandle, int gameLevel)
{
    struct Player *player = gameHandle;
    int addExp = 0; // Accumulated experience
    switch (gameLevel)
    {
        case 1:
            // Internal function to determine if the game is won
            addExp = isWin(90, 1);
            break;
        case 2:
            addExp = isWin(50, 2);
            break;
        case 3:
            addExp = isWin(30, 3);
            break;
        default:
            break;
    }
    // Assign acquired experience to the player
    player->exp += addExp;
    player->level = player->exp / 10;
    if (addExp == 0)
    {
        return 0; // Battle failed
    }
    else
    {
        return 1; // Battle won
    }
}
// View player information
void PRINT_GAME_COMPANY(void *gameHandle)
{
    struct Player *player = gameHandle;
    printf("Player <%s>-----Current Level <%d>------Current Experience-----<%d>\n", player->name, player->level, player->exp);
}
// Exit game
void CLOSE_GAME_COMPANY(void *gameHandle)
{
    if (gameHandle == NULL)
    {
        return;
    }
    free(gameHandle);
    gameHandle = NULL;
}
// Determine if the game is won     Parameter 1: Win rate    Parameter 2: Difficulty    Return value: Experience after winning, returns 0 to indicate battle failure
int isWin(int winRate, int diff)
{
    int random = rand() % 100 + 1; // 1~100
    if (random <= winRate)
    {
        // Indicate victory
        return 10 * diff;
    }
    else
    {
        // Battle failed
        return 0;
    }
}

GameCompany.h

#pragma once
#define CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Player
{
    char name[64]; // Player name
    int level; // Player level
    int exp; // Player experience
};
// Initialize game
void INIT_GAME_COMPANY(void **gameHandle, char *name);
// Game battle
int FIGHT_GAME_COMPANY(void *gameHandle, int gameLevel);
// View player information
void PRINT_GAME_COMPANY(void *gameHandle);
// Exit game
void CLOSE_GAME_COMPANY(void *gameHandle);
// Determine if the game is won    Parameter 1: Win rate    Parameter 2: Difficulty    Return value: Experience after winning, returns 0 to indicate battle failure
int isWin(int winRate, int diff);

Leave a Comment