Boggle

C++ code posted by Tensai
created at 07 Nov 00:39

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <list>
#include <map>
#include <cctype>
#include <math.h>
// #include "../print.h"

using namespace std;

string toLower(string w){
  for(char& l : w){
    l = tolower(l);
  }
  return w;
}

void create_dictionary(char* dict_file, set<string>& dictionary){
  string w;
  ifstream in;

  in.open(dict_file);
  while (in >> w){
    if (w.size() > 3){
      dictionary.insert(toLower(w));
    }
  }
  in.close();
}

int find_board_size(char* board_file){
  ifstream in;
  string w;
  int count = 0;

  in.open(board_file);
  while (in >> w){
    count++;
  }
  in.close();

  int size = sqrt(count);

  return size;
}

void make_board(char* board_file, vector<vector<string>>& board){
  int board_size = find_board_size(board_file);

  string w;
  ifstream in;
  
  in.open(board_file);

  vector<string> row;
  int pos = 0;
  while(in >> w){
    pos++;
    row.push_back(toLower(w));
    if (pos % board_size == 0){
      board.push_back(row);
      row.clear();
    }
  }  

  in.close();
}

void print_results(vector<vector<string>> board, set<string> found_words, char* out_file){   
  ofstream out;
  out.open(out_file);
  for (int i = 0; i < board.size(); i++){
    for (int j = 0; j < board[i].size(); j++){
      out << board[i][j] << " ";
    }
    out << endl;
  }

  set<string>::iterator it = found_words.begin();
  out << endl;
  for (it; it != found_words.end(); it++){
    out << *it << endl;
  }
  out.close();
}

bool validate(int x, int y, string word_so_far, vector<vector<string>>& board, set<string>& dictionary, vector<vector<bool>>& exists, set<string>::iterator closest_word){

  // crops the closest word to be same length as word_so_far
  string sub_closest_word = *closest_word;
  sub_closest_word = sub_closest_word.substr(0, word_so_far.size());

  // if word_so_far has no matches in dictionary
  if (word_so_far != sub_closest_word){
    return false;
  }
  // if it is off the board, stop
  if (x < 0 || x > board.size()-1 || y < 0 || y > board.size()-1){
    return false;
  }
  // if it has been used, stop
  if (exists[x][y] == true){
    // cout << "letter already used" << endl;
    return false;
  }

  return true;
}

void find(int x, int y, string word_so_far, vector<vector<string>>& board, set<string>& dictionary, set<string>& found_words, vector<vector<bool>>& exists){
  set<string>::iterator closest_word = dictionary.lower_bound(word_so_far);
  set<string>::iterator end = dictionary.end();

  // checks if passes word is beyond the last word of the set
  if (closest_word == end){
    return;
  }

  // adds word to found_words set if a match in the dictionary is found
  if (word_so_far == *closest_word){
    found_words.insert(word_so_far);
  }

  if (!validate(x, y, word_so_far, board, dictionary, exists, closest_word)){
    return;
  }

  exists[x][y] = true;
  word_so_far += board[x][y];

  find(x-1, y-1, word_so_far, board, dictionary, found_words, exists);  // top left
  find(x, y-1, word_so_far, board, dictionary, found_words, exists);    // top middle 
  find(x+1, y-1, word_so_far, board, dictionary, found_words, exists);  // top right
  find(x-1, y, word_so_far, board, dictionary, found_words, exists);    // left middle
  find(x+1, y, word_so_far, board, dictionary, found_words, exists);    // right middle
  find(x-1, y+1, word_so_far, board, dictionary, found_words, exists);  // bottom left
  find(x, y+1, word_so_far, board, dictionary, found_words, exists);    // bottom middle
  find(x+1, y+1, word_so_far, board, dictionary, found_words, exists);  // bottom right

  exists[x][y] = false;

  return;
}

int main(int argc, char* argv[]){
  set<string> dictionary;
  vector<vector<string>> board;
  set<string> found_words;

  create_dictionary(argv[1], dictionary);
  make_board(argv[2], board);

  vector<vector<bool>> exists;
  for (int i = 0; i < board.size(); i++){
    vector<bool> row;
    for (int j = 0; j < board.size(); j++){
      row.push_back(false);
    }
    exists.push_back(row);
  }

  cout << endl;

  for (int col = 0; col < board.size(); col++){
    for (int row = 0; row < board.size(); row++){
      find(col, row, "", board, dictionary, found_words, exists);
    }
  }

  print_results(board, found_words, argv[3]);

  return 0;
}
4.42 KB in 6 ms with coderay