-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.cpp
300 lines (217 loc) · 7.96 KB
/
Board.cpp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//
// Board.cpp
// Boggle_Word_Finder
//
// Created by Mudit Gurjar on 12/12/17.
// Copyright © 2017 Mudit Gurjar. All rights reserved.
//
#include "Board.hpp"
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>
#include <fstream>
// Constructor initializes board, and fills it with letters
Board::Board(int rows, int cols) : b_rows(rows), b_cols(cols), board(rows, primitive_Row(cols))
{
srand( (unsigned int)time(NULL) );
generate_vowel_indicators();
generate_letters();
}
void Board::word_finder()
{
// create dictionary from a .txt file of English words database
store_dictionary();
// recursively go to each cell, test for all possible words starting from that letter
// and store in vector 'valid_words_vec' all the correct English words found on the Board
find_all_words();
// sort the valid words vector alphabetically
std::sort(valid_words_vec.begin(), valid_words_vec.end());
// log words in a file "valid_words"
log_words();
}
// Given row and column indices, check when outside board
bool Board::is_outside_board(int row, int col)
{
if ( (row > -1) && (row < b_rows) && (col > -1) && (col < b_cols) )
return 0;
else
return 1;
}
// Before generating actual letters for the board,
// decide for each cell if it gets filled with a vowel or consonant
// 1: vowel & 2: consonant
void Board::generate_vowel_indicators()
{
int neighbor_info, neighbor_vowels, neighbor_consonants;
for (int i = 0; i < b_rows; i++) {
for (int j = 0; j < b_cols; j++) {
neighbor_info = get_neighbor_vowel_indicators(i, j);
neighbor_vowels = neighbor_info%10;
neighbor_consonants = (neighbor_info - neighbor_vowels)/10;
// if neighbor consonant/vowel difference is 0, prob(consonant)/prob(vowel) = 6/5
// if neighbor cells have how many more consonant than vowels, that difference
// skews the probability of current cell's vowel/consonant generation
int rand_int = std::rand()%11 + 1;
if (rand_int < 6 + (neighbor_consonants - neighbor_vowels)) {
(board[i][j]).set_vowel_indicator(1);
}
else
(board[i][j]).set_vowel_indicator(10);
}
}
}
int Board::get_neighbor_vowel_indicators(int row, int col)
{
int neighbor_indicators = 0;
// neighbor 1
if (!is_outside_board(row-1, col-1)) {
neighbor_indicators += (get_cell(row-1, col-1)).get_vowel_indicator();
}
// neighbor 2
if (!is_outside_board(row-1, col)) {
neighbor_indicators += (get_cell(row-1, col)).get_vowel_indicator();
}
// neighbor 3
if (!is_outside_board(row -1, col+1)) {
neighbor_indicators += (get_cell(row-1, col+1)).get_vowel_indicator();
}
// neighbor 4
if (!is_outside_board(row, col-1)) {
neighbor_indicators += (get_cell(row, col-1)).get_vowel_indicator();
}
return neighbor_indicators;
}
// Generate letters for the board
void Board::generate_letters()
{
int letter_indice;
// letter weightage based on scrabbles tile frequencies
// So (roughly) a,e,o have more probability of getting selected than i,u,y
std::string vowel_density = "eeeeaaaoooiiuy";
// Similarly probability d,g,l,n,r,s,t is more than, b,c,f,h,m,p,v,w, whic is itself more
// than probability of j,k,q,x,z to be selected
std::string consonant_density = "nnnnnrrrrrtttttllllssssdddggghhhmmmbbccffppvvwwjkqxz";
for (int i = 0; i < b_rows; i++) {
for (int j = 0; j < b_cols; j++) {
if ((get_cell(i, j)).get_vowel_indicator() == 1) {
// pick vowel
letter_indice = std::rand()%vowel_density.length();
(board[i][j]).set_letter(vowel_density[letter_indice]);
}
else {
// pick consonant
letter_indice = std::rand()%consonant_density.length();
(board[i][j]).set_letter(consonant_density[letter_indice]);
}
}
}
}
// Display the letter-only board as a boggle board for playing
void Board::display()
{
std::cout << std::endl << std::endl;
for (int i = 0; i < b_rows; i++) {
for (int j = 0; j < b_cols; j++) {
std::cout << " " << " " << (char)(toupper((get_cell(i,j)).get_letter())) << " ";
}
std::cout << std::endl << std::endl;
}
std::cout << std::endl << std::endl << std::endl;
}
// Creates a dictionary vector from a file database containing all English words
void Board::store_dictionary()
{
std::string word;
std::ifstream all_words_database;
all_words_database.open("resources/corncob_lowercase.txt");
while(std::getline(all_words_database, word) )
{
if( word[word.length()-1] == '\r' || word[word.length()-1] == '\n' )
word.resize(word.length()-1);
dictionary.push_back(word);
}
all_words_database.close();
}
void Board::find_all_words()
{
std::string word;
for (int i = 0; i < b_rows; i++) {
for (int j = 0; j < b_cols; j++) {
word = "";
extend_word(i, j, word);
}
}
}
void Board::move_to_cell_on_the_right(int current_cell_row, int current_cell_col, std::string word)
{
int step = 1;
if (word.length() <= max_word_length) {
switch (step) {
case 1:
extend_word(current_cell_row, current_cell_col, word);
case 2:
current_cell_row--;
extend_word(current_cell_row, current_cell_col, word);
case 3:
current_cell_col++;
extend_word(current_cell_row, current_cell_col, word);
case 4:
current_cell_col++;
extend_word(current_cell_row, current_cell_col, word);
case 5:
current_cell_row++;
extend_word(current_cell_row, current_cell_col, word);
case 6:
current_cell_row++;
extend_word(current_cell_row, current_cell_col, word);
case 7:
current_cell_col--;
extend_word(current_cell_row, current_cell_col, word);
case 8:
current_cell_col--;
extend_word(current_cell_row, current_cell_col, word);
}
}
}
void Board::extend_word(int current_cell_row, int current_cell_col, std::string word)
{
if( !is_outside_board(current_cell_row, current_cell_col) ) {
if ( !(board[current_cell_row][current_cell_col]).been_here() ) {
word = word + (board[current_cell_row][current_cell_col]).get_letter();
if (sequence_is_still_valid(word)) {
(board[current_cell_row][current_cell_col]).set_been_here(1);
move_to_cell_on_the_right(current_cell_row, current_cell_col - 1, word);
if (word.length() >= min_word_length) {
if (std::binary_search(dictionary.begin(), dictionary.end(), word)) {
valid_words_vec.push_back(word);
}
}
(board[current_cell_row][current_cell_col]).set_been_here(0);
}
}
}
}
bool Board::sequence_is_still_valid(std::string word)
{
std::vector<std::string>::iterator iter = std::upper_bound(dictionary.begin(), dictionary.end(), word);
std::string dic_word = *iter;
dic_word.resize(word.length());
if (dic_word == word) {
return 1;
}
else
return 0;
}
void Board::log_words()
{
// log into new file 'valid_words.txt'
std::ofstream valid_words("resources/valid_words.txt");
for (int i = 0; i < valid_words_vec.size(); i++)
{
valid_words << valid_words_vec.at(i) << '\n';
std::cout << valid_words_vec.at(i) << '\n';
}
std::cout << "Valid words = " << valid_words_vec.size() << std::endl;
valid_words.close();
}