Word game
-
Hi, Im trying to make a word game. But im kinda stuck with how to hussle the letters. This is the code i got so far: char letters[] = {'a', 'v', 'e', 'd', 'k', 'u', 'r', 'n', 'e'}; for(char * c = letters; c < &letters[sizeof(letters)]; c++) { } Im trying to search all possible letter combinations starting with a word containing 2 letters and max 9. After that, i'll compare the words it found with the dictionary so that the user knows if the word exist or not. But comparing wont be a problem. The problem right now is to search all possible word combinations. Anyone got some kind of an algorithm for this? Thanks in advance!
-
Hi, Im trying to make a word game. But im kinda stuck with how to hussle the letters. This is the code i got so far: char letters[] = {'a', 'v', 'e', 'd', 'k', 'u', 'r', 'n', 'e'}; for(char * c = letters; c < &letters[sizeof(letters)]; c++) { } Im trying to search all possible letter combinations starting with a word containing 2 letters and max 9. After that, i'll compare the words it found with the dictionary so that the user knows if the word exist or not. But comparing wont be a problem. The problem right now is to search all possible word combinations. Anyone got some kind of an algorithm for this? Thanks in advance!
You can use recursion, for instance. You could also have a look at STLs and some algorithms for handling strings, combination, permutations,... As a side note,
Yustme wrote:
c < &letters[sizeof(letters)]
this is freaking code.
http://www.readytogiveup.com/[^] - Do something special today.
-
You can use recursion, for instance. You could also have a look at STLs and some algorithms for handling strings, combination, permutations,... As a side note,
Yustme wrote:
c < &letters[sizeof(letters)]
this is freaking code.
http://www.readytogiveup.com/[^] - Do something special today.
-
Hi, Im trying to make a word game. But im kinda stuck with how to hussle the letters. This is the code i got so far: char letters[] = {'a', 'v', 'e', 'd', 'k', 'u', 'r', 'n', 'e'}; for(char * c = letters; c < &letters[sizeof(letters)]; c++) { } Im trying to search all possible letter combinations starting with a word containing 2 letters and max 9. After that, i'll compare the words it found with the dictionary so that the user knows if the word exist or not. But comparing wont be a problem. The problem right now is to search all possible word combinations. Anyone got some kind of an algorithm for this? Thanks in advance!
This takes me back. :) I have uploaded jumble.zip, which is a word game much like you describe. The code is hideous, and only covers words of 4 - 7 chars in length. The basic idea of the game is to unscramble letters to find the word. For example, if you entered
jumble eraop
it would output "opera". I'm sure you could improve the code 1000%, but it's fast because the dictionary is split into files of specific word lengths. If you had a decent dictionary, it would be trivial to do the same, for words of 2 - 9. Let me know how it turns out.
Best wishes, Hans
[CodeProject Forum Guidelines] [How To Ask A Question] [My Articles]
-
Hi, I found this: c < &letters[sizeof(letters)] , on the net, was looking what it would give me, since array's dont have .size() method.
-
Hi, Im trying to make a word game. But im kinda stuck with how to hussle the letters. This is the code i got so far: char letters[] = {'a', 'v', 'e', 'd', 'k', 'u', 'r', 'n', 'e'}; for(char * c = letters; c < &letters[sizeof(letters)]; c++) { } Im trying to search all possible letter combinations starting with a word containing 2 letters and max 9. After that, i'll compare the words it found with the dictionary so that the user knows if the word exist or not. But comparing wont be a problem. The problem right now is to search all possible word combinations. Anyone got some kind of an algorithm for this? Thanks in advance!
Hi, I found an algorithm which was used for an int array. I modified it so i can use it for my char array. It works good but not as how i wanted it to work. This is how it looks like right now:
void permute(char *v, const int start, const int n); void print(const char *v, const int size); int main(int argc, char* argv[]) { char v[] = {'a', 'v', 'e', 'd', 'k', 'u', 'r', 'n', 'e'}; permute(v, 0, sizeof(v)/sizeof(char)); system("pause"); return 0; } void print(const char *v, const int size) { if (v != 0) { for (int i = 0; i < size; i++) { cout << v[i]; } cout << endl; } } // print void permute(char *v, const int start, const int n) { if (start == n-1) print(v, n); else { for (int i = start; i < n; i++) { char tmp = v[i]; v[i] = v[start]; v[start] = tmp; permute(v, start+1, n); v[start] = v[i]; v[i] = tmp; } } }
It's making letter combinations with only all letters. It should actually start with 2 letters and then continue until all 9 letter combinations are found. -
Here a better way, IMHO, to retrieve the size of an array:
char array[] = {1,2,3};
int size = sizeof(array)/sizeof(array[0]);http://www.readytogiveup.com/[^] - Do something special today.
-
This takes me back. :) I have uploaded jumble.zip, which is a word game much like you describe. The code is hideous, and only covers words of 4 - 7 chars in length. The basic idea of the game is to unscramble letters to find the word. For example, if you entered
jumble eraop
it would output "opera". I'm sure you could improve the code 1000%, but it's fast because the dictionary is split into files of specific word lengths. If you had a decent dictionary, it would be trivial to do the same, for words of 2 - 9. Let me know how it turns out.
Best wishes, Hans
[CodeProject Forum Guidelines] [How To Ask A Question] [My Articles]
-
Hi, Im trying to make a word game. But im kinda stuck with how to hussle the letters. This is the code i got so far: char letters[] = {'a', 'v', 'e', 'd', 'k', 'u', 'r', 'n', 'e'}; for(char * c = letters; c < &letters[sizeof(letters)]; c++) { } Im trying to search all possible letter combinations starting with a word containing 2 letters and max 9. After that, i'll compare the words it found with the dictionary so that the user knows if the word exist or not. But comparing wont be a problem. The problem right now is to search all possible word combinations. Anyone got some kind of an algorithm for this? Thanks in advance!
Why don't you do something clever like computing the hash of your letter combinations / words, but do it in a way that is independent of letter order. The hash value of the jumbled combination would match the hash value of the unjumbled word. Then you would only have to search the (hopefully) few words with matching hash values. One way you could do this would be to use a standard hash routine, but sort the letters in the word into alphabetical order prior to hashing them.
Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."