int main(int argc, const char * argv[]) { clock_t t_start = clock(); ifstream cubes_file; string cube[64]; int cube_count = 0; vector<int> num_in_cube; if (argc!=3) { cout << "Needs exactly 2 arguments" << endl; return 0; } Boggle boggle(argv[2]); cubes_file.open(argv[1]); //tests dictionary on each cube. while(next_cube_line(cubes_file, cube)){ cube_count++; boggle.set_cube(cube); num_in_cube.push_back(boggle.test_dictionary()); } for(vector<int>::const_iterator it = num_in_cube.begin(); it != num_in_cube.end(); ++it) cout << *it << endl; cout << "scored " << cube_count << " cubes with dimension 4 in " << ((float)(clock()-t_start))/CLOCKS_PER_SEC << " seconds." << endl; return 0; }
/*--------------------------------------------------------------* * Main program * *--------------------------------------------------------------*/ int main(void) { char line[256]; char letters[256]; char name[40]; FILE *file; int i; printf("Boggle square size is %d lines of %d characters\n", YMAX, XMAX); strcpy(letters, ""); for (i=0; i<YMAX; i++) { printf("Enter line %d :", i+1); gets(line); if (strlen(line) != XMAX) { fprintf(stderr, "Wrong length of line\n"); exit(1); } strcat(letters, line); } makemap(letters); file = fopen("apet", "r"); if (file == NULL) { fprintf(stderr, "Could not open %s\n", name); exit(1); } for (; fgets(line, 256, file); ) { if (boggle(line, NONE, 0) != EXIT) { printf("-- %s", line); } } fclose(file); /* for (i=0; i<MAXTEST; i++) { if (boggle(test[i], NONE, 0) == EXIT) { printf("-- %s\n", test[i]); } } */ return 0; }
/*--------------------------------------------------------------* * recursively look for a word * *--------------------------------------------------------------*/ int boggle(char *word, /* word to look for */ int pos, /* current position in array */ bits used) /* positions visited already */ { int c, p; while (c = *word++, ! isalpha(c)) { /* ignore punctuation */ if (c == '\0') return EXIT; /* end of string, ok! */ } p = first[toupper(c) - 'A']; /* first square containing letter */ if (p == NONE) return FAIL; /* letter not in square */ for (; p != NONE; p=next[p]) { if (pos == NONE || isset(move[pos],p)) { /* can move to square */ if (! isset(used,p)) { /* not already used */ switch (boggle(word, p, adbit(used,p))) { /* recurse on remainder */ case FAIL: return FAIL; /* later letter impossible */ case EXIT: return EXIT; /* succeeded */ } } } } return REDO; /* no luck here */ }