Problem C: 小型飞机大战

Problem C: 小型飞机大战

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 851  Solved: 712
[Submit][Status][Web Board]

Description

飞机大战这个小游戏很好玩,作为一名编程狂人,自己不去写个好玩的游戏,怎么能说得过去呢?

现在,请使用C++来编写一个小的飞机大战游戏雏形,至少包括如下类:

1. FlyThing类:敌机和战机的父类,是一个抽象类。

(1)拥有名字属性和位置属性,其中名字是一个字符串,位置是二维空间下的坐标,代表了在屏幕上的位置。

(2)纯虚函数void fly(),模拟飞机的飞行动作。

(3)void show():显示飞机的名字和位置。格式为:“$ at x y”,其中$为飞机名字,x和y是横坐标和纵坐标。

(4)其他必要的函数。

2. EnemyPlane类:敌机类,是FlyThing的子类。其Fly()函数实现将飞机的纵坐标加1。

3. MyPlane类:战机类,是FlyThing的子类。其Fly()函数根据输入实现飞行。具体是:

(1)如果输入A或a,则横坐标减1;

(2)如果输入S或s,则纵坐标加1;

(3)如果输入W或w,则纵坐标减1;

(4)如果输入D或d,则横坐标加1。

Input

第1行N是一个正整数,之后N行,每行包括1个字符串、2个整数分别是敌机名字和横、纵坐标。

第N+2行是一个正整数M,之后M行每行是一个字符,为a、s、d、w之一,用于战机的飞行。

Output

见样例~

Sample Input

3enemy1 10 0enemy2 20 20enemy3 30 104aswd

Sample Output

WUDI at 50 50enemy1 at 10 0enemy2 at 20 20enemy3 at 30 10WUDI at 49 50enemy1 at 10 1enemy2 at 20 21enemy3 at 30 11WUDI at 49 51enemy1 at 10 2enemy2 at 20 22enemy3 at 30 12WUDI at 49 50enemy1 at 10 3enemy2 at 20 23enemy3 at 30 13WUDI at 50 50enemy1 at 10 4enemy2 at 20 24enemy3 at 30 14

HINT

 

Append Code

append.cc,

[Submit][Status][Web Board]

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

#include <iostream> 

#include <string> 

   

using namespace std; 

   

class FlyThing { 

protected

    string name; 

    double x, y; 

public

    virtual void fly() = 0; 

    void show() { 

        cout << name << " at " << x << " " << y << endl; 

    

    FlyThing(string s, double x, double y) : name(s), x(x), y(y) {} 

    ~FlyThing() {} 

}; 

   

class EnemyPlane : public FlyThing { 

public

    EnemyPlane(string name, double x, double y) : FlyThing(name, x, y){} 

    void fly() { 

        y++; 

    

}; 

   

class MyPlane : public FlyThing { 

public

    MyPlane(string name, double x, double y) : FlyThing(name, x, y){} 

    void fly() { 

       char c; 

       cin >> c; 

        if (c == 'a' || c == 'A') x--; 

        else if (c == 's' || c == 'S') y++; 

        else if (c == 'w' || c == 'W') y--; 

        else if (c == 'd' || c == 'D') x++; 

    

}; 

int main()

{

    FlyThing **planes;

    int numOfEnemplanes, i, x, y, numOfMoves, j;

    string str;

    cin>>numOfEnemplanes;

    planes = new FlyThing*[numOfEnemplanes + 1];

    planes[0] = new MyPlane("WUDI", 50, 50);

    for(i = 1; i <= numOfEnemplanes; i++)

    {

        cin>>str>>x>>y;

        planes[i] = new EnemyPlane(str, x, y);

    }

 

    for (j = 0; j < numOfEnemplanes + 1; j++)

    {

        planes[j]->show();

    }

 

    cin>>numOfMoves;

    for (i = 0; i < numOfMoves; i++)

    {

        for (j = 0; j < numOfEnemplanes + 1; j++)

        {

            planes[j]->fly();

            planes[j]->show();

        }

    }

    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值