00001
00002
00003
00004
00005
00006
00007
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
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
00045
00046
00047 typedef QVector<TicTacButton> TicTacButtons;
00048 typedef QArray<int> TicTacArray;
00049
00050
00051
00052
00053
00054
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();
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
00086
00087
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