我正在研究一个项目(拼字游戏中的AI),并且试图从地图中检索单词。我需要比较“手”中是否有足够的字母来构建单词。比较之后,我想将该单词(可播放)添加到向量中。
void compareBotHand(std::vector<std::string> npcHand) //npcHand contains 7 random letters from the english alphabet
{
std::vector<std::string> botVec; //vector used to insert the playable words
int matcher; //check if letters in "hand" and letters in word match
std::map<std::string,int>::iterator it = fR_existing_words.begin();
while (it != fR_existing_words.end())
{
std::string fWord = it->first;
matcher = 0;
for (int i = 0; i < fWord.length(); i++)
{
std::string s(1,fWord[i]); //convert char into string
for (int j = 0; j < npcHand.size(); j++)
{
if (npcHand[j] == fWord) //there are 1 letter words in the map
{
botVec.push_back(fWord);
}
else if (npcHand[j] == s)
{
matcher++;
}
}
if (matcher == fWord.length()) //if the number of letters in hand are the same has in word,they match
{
botVec.push_back(fWord);
}
}
it++;
}
std::cout << "Playable Words: " << std::endl;
for (int c = 0; c < botVec.size(); c++)
{
std::cout << botVec[c] << std::endl;
}
}