C ++
Saya menulis ini hari ini. Ini bukan cara yang paling efisien dan tidak selalu menghasilkan pencarian kata yang paling acak, tetapi menyelesaikan pekerjaan dan relatif cepat.
Bonus: Mendukung Palindrom juga !!!
Ini bekerja dengan mengambil input untuk kata dan ukuran pencarian kata. Ini kemudian menghasilkan infurator dengan menjatuhkan huruf, memasukkan huruf, atau membalikkan huruf. Itu kemudian menambahkan mereka ke grid serta kata yang benar. Itu kemudian memeriksa semua contoh dari huruf pertama di setiap arah untuk kata. Jika 1 instance tidak ditemukan (2 untuk palindrom), ia akan memaksa siklus untuk berakhir. Ini kemudian menampilkan pencarian kata ke konsol serta file.
Ini dia di 213 baris kode dengan spasi dan komentar.
#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;
}
Saya jauh dari pakar C ++ jadi saya yakin ada tempat-tempat di mana kode ini dapat ditingkatkan tetapi saya senang dengan bagaimana itu berakhir.
Inilah hasilnya.
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
Saya dapat memperbarui ini untuk menghasilkan sedikit lebih banyak pencarian kata yang tampak "acak".