| 1 | /****************************************************************************
|
|---|
| 2 | ** $Id: life.cpp 2 2005-11-16 15:49:26Z dmik $
|
|---|
| 3 | **
|
|---|
| 4 | ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of an example program for Qt. This example
|
|---|
| 7 | ** program may be used, distributed and modified without limitation.
|
|---|
| 8 | **
|
|---|
| 9 | *****************************************************************************/
|
|---|
| 10 |
|
|---|
| 11 | #include "life.h"
|
|---|
| 12 |
|
|---|
| 13 | #include <qpainter.h>
|
|---|
| 14 | #include <qdrawutil.h>
|
|---|
| 15 | #include <qcheckbox.h>
|
|---|
| 16 | #include <qevent.h>
|
|---|
| 17 | #include <qapplication.h>
|
|---|
| 18 |
|
|---|
| 19 | // The main game of life widget
|
|---|
| 20 |
|
|---|
| 21 | LifeWidget::LifeWidget( int s, QWidget *parent, const char *name )
|
|---|
| 22 | : QFrame( parent, name )
|
|---|
| 23 | {
|
|---|
| 24 | SCALE = s;
|
|---|
| 25 |
|
|---|
| 26 | maxi = maxj = 50;
|
|---|
| 27 | setMinimumSize( MINSIZE * SCALE + 2 * BORDER,
|
|---|
| 28 | MINSIZE * SCALE + 2 * BORDER );
|
|---|
| 29 | setMaximumSize( MAXSIZE * SCALE + 2 * BORDER,
|
|---|
| 30 | MAXSIZE * SCALE + 2 * BORDER );
|
|---|
| 31 | setSizeIncrement( SCALE, SCALE);
|
|---|
| 32 |
|
|---|
| 33 | clear();
|
|---|
| 34 | resize( maxi * SCALE + 2 * BORDER , maxj * SCALE + 2 * BORDER );
|
|---|
| 35 |
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | void LifeWidget::clear()
|
|---|
| 40 | {
|
|---|
| 41 | current = 0;
|
|---|
| 42 | for ( int t = 0; t < 2; t++ )
|
|---|
| 43 | for ( int i = 0; i < MAXSIZE + 2; i++ )
|
|---|
| 44 | for ( int j = 0; j < MAXSIZE + 2; j++ )
|
|---|
| 45 | cells[t][i][j] = FALSE;
|
|---|
| 46 |
|
|---|
| 47 | repaint();
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 | // We assume that the size will never be beyond the maximum size set
|
|---|
| 52 | // this is not in general TRUE, but in practice it's good enough for
|
|---|
| 53 | // this program
|
|---|
| 54 |
|
|---|
| 55 | void LifeWidget::resizeEvent( QResizeEvent * e )
|
|---|
| 56 | {
|
|---|
| 57 | maxi = (e->size().width() - 2 * BORDER) / SCALE;
|
|---|
| 58 | maxj = (e->size().height() - 2 * BORDER) / SCALE;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 | void LifeWidget::setPoint( int i, int j )
|
|---|
| 63 | {
|
|---|
| 64 | if ( i < 1 || i > maxi || j < 1 || j > maxi )
|
|---|
| 65 | return;
|
|---|
| 66 | cells[current][i][j] = TRUE;
|
|---|
| 67 | repaint( index2pos(i), index2pos(j), SCALE, SCALE, FALSE );
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 | void LifeWidget::mouseHandle( const QPoint &pos )
|
|---|
| 72 | {
|
|---|
| 73 | int i = pos2index( pos.x() );
|
|---|
| 74 | int j = pos2index( pos.y() );
|
|---|
| 75 | setPoint( i, j );
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 | void LifeWidget::mouseMoveEvent( QMouseEvent *e )
|
|---|
| 80 | {
|
|---|
| 81 | mouseHandle( e->pos() );
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 | void LifeWidget::mousePressEvent( QMouseEvent *e )
|
|---|
| 86 | {
|
|---|
| 87 | if ( e->button() == QMouseEvent::LeftButton )
|
|---|
| 88 | mouseHandle( e->pos() );
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 | void LifeWidget::nextGeneration()
|
|---|
| 93 | {
|
|---|
| 94 | for ( int i = 1; i <= MAXSIZE; i++ ) {
|
|---|
| 95 | for ( int j = 1; j <= MAXSIZE; j++ ) {
|
|---|
| 96 | int t = cells[current][i - 1][j - 1]
|
|---|
| 97 | + cells[current][i - 1][j]
|
|---|
| 98 | + cells[current][i - 1][j + 1]
|
|---|
| 99 | + cells[current][i][j - 1]
|
|---|
| 100 | + cells[current][i][j + 1]
|
|---|
| 101 | + cells[current][i + 1][j - 1]
|
|---|
| 102 | + cells[current][i + 1][j]
|
|---|
| 103 | + cells[current][i + 1][j + 1];
|
|---|
| 104 |
|
|---|
| 105 | cells[!current][i][j] = ( t == 3 ||
|
|---|
| 106 | t == 2 && cells[current][i][j] );
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|
| 109 | current = !current;
|
|---|
| 110 | repaint( FALSE ); // repaint without erase
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 | void LifeWidget::paintEvent( QPaintEvent * e )
|
|---|
| 115 | {
|
|---|
| 116 | int starti = pos2index( e->rect().left() );
|
|---|
| 117 | int stopi = pos2index( e->rect().right() );
|
|---|
| 118 | int startj = pos2index( e->rect().top() );
|
|---|
| 119 | int stopj = pos2index( e->rect().bottom() );
|
|---|
| 120 |
|
|---|
| 121 | if (stopi > maxi)
|
|---|
| 122 | stopi = maxi;
|
|---|
| 123 | if (stopj > maxj)
|
|---|
| 124 | stopj = maxj;
|
|---|
| 125 |
|
|---|
| 126 | QPainter paint( this );
|
|---|
| 127 | for ( int i = starti; i <= stopi; i++ ) {
|
|---|
| 128 | for ( int j = startj; j <= stopj; j++ ) {
|
|---|
| 129 | if ( cells[current][i][j] )
|
|---|
| 130 | qDrawShadePanel( &paint, index2pos( i ), index2pos( j ),
|
|---|
| 131 | SCALE - 1, SCALE - 1, colorGroup() );
|
|---|
| 132 | else if ( cells[!current][i][j] )
|
|---|
| 133 | erase(index2pos( i ), index2pos( j ), SCALE - 1, SCALE - 1);
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|
| 136 | drawFrame( &paint );
|
|---|
| 137 | }
|
|---|