C ++
我今天写了这个。它不是最有效的方法,并不总是生成看起来最随意的单词搜索,但是它可以完成工作,并且相对较快。
奖励:也支持回文!!!
通过输入单词和单词搜索的大小来工作。然后,通过删除字母,插入字母或翻转字母来生成补充符。然后,将这些以及正确的单词添加到网格中。然后,它将检查该单词在各个方向上第一个字母的所有实例。如果未找到1个实例(对于回文式,则为2个实例),则它将强制执行该循环。然后,它将单词搜索输出到控制台以及文件。
此处是213行带有空格和注释的代码。
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <time.h>
// Keep Track of Coordinates
struct XY {
public:
int X;
int Y;
};
// Just in case someone breaks the rules
std::string ToUppercase( const std::string& pWord ) {
char *myArray = new char[ pWord.size() + 1 ];
myArray[ pWord.size() ] = 0;
memcpy( myArray, pWord.c_str(), pWord.size() );
for ( unsigned int i = 0; i < pWord.size(); i++ ) {
if ( (int) myArray[ i ] >= 97 && (int) myArray[ i ] <= 122 ) {
myArray[ i ] = (char) ( (int) myArray[ i ] - 32 );
}
}
return std::string( myArray );
}
// Random number between a max and min inclusively
int RandomBetween( int pMin, int pMax ) {
return rand() % ( pMax - pMin + 1 ) + pMin;
}
// Find all instances of a character
std::vector<XY> FindHotspots( const std::vector<char> &pWordSearch, char pFirstChar, unsigned int pLength ) {
std::vector<XY> myPoints;
for ( unsigned int i = 0; i < pLength; i++ ) {
for ( unsigned int j = 0; j < pLength; j++ ) {
if ( pWordSearch[ i * pLength + j ] == pFirstChar ) {
XY myXY;
myXY.X = i;
myXY.Y = j;
myPoints.push_back( myXY );
}
}
}
return myPoints;
}
// Searchs each index from specific point in certain direction for word
// True if word is found
bool Found( const std::vector<char> &pWordSearch, const std::string &pWord, int pRow, int pCol, int pX, int pY, int pLength ) {
for ( unsigned int i = 0; i < pWord.length(); i++ ) {
if ( pRow < 0 || pCol < 0 || pRow > pLength - 1 || pCol > pLength - 1 )
return false;
if ( pWord[ i ] != pWordSearch[ pRow * pLength + pCol ] )
return false;
pRow += pX;
pCol += pY;
}
return true;
}
// Goes through all the hotspots and searchs all 8 directions for the word
int FindSolution( const std::vector<char> &pWordSearch, const std::string &pWord, unsigned int pLength ) {
std::vector<XY> myHotspots = FindHotspots( pWordSearch, pWord[ 0 ], pLength );
int mySolutions = 0;
for ( unsigned int i = 0; i < myHotspots.size(); i++ ) {
if ( Found( pWordSearch, pWord, myHotspots[ i ].X, myHotspots[ i ].Y, 1, 0, pLength ) )
mySolutions++;
if ( Found( pWordSearch, pWord, myHotspots[ i ].X, myHotspots[ i ].Y, 1, 1, pLength ) )
mySolutions++;
if ( Found( pWordSearch, pWord, myHotspots[ i ].X, myHotspots[ i ].Y, 0, 1, pLength ) )
mySolutions++;
if ( Found( pWordSearch, pWord, myHotspots[ i ].X, myHotspots[ i ].Y, -1, 1, pLength ) )
mySolutions++;
if ( Found( pWordSearch, pWord, myHotspots[ i ].X, myHotspots[ i ].Y, -1, 0, pLength ) )
mySolutions++;
if ( Found( pWordSearch, pWord, myHotspots[ i ].X, myHotspots[ i ].Y, -1, -1, pLength ) )
mySolutions++;
if ( Found( pWordSearch, pWord, myHotspots[ i ].X, myHotspots[ i ].Y, 0, -1, pLength ) )
mySolutions++;
if ( Found( pWordSearch, pWord, myHotspots[ i ].X, myHotspots[ i ].Y, 1, -1, pLength ) )
mySolutions++;
}
return mySolutions;
}
// Generate words that are similar
//
// 1. Word with last character same as first
// 2. Word with 1 character removed
// 3. Word with 1 character added
std::vector<std::string> GenerateInfurators( const std::string &pWord ) {
std::vector<std::string> myInfurators;
for ( unsigned int i = 0; i < pWord.size() / 2; i++ ) {
char myReplace = '0';
do {
myReplace = pWord[ RandomBetween( 0, pWord.size() - 1 ) ];
} while ( myReplace == pWord[ pWord.size() - 1 ] );
myInfurators.push_back( pWord.substr( 0, pWord.size() - 2 ) + myReplace );
}
for ( unsigned int i = 1; i < pWord.size() - 2; i++ ) {
myInfurators.push_back( pWord.substr( 0, i ) + pWord.substr( i + 1 ) );
std::string myWord = pWord;
myInfurators.push_back( myWord.insert( i, 1, pWord[ i - 1 ] ) );
}
return myInfurators;
}
// Adds word in random position in word search
void AddWordRandomly( std::vector<char> &pWordSearch, const std::string &pWord, unsigned int pLength ) {
int myXDirec = 0;
int myYDirec = 0;
do {
myXDirec = RandomBetween( -1, 1 );
myYDirec = RandomBetween( -1, 1 );
} while ( myXDirec == 0 && myYDirec == 0 );
int myRow = 0;
if ( myXDirec == 0 ) {
myRow = RandomBetween( 0, pLength - 1 );
} else if ( myXDirec > 0 ) {
myRow = RandomBetween( 0, pLength - pWord.size() - 1 );
} else {
myRow = RandomBetween( pWord.size(), pLength - 1 );
}
int myCol = 0;
if ( myYDirec == 0 ) {
myCol = RandomBetween( 0, pLength - 1 );
} else if ( myYDirec > 0 ) {
myCol = RandomBetween( 0, pLength - pWord.size() - 1 );
} else {
myCol = RandomBetween( pWord.size(), pLength - 1 );
}
for ( unsigned int i = 0; i < pWord.size(); i++ ) {
pWordSearch[ myRow * pLength + myCol ] = pWord[ i ];
myRow += myXDirec;
myCol += myYDirec;
}
}
// Checks for palindromes
bool WordIsPalindrome( const std::string &pWord ) {
for ( unsigned int i = 0; i < pWord.size(); i++ ) {
if ( pWord[ i ] != pWord[ pWord.size() - 1 - i ] )
return false;
}
return true;
}
int main() {
// Handle all input
std::string myWord;
std::cin >> myWord;
myWord = ToUppercase( myWord );
std::string myStrLength;
std::cin >> myStrLength;
unsigned int mySideLength = std::stoi( myStrLength );
// Setup variables
// New time seed
// Generate infurators
// Add words
std::vector<char> myWordSearch;
srand( ( unsigned int ) time( 0 ) );
std::vector<std::string> myWords = GenerateInfurators( myWord );
myWords.push_back( myWord );
bool myWordIsPalindrome = WordIsPalindrome( myWord );
// Brute force words until 1 instance only
// 2 instances for palindromes
do {
for ( unsigned int i = 0; i < mySideLength * mySideLength; i++ ) {
myWordSearch.push_back( myWord[ RandomBetween( 0, myWord.size() -1 ) ] );
}
for ( unsigned int j = 0; j < 10; j++ ) {
for ( unsigned int i = 0; i < myWords.size() - 1; i++ ) {
AddWordRandomly( myWordSearch, myWords[ i ], mySideLength );
}
}
AddWordRandomly( myWordSearch, myWord, mySideLength );
} while ( ( FindSolution( myWordSearch, myWord, mySideLength ) != 1 && !myWordIsPalindrome ) ||
( FindSolution( myWordSearch, myWord, mySideLength ) != 2 && myWordIsPalindrome ) );
// Output to console && text file
std::ofstream myFile( "word_search.txt" );
for ( unsigned int i = 0; i < mySideLength; i++ ) {
for ( unsigned int j = 0; j < mySideLength; j++ ) {
myFile << myWordSearch[ i * mySideLength + j ] << "";
std::cout << myWordSearch[ i * mySideLength + j ] << " ";
}
myFile << "\n";
std::cout << "\n" << std::endl;
}
myFile.close();
system( "pause" );
return 0;
}
我距离C ++专家还很远,所以我敢肯定有些地方可以改进此代码,但我对它的最终结果感到满意。
这是输出。
BANANA-12:
BBBBBABANABB
BBANAAANANBB
BABAANABBABA
BNAANAAANABN
BANNNNNANBBB
AANABNNANNAA
NAANANAAAABA
ANNNBAANNNBN
ABABNAANABNA
BNNANNAAANNB
BBABAAANBBAB
BBANANNBBABB
ELEMENT-14:
MLELEMEEETNEET
EMTTELTEETELEE
EMNNELEENLNTEL
TLTNEEMEEELMTM
TLEMLNLMEEEETE
TTEEEEEENLNTLE
NETNENEEMTTMEN
ELELTETEEMNMEE
MTEEMTNEEEENEM
ELENEEEMMENNME
ELLEELELTMLETL
ETLTNEMEEELELE
EELTLLLLLMNEEE
EEEELEMNTLEEEE
ABRACADABRA-22:
ACRABAACADABBADBRRAAAA
BBABAABAARBAAAARBAABAA
RARRARBAAABRRRRAAABBRA
DABARRARABBBBABABARABR
BRBACABBAAAABAARRABDAD
ABRRCBDADRARABAACABACR
DCAAARAABCABRCAAAARAAA
AAABACCDCCRACCDARBADBA
CAARAAAAAACAAABBAACARA
ADRABCDBCARBRRRDAABAAR
RARBADAARRAADADAABAAAB
BBRRABAABAAADACACRBRAA
ARBBBDBRADCACCACARBABD
CAARARAAAACACCDARARAAA
ARABADACRARCDADABADBBA
RARAADRRABADBADAABBAAA
RAAAABBBARRDCAAAAAARRA
BABBAAACRDBABCABBBRAAR
AARARAARABRAAARBRRRAAB
BAAAARBRARARACAARAAAAA
ADCBBABRBCBDBRARAARBAA
AARBADAAAARACADABRAABB
Bonus: RACECAR-14:
RRACEARCECARAR
RRRRRRRRRCARAR
RARRAACECARAAA
CCACECRREAECAR
RECEAEEAAECEAE
RCRRREACCCCEAR
RRRARCERREERRR
CCARAAAAECCARC
ECECARRCCECCRR
CARRRACECCARAC
ACRACCAAACCCAR
ARRECRARRAAERR
RRCRACECARRCRR
RRRRACECRARRRR
我可能会对此进行更新,以生成看起来更加“随机”的单词搜索。