source: liacs/pm/qt-nim/mainForm.cc@ 50

Last change on this file since 50 was 2, checked in by Rick van der Zwet, 15 years ago

Initial import of data of old repository ('data') worth keeping (e.g. tracking
means of URL access statistics)

  • Property svn:executable set to *
File size: 15.1 KB
RevLine 
[2]1/*programeeropdracht 4 The Nim Game
2 *Pascal de Vos (ID = 0446916)
3 *Rick van der Zwet (ID = 0433373)
4 *file: mainForm.cc
5 *mainForm.cc, het hoofdprogramma hierin worden alle aanroepen behandeld
6 *en wordt het menu opgebouwd
7 */
8
9#include "mainForm.h"
10#include "optionsForm.h"
11#include "stapelGroupBox.h"
12#include "nimSpel.h"
13#include "const.h"
14
15#include <qaction.h>
16#include <qapplication.h>
17#include <qcheckbox.h>
18#include <qframe.h>
19#include <qlabel.h>
20#include <qlcdnumber.h>
21#include <qmenubar.h>
22#include <qmessagebox.h>
23#include <qpopupmenu.h>
24#include <qpushbutton.h>
25#include <qprogressbar.h>
26#include <qradiobutton.h>
27#include <qslider.h>
28#include <qstatusbar.h>
29
30
31MainForm::MainForm( ) //constructor
32 : QMainWindow( 0, 0, WDestructiveClose)
33{
34 //global variablen standard value
35 for(int i = 0; i < MAX_STAPELS; i++) { stapelBox[i] = NULL; }
36 stapelAantal = -1;
37 stokjesMin = 0;
38 stokjesMax = 0;
39 niveauComputer = 0;
40 spelData = new NimSpel();
41 //end standard value
42
43 setCaption( tr( "The Nim Game" ) );
44
45 //menubar
46 QAction *fileNewAction;
47 QAction *fileQuitAction;
48 fileNewAction = new QAction(
49 "New Game",
50 "&New", CTRL+Key_N,this, "new");
51 connect( fileNewAction, SIGNAL( activated() ),
52 this, SLOT( fileNew() ) );
53 fileQuitAction = new QAction(
54 "Quit",
55 "&Quit", CTRL+Key_Q, this, "quit" );
56 connect( fileQuitAction, SIGNAL( activated() ),
57 this, SLOT( fileQuit() ) );
58
59 fileMenu = new QPopupMenu( this );
60 menuBar()->insertItem( "&File", fileMenu );
61 fileNewAction->addTo( fileMenu );
62 fileQuitAction->addTo ( fileMenu );
63
64 QAction *helpAboutAction;
65 QAction *helpAboutQtAction;
66 helpAboutAction = new QAction(
67 "&About",
68 "&About", CTRL+Key_H,this, "about");
69 connect( helpAboutAction, SIGNAL( activated() ),
70 this, SLOT( helpAbout() ) );
71 helpAboutQtAction = new QAction(
72 "AboutQt",
73 "AboutQt", CTRL+Key_A,this, "aboutQt");
74 connect( helpAboutQtAction, SIGNAL( activated() ),
75 this, SLOT( helpAboutQt() ) );
76
77 helpMenu = new QPopupMenu( this );
78 menuBar()->insertItem( "&Help", helpMenu );
79 helpAboutAction->addTo ( helpMenu );
80 helpAboutQtAction->addTo ( helpMenu );
81 //end menubar
82
83 //winnend
84 winnendFrame = new QFrame( this, "winnendFrame" );
85 winnendFrame->setEnabled( TRUE );
86 winnendFrame->setGeometry( QRect( 11, 31, 234, 138 ) );
87 winnendFrame->setFrameShape( QFrame::StyledPanel );
88 winnendFrame->setFrameShadow( QFrame::Plain );
89
90 winnendAantalLCDNumber = new QLCDNumber( winnendFrame, "winnendAantalLCDNumber" );
91 winnendAantalLCDNumber->setGeometry( QRect( 110, 80, 60, 41 ) );
92 winnendAantalLCDNumber->setNumDigits( 2 );
93 winnendAantalLCDNumber->setProperty( "intValue", 0 );
94
95 winnendStapelLCDNumber = new QLCDNumber( winnendFrame, "winnendStapelLCDNumber" );
96 winnendStapelLCDNumber->setGeometry( QRect( 10, 80, 61, 41 ) );
97 winnendStapelLCDNumber->setNumDigits( 2 );
98
99 winnendStapelTextLabel = new QLabel( winnendFrame, "winnendStapelTextLabel" );
100 winnendStapelTextLabel->setGeometry( QRect( 20, 50, 52, 20 ) );
101 winnendStapelTextLabel->setText( tr( "<h3>Stapel</h3>" ) );
102
103 winnendAantalTextLabel = new QLabel( winnendFrame, "winnendAantalTextLabel" );
104 winnendAantalTextLabel->setGeometry( QRect( 120, 50, 52, 20 ) );
105 winnendAantalTextLabel->setText( tr( "<h3>Aantal</h3>" ) );
106
107 winnendCheckBox = new QCheckBox( winnendFrame, "winnendCheckBox" );
108 winnendCheckBox->setGeometry( QRect( 10, 20, 170, 20 ) );
109 winnendCheckBox->setText( tr( "\"Winnende\" zet laten zien" ) );
110
111 winnendEnable( 0 ); //het disable van alle blokjes, begin waarde
112 //end winnend
113
114 //compDialog
115 compDialogFrame = new QFrame( this, "compDialogFrame" );
116 compDialogFrame->setEnabled( TRUE );
117 compDialogFrame->setGeometry( QRect( 270, 31, 180, 138 ) );
118 compDialogFrame->setFrameShape( QFrame::StyledPanel );
119 compDialogFrame->setFrameShadow( QFrame::Plain );
120 compDialogFrame->hide();
121
122 compDialogStapelLCDNumber = new QLCDNumber( compDialogFrame, "compDialogStapelLCDNumber" );
123 compDialogStapelLCDNumber->setGeometry( QRect( 10, 50, 61, 41 ) );
124 compDialogStapelLCDNumber->setNumDigits( 2 );
125
126 compDialogStapelTextLabel = new QLabel( compDialogFrame, "compDialogStapelTextLabel" );
127 compDialogStapelTextLabel->setGeometry( QRect( 20, 20, 52, 20 ) );
128 compDialogStapelTextLabel->setText( tr( "<h3>Stapel</h3>" ) );
129
130 compDialogAantalLCDNumber = new QLCDNumber( compDialogFrame, "compDialogAantalLCDNumber" );
131 compDialogAantalLCDNumber->setGeometry( QRect( 100, 50, 60, 41 ) );
132 compDialogAantalLCDNumber->setNumDigits( 2 );
133 compDialogAantalLCDNumber->setProperty( "intValue", 0 );
134
135 compDialogAantalTextLabel = new QLabel( compDialogFrame, "compDialogAantalTextLabel" );
136 compDialogAantalTextLabel->setGeometry( QRect( 110, 20, 52, 20 ) );
137 compDialogAantalTextLabel->setText( tr( "<h3>Aantal</h3>" ) );
138
139 compDialogPushButton = new QPushButton( compDialogFrame, "compDialogPushButton" );
140 compDialogPushButton->setGeometry( QRect( 11, 100, 160, 31 ) );
141 compDialogPushButton->setText( tr( "OK, verwerk computerzet" ) );
142 //end compDialog
143
144 //terugzetten
145 terugzettenFrame = new QFrame( this, "terugzettenFrame" );
146 terugzettenFrame->setGeometry( QRect( 480, 31, 234, 138 ) );
147 terugzettenFrame->setFrameShape( QFrame::StyledPanel );
148 terugzettenFrame->setFrameShadow( QFrame::Plain );
149
150 terugzettenProgressBar = new QProgressBar( terugzettenFrame, "terugzettenProgressBar" );
151 terugzettenProgressBar->setGeometry( QRect( 20, 80, 190, 22 ) );
152 terugzettenProgressBar->setTotalSteps( 1 );
153 terugzettenProgressBar->setIndicatorFollowsStyle( TRUE );
154
155 terugzettenLCDNumber = new QLCDNumber( terugzettenFrame, "terugzettenLCDNumber" );
156 terugzettenLCDNumber->setGeometry( QRect( 20, 30, 190, 40 ) );
157 terugzettenLCDNumber->setNumDigits( 2 );
158 terugzettenLCDNumber->setProperty( "intValue", 0 );
159
160 terugzettenSlider = new QSlider( terugzettenFrame,
161 "terugzettenSlider" );
162 terugzettenSlider->setGeometry( QRect( 20, 10, 190, 20 ) );
163 terugzettenSlider->setOrientation( QSlider::Horizontal );
164 terugzettenSlider->setTickmarks( QSlider::NoMarks );
165 terugzettenSlider->setRange( 0, 0);
166
167 terugzettenPushButton = new QPushButton( terugzettenFrame,
168 "terugzettenPushButton" );
169 terugzettenPushButton->setGeometry( QRect( 130, 110, 100, 21 ) );
170 terugzettenPushButton->setText( tr( "&Terug Zetten" ) );
171 terugzettenPushButton->setAccel( QKeySequence( tr( "Alt+T" ) ) );
172 //end terugzetten
173
174 statusBar()->message( "Welcome to Nim");
175 resize( QSize(720, 768).expandedTo(minimumSizeHint()) );
176 clearWState( WState_Polished );
177
178 //connections
179 connect( winnendCheckBox, SIGNAL ( stateChanged(int) ),
180 this, SLOT ( winnendEnable(int) ) );
181 connect( compDialogPushButton, SIGNAL( clicked() ),
182 this, SLOT ( computerZet() ) );
183 connect( terugzettenSlider, SIGNAL( valueChanged(int) ),
184 terugzettenLCDNumber, SLOT( display(int) ) );
185 connect( terugzettenSlider, SIGNAL( valueChanged(int) ),
186 terugzettenProgressBar, SLOT( setProgress( int ) ) );
187 connect( terugzettenPushButton, SIGNAL( clicked() ),
188 this, SLOT ( terugZetten() ) );
189 this->show();
190 fileNew(); //om te zorgen dat gelijk in het begin het optiescherm
191 //wordt geopend
192} //end MainForm::MainForm
193
194
195MainForm::~MainForm() //destructor
196{
197
198} //end MainForm:~MainForm
199
200
201//SLOTS
202
203
204void MainForm::fileNew() //creer new speelveld
205{
206 OptionsForm * optionsForm = new OptionsForm( this );
207 if ( optionsForm->exec() ) {
208 clearBord( stapelAantal, 0 , 0 );
209 int stapelAantalOptions = optionsForm->stapelsSlider->value();
210 int stokjesMinOptions = 0;
211 int stokjesMaxOptions = optionsForm->stokjesSlider->value();
212 if ( optionsForm->niveau1RadioButton->isChecked() )
213 { niveauComputer = 1; }
214 else if (optionsForm->niveau2RadioButton->isChecked() )
215 { niveauComputer = 2; }
216 createBord( stapelAantalOptions, stokjesMinOptions,
217 stokjesMaxOptions );
218 }
219 delete optionsForm;
220} //end MainForm::fileNew
221
222
223void MainForm::fileQuit() //verlaat applicatie
224{
225 qApp->exit ( 0 );
226} //end MainForm::fileQuit
227
228
229void MainForm::helpAbout() //laat about venster zien
230{
231 QMessageBox::about( this, "The Nim Game -- About",
232 "Dit simpele spelletje was programeeropdracht nummer 4\n"
233 "van de Universiteit Leiden\n"
234 "\n"
235 "U kunt dit spel enkel tegen de computer spelen\n"
236 "U neemt om de beurt 1 of 2 stokjes van de stapel\n"
237 "U kunt winnen door het laatste stokje van het veld weg te nemen\n"
238 "\n"
239 "Heel veel speelsplezier,\n"
240 "Pascal de Vos, Rick van der Zwet\n"
241 );
242}
243
244
245void MainForm::helpAboutQt() //laat aboutQt venster zien
246{
247 QMessageBox::aboutQt( this, "The Nim Game -- About Qt" );
248} //end MainForm::helpAboutQt
249
250
251void MainForm::winnendEnable( int state )
252//laat winnend venster oplichten
253{
254 winnendAantalTextLabel->setEnabled( state );
255 winnendAantalLCDNumber->setEnabled( state );
256 winnendStapelTextLabel->setEnabled( state );
257 winnendStapelLCDNumber->setEnabled( state );
258 updateWinnend( );
259} //end MainForm::winnendEnable
260
261
262void MainForm::computerZet( ) //computerzet doorvoeren
263{
264 updateStapelBox( compStapelNummer, compAantalPakken, 2);
265 computerDialog( false );
266} //end MainForm::computerZet
267
268
269void MainForm::terugZetten( ) //zet x aantal zetten terug
270{
271 int tmpAantal = 0;
272 spelData->zetTerug( terugzettenSlider->value() );
273 for( int i = 0; i < stapelAantal; i++ ) {
274 tmpAantal = spelData->aantalOpStapel( i );
275 stapelBox[i]->stapelProgressBar->reset();
276 stapelBox[i]->stapelProgressBar->setProgress( tmpAantal );
277 stapelBox[i]->stapelLCDNumber->display ( tmpAantal );
278 if (tmpAantal > 0) {
279 //als 0 was waren de buttons uit, nu dus weer aan zetten
280 stapelBox[i]->stapelMin1PushButton->setEnabled ( true );
281 stapelBox[i]->stapelMin2PushButton->setEnabled ( true );
282 }
283 }
284 updateWinnend();
285 updateTerugzetten();
286} //end MainForm::terugZetten
287
288
289void MainForm::stapelBoxPressedMinButton( int stapelNummer , int aantalPakken )
290//Functie wordt aangeroepen door de knoppen op het spelveld, hier worden
291//deze gegevens verwerkt en de computerzet berekend
292{
293 updateStapelBox( stapelNummer, aantalPakken, 1 ); //eigen zet doorwerken
294
295 if ( spelData->afgelopen() != true ){
296 spelData->computerZet( niveauComputer, compStapelNummer, //computerzet uitrekenen
297 compAantalPakken );
298 if ( compStapelNummer == -1 ) {
299 for ( int i = 0; i < stapelAantal; i++ ) {
300 if ( spelData->aantalOpStapel(i) != 0 ) {
301 compAantalPakken = 1;
302 compStapelNummer = i;
303 break;
304 }
305 }
306 }
307 computerDialog( true );//computerdialog laten zien
308 }
309}
310
311//END SLOTS
312
313
314//maak het spelveld leeg, maw verwijder de stapels
315void MainForm::clearBord( int stapelAantalLocal, int stokjesMinLocal,
316 int stokjesMaxLocal )
317{
318 spelData->beginWaarden( stapelAantalLocal, stokjesMaxLocal-stokjesMinLocal );
319 stapelAantal = stapelAantalLocal;
320 stokjesMin = stokjesMinLocal;
321 stokjesMax = stokjesMaxLocal;
322
323 for(int i = 0; i < stapelAantalLocal; i++) {
324 if (not( stapelBox[i] == NULL )) {delete stapelBox[i];}
325 }
326} //end MainForm::clearBord
327
328//maak nieuw spelbord, maw maak nieuwe stapels
329void MainForm::createBord( int stapelAantalLocal, int stokjesMinLocal,
330 int stokjesMaxLocal )
331{
332 int Y = 100; //xcoord
333 int X = 11; //ycoord
334 int tmpAantal = 0; //aantal onthouden ivm met rijen
335
336 spelData->beginWaarden( stapelAantalLocal, stokjesMaxLocal-stokjesMinLocal );
337 stapelAantal = stapelAantalLocal;
338 stokjesMin = stokjesMinLocal;
339 stokjesMax = stokjesMaxLocal;
340
341 for(int i = 0; i < stapelAantalLocal; i++) {
342 if (tmpAantal == 7) {
343 X = X + 360;
344 Y = 170;
345 tmpAantal = 1;
346 }
347 else {
348 tmpAantal++;
349 Y = Y + 70;
350 }
351 stapelBox[i] = new StapelGroupBox( this, "stapelGroupBox" );
352 stapelBox[i]->setXY( X, Y );
353 stapelBox[i]->setRange( stokjesMinLocal, stokjesMaxLocal );
354 stapelBox[i]->setCaption(i);
355 connect( stapelBox[i], SIGNAL( buttonPressed(int,int) ),
356 this, SLOT( stapelBoxPressedMinButton(int,int) ) );
357 stapelBox[i]->show();
358 }
359 updateTerugzetten();
360 updateWinnend();
361 resize( QSize(720, 768).expandedTo(minimumSizeHint()) );
362 clearWState( WState_Polished );
363} //end MainForm::createBord
364
365
366void MainForm::updateWinnend( ) //werk het winnend frame bij
367{
368 int tmpStapel = 0;
369 int tmpAantal = 0;
370
371 if ( winnendAantalTextLabel->isEnabled() ) {
372 spelData->winnend( tmpStapel, tmpAantal );
373 }
374 else {
375 tmpStapel = -8;
376 tmpAantal = -8;
377 }
378 winnendStapelLCDNumber->display(tmpStapel);
379 winnendAantalLCDNumber->display(tmpAantal);
380} //end MainForm::updateWinnend
381
382
383void MainForm::updateTerugzetten( ) //werk terugzetten frame bij
384{
385 int aantalBeurten = spelData->aantalBeurten();
386 terugzettenSlider->setRange( 0, aantalBeurten );
387 terugzettenProgressBar->setTotalSteps( aantalBeurten );
388} //end MainForm::updateTerugzetten
389
390
391void MainForm::winstDialog( int spelerNummer ) {
392//laat een kleine melding zien als iemand gewonnen heeft
393 if ( spelerNummer == 1 ) {
394 statusBar()->message("Voila, u heeft gewonnen!");
395 }
396 else if ( spelerNummer == 2 ) {
397 statusBar()->message("Helaas, computer heeft gewonnen");
398 }
399} //end MainForm::gewonnen
400
401
402void MainForm::updateStapelBox(int stapelNummer, int aantalPakken,
403 int spelerNummer )
404//Aanroepen Bijwerken van LCDNumber, ProgressBar, winnend ,
405//terugzetten en spelData
406{
407 //grafisch
408 int tmpOudAantal = spelData->aantalOpStapel( stapelNummer );
409 int tmpNieuwAantal = 0;
410 if (aantalPakken > tmpOudAantal) { aantalPakken = tmpOudAantal; }
411 if (tmpOudAantal == aantalPakken)
412 {
413 tmpNieuwAantal = 0;
414 stapelBox[stapelNummer]->stapelMin1PushButton->setEnabled ( FALSE );
415 stapelBox[stapelNummer]->stapelMin2PushButton->setEnabled ( FALSE );
416 }
417 else {
418 tmpNieuwAantal = tmpOudAantal - aantalPakken;
419 }
420 stapelBox[stapelNummer]->stapelLCDNumber->display( tmpNieuwAantal );
421 stapelBox[stapelNummer]->stapelProgressBar->reset();
422 stapelBox[stapelNummer]->stapelProgressBar->setProgress( tmpNieuwAantal );
423 //spelData bijwerken
424 spelData->pakWeg( stapelNummer, aantalPakken );
425
426 updateTerugzetten();
427 if ( spelData->afgelopen() )
428 {
429 winstDialog( spelerNummer );
430 }
431 else
432 {
433 updateWinnend();
434 }
435} //end MainForm::updateStapelBox
436
437
438void MainForm::computerDialog( bool enabled ) {
439//als de computer dialog aan is, dan mag de gebruiker daar knoppen indrukken, de
440//rest is dan niet zichtbaar
441 if ( enabled ){
442 for ( int i = 0;i < stapelAantal; i++ ) {
443 stapelBox[i]->stapelMin1PushButton->hide ( );
444 stapelBox[i]->stapelMin2PushButton->hide ( );
445 }
446 terugzettenPushButton->hide ( );
447 compDialogStapelLCDNumber->display( compStapelNummer );
448 compDialogAantalLCDNumber->display( compAantalPakken );
449 compDialogFrame->show( );
450 }
451 else {
452 for ( int i = 0;i < stapelAantal; i++ ) {
453 stapelBox[i]->stapelMin1PushButton->show ( );
454 stapelBox[i]->stapelMin2PushButton->show ( );
455 }
456 terugzettenPushButton->show ( );
457 compDialogFrame->hide( );
458 }
459} //end MainForm::computerDialog
460
461
462
463
464
Note: See TracBrowser for help on using the repository browser.