author | bipolar <bipolar> | 2002-03-05 03:45:22 (UTC) |
---|---|---|
committer | bipolar <bipolar> | 2002-03-05 03:45:22 (UTC) |
commit | 85175dadfdd576119935b3d612aab029a50c322f (patch) (unidiff) | |
tree | a2d3a08a21102a01982c725752e04cfcfec4e5bb /noncore/games/tictac/tictac.h | |
parent | cde5431fda4046920567164deec6e6bb9724199d (diff) | |
download | opie-85175dadfdd576119935b3d612aab029a50c322f.zip opie-85175dadfdd576119935b3d612aab029a50c322f.tar.gz opie-85175dadfdd576119935b3d612aab029a50c322f.tar.bz2 |
ljp:
Diffstat (limited to 'noncore/games/tictac/tictac.h') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/games/tictac/tictac.h | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/noncore/games/tictac/tictac.h b/noncore/games/tictac/tictac.h new file mode 100644 index 0000000..ec6c79c --- a/dev/null +++ b/noncore/games/tictac/tictac.h | |||
@@ -0,0 +1,108 @@ | |||
1 | /**************************************************************************** | ||
2 | ** $Id$ | ||
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 | #ifndef TICTAC_H | ||
12 | #define TICTAC_H | ||
13 | |||
14 | |||
15 | #include <qpushbutton.h> | ||
16 | #include <qvector.h> | ||
17 | |||
18 | class QComboBox; | ||
19 | class QLabel; | ||
20 | |||
21 | |||
22 | // -------------------------------------------------------------------------- | ||
23 | // TicTacButton implements a single tic-tac-toe button | ||
24 | // | ||
25 | |||
26 | class TicTacButton : public QPushButton | ||
27 | { | ||
28 | Q_OBJECT | ||
29 | public: | ||
30 | TicTacButton( QWidget *parent ); | ||
31 | enum Type { Blank, Circle, Cross }; | ||
32 | Type type() const { return t; } | ||
33 | void setType( Type type ){ t = type; repaint(); } | ||
34 | QSizePolicy sizePolicy() const | ||
35 | { return QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred ); } | ||
36 | QSize sizeHint() const { return QSize( 32, 32 ); } | ||
37 | QSize minimumSizeHint() const { return QSize( 10, 10 ); } | ||
38 | protected: | ||
39 | voiddrawButtonLabel( QPainter * ); | ||
40 | private: | ||
41 | Type t; | ||
42 | }; | ||
43 | |||
44 | // Using template vector to make vector-class of TicTacButton. | ||
45 | // This vector is used by the TicTacGameBoard class defined below. | ||
46 | |||
47 | typedef QVector<TicTacButton>TicTacButtons; | ||
48 | typedef QArray<int> TicTacArray; | ||
49 | |||
50 | |||
51 | // -------------------------------------------------------------------------- | ||
52 | // TicTacGameBoard implements the tic-tac-toe game board. | ||
53 | // TicTacGameBoard is a composite widget that contains N x N TicTacButtons. | ||
54 | // N is specified in the constructor. | ||
55 | // | ||
56 | |||
57 | class TicTacGameBoard : public QWidget | ||
58 | { | ||
59 | Q_OBJECT | ||
60 | public: | ||
61 | TicTacGameBoard( int n, QWidget *parent=0, const char *name=0 ); | ||
62 | ~TicTacGameBoard(); | ||
63 | enumState { Init, HumansTurn, HumanWon, ComputerWon, NobodyWon }; | ||
64 | State state() const { return st; } | ||
65 | voidcomputerStarts( bool v ); | ||
66 | void newGame(); | ||
67 | signals: | ||
68 | void finished(); // game finished | ||
69 | private slots: | ||
70 | voidbuttonClicked(); | ||
71 | private: | ||
72 | void setState( State state ) { st = state; } | ||
73 | voidupdateButtons(); | ||
74 | int checkBoard( TicTacArray * ); | ||
75 | void computerMove(); | ||
76 | Statest; | ||
77 | int nBoard; | ||
78 | boolcomp_starts; | ||
79 | TicTacArray *btArray; | ||
80 | TicTacButtons *buttons; | ||
81 | }; | ||
82 | |||
83 | |||
84 | // -------------------------------------------------------------------------- | ||
85 | // TicTacToe implements the complete game. | ||
86 | // TicTacToe is a composite widget that contains a TicTacGameBoard and | ||
87 | // two push buttons for starting the game and quitting. | ||
88 | // | ||
89 | |||
90 | class TicTacToe : public QWidget | ||
91 | { | ||
92 | Q_OBJECT | ||
93 | public: | ||
94 | TicTacToe( int boardSize=3, QWidget *parent=0, const char *name=0 ); | ||
95 | private slots: | ||
96 | voidnewGameClicked(); | ||
97 | voidgameOver(); | ||
98 | private: | ||
99 | voidnewState(); | ||
100 | QComboBox*whoStarts; | ||
101 | QPushButton *newGame; | ||
102 | QPushButton *quit; | ||
103 | QLabel*message; | ||
104 | TicTacGameBoard *board; | ||
105 | }; | ||
106 | |||
107 | |||
108 | #endif // TICTAC_H | ||