美文网首页
C++11模板方法模式展示

C++11模板方法模式展示

作者: FredricZhu | 来源:发表于2021-07-17 08:14 被阅读0次

题目,


image.png

这个题目我做的答案判定是错误的,但是思路是一致的,就是说在父类中定义抽象行为,在子类中实现行为方法。
这个作者的测试用例写的有问题,同样的输入,
1/2, 1/3,
永久类型的游戏,
一会说他们两个平手,返回-1.
一会又说 B会赢,因为他血量多。
我觉得应该是 B 赢。
要不然人家血量不是白多了。
设计者这样设计的话,会被打死的。
代码如下,

#include <iostream>
#include <vector>
#include <complex>
#include <tuple>
#include <string>
using namespace std;

struct Creature
{
    int attack, health;

    Creature(int attack, int health) : attack(attack), health(health) {}
};

struct CardGame
{
    vector<Creature> creatures;

    CardGame(const vector<Creature> &creatures) : creatures(creatures) {}

    // return the index of the creature that won (is a live)
    // example:
    // - creature1 alive, creature2 dead, return creature1
    // - creature1 dead, creature2 alive, return creature2
    // - no clear winner: return -1
    int combat(int creature1, int creature2)
    {
        auto creat1 = creatures[creature1];
        auto creat2 = creatures[creature2];
        if(creat1.attack == creat2.attack && creat1.health == creat2.health) {
            return -1;
        } 
        while(creat2.health > 0 && !is_end()) {
                hit(creat1, creat2);
                if(creat2.health>0) {
                    auto tmp = creat1;
                    creat1 = creat2;
                    creat2 = tmp;
                    
                    int tmp_idx =  creature1;
                    creature1 = creature2;
                    creature2 = tmp_idx;
                }
        }

        if(creat2.health <=0) {
            return creature1;
        }
        
        return -1;
    }

    virtual void hit(Creature& attacker, Creature& other) = 0;
    virtual bool is_end() = 0;
};

struct TemporaryCardDamageGame : CardGame
{
    int current_round {0};
    int max_round{2};
    
    TemporaryCardDamageGame(const vector<Creature> &creatures) : CardGame(creatures) {}

    void hit(Creature &attacker, Creature &other) override {
        cout << "Temporary Game..    ";
        cout << "attacker: " << attacker.attack << "  health: " << attacker.health << endl;
        cout << "other: " << other.attack << "  health: " << other.health << endl;
        other.health -= attacker.attack;
        if(other.health > 0) {
            other.health += attacker.attack;
        }
        ++current_round;
    }
    bool is_end() override {
        return current_round >= max_round;
    }
};

struct PermanentCardDamageGame : CardGame
{
    PermanentCardDamageGame(const vector<Creature> &creatures) : CardGame(creatures) {}

    void hit(Creature &attacker, Creature &other) override
    {
      cout << " Permanent Game..    ";
      cout << "attacker: " << attacker.attack << "  health: " << attacker.health << endl;
      cout << "other: " << other.attack << "  health: " << other.health << endl;
      other.health -= attacker.attack;
    }

    bool is_end() override {
        return false;
    }
};

相关文章

  • C++11模板方法模式展示

    题目, 这个题目我做的答案判定是错误的,但是思路是一致的,就是说在父类中定义抽象行为,在子类中实现行为方法。这个作...

  • 11.8设计模式-模板模式-详解

    设计模式-模式模式 模板方法模式详解 模板方法模式在android中的实际运用 1.模板方法模式详解 2.模板方法...

  • 第5章 -行为型模式-模板方法模式

    一、模板方法模式的简介 二、模板方法模式的优点 三、模板方法模式的应用场景 四、模板方法模式的实例

  • 模板方法模式

    模板方法模式 模板方法模式的定义 模板方法模式(Template Method Pattern)是如此简单,以致让...

  • 设计模式系列-模板方法模式

    JAVA设计模式系列: 单例模式 观察者模式 模板方法模式 模板方法模式 定义 模板方法模式在一个方法中定义了算法...

  • 模板方法模式

    一、模板方法模式介绍 二、模板方法模式代码实例

  • 模板方法模式

    一、概念 二、模板方法模式UML图 三、模板方法模式的2个角色 四、模板方法与基本方法的概念 模板方法: 基本方法...

  • 设计模式 | 模板方法模式及典型应用

    本文的主要内容: 介绍模板方法模式 源码分析模板方法模式的典型应用Servlet 中的模板方法模式Mybatis ...

  • 模板方法及观察者模式

    1、本文主要内容 模板方法定义 模板方法示例 观察者模式 本文主要讲两个设计模式,模板方法以及观察者模式,模板方法...

  • 设计模式之十——模板方法模式

    原文传送门 1 介绍 模板方法模式是类的行为模式。 1.1 什么是模板方法模式 模板方法模式是所有模式中最为常见的...

网友评论

      本文标题:C++11模板方法模式展示

      本文链接:https://www.haomeiwen.com/subject/jinipltx.html