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

📄 tictac-main-cpp.html

📁 qtopiaphone英文帮助,用于初学者和开发人员,初学者可以用来学习,开发人员可以用来资料查询.
💻 HTML
📖 第 1 页 / 共 2 页
字号:
    TicTacButton *b = buttons-&gt;at(i);           // get piece that was pressed    if ( b-&gt;type() == TicTacButton::Blank ) {   // empty piece?        btArray-&gt;at(i) = TicTacButton::Circle;        <a href=#345>updateButtons</a>();        if ( checkBoard( btArray ) == 0 )       // not a winning move?            <a href=#349>computerMove</a>();        int s = checkBoard( btArray );        if ( s ) {                              // any winners yet?            st = s == TicTacButton::Circle ? HumanWon : ComputerWon;            emit finished();        }    }}// --------------------------------------------------------------------------// <a name="345"></a>TicTacGameBoard::updateButtons()//// Updates all buttons that have changed state//void <a name="345"></a>TicTacGameBoard::updateButtons(){    for ( int i=0; i&lt;nBoard*nBoard; i++ ) {        if ( buttons-&gt;at(i)-&gt;type() != btArray-&gt;at(i) )            buttons-&gt;at(i)-&gt;setType( (TicTacButton::Type)btArray-&gt;at(i) );        buttons-&gt;at(i)-&gt;setEnabled( buttons-&gt;at(i)-&gt;type() ==                                    TicTacButton::Blank );    }}// --------------------------------------------------------------------------// <a name="347"></a>TicTacGameBoard::checkBoard()//// Checks if one of the players won the game, works for any board size.//// Returns://  - TicTacButton::Cross  if the player with X buttons won//  - TicTacButton::Circle if the player with O buttons won//  - Zero (0) if there is no winner yet//int <a name="347"></a>TicTacGameBoard::checkBoard( TicTacArray *a ){    int  t = 0;    int  row, col;    bool won = FALSE;    for ( row=0; row&lt;nBoard &amp;&amp; !won; row++ ) {  // check horizontal        t = a-&gt;at(row*nBoard);        if ( t == TicTacButton::Blank )            continue;        col = 1;        while ( col&lt;nBoard &amp;&amp; a-&gt;at(row*nBoard+col) == t )            col++;        if ( col == nBoard )            won = TRUE;    }    for ( col=0; col&lt;nBoard &amp;&amp; !won; col++ ) {  // check vertical        t = a-&gt;at(col);        if ( t == TicTacButton::Blank )            continue;        row = 1;        while ( row&lt;nBoard &amp;&amp; a-&gt;at(row*nBoard+col) == t )            row++;        if ( row == nBoard )            won = TRUE;    }    if ( !won ) {                               // check diagonal top left        t = a-&gt;at(0);                           //   to bottom right        if ( t != TicTacButton::Blank ) {            int i = 1;            while ( i&lt;nBoard &amp;&amp; a-&gt;at(i*nBoard+i) == t )                i++;            if ( i == nBoard )                won = TRUE;        }    }    if ( !won ) {                               // check diagonal bottom left        int j = nBoard-1;                       //   to top right        int i = 0;        t = a-&gt;at(i+j*nBoard);        if ( t != TicTacButton::Blank ) {            i++; j--;            while ( i&lt;nBoard &amp;&amp; a-&gt;at(i+j*nBoard) == t ) {                i++; j--;            }            if ( i == nBoard )                won = TRUE;        }    }    if ( !won )                                 // no winner        t = 0;    return t;}// --------------------------------------------------------------------------// <a name="349"></a>TicTacGameBoard::computerMove()//// Puts a piece on the game board. Very, very simple.//void <a name="349"></a>TicTacGameBoard::computerMove(){    int numButtons = nBoard*nBoard;    int *altv = new int[numButtons];            // buttons alternatives    int altc = 0;    int stopHuman = -1;    TicTacArray a = btArray-&gt;copy();    int i;    for ( i=0; i&lt;numButtons; i++ ) {            // try all positions        if ( a[i] != TicTacButton::Blank )      // already a piece there            continue;        a[i] = TicTacButton::Cross;             // test if computer wins        if ( checkBoard(&amp;a) == a[i] ) {         // computer will win            st = ComputerWon;            stopHuman = -1;            break;        }        a[i] = TicTacButton::Circle;            // test if human wins        if ( checkBoard(&amp;a) == a[i] ) {         // oops...            stopHuman = i;                      // remember position            a[i] = TicTacButton::Blank;         // restore button            continue;                           // computer still might win        }        a[i] = TicTacButton::Blank;             // restore button        altv[altc++] = i;                       // remember alternative    }    if ( stopHuman &gt;= 0 )                       // must stop human from winning        a[stopHuman] = TicTacButton::Cross;    else if ( i == numButtons ) {               // tried all alternatives        if ( altc &gt; 0 )                         // set random piece            a[altv[rand()%(altc--)]] = TicTacButton::Cross;        if ( altc == 0 ) {                      // no more blanks            st = NobodyWon;            emit finished();        }    }    *btArray = a;                               // update model    <a href=#345>updateButtons</a>();                            // update buttons    delete[] altv;}//***************************************************************************//* TicTacToe member functions//***************************************************************************// --------------------------------------------------------------------------// Creates a game widget with a game board and two push buttons, and connects// signals of child widgets to slots.//TicTacToe::TicTacToe( int boardSize, QWidget *parent, const char *name )    : <a href="qwidget.html">QWidget</a>( parent, name ){    <a href="qvboxlayout.html">QVBoxLayout</a> * l = new <a href="qvboxlayout.html">QVBoxLayout</a>( this, 6 );    // Create a message label    message = new <a href="qlabel.html">QLabel</a>( this );    message-&gt;setFrameStyle( QFrame::WinPanel | QFrame::Sunken );    message-&gt;setAlignment( AlignCenter );    l-&gt;<a href="qboxlayout.html#ebba99">addWidget</a>( message );    // Create the game board and connect the signal finished() to this    // gameOver() slot    board = new TicTacGameBoard( boardSize, this );    <a href="qobject.html#fbde73">connect</a>( board, SIGNAL(finished()), SLOT(<a href=#335>gameOver</a>()) );    l-&gt;<a href="qboxlayout.html#ebba99">addWidget</a>( board );    // Create a horizontal frame line    <a href="qframe.html">QFrame</a> *line = new <a href="qframe.html">QFrame</a>( this );    line-&gt;<a href="qframe.html#558f79">setFrameStyle</a>( QFrame::HLine | QFrame::Sunken );    l-&gt;<a href="qboxlayout.html#ebba99">addWidget</a>( line );    // Create the combo box for deciding who should start, and    // connect its clicked() signals to the buttonClicked() slot    whoStarts = new <a href="qcombobox.html">QComboBox</a>( this );    whoStarts-&gt;insertItem( "Computer starts" );    whoStarts-&gt;insertItem( "Human starts" );    l-&gt;<a href="qboxlayout.html#ebba99">addWidget</a>( whoStarts );    // Create the push buttons and connect their clicked() signals    // to this right slots.    newGame = new <a href="qpushbutton.html">QPushButton</a>( "Play!", this );    <a href="qobject.html#fbde73">connect</a>( newGame, SIGNAL(clicked()), SLOT(<a href=#333>newGameClicked</a>()) );    quit = new <a href="qpushbutton.html">QPushButton</a>( "Quit", this );    <a href="qobject.html#fbde73">connect</a>( quit, SIGNAL(clicked()), qApp, SLOT(quit()) );    <a href="qhboxlayout.html">QHBoxLayout</a> * b = new <a href="qhboxlayout.html">QHBoxLayout</a>;    l-&gt;<a href="qboxlayout.html#6ff301">addLayout</a>( b );    b-&gt;<a href="qboxlayout.html#ebba99">addWidget</a>( newGame );    b-&gt;<a href="qboxlayout.html#ebba99">addWidget</a>( quit );    <a href=#336>newState</a>();}// --------------------------------------------------------------------------// <a name="333"></a>TicTacToe::newGameClicked()                  - SLOT//// This slot is activated when the new game button is clicked.//void <a name="333"></a>TicTacToe::newGameClicked(){    board-&gt;computerStarts( whoStarts-&gt;currentItem() == 0 );    board-&gt;newGame();    <a href=#336>newState</a>();}// --------------------------------------------------------------------------// <a name="335"></a>TicTacToe::gameOver()                        - SLOT//// This slot is activated when the TicTacGameBoard emits the signal// "finished()", i.e. when a player has won or when it is a draw.//void <a name="335"></a>TicTacToe::gameOver(){    <a href=#336>newState</a>();                                 // update text box}// --------------------------------------------------------------------------// Updates the message to reflect a new state.//void <a name="336"></a>TicTacToe::newState(){    static const char *msg[] = {                // TicTacGameBoard::State texts        "Click Play to start", "Make your move",        "You won!", "Computer won!", "It's a draw" };    message-&gt;setText( msg[board-&gt;state()] );    return;}</pre>  <hr>  Main:<pre>/****************************************************************************** &#36;Id&#58; qt/examples/tictac/main.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 &lt;<a name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>&gt;#include &lt;stdlib.h&gt;#include "tictac.h"int main( int argc, char **argv ){    <a name="QApplication"></a><a href="qapplication.html">QApplication</a> a( argc, argv );    int n = 3;    if ( argc == 2 )                            // get board size n        n = atoi(argv[1]);    if ( n &lt; 3 || n &gt; 10 ) {                    // out of range        <a name="qWarning"></a><a href="qapplication.html#290ef4">qWarning</a>( "%s: Board size must be from 3x3 to 10x10", argv[0] );        return 1;    }    TicTacToe ttt( n );                         // create game    a.<a name="setMainWidget"></a><a href="qapplication.html#7ad759">setMainWidget</a>( &amp;ttt );    ttt.<a name="setCaption"></a><a href="qwidget.html#d6a291">setCaption</a>("Qt Example - TicTac");    ttt.<a name="show"></a><a href="qwidget.html#200ee5">show</a>();                                 // show widget    return a.<a name="exec"></a><a href="qapplication.html#84c7bf">exec</a>();                            // go}</pre><p><address><hr><div align="center"><table width="100%" cellspacing="0" border="0"><tr><td>Copyright 

⌨️ 快捷键说明

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