-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoggleWindow.cpp
More file actions
201 lines (158 loc) · 5.12 KB
/
BoggleWindow.cpp
File metadata and controls
201 lines (158 loc) · 5.12 KB
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
#include "BoggleWindow.h"
#include "lexicon.h"
#include <QFile>
#include <QHBoxLayout>
#include <QTextEdit>
#include <iostream>
#include <QtDebug>
#include <QStack>
BoggleWindow::BoggleWindow(QWidget *parent)
: QMainWindow(parent)
{
this->setWindowTitle("QBoggle!");
this->setFixedSize(BOGGLE_WINDOW_WIDTH, BOGGLE_WINDOW_HEIGHT);
me = new WordListWidget(this, "Me");
computer = new WordListWidget(this, "Computer");
board = new Board(this);
console = new Console(this);
QString letters;
for(int i = 0; i < 25;++i) letters.append(board->cubes[i]->text());
ai = new Depo(letters);
me->setGeometry(20, 20, 230, 320);
board->setGeometry(250, 20, 300, 300);
computer->setGeometry(800 - 50 - 200, 20, 230, 320);
console->setGeometry(30, 360, 740, 210);
/*
for (std::string s: lex) {
std::cout << s << std::endl;
}
*/
QFile qFile(":/res/EnglishWords.txt");
if (!qFile.open(QIODevice::ReadOnly)) {
throw new std::runtime_error("Resource file not found!");
}
Lexicon temp(qFile);
this->flex = temp;
console->write("Welcome to the game Boggle!\ninput s ->shake\ninput --- ->recursion\ninput 1 space ->clear board\ninput 2 space ->clear me and computer and the board");
connect(this->console,SIGNAL(newLineWritten(QString)),this,SLOT(consoleTextChange()));
connect(this->board,SIGNAL(isWord()),this,SLOT(addWordToME()));
connect(this->ai,SIGNAL(isWord()),this,SLOT(addWordToComputer()));
}
BoggleWindow::~BoggleWindow()
{
}
/* posibilities
* 1.totally wrong -> do nothing
* 2.posible to be a word -> do nothing
* 3.the word has been added (no word after extension) -> do nothing
* 4.the word has been added (there are words after extension)
* 5.new word
*/
/*
* input"s" ->shake
* input"---" ->recursion
* input" " ->clear board
* input" " ->clear me and computer and the board
*
*/
void BoggleWindow::consoleTextChange()
{
QString temp = console->text;
if(temp == "s") this->shake();
else if (temp == "---") computerSearch();
else if (temp == " ") ALLreset();
else if(temp == " ") reset();
else {
int *p = isWord(temp);
if (p == nullptr) return;
for (int i = 0;i < 25;++i) {
this->board->cubes[i]->ifChosen = true;
this->board->cubes[i]->setStyleSheet("background-color: white; border-radius: 15px; border: 2px solid");;
}
for (int i = 0;i < temp.length();++i) {
this->board->cubes[p[i]]->setStyleSheet("background-color:#FFE4E1; border: 3px solid; border-radius: 15px");
}
this->board->currentWord = "";
this->me->addWord(temp);
this->me->addScore(temp.length()-3);
delete [] p;
}
}
bool BoggleWindow::findCube(QString letter,int *path,int pos,bool *b)
{
if(pos == letter.length()) return true;
for (int i = 0;i < 25; ++i) {
if(!b[i] && this->board->ifAdjoin(i,path[pos-1]) && letter.at(pos) == this->board->cubes[i]->getLetter().toLower()){
path[pos] = i;b[i] = true;
if(findCube(letter,path,pos+1,b)) return true;
b[i] = false;
}
}
return false;
}
int * BoggleWindow::isWord(QString text)
{
if(text.length() < 4 || !this->flex.contains(text.toStdString())) return nullptr;
text = text.toLower();
int length = text.length();
int *index = new int[length];
bool *ifChosen = new bool[25];
for (int i = 0;i < 25;++i) {
if(this->board->cubes[i]->getLetter().toLower() == text.at(0)){
index[0] = i;ifChosen[i] = true;
if(findCube(text,index,1,ifChosen)) return index;
ifChosen[i] = false;
}
}
return nullptr;
}
void BoggleWindow::addWordToME()
{
QString word = this->board->currentWord;
if(this->me->words.contains(word) || this->computer->words.contains(word)) return;
if(word.length() > 3){
this->me->addWord(word);
this->me->addScore(word.length()-3);
}
}
void BoggleWindow::addWordToComputer()
{
QString word = this->ai->currentWord;
if(this->me->words.contains(word) || this->computer->words.contains(word)) return;
if(word.length() > 3){
this->computer->addWord(word);
this->computer->addScore(word.length()-3);
}
}
void BoggleWindow::computerSearch()
{
ai->addALlWords();
}
void BoggleWindow::shake()
{
this->board->shake();
me->reset();
computer->reset();
QString letters;
for(int i = 0; i < 25;++i) letters.append(board->cubes[i]->text());
ai->setBoardLetters(letters);
}
void BoggleWindow::reset()
{
for (int i = 0;i < 25;++i) {
this->board->cubes[i]->ifChosen = false;
this->board->cubes[i]->setStyleSheet("background-color: white; border-radius: 15px; border: 2px solid");;
}
this->board->currentWord = "";
}
void BoggleWindow::ALLreset()
{
me->reset();
me->wordTable->clear();
computer->reset();
me->wordTable->clear();
for (int i = 0;i < 25;++i) {
this->board->cubes[i]->ifChosen = false;
this->board->cubes[i]->setStyleSheet("background-color: white; border-radius: 15px; border: 2px solid");;
}
}