单词搜索拼图:如何搜索一组字母来查找单词?

时间:2016-12-05 20:33:04

标签: c++ arrays search

我正在尝试创建一个单词搜索益智游戏,要求用户输入他们认为在数组中的单词,如果找到该单词,则给用户一个点(总共10个点,之后会打印一条祝贺信息。)

在这段代码中,我有一个常规数组,但根据一些研究,我认为2D数组可能会更好。你的想法是什么?具体来说,我对我应该考虑采用何种搜索算法感兴趣

#include <iostream>
using namespace std;

int main() {
// your code goes here

  cout<<"=================================================================================="<<endl; 
cout<<"||                                                                              ||"<<endl;
cout<<"||                        Welcome to the ECE 114 Game Design!                   ||"<<endl;
cout<<"||                                                                              ||"<<endl;
cout<<"=================================================================================="<<endl;   
cout<<"This is a sample output"<<endl; 


//We want to ask the user to enter a word that they think the cross-word puzzle contains
//if the puzzle contains that word, we tell the user they are correct and update their points
//score by 1. Total of 10 points to gain, when the points score reaches 10 the user gets congratulatory message

string myArray[] = { "A B C D E F G H
                      S P A D E T R Y
                      T I G O A L E A
                      T R A I N E A W
                      O A P B E A T N  "};
//The first array above will just be a visual for the player to see

//This second array is the one that I will use to search for words

 char wordBank[5][8] = [[ 'A', 'B ','C', 'D', 'E', 'F', 'G', 'H'],  
                    ['S','P', 'A', 'D', 'E', 'T', 'R', 'Y'], 
                    ['T', 'I', 'G', 'O', 'A', 'L', 'L', 'E', 'A'], 
                    ['T', 'R','A', 'I', 'N', 'E', 'A', 'W'], 
                    ['O', 'A', 'P', 'B', 'E', 'A', 'T', 'N']];

cout<<myArray<<endl;





return 0;
}

1 个答案:

答案 0 :(得分:0)

我建议您添加一些额外的变量以使搜索更容易。例如,在垂直和水平方向上创建文本的字符串。

static const std::string puzzle_text[] =
{
  "ABCDEFGH",
  "SPADETRY",
  "TIGOALEA",
  "TRAINEAW",
  "OAPBEATN"
};

int main(void)
{
  std::string vertical_texts[8];
  for (unsigned int column = 0; column < 8; ++column)
  {
    for (unsigned int row = 0; row < 5; ++row)
    {
      vertical_text[column] += puzzle_text[row].at(column);
    }
  }
  return 0;
}

通过为垂直字母创建字符串,可以使用std::string::find。每个拼图只需要创建一次垂直字符串。

OP的任务:反向搜索(参见std::string方法)和对角线创建。

编辑1:搜索算法
这是我用来搜索这些谜题的算法:
1.提取关键词的第一个字母 2.在文本行中搜索字母 3.如果找到了信件,请检查关键字第二个字母的相邻字母 3.1。如果找到第二个字母,继续指示并搜索剩余的关键字。

这对我有好处,特别是对于难以找到的单词。

相关问题