summaryrefslogtreecommitdiff
path: root/noncore/games/tictac/tictac.h
Unidiff
Diffstat (limited to 'noncore/games/tictac/tictac.h') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/tictac/tictac.h108
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
18class QComboBox;
19class QLabel;
20
21
22// --------------------------------------------------------------------------
23// TicTacButton implements a single tic-tac-toe button
24//
25
26class TicTacButton : public QPushButton
27{
28 Q_OBJECT
29public:
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 ); }
38protected:
39 voiddrawButtonLabel( QPainter * );
40private:
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
57class TicTacGameBoard : public QWidget
58{
59 Q_OBJECT
60public:
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();
67signals:
68 void finished(); // game finished
69private slots:
70 voidbuttonClicked();
71private:
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
90class TicTacToe : public QWidget
91{
92 Q_OBJECT
93public:
94 TicTacToe( int boardSize=3, QWidget *parent=0, const char *name=0 );
95private slots:
96 voidnewGameClicked();
97 voidgameOver();
98private:
99 voidnewState();
100 QComboBox*whoStarts;
101 QPushButton *newGame;
102 QPushButton *quit;
103 QLabel*message;
104 TicTacGameBoard *board;
105};
106
107
108#endif // TICTAC_H