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
296
297
298
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/life/life.doc:4 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Conway's Game of Life</title>
<style type="text/css"><!--
fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
<a href="index.html">
<font color="#004faf">Home</font></a>
| <a href="classes.html">
<font color="#004faf">All Classes</font></a>
| <a href="mainclasses.html">
<font color="#004faf">Main Classes</font></a>
| <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
| <a href="groups.html">
<font color="#004faf">Grouped Classes</font></a>
| <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Conway's Game of Life</h1>
<p>
<hr>
<p> Header file:
<p> <pre>/****************************************************************************
** $Id: qt/life.h 3.3.8 edited Jan 11 14:37 $
**
** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
**
** This file is part of an example program for TQt. This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
#ifndef LIFE_H
#define LIFE_H
#include <<a href="qframe-h.html">ntqframe.h</a>>
class LifeWidget : public <a href="ntqframe.html">TQFrame</a>
{
<a href="metaobjects.html#TQ_OBJECT">TQ_OBJECT</a>
public:
LifeWidget( int s = 10, TQWidget *parent = 0, const char *name = 0 );
void setPoint( int i, int j );
int maxCol() { return maxi; }
int maxRow() { return maxj; }
public slots:
void nextGeneration();
void clear();
protected:
virtual void paintEvent( <a href="qpaintevent.html">TQPaintEvent</a> * );
virtual void mouseMoveEvent( <a href="qmouseevent.html">TQMouseEvent</a> * );
virtual void mousePressEvent( <a href="qmouseevent.html">TQMouseEvent</a> * );
virtual void resizeEvent( <a href="qresizeevent.html">TQResizeEvent</a> * );
void mouseHandle( const <a href="ntqpoint.html">TQPoint</a> &pos );
private:
enum { MAXSIZE = 50, MINSIZE = 10, BORDER = 5 };
bool cells[2][MAXSIZE + 2][MAXSIZE + 2];
int current;
int maxi, maxj;
int pos2index( int x )
{
return ( x - BORDER ) / SCALE + 1;
}
int index2pos( int i )
{
return ( i - 1 ) * SCALE + BORDER;
}
int SCALE;
};
#endif // LIFE_H
</pre>
<p> <hr>
<p> Implementation:
<p> <pre>/****************************************************************************
** $Id: qt/life.cpp 3.3.8 edited Jan 11 14:37 $
**
** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
**
** This file is part of an example program for TQt. This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
#include "life.h"
#include <<a href="qpainter-h.html">ntqpainter.h</a>>
#include <<a href="qdrawutil-h.html">ntqdrawutil.h</a>>
#include <<a href="qcheckbox-h.html">ntqcheckbox.h</a>>
#include <<a href="qevent-h.html">ntqevent.h</a>>
#include <<a href="qapplication-h.html">ntqapplication.h</a>>
// The main game of life widget
<a name="f517"></a>LifeWidget::LifeWidget( int s, TQWidget *parent, const char *name )
: <a href="ntqframe.html">TQFrame</a>( parent, name )
{
SCALE = s;
maxi = maxj = 50;
<a href="ntqwidget.html#setMinimumSize">setMinimumSize</a>( MINSIZE * SCALE + 2 * BORDER,
MINSIZE * SCALE + 2 * BORDER );
<a href="ntqwidget.html#setMaximumSize">setMaximumSize</a>( MAXSIZE * SCALE + 2 * BORDER,
MAXSIZE * SCALE + 2 * BORDER );
<a href="ntqwidget.html#setSizeIncrement">setSizeIncrement</a>( SCALE, SCALE);
clear();
<a href="ntqwidget.html#resize">resize</a>( maxi * SCALE + 2 * BORDER , maxj * SCALE + 2 * BORDER );
}
void <a name="f518"></a>LifeWidget::clear()
{
current = 0;
for ( int t = 0; t < 2; t++ )
for ( int i = 0; i < MAXSIZE + 2; i++ )
for ( int j = 0; j < MAXSIZE + 2; j++ )
cells[t][i][j] = FALSE;
<a href="ntqwidget.html#repaint">repaint</a>();
}
// We assume that the size will never be beyond the maximum size set
// this is not in general TRUE, but in practice it's good enough for
// this program
<a name="x1889"></a>void LifeWidget::<a href="ntqframe.html#resizeEvent">resizeEvent</a>( <a href="qresizeevent.html">TQResizeEvent</a> * e )
{
<a name="x1895"></a> maxi = (e-><a href="qresizeevent.html#size">size</a>().width() - 2 * BORDER) / SCALE;
maxj = (e-><a href="qresizeevent.html#size">size</a>().height() - 2 * BORDER) / SCALE;
}
void <a name="f519"></a>LifeWidget::setPoint( int i, int j )
{
if ( i < 1 || i > maxi || j < 1 || j > maxi )
return;
cells[current][i][j] = TRUE;
<a href="ntqwidget.html#repaint">repaint</a>( index2pos(i), index2pos(j), SCALE, SCALE, FALSE );
}
void <a name="f520"></a>LifeWidget::mouseHandle( const <a href="ntqpoint.html">TQPoint</a> &pos )
{
<a name="x1893"></a> int i = pos2index( pos.<a href="ntqpoint.html#x">x</a>() );
<a name="x1894"></a> int j = pos2index( pos.<a href="ntqpoint.html#y">y</a>() );
setPoint( i, j );
}
<a name="x1896"></a>void LifeWidget::<a href="ntqwidget.html#mouseMoveEvent">mouseMoveEvent</a>( <a href="qmouseevent.html">TQMouseEvent</a> *e )
{
<a name="x1891"></a> mouseHandle( e-><a href="qmouseevent.html#pos">pos</a>() );
}
<a name="x1897"></a>void LifeWidget::<a href="ntqwidget.html#mousePressEvent">mousePressEvent</a>( <a href="qmouseevent.html">TQMouseEvent</a> *e )
{
<a name="x1890"></a> if ( e-><a href="qmouseevent.html#button">button</a>() == TQMouseEvent::LeftButton )
mouseHandle( e-><a href="qmouseevent.html#pos">pos</a>() );
}
void <a name="f521"></a>LifeWidget::nextGeneration()
{
for ( int i = 1; i <= MAXSIZE; i++ ) {
for ( int j = 1; j <= MAXSIZE; j++ ) {
int t = cells[current][i - 1][j - 1]
+ cells[current][i - 1][j]
+ cells[current][i - 1][j + 1]
+ cells[current][i][j - 1]
+ cells[current][i][j + 1]
+ cells[current][i + 1][j - 1]
+ cells[current][i + 1][j]
+ cells[current][i + 1][j + 1];
cells[!current][i][j] = ( t == 3 ||
t == 2 && cells[current][i][j] );
}
}
current = !current;
<a href="ntqwidget.html#repaint">repaint</a>( FALSE ); // repaint without erase
}
<a name="x1888"></a>void LifeWidget::<a href="ntqframe.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">TQPaintEvent</a> * e )
{
<a name="x1892"></a> int starti = pos2index( e-><a href="qpaintevent.html#rect">rect</a>().left() );
int stopi = pos2index( e-><a href="qpaintevent.html#rect">rect</a>().right() );
int startj = pos2index( e-><a href="qpaintevent.html#rect">rect</a>().top() );
int stopj = pos2index( e-><a href="qpaintevent.html#rect">rect</a>().bottom() );
if (stopi > maxi)
stopi = maxi;
if (stopj > maxj)
stopj = maxj;
<a href="ntqpainter.html">TQPainter</a> paint( this );
for ( int i = starti; i <= stopi; i++ ) {
for ( int j = startj; j <= stopj; j++ ) {
if ( cells[current][i][j] )
<a href="ntqpainter.html#qDrawShadePanel">qDrawShadePanel</a>( &paint, index2pos( i ), index2pos( j ),
SCALE - 1, SCALE - 1, colorGroup() );
else if ( cells[!current][i][j] )
<a href="ntqwidget.html#erase">erase</a>(index2pos( i ), index2pos( j ), SCALE - 1, SCALE - 1);
}
}
<a href="ntqframe.html#drawFrame">drawFrame</a>( &paint );
}
</pre>
<p> <hr>
<p> Main:
<p> <pre>/****************************************************************************
** $Id: qt/main.cpp 3.3.8 edited Jan 11 14:37 $
**
** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
**
** This file is part of an example program for TQt. This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
#include "lifedlg.h"
#include <<a href="qapplication-h.html">ntqapplication.h</a>>
#include <stdlib.h>
void usage()
{
<a href="ntqapplication.html#qWarning">qWarning</a>( "Usage: life [-scale scale]" );
}
int main( int argc, char **argv )
{
<a href="ntqapplication.html">TQApplication</a> a( argc, argv );
int scale = 10;
for ( int i = 1; i < argc; i++ ){
<a href="ntqstring.html">TQString</a> arg = argv[i];
if ( arg == "-scale" )
scale = atoi( argv[++i] );
else {
usage();
exit(1);
}
}
if ( scale < 2 )
scale = 2;
LifeDialog *life = new LifeDialog( scale );
a.<a href="ntqapplication.html#setMainWidget">setMainWidget</a>( life );
life-><a href="ntqwidget.html#setCaption">setCaption</a>("TQt Example - Life");
life-><a href="ntqwidget.html#show">show</a>();
return a.<a href="ntqapplication.html#exec">exec</a>();
}
</pre>
<p>See also <a href="examples.html">Examples</a>.
<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright © 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>TQt 3.3.8</div>
</table></div></address></body>
</html>
|