/[projects]/queensgui/src/queensmain.cpp
ViewVC logotype

Contents of /queensgui/src/queensmain.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 15 - (show annotations) (download)
Wed Jul 25 19:39:37 2007 UTC (16 years, 9 months ago) by torben
File size: 9648 byte(s)
Corrected the totalSolutions() of all container classes


1 /***************************************************************************
2 * Copyright (C) 2005 by Torben Nielsen *
3 * torben@t-hoerup.dk *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #include <qapplication.h>
21 #include <qlabel.h>
22 #include <qlayout.h>
23 #include <qlistbox.h>
24 #include <qpopupmenu.h>
25 #include <qpushbutton.h>
26 #include <qspinbox.h>
27 #include <qstring.h>
28
29
30
31 #include "queensmain.h"
32 #include "board.h"
33 #include "queens.h"
34
35 #include "solution.h"
36 #include "solutionmatrix.h"
37 #include "solutionint.h"
38
39 #include "containervector.h"
40 #include "containerlist.h"
41 #include "containerhash.h"
42 #include "containermnvector.h"
43 #include "containerminimalvector.h"
44
45 #include "config.h"
46
47
48
49 QueensMain::QueensMain(QWidget *parent, const char *name)
50 : QDialog(parent, name)
51 {
52 QVBoxLayout *mainlayout = new QVBoxLayout( this );
53
54 m_board = new Board( this );
55
56 m_start = new QPushButton("Start", this);
57 m_quit = new QPushButton("Quit", this);
58 m_stop = new QPushButton("Stop", this);
59 m_stop->setEnabled( false );
60
61 QHBoxLayout *upperlayout = new QHBoxLayout( mainlayout );
62 QVBoxLayout *left = new QVBoxLayout( upperlayout );
63
64 upperlayout->add( m_board );
65 m_list = new QListBox( this );
66
67 m_list->setMinimumWidth( 180 );
68 m_sizeSelector = new QSpinBox( this );
69 m_status = new QLabel( this );
70
71 left->add( m_list );
72 left->add( m_sizeSelector );
73
74 QHBoxLayout *buttons = new QHBoxLayout( mainlayout );
75 buttons->add( m_start );
76 buttons->add( m_stop );
77 buttons->add( m_quit );
78
79 mainlayout->add( m_status );
80
81 m_sizeSelector->setMinValue( MIN_SIZE );
82 m_sizeSelector->setMaxValue( MAX_SIZE );
83 m_sizeSelector->setValue( 8 );
84 m_board->setSize( 8 );
85
86 m_storage = StorageInt;
87 m_sortalgo = SortList;
88 m_solutions = NULL;
89 m_queens = NULL;
90 m_sol = NULL;
91
92 connect(m_quit,
93 SIGNAL( clicked() ),
94 qApp,
95 SLOT( quit() )
96 );
97
98 connect(m_sizeSelector,
99 SIGNAL( valueChanged(int) ),
100 this,
101 SLOT( resize(int) )
102 );
103
104 connect(m_start,
105 SIGNAL( clicked() ),
106 this,
107 SLOT( start() )
108 );
109
110 connect(m_stop,
111 SIGNAL( clicked() ),
112 this,
113 SLOT( stop() )
114 );
115
116 connect(m_list,
117 SIGNAL( selectionChanged() ),
118 this,
119 SLOT( showSolution() )
120 );
121
122 connect( qApp,
123 SIGNAL( lastWindowClosed() ),
124 qApp,
125 SLOT( quit() )
126 );
127 }
128
129
130 QueensMain::~QueensMain()
131 {
132 delete m_board;
133
134 if (m_solutions != NULL)
135 delete m_solutions;
136 if (m_sol != NULL)
137 delete m_sol;
138 }
139
140 void QueensMain::start()
141 {
142 m_sizeSelector->setEnabled( false );
143 m_start->setEnabled( false );
144 m_stop->setEnabled( true );
145 m_list->clear();
146 m_status->setText( QString("Searching ...") );
147
148 m_board->setMatrix(0);
149
150 if (m_queens != NULL) {
151 m_queens->wait();
152 delete m_queens;
153 m_queens = 0;
154 }
155
156 if (m_solutions != NULL)
157 delete m_solutions;
158 switch (m_sortalgo) {
159 case SortList:
160 m_solutions = new ContainerList(this);
161 break;
162 case SortVector:
163 m_solutions = new ContainerVector(this);
164 break;
165 case SortHash:
166 m_solutions = new ContainerHash(this);
167 break;
168 case SortMNVector:
169 m_solutions = new ContainerMNVector(this);
170 break;
171 case SortMinimalVector:
172 m_solutions = new ContainerMinimalVector(this);
173 break;
174 }
175
176 if (m_sol != NULL)
177 delete m_sol;
178
179 switch(this->m_storage) {
180 case StorageInt:
181 m_sol = new SolutionInt(m_sizeSelector->value());
182 break;
183 case StorageMatrix:
184 m_sol = new SolutionMatrix(m_sizeSelector->value());
185 break;
186 }
187
188
189 m_elapsed.start();
190 m_time.start();
191 m_queens = new Queens(this, m_sol, m_solutions, m_sizeSelector->value() ,false);
192 m_queens->start();
193 }
194
195 void QueensMain::stop()
196 {
197 m_solutions->halt();
198 m_queens->stop();
199 m_queens->wait();
200
201 int num = m_solutions->numSolutions();
202 m_status->setText( QString("Aborted. Found ") + QString::number(num,10).append(" solutions") );
203 m_list->clear();
204 for (int i=1; i<=num; i++)
205 m_list->insertItem( QString("Solution no ") + QString::number(i,10), i);
206
207 delete m_queens;
208 m_queens = NULL;
209
210 m_list->setEnabled( true );
211 m_sizeSelector->setEnabled( true );
212 m_start->setEnabled( true );
213 m_stop->setEnabled( false );
214 }
215
216 void QueensMain::foundSolution()
217 {
218 int num = m_solutions->numSolutions();
219 if (m_elapsed.elapsed() > 500)
220 {
221 m_status->setText( QString("Searching ... found ") + QString::number(num,10).append(" solutions") );
222 m_elapsed.restart();
223 }
224 }
225
226 void QueensMain::finishedSearch()
227 {
228 uniqueSolutions();
229 m_sizeSelector->setEnabled( true );
230 m_start->setEnabled( true );
231 m_stop->setEnabled( false );
232 }
233
234
235 void QueensMain::showSolution()
236 {
237 int index = m_list->currentItem();
238 Solution* sol = m_solutions->solution( index );
239 m_board->setMatrix( sol );
240 }
241
242 void QueensMain::resize(int size)
243 {
244 m_board->setSize( size );
245 m_board->repaint( true );
246 m_list->clear();
247 }
248
249 QString QueensMain::elapsed()
250 {
251 int time = m_time.elapsed();
252 int msec = time % 1000;
253 int sec = time / 1000;
254 int min = sec / 60;
255 sec %= 60;
256 QString smsec = (QString::number(msec)).rightJustify(3, '0');
257 QString ssec = (QString::number(sec)).rightJustify(2,'0');
258
259 QString res;
260 if (min!=0)
261 res = QString("%1:%2.%3").arg(min).arg(ssec).arg(smsec);
262 else
263 res = QString("%1.%2").arg(sec).arg(smsec);
264 return res;
265 }
266
267 void QueensMain::uniqueSolutions()
268 {
269 m_totalcount = m_solutions->totalSolutions();
270 m_status->setText( QString("sorting ") + QString::number(m_totalcount,10).append(" solutions...") );
271
272 m_elapsed.restart();
273 m_solutions->uniqueSolutions();
274
275 int uniq = m_solutions->numSolutions();
276
277
278 QString msg;
279 msg = QString( "Found %1 unique solutions, of %2 total solutions. Time elapsed: %3" ).arg(uniq).arg(m_totalcount).arg( elapsed() );
280
281 m_list->clear();
282 for (int i=1; i<=uniq; i++)
283 m_list->insertItem( QString("Unique solution no ") + QString::number(i,10), i);
284
285 m_status->setText( msg );
286 }
287
288 void QueensMain::contextMenuEvent( QContextMenuEvent *event)
289 {
290 QPopupMenu *contextMenu = new QPopupMenu( this );
291 contextMenu->setCheckable( true );
292 QLabel *sortCaption = new QLabel("<b><i>Container class</i></b>", this);
293 sortCaption->setAlignment( Qt::AlignCenter );
294 contextMenu->insertItem( sortCaption );
295
296 contextMenu->insertItem("List", this, SLOT( sortList() ), 0, 1);
297 contextMenu->insertItem("Vector", this, SLOT( sortVector() ), 0, 2);
298 contextMenu->insertItem("Hash", this, SLOT( sortHash() ), 0, 3);
299 contextMenu->insertItem("M*N Vector", this, SLOT(sortMNVector()), 0, 4);
300 contextMenu->insertItem("Minimal Vector", this, SLOT(sortMinimalVector()), 0, 5);
301
302 QLabel *storageCaption = new QLabel("<b><i>Solution class</i></b>", this);
303 storageCaption->setAlignment( Qt::AlignCenter );
304 contextMenu->insertItem( storageCaption );
305 contextMenu->insertItem("Matrix Solution", this, SLOT( storageMatrix() ), 0, 6);
306 contextMenu->insertItem("Int Solution", this, SLOT( storageInt() ), 0, 7);
307
308
309 switch (m_sortalgo) {
310 case SortList:
311 contextMenu->setItemChecked( 1, true);
312 break;
313 case SortVector:
314 contextMenu->setItemChecked( 2, true);
315 break;
316 case SortHash:
317 contextMenu->setItemChecked( 3, true);
318 break;
319 case SortMNVector:
320 contextMenu->setItemChecked( 4, true);
321 break;
322 case SortMinimalVector:
323 contextMenu->setItemChecked( 5, true);
324 break;
325 }
326
327 switch (m_storage) {
328 case StorageMatrix:
329 contextMenu->setItemChecked(6, true);
330 break;
331 case StorageInt:
332 contextMenu->setItemChecked(7, true);
333 break;
334 }
335
336
337 contextMenu->exec( event->globalPos() );
338 delete contextMenu;
339 }
340
341 void QueensMain::duplicateRemoved()
342 {
343 if (m_elapsed.elapsed() > 500)
344 {
345 int uniq = m_solutions->getUniqueRemoved();
346 QString status;
347 status = QString ("Sorting %1 solutions. So far identified %2 duplicates").arg(m_totalcount).arg(uniq);
348 m_status->setText(status);
349 m_elapsed.restart();
350 }
351 }
352
353 void QueensMain::sortList()
354 {
355 m_sortalgo = SortList;
356 }
357
358 void QueensMain::sortVector()
359 {
360 m_sortalgo = SortVector;
361 }
362
363 void QueensMain::sortHash()
364 {
365 m_sortalgo = SortHash;
366 }
367
368 void QueensMain::sortMNVector()
369 {
370 m_sortalgo = SortMNVector;
371 }
372
373 void QueensMain::sortMinimalVector()
374 {
375 m_sortalgo = SortMinimalVector;
376 }
377
378
379 void QueensMain::storageInt()
380 {
381 m_storage = StorageInt;
382 }
383
384 void QueensMain::storageMatrix()
385 {
386 m_storage = StorageMatrix;
387 }

Properties

Name Value
svn:eol-style native
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.20