C ++
J'ai écrit celui-ci aujourd'hui. Ce n'est pas le moyen le plus efficace et ne génère pas toujours les recherches de mots les plus aléatoires, mais il fait le travail et le fait relativement rapidement.
Bonus: Supporte aussi les Palindromes !!!
Cela fonctionne en prenant une entrée pour le mot et la taille de la recherche par mot. Il génère ensuite des infurateurs en déposant des lettres, en insérant des lettres ou en retournant des lettres. Il ajoute ensuite ceux-ci à la grille ainsi que le mot correct. Il vérifie ensuite toutes les occurrences de la première lettre dans toutes les directions pour le mot. Si 1 instance n'est pas trouvée (2 pour les palindromes), le cycle est brutalement forcé. Il envoie ensuite le mot de recherche à la console ainsi qu’un fichier.
La voici à 213 lignes de code avec des espaces et des commentaires.
#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;
}
Je suis loin d'être un expert en C ++, donc je suis sûr qu'il y a des endroits où ce code pourrait être amélioré, mais j'étais satisfait de la manière dont il a abouti.
Voici les sorties.
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
Je peux le mettre à jour pour générer des recherches de mots légèrement plus "aléatoires".