1 | /****************************************************************************
|
---|
2 | ** $Id: tictac.h 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 | #ifndef TICTAC_H
|
---|
12 | #define TICTAC_H
|
---|
13 |
|
---|
14 |
|
---|
15 | #include <qpushbutton.h>
|
---|
16 | #include <qptrvector.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 | void drawButtonLabel( 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 QPtrVector<TicTacButton> TicTacButtons;
|
---|
48 | typedef QMemArray<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 | enum State { Init, HumansTurn, HumanWon, ComputerWon, NobodyWon };
|
---|
64 | State state() const { return st; }
|
---|
65 | void computerStarts( bool v );
|
---|
66 | void newGame();
|
---|
67 | signals:
|
---|
68 | void finished(); // game finished
|
---|
69 | private slots:
|
---|
70 | void buttonClicked();
|
---|
71 | private:
|
---|
72 | void setState( State state ) { st = state; }
|
---|
73 | void updateButtons();
|
---|
74 | int checkBoard( TicTacArray * );
|
---|
75 | void computerMove();
|
---|
76 | State st;
|
---|
77 | int nBoard;
|
---|
78 | bool comp_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 | void newGameClicked();
|
---|
97 | void gameOver();
|
---|
98 | private:
|
---|
99 | void newState();
|
---|
100 | QComboBox *whoStarts;
|
---|
101 | QPushButton *newGame;
|
---|
102 | QPushButton *quit;
|
---|
103 | QLabel *message;
|
---|
104 | TicTacGameBoard *board;
|
---|
105 | };
|
---|
106 |
|
---|
107 |
|
---|
108 | #endif // TICTAC_H
|
---|