简体   繁体   English

在匹配游戏逻辑的图块中使用STL算法

[英]Use of STL algorithms in a tile matching game logic

I have made a tile matching game. 我做了一个瓷砖配对游戏。 It has a Board object that holds a vector of card objects and delegates events to them. 它具有一个Board对象,该对象保存一个纸牌对象向量并将事件委托给它们。 Board event handling has following code in it: 董事会事件处理中包含以下代码:

// Counting logic-driving card states
int cardFaceUpCounter = 0;
std::vector<Card*> faceUpCards(2);

// Checking for logic-driving card states 
for (auto& card : this->boardMatrix)
{
    if (this->isCardInAnim(&card))
    {
        return;
    }
    if (this->isCardFaceUp(&card))
    {
        ++cardFaceUpCounter;
        faceUpCards[cardFaceUpCounter - 1] = &card;
    }       
}

I've just finished studying Beautiful C++ by Kate Gregory on Pluralsight. 我刚刚完成了Pluralsight上的Kate Gregory的Beautiful C ++学习。 She argues that we should avoid writing loops, and should use the STL algorithm header as much as possible. 她认为,我们应该避免编写循环,而应尽可能使用STL算法头。 I find her argument and approach very compelling, thus I have tried to refactor my latest pet project to reflect her teachings. 我发现她的论点和方法非常有说服力,因此我尝试重构我最新的宠物项目以反映她的教s。

The example above is where I just can't see how I can use STL algorithms to both communicate the intent better and keep the performance - single loop instead of two or three loops, albeit hidden in the algorithm calls. 上面的示例使我看不到如何使用STL算法来更好地传达意图并保持性能-尽管隐藏在算法调用中,但使用单循环而不是两个或三个循环。

The second question would be if single loop efficiency can't be achieved using STL algorithms would you still prefer that approach for readability sake. 第二个问题是,如果使用STL算法不能达到单循环效率,那么出于可读性的考虑,您还是会喜欢这种方法。

Here is an example of what I was thinking. 这是我在想的一个例子。

int cardFaceUpCounter = 0;
std::vector<Card*> faceUpCards(2);

if (std::any_of(boardMatrix.begin(), boardMatrix.end(), [&](auto& card) {
    if (isCardFaceUp(&card))
        faceUpCards[cardFaceUpCounter++] = &card;

    return isCardInAnim(&card);    
})) return;

With range-v3 , it would be something like: 使用range-v3 ,它将类似于:

std::vector<Card*> faceUpCards = this->boardMatrix
    | ranges::view::take_while([this](const auto& card){ return !isCardInAnim(&card);})
    | ranges::view::filter([this](const auto& card){ return isCardFaceUp(&card); })
    | ranges::view::transform([](auto& e){ return &e; });

With only STL, I would do something like: 仅使用STL,我会做类似的事情:

auto it = std::find_if(boardMatrix.begin(), boardMatrix.end(),
                       [this](const auto& card){ return isCardInAnim(&card);});
std::vector<Card*> faceUpCards(std::distance(boardMatrix.begin(), it), nullptr);
std::transform(boardMatrix.begin(), it,
               faceUpCards.begin(),
               [](auto& card){ return &card;});
faceUpCards.erase(std::remove_if(faceUpCards.begin(), faceUpCards.end(),
                                [this](const auto& card){ return !isCardFaceUp(&card); }),
                  faceUpCards.end());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM