⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tictac-main-cpp.html

📁 qtopiaphone英文帮助,用于初学者和开发人员,初学者可以用来学习,开发人员可以用来资料查询.
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Qt Toolkit - tictac/main.cpp example file</title><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: white; color: black; }--></style></head><body bgcolor="#ffffff"><table width="100%"><tr><td><a href="index.html"><img width="100" height="100" src="qtlogo.png"alt="Home" border="0"><img width="100"height="100" src="face.png" alt="Home" border="0"></a><td valign="top"><div align="right"><img src="dochead.png" width="472" height="27"><br><a href="classes.html"><b>Classes</b></a>- <a href="annotated.html">Annotated</a>- <a href="hierarchy.html">Tree</a>- <a href="functions.html">Functions</a>- <a href="index.html">Home</a>- <a href="topicals.html"><b>Structure</b>  <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></a></div></table><h1 align=center>Tic Tac Toe</h1><br clear="all">  This is an implementation of the Tic-tac-toe game.  We didn't put much effort in making a clever algorithm so it's not a  challenge to play against the computer. Instead, study the source code  to see how you can make reusable components such as the TicTacGameBoard  widget.  <hr>  Header file: <pre>/****************************************************************************** &#36;Id&#58; qt/examples/tictac/tictac.h   2.3.8   edited 2004-05-12 $**** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.**** This file is part of an example program for Qt.  This example** program may be used, distributed and modified without limitation.*******************************************************************************/#ifndef TICTAC_H#define TICTAC_H#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;#include &lt;<a href="qvector-h.html">qvector.h</a>&gt;class QComboBox;class QLabel;// --------------------------------------------------------------------------// TicTacButton implements a single tic-tac-toe button//class TicTacButton : public QPushButton{    Q_OBJECTpublic:    TicTacButton( <a href="qwidget.html">QWidget</a> *parent );    enum Type { Blank, Circle, Cross };    Type        type() const            { return t; }    void        setType( Type type )    { t = type; repaint(); }    <a href="qsizepolicy.html">QSizePolicy</a> sizePolicy() const    { return QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred ); }    <a href="qsize.html">QSize</a> sizeHint() const { return QSize( 32, 32 ); }    <a href="qsize.html">QSize</a> minimumSizeHint() const { return QSize( 10, 10 ); }protected:    void        drawButtonLabel( <a href="qpainter.html">QPainter</a> * );private:    Type t;};// Using template vector to make vector-class of TicTacButton.// This vector is used by the TicTacGameBoard class defined below.typedef QVector&lt;TicTacButton&gt;   TicTacButtons;typedef QArray&lt;int&gt;             TicTacArray;// --------------------------------------------------------------------------// TicTacGameBoard implements the tic-tac-toe game board.// TicTacGameBoard is a composite widget that contains N x N TicTacButtons.// N is specified in the constructor.//class TicTacGameBoard : public QWidget{    Q_OBJECTpublic:    TicTacGameBoard( int n, QWidget *parent=0, const char *name=0 );   ~TicTacGameBoard();    enum        State { Init, HumansTurn, HumanWon, ComputerWon, NobodyWon };    State       state() const           { return st; }    void        computerStarts( bool v );    void        newGame();signals:    void        finished();                     // game finishedprivate slots:    void        buttonClicked();private:    void        setState( State state ) { st = state; }    void        updateButtons();    int         checkBoard( TicTacArray * );    void        computerMove();    State       st;    int         nBoard;    bool        comp_starts;    TicTacArray *btArray;    TicTacButtons *buttons;};// --------------------------------------------------------------------------// TicTacToe implements the complete game.// TicTacToe is a composite widget that contains a TicTacGameBoard and// two push buttons for starting the game and quitting.//class TicTacToe : public QWidget{    Q_OBJECTpublic:    TicTacToe( int boardSize=3, QWidget *parent=0, const char *name=0 );private slots:    void        newGameClicked();    void        gameOver();private:    void        newState();    <a href="qcombobox.html">QComboBox</a>   *whoStarts;    <a href="qpushbutton.html">QPushButton</a> *newGame;    <a href="qpushbutton.html">QPushButton</a> *quit;    <a href="qlabel.html">QLabel</a>      *message;    TicTacGameBoard *board;};#endif // TICTAC_H</pre>  <hr>  Implementation: <pre>/****************************************************************************** &#36;Id&#58; qt/examples/tictac/tictac.cpp   2.3.8   edited 2004-05-12 $**** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.**** This file is part of an example program for Qt.  This example** program may be used, distributed and modified without limitation.*******************************************************************************/#include "tictac.h"#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;#include &lt;<a href="qpainter-h.html">qpainter.h</a>&gt;#include &lt;<a href="qdrawutil-h.html">qdrawutil.h</a>&gt;#include &lt;<a href="qcombobox-h.html">qcombobox.h</a>&gt;#include &lt;<a href="qcheckbox-h.html">qcheckbox.h</a>&gt;#include &lt;<a href="qlabel-h.html">qlabel.h</a>&gt;#include &lt;<a href="qlayout-h.html">qlayout.h</a>&gt;#include &lt;stdlib.h&gt;                             // rand() function#include &lt;<a href="qdatetime-h.html">qdatetime.h</a>&gt;                          // seed for rand()//***************************************************************************//* TicTacButton member functions//***************************************************************************// --------------------------------------------------------------------------// Creates a TicTacButton//TicTacButton::TicTacButton( <a href="qwidget.html">QWidget</a> *parent ) : <a href="qpushbutton.html">QPushButton</a>( parent ){    t = Blank;                                  // initial type}// --------------------------------------------------------------------------// Paints TicTacButton//void <a name="337"></a>TicTacButton::drawButtonLabel( <a href="qpainter.html">QPainter</a> *p ){    <a href="qrect.html">QRect</a> r = rect();    p-&gt;setPen( <a href="qpen.html">QPen</a>( white,2 ) );               // set fat pen    if ( t == Circle ) {        p-&gt;drawEllipse( r.<a href="qrect.html#369cab">left</a>()+4, r.<a href="qrect.html#4dd27e">top</a>()+4, r.<a href="qrect.html#45fe95">width</a>()-8, r.<a href="qrect.html#581ab8">height</a>()-8 );    } else if ( t == Cross ) {                  // draw cross        p-&gt;drawLine( r.<a href="qrect.html#349186">topLeft</a>()   +QPoint(4,4), r.<a href="qrect.html#9f41f2">bottomRight</a>()-QPoint(4,4));        p-&gt;drawLine( r.<a href="qrect.html#5dd6a8">bottomLeft</a>()+QPoint(4,-4),r.<a href="qrect.html#d59dee">topRight</a>()   -QPoint(4,-4));    }}//***************************************************************************//* TicTacGameBoard member functions//***************************************************************************// --------------------------------------------------------------------------// Creates a game board with N x N buttons and connects the "clicked()"// signal of all buttons to the "buttonClicked()" slot.//TicTacGameBoard::TicTacGameBoard( int n, QWidget *parent, const char *name )    : <a href="qwidget.html">QWidget</a>( parent, name ){    st = Init;                                  // initial state    nBoard = n;    n *= n;                                     // make square    comp_starts = FALSE;                        // human starts    buttons = new TicTacButtons(n);             // create real buttons    btArray = new TicTacArray(n);               // create button model    <a href="qgridlayout.html">QGridLayout</a> * grid = new <a href="qgridlayout.html">QGridLayout</a>( this, 3, 3, 4 );    <a href="qpalette.html">QPalette</a> p( blue );    for ( int i=0; i&lt;n; i++ ) {                 // create and connect buttons        TicTacButton *ttb = new TicTacButton( this );        ttb-&gt;<a href="qwidget.html#d7e4b9">setPalette</a>( p );        ttb-&gt;<a href="qwidget.html#4b103c">setEnabled</a>( FALSE );        <a href="qobject.html#fbde73">connect</a>( ttb, SIGNAL(clicked()), SLOT(<a href=#343>buttonClicked</a>()) );        grid-&gt;<a href="qgridlayout.html#dac29c">addWidget</a>( ttb, i%3, i/3 );        buttons-&gt;insert( i, ttb );        btArray-&gt;at(i) = TicTacButton::Blank;   // initial button type    }    <a href="qtime.html">QTime</a> t = QTime::currentTime();             // set random seed    srand( t.<a href="qtime.html#e582de">hour</a>()*12+t.<a href="qtime.html#83f0e2">minute</a>()*60+t.<a href="qtime.html#ca67be">second</a>()*60 );}TicTacGameBoard::~TicTacGameBoard(){    delete buttons;    delete btArray;}// --------------------------------------------------------------------------// <a name="339"></a>TicTacGameBoard::computerStarts( bool v )//// Computer starts if v=TRUE. The human starts by default.//void <a name="339"></a>TicTacGameBoard::computerStarts( bool v ){    comp_starts = v;}// --------------------------------------------------------------------------// <a name="341"></a>TicTacGameBoard::newGame()//// Clears the game board and prepares for a new game//void <a name="341"></a>TicTacGameBoard::newGame(){    st = HumansTurn;    for ( int i=0; i&lt;nBoard*nBoard; i++ )        btArray-&gt;at(i) = TicTacButton::Blank;    if ( comp_starts )        <a href=#349>computerMove</a>();    else        <a href=#345>updateButtons</a>();}// --------------------------------------------------------------------------// <a name="343"></a>TicTacGameBoard::buttonClicked()             - SLOT//// This slot is activated when a TicTacButton emits the signal "clicked()",// i.e. the user has clicked on a TicTacButton.//void <a name="343"></a>TicTacGameBoard::buttonClicked(){    if ( st != HumansTurn )                     // not ready        return;    int i = buttons-&gt;findRef( (TicTacButton*)sender() );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -