Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

tictac.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** tictac.h,v 1.1.10.1 2003/08/29 06:50:40 harlekin Exp
00003 **
00004 ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
00005 **
00006 ** This file is part of an example program for Qt.  This example
00007 ** program may be used, distributed and modified without limitation.
00008 **
00009 *****************************************************************************/
00010 
00011 #ifndef TICTAC_H
00012 #define TICTAC_H
00013 
00014 
00015 #include <qpushbutton.h>
00016 #include <qvector.h>
00017 
00018 class QComboBox;
00019 class QLabel;
00020 
00021 
00022 // --------------------------------------------------------------------------
00023 // TicTacButton implements a single tic-tac-toe button
00024 //
00025 
00026 class TicTacButton : public QPushButton
00027 {
00028     Q_OBJECT
00029 public:
00030     TicTacButton( QWidget *parent );
00031     enum Type { Blank, Circle, Cross };
00032     Type        type() const            { return t; }
00033     void        setType( Type type )    { t = type; repaint(); }
00034     QSizePolicy sizePolicy() const
00035     { return QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred ); }
00036     QSize sizeHint() const { return QSize( 32, 32 ); }
00037     QSize minimumSizeHint() const { return QSize( 10, 10 ); }
00038 protected:
00039     void        drawButtonLabel( QPainter * );
00040 private:
00041     Type t;
00042 };
00043 
00044 // Using template vector to make vector-class of TicTacButton.
00045 // This vector is used by the TicTacGameBoard class defined below.
00046 
00047 typedef QVector<TicTacButton>   TicTacButtons;
00048 typedef QArray<int>             TicTacArray;
00049 
00050 
00051 // --------------------------------------------------------------------------
00052 // TicTacGameBoard implements the tic-tac-toe game board.
00053 // TicTacGameBoard is a composite widget that contains N x N TicTacButtons.
00054 // N is specified in the constructor.
00055 //
00056 
00057 class TicTacGameBoard : public QWidget
00058 {
00059     Q_OBJECT
00060 public:
00061     TicTacGameBoard( int n, QWidget *parent=0, const char *name=0 );
00062    ~TicTacGameBoard();
00063     enum        State { Init, HumansTurn, HumanWon, ComputerWon, NobodyWon };
00064     State       state() const           { return st; }
00065     void        computerStarts( bool v );
00066     void        newGame();
00067 signals:
00068     void        finished();                     // game finished
00069 private slots:
00070     void        buttonClicked();
00071 private:
00072     void        setState( State state ) { st = state; }
00073     void        updateButtons();
00074     int         checkBoard( TicTacArray * );
00075     void        computerMove();
00076     State       st;
00077     int         nBoard;
00078     bool        comp_starts;
00079     TicTacArray *btArray;
00080     TicTacButtons *buttons;
00081 };
00082 
00083 
00084 // --------------------------------------------------------------------------
00085 // TicTacToe implements the complete game.
00086 // TicTacToe is a composite widget that contains a TicTacGameBoard and
00087 // two push buttons for starting the game and quitting.
00088 //
00089 
00090 class TicTacToe : public QWidget
00091 {
00092     Q_OBJECT
00093 public:
00094     TicTacToe( QWidget *parent=0, const char *name=0, WFlags fl = 0 );
00095     static QString appName() { return QString::fromLatin1("tictac"); }
00096 
00097 private slots:
00098     void        newGameClicked();
00099     void        gameOver();
00100 private:
00101     void        newState();
00102     QComboBox   *whoStarts;
00103     QPushButton *newGame;
00104     QPushButton *quit;
00105     QLabel      *message;
00106     TicTacGameBoard *board;
00107     int boardSize;
00108 };
00109 
00110 
00111 #endif // TICTAC_H

Generated on Sat Nov 5 16:17:27 2005 for OPIE by  doxygen 1.4.2