📄 tictac-main-cpp.html
字号:
TicTacButton *b = buttons->at(i); // get piece that was pressed if ( b->type() == TicTacButton::Blank ) { // empty piece? btArray->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<nBoard*nBoard; i++ ) { if ( buttons->at(i)->type() != btArray->at(i) ) buttons->at(i)->setType( (TicTacButton::Type)btArray->at(i) ); buttons->at(i)->setEnabled( buttons->at(i)->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<nBoard && !won; row++ ) { // check horizontal t = a->at(row*nBoard); if ( t == TicTacButton::Blank ) continue; col = 1; while ( col<nBoard && a->at(row*nBoard+col) == t ) col++; if ( col == nBoard ) won = TRUE; } for ( col=0; col<nBoard && !won; col++ ) { // check vertical t = a->at(col); if ( t == TicTacButton::Blank ) continue; row = 1; while ( row<nBoard && a->at(row*nBoard+col) == t ) row++; if ( row == nBoard ) won = TRUE; } if ( !won ) { // check diagonal top left t = a->at(0); // to bottom right if ( t != TicTacButton::Blank ) { int i = 1; while ( i<nBoard && a->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->at(i+j*nBoard); if ( t != TicTacButton::Blank ) { i++; j--; while ( i<nBoard && a->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->copy(); int i; for ( i=0; i<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(&a) == a[i] ) { // computer will win st = ComputerWon; stopHuman = -1; break; } a[i] = TicTacButton::Circle; // test if human wins if ( checkBoard(&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 >= 0 ) // must stop human from winning a[stopHuman] = TicTacButton::Cross; else if ( i == numButtons ) { // tried all alternatives if ( altc > 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->setFrameStyle( QFrame::WinPanel | QFrame::Sunken ); message->setAlignment( AlignCenter ); l-><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-><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-><a href="qframe.html#558f79">setFrameStyle</a>( QFrame::HLine | QFrame::Sunken ); l-><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->insertItem( "Computer starts" ); whoStarts->insertItem( "Human starts" ); l-><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-><a href="qboxlayout.html#6ff301">addLayout</a>( b ); b-><a href="qboxlayout.html#ebba99">addWidget</a>( newGame ); b-><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->computerStarts( whoStarts->currentItem() == 0 ); board->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->setText( msg[board->state()] ); return;}</pre> <hr> Main:<pre>/****************************************************************************** $Id: 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 <<a name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>>#include <stdlib.h>#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 < 3 || n > 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>( &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 + -