source: trunk/doc/html/tictac-example.html

Last change on this file was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 22.7 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/tictac/tictac.doc:4 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>Tic Tac Toe</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { background: #ffffff; color: black; }
12--></style>
13</head>
14<body>
15
16<table border="0" cellpadding="0" cellspacing="0" width="100%">
17<tr bgcolor="#E5E5E5">
18<td valign=center>
19 <a href="index.html">
20<font color="#004faf">Home</font></a>
21 | <a href="classes.html">
22<font color="#004faf">All&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;Classes</font></a>
25 | <a href="annotated.html">
26<font color="#004faf">Annotated</font></a>
27 | <a href="groups.html">
28<font color="#004faf">Grouped&nbsp;Classes</font></a>
29 | <a href="functions.html">
30<font color="#004faf">Functions</font></a>
31</td>
32<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Tic Tac Toe</h1>
33
34
35<p>
36This is an implementation of the Tic-tac-toe game.
37<p> We didn't put much effort in making a clever algorithm so it's not a
38challenge to play against the computer. Instead, study the source code
39to see how you can make reusable components such as the <em>TicTacGameBoard</em>
40widget.
41<p> <hr>
42<p> Header file:
43<p> <pre>/****************************************************************************
44** $Id: tictac-example.html 2051 2007-02-21 10:04:20Z chehrlic $
45**
46** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
47**
48** This file is part of an example program for Qt. This example
49** program may be used, distributed and modified without limitation.
50**
51*****************************************************************************/
52
53#ifndef TICTAC_H
54#define TICTAC_H
55
56
57#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
58#include &lt;<a href="qptrvector-h.html">qptrvector.h</a>&gt;
59
60class QComboBox;
61class QLabel;
62
63
64// --------------------------------------------------------------------------
65// TicTacButton implements a single tic-tac-toe button
66//
67
68class TicTacButton : public <a href="qpushbutton.html">QPushButton</a>
69{
70 <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
71public:
72 TicTacButton( <a href="qwidget.html">QWidget</a> *parent );
73 enum Type { Blank, Circle, Cross };
74 Type type() const { return t; }
75 void setType( Type type ) { t = type; repaint(); }
76 <a href="qsizepolicy.html">QSizePolicy</a> sizePolicy() const
77 { return QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred ); }
78 <a href="qsize.html">QSize</a> sizeHint() const { return QSize( 32, 32 ); }
79 <a href="qsize.html">QSize</a> minimumSizeHint() const { return QSize( 10, 10 ); }
80protected:
81 void drawButtonLabel( <a href="qpainter.html">QPainter</a> * );
82private:
83 Type t;
84};
85
86// Using template vector to make vector-class of TicTacButton.
87// This vector is used by the TicTacGameBoard class defined below.
88
89typedef QPtrVector&lt;TicTacButton&gt; TicTacButtons;
90typedef QMemArray&lt;int&gt; TicTacArray;
91
92
93// --------------------------------------------------------------------------
94// TicTacGameBoard implements the tic-tac-toe game board.
95// TicTacGameBoard is a composite widget that contains N x N TicTacButtons.
96// N is specified in the constructor.
97//
98
99class TicTacGameBoard : public <a href="qwidget.html">QWidget</a>
100{
101 Q_OBJECT
102public:
103 TicTacGameBoard( int n, QWidget *parent=0, const char *name=0 );
104 ~TicTacGameBoard();
105 enum State { Init, HumansTurn, HumanWon, ComputerWon, NobodyWon };
106 State state() const { return st; }
107 void computerStarts( bool v );
108 void newGame();
109signals:
110 void finished(); // game finished
111private slots:
112 void buttonClicked();
113private:
114 void setState( State state ) { st = state; }
115 void updateButtons();
116 int checkBoard( TicTacArray * );
117 void computerMove();
118 State st;
119 int nBoard;
120 bool comp_starts;
121 TicTacArray *btArray;
122 TicTacButtons *buttons;
123};
124
125
126// --------------------------------------------------------------------------
127// TicTacToe implements the complete game.
128// TicTacToe is a composite widget that contains a TicTacGameBoard and
129// two push buttons for starting the game and quitting.
130//
131
132class TicTacToe : public <a href="qwidget.html">QWidget</a>
133{
134 Q_OBJECT
135public:
136 TicTacToe( int boardSize=3, QWidget *parent=0, const char *name=0 );
137private slots:
138 void newGameClicked();
139 void gameOver();
140private:
141 void newState();
142 <a href="qcombobox.html">QComboBox</a> *whoStarts;
143 <a href="qpushbutton.html">QPushButton</a> *newGame;
144 <a href="qpushbutton.html">QPushButton</a> *quit;
145 <a href="qlabel.html">QLabel</a> *message;
146 TicTacGameBoard *board;
147};
148
149
150#endif // TICTAC_H
151</pre>
152
153<p> <hr>
154<p> Implementation:
155<p> <pre>/****************************************************************************
156** $Id: tictac-example.html 2051 2007-02-21 10:04:20Z chehrlic $
157**
158** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
159**
160** This file is part of an example program for Qt. This example
161** program may be used, distributed and modified without limitation.
162**
163*****************************************************************************/
164
165#include "tictac.h"
166#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
167#include &lt;<a href="qpainter-h.html">qpainter.h</a>&gt;
168#include &lt;<a href="qdrawutil-h.html">qdrawutil.h</a>&gt;
169#include &lt;<a href="qcombobox-h.html">qcombobox.h</a>&gt;
170#include &lt;<a href="qcheckbox-h.html">qcheckbox.h</a>&gt;
171#include &lt;<a href="qlabel-h.html">qlabel.h</a>&gt;
172#include &lt;<a href="qlayout-h.html">qlayout.h</a>&gt;
173#include &lt;stdlib.h&gt; // rand() function
174#include &lt;<a href="qdatetime-h.html">qdatetime.h</a>&gt; // seed for rand()
175
176
177//***************************************************************************
178//* TicTacButton member functions
179//***************************************************************************
180
181// --------------------------------------------------------------------------
182// Creates a TicTacButton
183//
184
185<a name="f188"></a>TicTacButton::TicTacButton( <a href="qwidget.html">QWidget</a> *parent ) : <a href="qpushbutton.html">QPushButton</a>( parent )
186{
187 t = Blank; // initial type
188}
189
190// --------------------------------------------------------------------------
191// Paints TicTacButton
192//
193
194<a name="x31"></a>void TicTacButton::<a href="qbutton.html#drawButtonLabel">drawButtonLabel</a>( <a href="qpainter.html">QPainter</a> *p )
195{
196 <a href="qrect.html">QRect</a> r = <a href="qwidget.html#rect">rect</a>();
197 p-&gt;<a href="qpainter.html#setPen">setPen</a>( QPen( white,2 ) ); // set fat pen
198 if ( t == Circle ) {
199<a name="x36"></a><a name="x35"></a><a name="x28"></a> p-&gt;<a href="qpainter.html#drawEllipse">drawEllipse</a>( r.<a href="qrect.html#left">left</a>()+4, r.<a href="qrect.html#top">top</a>()+4, r.<a href="qrect.html#width">width</a>()-8, r.<a href="qrect.html#height">height</a>()-8 );
200 } else if ( t == Cross ) { // draw cross
201<a name="x37"></a><a name="x33"></a> p-&gt;<a href="qpainter.html#drawLine">drawLine</a>( r.<a href="qrect.html#topLeft">topLeft</a>() +QPoint(4,4), r.<a href="qrect.html#bottomRight">bottomRight</a>()-QPoint(4,4));
202<a name="x38"></a><a name="x32"></a> p-&gt;<a href="qpainter.html#drawLine">drawLine</a>( r.<a href="qrect.html#bottomLeft">bottomLeft</a>()+QPoint(4,-4),r.<a href="qrect.html#topRight">topRight</a>() -QPoint(4,-4));
203 }
204}
205
206
207//***************************************************************************
208//* TicTacGameBoard member functions
209//***************************************************************************
210
211// --------------------------------------------------------------------------
212// Creates a game board with N x N buttons and connects the "clicked()"
213// signal of all buttons to the "buttonClicked()" slot.
214//
215
216<a name="f189"></a>TicTacGameBoard::TicTacGameBoard( int n, QWidget *parent, const char *name )
217 : <a href="qwidget.html">QWidget</a>( parent, name )
218{
219 st = Init; // initial state
220 nBoard = n;
221 n *= n; // make square
222 comp_starts = FALSE; // human starts
223 buttons = new TicTacButtons(n); // create real buttons
224 btArray = new TicTacArray(n); // create button model
225 <a href="qgridlayout.html">QGridLayout</a> * grid = new <a href="qgridlayout.html">QGridLayout</a>( this, nBoard, nBoard, 4 );
226 <a href="qpalette.html">QPalette</a> p( blue );
227 for ( int i=0; i&lt;n; i++ ) { // create and connect buttons
228 TicTacButton *ttb = new TicTacButton( this );
229<a name="x45"></a> ttb-&gt;<a href="qwidget.html#setPalette">setPalette</a>( p );
230 ttb-&gt;<a href="qwidget.html#setEnabled">setEnabled</a>( FALSE );
231 <a href="qobject.html#connect">connect</a>( ttb, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), SLOT(buttonClicked()) );
232 grid-&gt;<a href="qgridlayout.html#addWidget">addWidget</a>( ttb, i%nBoard, i/nBoard );
233 buttons-&gt;insert( i, ttb );
234 btArray-&gt;at(i) = TicTacButton::Blank; // initial button type
235 }
236<a name="x40"></a> <a href="qtime.html">QTime</a> t = QTime::<a href="qtime.html#currentTime">currentTime</a>(); // set random seed
237<a name="x43"></a><a name="x42"></a><a name="x41"></a> srand( t.<a href="qtime.html#hour">hour</a>()*12+t.<a href="qtime.html#minute">minute</a>()*60+t.<a href="qtime.html#second">second</a>()*60 );
238}
239
240TicTacGameBoard::~TicTacGameBoard()
241{
242 delete buttons;
243 delete btArray;
244}
245
246
247// --------------------------------------------------------------------------
248// TicTacGameBoard::computerStarts( bool v )
249//
250// Computer starts if v=TRUE. The human starts by default.
251//
252
253void <a name="f190"></a>TicTacGameBoard::computerStarts( bool v )
254{
255 comp_starts = v;
256}
257
258
259// --------------------------------------------------------------------------
260// TicTacGameBoard::newGame()
261//
262// Clears the game board and prepares for a new game
263//
264
265void <a name="f191"></a>TicTacGameBoard::newGame()
266{
267 st = HumansTurn;
268 for ( int i=0; i&lt;nBoard*nBoard; i++ )
269 btArray-&gt;at(i) = TicTacButton::Blank;
270 if ( comp_starts )
271 computerMove();
272 else
273 updateButtons();
274}
275
276
277// --------------------------------------------------------------------------
278// TicTacGameBoard::buttonClicked() - SLOT
279//
280// This slot is activated when a TicTacButton emits the signal "clicked()",
281// i.e. the user has clicked on a TicTacButton.
282//
283
284void <a name="f192"></a>TicTacGameBoard::buttonClicked()
285{
286 if ( st != HumansTurn ) // not ready
287 return;
288 int i = buttons-&gt;findRef( (TicTacButton*)<a href="qobject.html#sender">sender</a>() );
289 TicTacButton *b = buttons-&gt;at(i); // get piece that was pressed
290 if ( b-&gt;type() == TicTacButton::Blank ) { // empty piece?
291 btArray-&gt;at(i) = TicTacButton::Circle;
292 updateButtons();
293 if ( checkBoard( btArray ) == 0 ) // not a winning move?
294 computerMove();
295 int s = checkBoard( btArray );
296 if ( s ) { // any winners yet?
297 st = s == TicTacButton::Circle ? HumanWon : ComputerWon;
298 emit finished();
299 }
300 }
301}
302
303
304// --------------------------------------------------------------------------
305// TicTacGameBoard::updateButtons()
306//
307// Updates all buttons that have changed state
308//
309
310void <a name="f193"></a>TicTacGameBoard::updateButtons()
311{
312 for ( int i=0; i&lt;nBoard*nBoard; i++ ) {
313 if ( buttons-&gt;at(i)-&gt;type() != btArray-&gt;at(i) )
314 buttons-&gt;at(i)-&gt;setType( (TicTacButton::Type)btArray-&gt;at(i) );
315 buttons-&gt;at(i)-&gt;setEnabled( buttons-&gt;at(i)-&gt;type() ==
316 TicTacButton::Blank );
317 }
318}
319
320
321// --------------------------------------------------------------------------
322// TicTacGameBoard::checkBoard()
323//
324// Checks if one of the players won the game, works for any board size.
325//
326// Returns:
327// - TicTacButton::Cross if the player with X buttons won
328// - TicTacButton::Circle if the player with O buttons won
329// - Zero (0) if there is no winner yet
330//
331
332int <a name="f194"></a>TicTacGameBoard::checkBoard( TicTacArray *a )
333{
334 int t = 0;
335 int row, col;
336 bool won = FALSE;
337 for ( row=0; row&lt;nBoard &amp;&amp; !won; row++ ) { // check horizontal
338 t = a-&gt;at(row*nBoard);
339 if ( t == TicTacButton::Blank )
340 continue;
341 col = 1;
342 while ( col&lt;nBoard &amp;&amp; a-&gt;at(row*nBoard+col) == t )
343 col++;
344 if ( col == nBoard )
345 won = TRUE;
346 }
347 for ( col=0; col&lt;nBoard &amp;&amp; !won; col++ ) { // check vertical
348 t = a-&gt;at(col);
349 if ( t == TicTacButton::Blank )
350 continue;
351 row = 1;
352 while ( row&lt;nBoard &amp;&amp; a-&gt;at(row*nBoard+col) == t )
353 row++;
354 if ( row == nBoard )
355 won = TRUE;
356 }
357 if ( !won ) { // check diagonal top left
358 t = a-&gt;at(0); // to bottom right
359 if ( t != TicTacButton::Blank ) {
360 int i = 1;
361 while ( i&lt;nBoard &amp;&amp; a-&gt;at(i*nBoard+i) == t )
362 i++;
363 if ( i == nBoard )
364 won = TRUE;
365 }
366 }
367 if ( !won ) { // check diagonal bottom left
368 int j = nBoard-1; // to top right
369 int i = 0;
370 t = a-&gt;at(i+j*nBoard);
371 if ( t != TicTacButton::Blank ) {
372 i++; j--;
373 while ( i&lt;nBoard &amp;&amp; a-&gt;at(i+j*nBoard) == t ) {
374 i++; j--;
375 }
376 if ( i == nBoard )
377 won = TRUE;
378 }
379 }
380 if ( !won ) // no winner
381 t = 0;
382 return t;
383}
384
385
386// --------------------------------------------------------------------------
387// TicTacGameBoard::computerMove()
388//
389// Puts a piece on the game board. Very, very simple.
390//
391
392void <a name="f195"></a>TicTacGameBoard::computerMove()
393{
394 int numButtons = nBoard*nBoard;
395 int *altv = new int[numButtons]; // buttons alternatives
396 int altc = 0;
397 int stopHuman = -1;
398 TicTacArray a = btArray-&gt;copy();
399 int i;
400 for ( i=0; i&lt;numButtons; i++ ) { // try all positions
401 if ( a[i] != TicTacButton::Blank ) // already a piece there
402 continue;
403 a[i] = TicTacButton::Cross; // test if computer wins
404 if ( checkBoard(&amp;a) == a[i] ) { // computer will win
405 st = ComputerWon;
406 stopHuman = -1;
407 break;
408 }
409 a[i] = TicTacButton::Circle; // test if human wins
410 if ( checkBoard(&amp;a) == a[i] ) { // oops...
411 stopHuman = i; // remember position
412 a[i] = TicTacButton::Blank; // restore button
413 continue; // computer still might win
414 }
415 a[i] = TicTacButton::Blank; // restore button
416 altv[altc++] = i; // remember alternative
417 }
418 if ( stopHuman &gt;= 0 ) // must stop human from winning
419 a[stopHuman] = TicTacButton::Cross;
420 else if ( i == numButtons ) { // tried all alternatives
421 if ( altc &gt; 0 ) // set random piece
422 a[altv[rand()%(altc--)]] = TicTacButton::Cross;
423 if ( altc == 0 ) { // no more blanks
424 st = NobodyWon;
425 emit finished();
426 }
427 }
428 *btArray = a; // update model
429 updateButtons(); // update buttons
430 delete[] altv;
431}
432
433
434//***************************************************************************
435//* TicTacToe member functions
436//***************************************************************************
437
438// --------------------------------------------------------------------------
439// Creates a game widget with a game board and two push buttons, and connects
440// signals of child widgets to slots.
441//
442
443<a name="f196"></a>TicTacToe::TicTacToe( int boardSize, QWidget *parent, const char *name )
444 : <a href="qwidget.html">QWidget</a>( parent, name )
445{
446 <a href="qvboxlayout.html">QVBoxLayout</a> * l = new <a href="qvboxlayout.html">QVBoxLayout</a>( this, 6 );
447
448 // Create a message label
449
450 message = new <a href="qlabel.html">QLabel</a>( this );
451<a name="x24"></a> message-&gt;<a href="qframe.html#setFrameStyle">setFrameStyle</a>( QFrame::WinPanel | QFrame::Sunken );
452 message-&gt;<a href="qlabel.html#setAlignment">setAlignment</a>( AlignCenter );
453 l-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( message );
454
455 // Create the game board and connect the signal finished() to this
456 // gameOver() slot
457
458 board = new TicTacGameBoard( boardSize, this );
459 <a href="qobject.html#connect">connect</a>( board, SIGNAL(finished()), SLOT(gameOver()) );
460 l-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( board );
461
462 // Create a horizontal frame line
463
464 <a href="qframe.html">QFrame</a> *line = new <a href="qframe.html">QFrame</a>( this );
465 line-&gt;<a href="qframe.html#setFrameStyle">setFrameStyle</a>( QFrame::HLine | QFrame::Sunken );
466 l-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( line );
467
468 // Create the combo box for deciding who should start, and
469 // connect its clicked() signals to the buttonClicked() slot
470
471 whoStarts = new <a href="qcombobox.html">QComboBox</a>( this );
472<a name="x23"></a> whoStarts-&gt;<a href="qcombobox.html#insertItem">insertItem</a>( "Computer starts" );
473 whoStarts-&gt;<a href="qcombobox.html#insertItem">insertItem</a>( "Human starts" );
474 l-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( whoStarts );
475
476 // Create the push buttons and connect their clicked() signals
477 // to this right slots.
478
479 newGame = new <a href="qpushbutton.html">QPushButton</a>( "Play!", this );
480 <a href="qobject.html#connect">connect</a>( newGame, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), SLOT(newGameClicked()) );
481 quit = new <a href="qpushbutton.html">QPushButton</a>( "Quit", this );
482 <a href="qobject.html#connect">connect</a>( quit, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), qApp, SLOT(<a href="qapplication.html#quit">quit</a>()) );
483 <a href="qhboxlayout.html">QHBoxLayout</a> * b = new <a href="qhboxlayout.html">QHBoxLayout</a>;
484<a name="x19"></a> l-&gt;<a href="qboxlayout.html#addLayout">addLayout</a>( b );
485 b-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( newGame );
486 b-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( quit );
487
488 newState();
489}
490
491
492// --------------------------------------------------------------------------
493// TicTacToe::newGameClicked() - SLOT
494//
495// This slot is activated when the new game button is clicked.
496//
497
498void <a name="f197"></a>TicTacToe::newGameClicked()
499{
500<a name="x22"></a> board-&gt;computerStarts( whoStarts-&gt;<a href="qcombobox.html#currentItem">currentItem</a>() == 0 );
501 board-&gt;newGame();
502 newState();
503}
504
505
506// --------------------------------------------------------------------------
507// TicTacToe::gameOver() - SLOT
508//
509// This slot is activated when the TicTacGameBoard emits the signal
510// "finished()", i.e. when a player has won or when it is a draw.
511//
512
513void <a name="f198"></a>TicTacToe::gameOver()
514{
515 newState(); // update text box
516}
517
518
519// --------------------------------------------------------------------------
520// Updates the message to reflect a new state.
521//
522
523void <a name="f199"></a>TicTacToe::newState()
524{
525 static const char *msg[] = { // TicTacGameBoard::State texts
526 "Click Play to start", "Make your move",
527 "You won!", "Computer won!", "It's a draw" };
528 message-&gt;<a href="qlabel.html#setText">setText</a>( msg[board-&gt;state()] );
529 return;
530}
531</pre>
532
533<p> <hr>
534<p> Main:
535<p> <pre>/****************************************************************************
536** $Id: tictac-example.html 2051 2007-02-21 10:04:20Z chehrlic $
537**
538** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
539**
540** This file is part of an example program for Qt. This example
541** program may be used, distributed and modified without limitation.
542**
543*****************************************************************************/
544
545#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
546#include &lt;stdlib.h&gt;
547#include "tictac.h"
548
549
550int main( int argc, char **argv )
551{
552 <a href="qapplication.html">QApplication</a> a( argc, argv );
553 int n = 3;
554 if ( argc == 2 ) // get board size n
555 n = atoi(argv[1]);
556 if ( n &lt; 3 || n &gt; 10 ) { // out of range
557 <a href="qapplication.html#qWarning">qWarning</a>( "%s: Board size must be from 3x3 to 10x10", argv[0] );
558 return 1;
559 }
560 TicTacToe ttt( n ); // create game
561 a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;ttt );
562 ttt.<a href="qwidget.html#setCaption">setCaption</a>("Qt Example - TicTac");
563 ttt.<a href="qwidget.html#show">show</a>(); // show widget
564 return a.<a href="qapplication.html#exec">exec</a>(); // go
565}
566</pre>
567
568<p>See also <a href="examples.html">Examples</a>.
569
570<!-- eof -->
571<p><address><hr><div align=center>
572<table width=100% cellspacing=0 border=0><tr>
573<td>Copyright &copy; 2007
574<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
575<td align=right><div align=right>Qt 3.3.8</div>
576</table></div></address></body>
577</html>
Note: See TracBrowser for help on using the repository browser.