C语言模拟面向对象

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>  /* for sleep() in dos/mingw */

/*
 * 关键字模拟
 */
#ifndef class
    #define class  struct
#endif
#define false 0
#define true (!0)

/*
 * 游戏角色抽象类, 继承自目标类,( c 无法实现继承,采用相同定义)
 */
typedef class Role{
   class Role *this;
    void (*fight) (class Role *this,class Role *other);
    void (*response)(class Role *this);
    int (*is_die)(class Role *this);
    int dps;
    int level;
    int defense;
    int healthPoint;
};
typedef class DeathKnight;

/*
 * 步兵footman
 *
 */
typedef class Footman{
    class Footman *this;
    void (*fight)(class Footman *this,class Role *other );
    void (*response)(class Footman *this);
    int (*is_die)(class Footman *this);
    int dps;
    int level;
    int defense;
    int healthPoint;
};
/*
 * 死亡骑士
 */
typedef class DeathKnight{
    class DeathKnight *this;
    void (*fight)(class DeathKnight *this,class Role *other );
    void (*response)(class DeathKnight *this);
    int (*is_die)(class DeathKnight *this);
    int dps;
    int level;
    int defense;
    int healthPoint;
};

/*
 * 步兵鼠标点中时回应
 * footman_object :Footman对象
 */
void footman_response()
{
    printf("FT:Yes Sir!\n");
}
/**
 * 步兵死了吗?
 * @param footman
 */
int footman_is_die(class Footman *footman)
{
    if(footman->healthPoint<0)
    {
        printf("FT:Footman was dead.\n");
        return 1;
    }
    else
        return 0;
}
/*
 * 步兵攻击目标
 * */
void footman_fight(class Footman *footman,class Role *other)
{
    if(other->is_die(other))
        printf("FT:other was dead\n");
    else
    {
        printf("FT:A~A~ I'm foot man , fight for the king! pilipala...\n");
        int damage=footman->dps-other->defense;
        other->healthPoint -=damage;
        printf("FT:Footman Damage:%d\n",damage);
        printf("other's healthpoint is %d.\n",other->healthPoint);
        other->is_die(other);
    }

}
void deathknight_response()
{
    printf("DK:Nerver die...\n");
}
/*
 * 死亡骑士攻击目标
 */
void deathknight_fight(class DeathKnight *dk,class Role *other)
{
    
     if(other->is_die(other))
         printf("DK:other was dead\n");
    else
    {
        printf("DK:go to hell!\n");
        int damage=dk->dps-other->defense;
        other->healthPoint -=damage;
        printf("DK:DeathKnight Damage:%d\n",damage);
        printf("other's healthpoint is %d.\n",other->healthPoint);
        other->is_die(other);
    }
    
}
/**
 * 死亡骑士死了吗?
 * @param footman
 */
int deathknight_is_die(class DeathKnight* dk)
{
    if(dk->healthPoint<0){
        printf("DK:DeathKnight was died.\n");
        return 1;
    }
    else
        return 0;
}
/*
 * 构造函数
 */
void Footman_New(class Footman* footman)
{
    footman->dps=30;
    footman->level=1;
    footman->defense=10;
    footman->healthPoint=120;
    footman->fight=footman_fight;
    footman->is_die=footman_is_die;
    footman->response=footman_response;
}
void DeathKnight_New(class DeathKnight* dk)
{
    dk->dps=54;
    dk->level=1;
    dk->defense=15;
    dk->healthPoint=240;
    dk->is_die=deathknight_is_die;
    dk->fight=deathknight_fight;
    dk->response=deathknight_response;
}

/*
 * 
 */
int main(int argc, char** argv) {
    class DeathKnight *dk,deathknight;
    class Footman *footman,soilder;
    Footman_New(&soilder);
    DeathKnight_New(&deathknight);
    dk=&deathknight;
    footman=&soilder;
    footman->response(footman);
    dk->response(dk);
    int  i=1;
    do
    {
        printf("\n\n\nROUND %d\n",i);
        if(i%2==0)
        {
            if(!(footman->is_die(footman)))
            {
                if(!(dk->is_die(dk)))
                {
                    footman->fight(footman,dk);
                }
            }
        }else{
            if(!(dk->is_die(dk)))
            {
                if(!(footman->is_die(footman)))
                {
                    dk->fight(dk,footman);
                }
            }
        }
       Sleep(1500);
        i++;
    }while(!(footman->is_die(footman))&&!(dk->is_die(dk)));
    printf("\n\n\nThe END.\n");
    return (EXIT_SUCCESS);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值