过滤MUD游戏中的坏词

时间:2014-12-01 18:15:18

标签: c++ validation mud

在我的C ++ MUD游戏中,我希望能够消除玩家输入诅咒词。你们都知道它们是什么,不需要例子。所以我尝试过这样的事情:

vector<string> vulger = { "You know what goes in here");

void Player::SendString(const std::string& p_string)
    {
for (vector<string>::iterator it = vulger.begin(); it != vulger.end(); ++it)
    {
        if (!p_string.compare(*it) || !p_string.find(*it))
        {
            //Testing what words are been neglected by the if statement.
            //cout << *it;
                Conn()->Protocol().SendString(*Conn(), p_string + newline);
        }
    }

    }

但所有这一切都是通过发送到网络的字符串循环。包括公告。

任何人都可以看到我做错了或建议什么吗?

2 个答案:

答案 0 :(得分:0)

||声明中的if更改为&&

    void Player::SendString(const std::string& p_string)
    {
      for (vector<string>::iterator it = vulger.begin(); it != vulger.end(); ++it)
      {
#if 0
// Original:
        if (!p_name.compare(*it) && !p_name.find(*it))
        {
          Conn()->Protocol().SendString(*Conn(), p_string + newline);
        }
        else
        {
          cout << "Vulgar word found: " << *it << "\n";
        }
#else
// Edit 1:  Changed ordering.
        if ((p_name == *it) || (p_name.find(*it) != std::string::npos))
        {
          cout << "Vulgar word found: " << *it << "\n";
        }
        else
        {
          Conn()->Protocol().SendString(*Conn(), p_string + newline);
        }
#endif

      }
    }

答案 1 :(得分:0)

您正在将std::string::find的返回值视为bool,实际上,如果找不到npos的特殊魔法值,它会在字符串中返回一个偏移量

if (my_string.find("foo") == std::npos) {
   // foo is NOT in my_string
} else {
   // foo is in my_string
}