继承“虚拟”类

问题描述

我目前正在尝试编写一个通用的 minmax 算法。 我对 C++ 相当陌生,具有 c 和 arm asm 编程背景。 对于 MinMax-Algorithm,我使用了一种称为 Negamax 的变体,这使它更加紧凑:

#define move typename game<evaluationtype>::_move
#define gamestate typename game<evaluationtype>::_gamestate
#define player typename game<evaluationtype>::_player


template <typename gametype,typename evaluationtype>
std::pair<evaluationtype,move> Negamax(game <evaluationtype> Game,int depth,evaluationtype alpha,evaluationtype beta) {
    if (depth == 0)
        return std::make_pair(Game.evaluation(),move());
    else {
        move savebestmove;
        evaluationtype maxValue = alpha;
        std::list<move> allpossiblemoves = Game.getpossiblemoves();
        for (move Move : allpossiblemoves) {
            gamestate state = Game.makemove(Move);
            evaluationtype checkvalue = -((Negamax<gametype,evaluationtype>(Game,depth - 1,-beta,-maxValue)).first);
            Game.undomove(Move,state);
            if (checkvalue > maxValue) {
                maxValue = checkvalue;
                savebestmove = Move;
                if (maxValue >= beta)
                    break;
            }
        }
        return std::make_pair(maxValue,savebestmove);
    }
}
#undef move
#undef gamestate
#undef player

代码中为游戏的基类设置所需的成员函数,可能如下所示:

    template <typename evaluationtype>
class game {
    enum class _player;
    class _move;
    class _gamestate;
private:
    _player playersturn;
public:
    game() = delete;
    virtual evaluationtype evaluation() const;
    virtual std::list<_move>&& getpossiblemoves() const;
    virtual _gamestate makemove(const _move&);
    virtual void undomove(const _move&,const _gamestate&);
};

现在我正在尝试使用井字游戏实现派生类:

template <typename evaluationtype>
class TicTacToe : game<evaluationtype> {
private:
    enum class _player {
        none,cross,circle
    };
    class _move {
        unsigned x;
        unsigned y;
    };
    class _gamestate {
        std::array<std::array<_player,3>,3> field;
        _player turn = cross;
    };
    _gamestate currentfield;
public:
    TicTacToe(); 
    evaluationtype evaluation() const;
    std::list<_move>&& getpossiblemoves() const;
    _gamestate makemove(const _move&);
    void undomove(const _move&,const _gamestate&);
};

目前唯一有定义的方法一个空的构造函数。 Visual Studio 没有向我显示任何错误,但是一旦我尝试创建 TicTacToe 的实例,我就会得到以下输出

1>C:\AlphaBetaNegamax\TicTacToe.h(17,3): error C2079: 'TicTacToe<int>::_gamestate::field' uses undefined class 'std::array<std::array<TicTacToe<int>::_player,3>'
1>C:\AlphaBetaNegamax\TicTacToe.h(20): message : see reference to class template instantiation 'TicTacToe<int>::_gamestate' being compiled
1>C:\AlphaBetaNegamax\Source.cpp(3): message : see reference to class template instantiation 'TicTacToe<int>' being compiled
1>C:\AlphaBetaNegamax\TicTacToe.h(24,21): error C2555: 'TicTacToe<int>::getpossiblemoves': overriding virtual function return type differs and is not covariant from 'game<evaluationtype>::getpossiblemoves'
1>        with
1>        [
1>            evaluationtype=int
1>        ]
1>C:\AlphaBetaNegamax\Negamax_and_Gamestructure.h(17): message : see declaration of 'game<evaluationtype>::getpossiblemoves'
1>        with
1>        [
1>            evaluationtype=int
1>        ]

我知道我在继承嵌套类部分的某个地方搞砸了,我不确定在哪里。 这是我真的不应该做的事情,还是我的继承结构有问题?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)