MOSAD第一次作业

这篇博客介绍了使用Objective-C编程实现的一个三国英雄对决的游戏。作者通过创建Hero类和多个英雄子类,利用多态实现了不同英雄的技能和随机选择。游戏中,英雄的属性如血量、攻击力等由骰子决定,通过掷骰子决定攻击顺序和技能发动。博客总结了项目中的多态和面向对象特性,并指出了main函数代码过长的问题,需要学习函数定义以优化代码结构。
摘要由CSDN通过智能技术生成

MOSAD第一次作业

介绍

创建第一个XCode项目、面向对象与Objective-C语法学习

开发环境

Mac OS
Objective-C
Xcode

任务

安装配置Mac OS和Xcode

熟悉Mac OS按键操作

熟悉Xcode项目结构,编译运行第一个Xcode项目,项目创建流程详见tutorial

选取并设计N(N>10)个三国英雄

实现场景输出(log形式即可):随机选择两名英雄,挑战M(M<=10)个回合,其中每个回合各英雄可发动不同技能,从而导致不同的结果发生(英雄各属性变化及每个回合的输赢)。

要求

输出随机选择的英雄及其关键属性,输出单挑中每回合的过程、结果,以及最终胜负结果
需要用到多态
输出程序设计和运行过程中,相关设计体现的oo特性(数据隐秘性、封装性、继承、多态)

验收要点

输出随机选择的英雄及其关键属性,输出单挑中每回合的过程、结果,以及最终胜负结果
类的设计与封装
类与类之间的继承关系与多态的体现

设计思路

在这里插入图片描述
大体上先把英雄的属性想好了

大概就是名字、所属的国家地区(魏蜀吴群)、血量、攻击力
和技能以及技能发动条件

魏蜀吴群各3个英雄,攻击12个英雄

我经过斟酌最后决定让这个游戏的大部分操作都经由骰子来驱动
例如能否发动攻击(双方各掷一个骰子),能否发动技能(再掷一个骰子)

骰子用随机数实现

刚开始写的时候认为重点难点应该在于人物的平衡性以及技能的生效方式。

编写一个Hero类作为所有英雄的父类,其中PKOneUnit函数和skill函数体现多态。

具体实现

Hero.h

#ifndef Hero_h
#define Hero_h
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface Hero: NSObject
{
    NSString* name;
    NSString* country;
    int blood_value;
    int attack;
}

-(void)PKOneUnit;//这里我把这个函数用作初始化人物属性
-(NSString*)getName;
-(NSString*)getCountry;
-(NSInteger)getBLood_value;
-(NSInteger)getAttack;
-(void)changeBlood:(NSInteger)blood;//扣血或加血
-(NSInteger)skill:(NSInteger) di andAtt:(NSInteger) att;//技能
-(NSInteger)dice;//掷骰子

@end

@interface LiuBei : Hero
{
    
}

@end

@interface GuanYu:Hero
{
    
}

@end

@interface Zhangfei: Hero
{
    
}

@end

@interface CaoCao:Hero
{
    
}

@end

@interface XiaHoudun:Hero
{
    
}

@end

@interface SiMayi:Hero
{
    
}

@end

@interface SunCe:Hero
{
    
}

@end

@interface ZhouYu:Hero
{
    
}

@end

@interface SunQuan: Hero
{
    
}

@end

@interface LvBu:Hero
{
    
}

@end

@interface DongZhuo: Hero
{
    
}

@end

@interface HuaTuo:Hero
{
    
}

@end

NS_ASSUME_NONNULL_END
#endif /* Hero_h */

Hero.m

//
//  Hero.m
//  homework1
//
//  Created by itlab on 2020/10/6.
//  Copyright © 2020 itlab. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Hero.h"


@implementation Hero

-(void)PKOneUnit{}
-(NSString*)getName
{
    return name;
}
-(NSString*)getCountry
{
    return country;
}
-(NSInteger)getAttack
{
    return attack;
}
-(NSInteger)getBLood_value
{
    return blood_value;
}
-(NSInteger)skill:(NSInteger) di andAtt:(NSInteger) att
{
    return 0;
}
-(void)changeBlood:(NSInteger)blood
{
    blood_value-=blood;
    if(blood_value<0)
        blood_value=0;
}
-(NSInteger)dice
{
    NSInteger di=(arc4random()% 6)+1;
    return di;
}

@end

@implementation LiuBei

-(void)PKOneUnit
{
    name=@"刘备";
    country=@"蜀";
    blood_value=4000;
    attack=1000;
}

-(NSInteger)skill:(NSInteger)di andAtt:(NSInteger)att
{
    NSInteger d=(arc4random()%6)+1;
    if(d>=5)
    {
        printf("刘备发动技能:一波多折,骰子重铸\n");
        return 5;
    }
    return 0;
}

@end

@implementation GuanYu

-(void)PKOneUnit
{
    name=@"关羽";
    country=@"蜀";
    blood_value=4000;
    attack=1000;
}

-(NSInteger)skill:(NSInteger)di andAtt:(NSInteger)att
{
    NSInteger d=(arc4random()%6)+1;
    if(d>=4)
    {
        printf("关羽发动技能:赤兔马,攻击力永久+500\n");
        attack+=500;
        return 4;
    }
    return 0;
}

@end

@implementation Zhangfei

-(void)PKOneUnit
{
    name=@"张飞";
    country=@"蜀";
    blood_value=4500;
    attack=1000;
}

-(NSInteger)skill:(NSInteger)di andAtt:(NSInteger)att
{
    NSInteger d=(arc4random()%6)+1;
    if(d>=6)
    {
        printf("张飞发动技能:莽夫,血量-1000\n");
        blood_value-=1000;
        if(blood_value<0)
            blood_value=0;
    }
    return 0;
}

@end

@implementation CaoCao

-(void)PKOneUnit
{
    name=@"曹操";
    country=@"魏";
    blood_value=4000;
    attack=1000;
}

-(NSInteger)skill:(NSInteger)di andAtt:(NSInteger)att
{
    NSInteger d=(arc4random()%6)+1;
    if(d>=5)
    {
        printf("曹操发动技能:奸雄,骰子点数+2\n");
        return 6;
    }
    return 0;
}

@end

@implementation XiaHoudun

-(void)PKOneUnit
{
    name=@"夏侯惇";
    country=@"魏";
    blood_value=4500;
    attack=1000;
}

-(NSInteger)skill:(NSInteger)di andAtt:(NSInteger)att
{
    NSInteger d=(arc4random()%6)+1;
    if(d>=6)
    {
        printf("夏侯惇发动技能:硬汉,血量+1000\n");
        blood_value+=1000;
        if(blood_value>4500)
            blood_value=4500;
    }
    return 0;
}

@end

@implementation SiMayi

-(void)PKOneUnit
{
    name=@"司马懿";
    country=@"魏";
    blood_value=3500;
    attack=1500;
}

-(NSInteger)skill:(NSInteger)di andAtt:(NSInteger)att
{
    NSInteger d=(arc4random()%6)+1;
    if(d>=5)
    {
        printf("司马懿发动技能:路人皆知,敌方骰子点数-2\n");
        return 7;
    }
    return 0;
}

@end

@implementation SunCe

-(void)PKOneUnit
{
    name=@"孙策";
    country=@"吴";
    blood_value=5000;
    attack=1000;
}

-(NSInteger)skill:(NSInteger)di andAtt:(NSInteger)att
{
    NSInteger d=(arc4random()%6)+1;
    if(d>=6)
    {
        printf("孙策发动技能:遇刺,血量-1000\n");
        blood_value-=1000;
        if(blood_value<0)
            blood_value=0;
    }
    return 0;
}

@end

@implementation SunQuan

-(void)PKOneUnit
{
    name=@"孙权";
    country=@"吴";
    blood_value=3500;
    attack=1000;
}

-(NSInteger)skill:(NSInteger)di andAtt:(NSInteger)att
{
    NSInteger d=(arc4random()%6)+1;
    if(d>=5)
    {
        printf("孙权发动技能:运气好,骰子点数变6\n");
        return 2;
    }
    return 0;
}

@end

@implementation ZhouYu

-(void)PKOneUnit
{
    name=@"周瑜";
    country=@"吴";
    blood_value=3500;
    attack=1000;
}

-(NSInteger)skill:(NSInteger)di andAtt:(NSInteger)att
{
    NSInteger d=(arc4random()%6)+1;
    if(d>=5)
    {
        printf("周瑜发动技能:妙计,双方互换骰子点数\n");
        return 8;
    }
    return 0;
}

@end

@implementation LvBu

-(void)PKOneUnit
{
    name=@"吕布";
    country=@"群";
    blood_value=5000;
    attack=1000;
}

-(NSInteger)skill:(NSInteger)di andAtt:(NSInteger)att
{
    NSInteger d=(arc4random()%6)+1;
    if(d>=6)
    {
        printf("吕布发动技能:方天画戟,攻击力翻倍\n");
        return 1;
    }
    return 0;
}

@end

@implementation DongZhuo

-(void)PKOneUnit
{
    name=@"董卓";
    country=@"群";
    blood_value=9000;
    attack=500;
}

-(NSInteger)skill:(NSInteger)di andAtt:(NSInteger)att
{
    NSInteger d=(arc4random()%6)+1;
    if(d>=2)
    {
        printf("董卓发动技能:被针对,血量-1000\n");
        blood_value-=1000;
        if(blood_value<0)
            blood_value=0;
    }
    return 0;
}

@end

@implementation HuaTuo

-(void)PKOneUnit
{
    name=@"华佗";
    country=@"群";
    blood_value=3500;
    attack=1000;
}

-(NSInteger)skill:(NSInteger)di andAtt:(NSInteger)att
{
    NSInteger d=(arc4random()%6)+1;
    if(d>=5)
    {
        printf("华佗发动技能:回血,血量+500\n");
        blood_value+=500;
        if(blood_value>3500)
            blood_value=3500;
    }
    return 0;
}

@end


main.m

//
//  main.m
//  homework1
//
//  Created by itlab on 2020/10/6.
//  Copyright © 2020 itlab. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Hero.h"


int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        
        printf("Game start!\n");
        
        
        Hero* one;
        Hero* two;
        
        
        //以下为随机挑选英雄阶段
        NSInteger di=(arc4random() % 12)+1;
        if(di==1)
        {
            one=[[LiuBei alloc]init];
        }
        else if(di==2)
        {
            one=[[GuanYu alloc]init];
        }
        else if(di==3)
        {
            one=[[Zhangfei alloc]init];
        }
        else if(di==4)
        {
            one=[[CaoCao alloc]init];
        }
        else if(di==5)
        {
            one=[[XiaHoudun alloc]init];
        }
        else if(di==6)
        {
            one=[[SiMayi alloc]init];
        }
        else if(di==7)
        {
            one=[[SunCe alloc]init];
        }
        else if(di==8)
        {
            one=[[SunQuan alloc]init];
        }
        else if(di==9)
        {
            one=[[LvBu alloc]init];
        }
        else if(di==10)
        {
            one=[[DongZhuo alloc]init];
        }
        else if(di==11)
        {
            one=[[ZhouYu alloc]init];
        }
        else
        {
            one=[[HuaTuo alloc]init];
        }
        
        
        
        di=(arc4random() % 12)+1;
        if(di==1)
        {
            two=[[LiuBei alloc]init];
        }
        else if(di==2)
        {
            two=[[GuanYu alloc]init];
        }
        else if(di==3)
        {
            two=[[Zhangfei alloc]init];
        }
        else if(di==4)
        {
            two=[[CaoCao alloc]init];
        }
        else if(di==5)
        {
            two=[[XiaHoudun alloc]init];
        }
        else if(di==6)
        {
            two=[[SiMayi alloc]init];
        }
        else if(di==7)
        {
            two=[[SunCe alloc]init];
        }
        else if(di==8)
        {
            two=[[SunQuan alloc]init];
        }
        else if(di==9)
        {
            two=[[LvBu alloc]init];
        }
        else if(di==10)
        {
            two=[[DongZhuo alloc]init];
        }
        else if(di==11)
        {
            two=[[ZhouYu alloc]init];
        }
        else
        {
            two=[[HuaTuo alloc]init];
        }
        
        //初始化英雄
        [one PKOneUnit];
        [two PKOneUnit];
        
        //输出英雄信息
        NSString *name1=[one getName];
        NSString *name2=[two getName];
        
        printf("%svs%s\n\n", [name1 UTF8String], [name2 UTF8String]);
        printf("%s信息:\n", [name1 UTF8String]);
        printf("国家:%s\n", [[one getCountry] UTF8String]);
        printf("血量:%ld\n", [one getBLood_value]);
        printf("攻击力:%ld\n\n", [one getAttack]);
        printf("%s信息:\n", [name2 UTF8String]);
        printf("国家:%s\n", [[two getCountry] UTF8String]);
        printf("血量:%ld\n", [two getBLood_value]);
        printf("攻击力:%ld\n\n", [two getAttack]);
        
        //开始战斗
        NSInteger round;
        for(round=1;;round++)
        {
        	//掷骰子决定谁可以攻击,点数平就不攻击
            printf("Round %ld\n", round);
            NSInteger d1=[one dice];
            NSInteger d2=[two dice];
            NSInteger att1=[one getAttack];
            NSInteger att2=[two getAttack];
            printf("骰子点数:%ld : %ld\n", d1, d2);
            
            //判断技能是否发动,根据返回值不同对属性进行更改
            NSInteger check1=[one skill:d1 andAtt:att1];
            NSInteger check2=[two skill:d2 andAtt:att2];
            if(check1==1)
            {
                att1+=att1;
            }
            else if(check1==2)
            {
                d1=6;
                printf("骰子点数:%ld : %ld\n", d1, d2);
            }
            else if(check1==3)
            {
                att2=att2/2;
            }
            else if(check1==4)
            {
                att1=[one getAttack];
            }
            else if(check1==5)
            {
                d1=[one dice];
                printf("骰子点数:%ld : %ld\n", d1, d2);
            }
            else if(check1==6)
            {
                d1+=2;
                if(d1>6)
                    d1=6;
                printf("骰子点数:%ld : %ld\n", d1, d2);
            }
            else if(check1==7)
            {
                d2-=2;
                if(d2<1)
                    d2=1;
                printf("骰子点数:%ld : %ld\n", d1, d2);
            }
            else if(check1==8)
            {
                NSInteger temp=d1;
                d1=d2;
                d2=temp;
                printf("骰子点数:%ld : %ld\n", d1, d2);
            }
            
            if(check2==1)
            {
                att2+=att2;
            }
            else if(check2==2)
            {
                d2=6;
                printf("骰子点数:%ld : %ld\n", d1, d2);
            }
            else if(check2==3)
            {
                att1=att1/2;
            }
            else if(check2==4)
            {
                att2=[two getAttack];
            }
            else if(check2==5)
            {
                d2=[two dice];
                printf("骰子点数:%ld : %ld\n", d1, d2);
            }
            else if(check2==6)
            {
                d2+=2;
                if(d2>6)
                    d2=6;
                printf("骰子点数:%ld : %ld\n", d1, d2);
            }
            else if(check2==7)
            {
                d1-=2;
                if(d1<1)
                    d1=1;
                printf("骰子点数:%ld : %ld\n", d1, d2);
            }
            else if(check2==8)
            {
                NSInteger temp=d1;
                d1=d2;
                d2=temp;
                printf("骰子点数:%ld : %ld\n", d1, d2);
            }


            //攻击阶段
            if(d1>d2)
            {
                printf("%s对%s造成%ld点伤害!", [name1 UTF8String], [name2 UTF8String], att1);
                [two changeBlood:att1];
            }
            else if(d1<d2)
            {
                printf("%s对%s造成%ld点伤害!\n", [name2 UTF8String], [name1 UTF8String], att2);
                [one changeBlood:att2];
            }
            
            //检测是否存活
            printf("剩余血量:%ld:%ld\n\n", [one getBLood_value], [two getBLood_value]);
            if([one getBLood_value]==0 && [two getBLood_value]!=0)
            {
                printf("%s获胜!\n", [name2 UTF8String]);
                break;
            }
            else if([two getBLood_value]==0 && [one getBLood_value]!=0)
            {
                printf("%s获胜!\n", [name1 UTF8String]);
                break;
            }
            else if([one getBLood_value]==0 && [two getBLood_value]==0)
            {
                printf("平局!\n");
                break;
            }
        }
    }
    return 0;
}

总结

首先我掌握了object-c的类如何实现,了解了多态继承的实现方式

不足之处在于main函数代码过多,原因在于我没有学会怎么定义一个函数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的文件内容,本文将详细解析“飞行弹道仿真”的核心知识点,主要涉及MATLAB编程环境下的弹道仿真实现过程。 ### 弹道仿真概述 弹道仿真是一种通过数学模型来预测导弹、炮弹等飞行器在空中飞行轨迹的技术。在军事、航天等多个领域都有着广泛的应用。对于弹道仿真的研究不仅有助于提升武器系统的精确度,还能帮助科研人员更好地理解空气动力学原理以及飞行器的动力特性。 ### MATLAB环境下弹道仿真的实现 #### 1. **初始化参数** 在代码中,作者首先对一系列变量进行了初始化处理。这些变量包括但不限于:质量(`m`), 速度(`V`), 高度(`H`)等关键物理量。此外,还定义了一些常量如重力加速度(`g`)、空气密度(`rho_air`)等。 #### 2. **地面高度分布设定** 通过设定地面高度随距离变化的函数(`x_d` 表示水平距离,`H_d` 表示对应的高度),可以模拟不同的地形特征。这里使用了一个分段函数来表示地面高度的变化情况。 #### 3. **动态方程与运动方程** - **动态方程**:描述了导弹受到的外力作用(推力、阻力、升力)以及重力对其运动状态的影响。 - 推力(`P`)、阻力(`X`)、升力(`Y`)等参数被用于计算导弹的速度和角度变化。 - 通过积分运算更新速度、角度等状态量。 - **运动方程**:描述了导弹在三维空间中的位置变化情况。 - 包括水平方向速度(`equ4_Kinematic_x`)、垂直方向速度(`equ5_Kinematic_y`)以及姿态角(`equ6_Kinematic_Theta`)的变化。 - 这些方程同样通过积分方法进行求解。 #### 4. **控制律设计** 控制律设计是确保导弹按照预定轨迹飞行的关键环节。例如,代码中采用了简单的PID控制策略来调整导弹的姿态角。具体地: - `k_phi` 和 `k_phidiff` 分别代表比例系数和微分系数。 - 通过调整这些系数的值,可以优化导弹的飞行性能,使其更加稳定且能够准确跟踪目标。 #### 5. **数值积分方法** 为了求解动态方程与运动方程,文中采用了一种数值积分方法(`integral_to_next`)。该方法可以近似计算出导弹在下一时刻的状态量(速度、角度等)。虽然具体的实现细节没有给出,但通常这类方法基于欧拉法或者更高级的龙格-库塔法等。 ### 结论 本文通过对“飞行弹道仿真”这一主题的深入探讨,不仅详细介绍了如何使用MATLAB进行弹道仿真,而且还重点讲解了其中涉及到的重要概念和技术细节,如地面高度分布设定、动态方程与运动方程、控制律设计以及数值积分方法等。对于希望深入了解弹道仿真技术的研究者来说,本文提供了丰富的参考资料和实施指南。通过这样的仿真研究,不仅可以提高导弹等飞行器的设计精度,还能为未来航空航天技术的发展提供强有力的支持。
项目:使用AngularJs编写的简单 益智游戏(附源代码)  这是一个简单的 javascript 项目。这是一个拼图游戏,也包含一个填字游戏。这个游戏玩起来很棒。有两个不同的版本可以玩这个游戏。你也可以玩填字游戏。 关于游戏 这款游戏的玩法很简单。如上所述,它包含拼图和填字游戏。您可以通过移动图像来玩滑动拼图。您还可以选择要在滑动面板中拥有的列数和网格数。 另一个是填字游戏。在这里你只需要找到浏览器左侧提到的那些单词。 要运行此游戏,您需要在系统上安装浏览器。下载并在代码编辑器中打开此项目。然后有一个 index.html 文件可供您修改。在命令提示符中运行该文件,或者您可以直接运行索引文件。使用 Google Chrome 或 FireFox 可获得更好的用户体验。此外,这是一款多人游戏,双方玩家都是人类。 这个游戏包含很多 JavaScript 验证。这个游戏很有趣,如果你能用一点 CSS 修改它,那就更好了。 总的来说,这个项目使用了很多 javascript 和 javascript 库。如果你可以添加一些具有不同颜色选项的级别,那么你一定可以利用其库来提高你的 javascript 技能。 演示: 该项目为国外大神项目,可以作为毕业设计的项目,也可以作为大作业项目,不用担心代码重复,设计重复等,如果需要对项目进行修改,需要具备一定基础知识。 注意:如果装有360等杀毒软件,可能会出现误报的情况,源码本身并无病毒,使用源码时可以关闭360,或者添加信任。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值