void mythread::run() { //ovde thread krece sa izvrsavanjem threadFrame myFrame; QTimer *timer = new QTimer(0); timer->start(10); qDebug() << socketDescriptor << "Starting thread"; socket = new QTcpSocket(); if (!socket->setSocketDescriptor(this->socketDescriptor)) { emit error(socket->error()); return; } myFrame.frame = *sysFrame; myFrame.socketDescriptor=this->socketDescriptor; sysFrameList->append(myFrame); connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()),Qt::DirectConnection); connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()),Qt::DirectConnection); connect(timer,SIGNAL(timeout()),this,SLOT(timeUp()),Qt::DirectConnection); //still need this. connect(this,SIGNAL(timeSig()),this,SLOT(readyWrite()),Qt::DirectConnection); qDebug() << socketDescriptor << "Client connected."; QMutex lock; exec(); //thread will stay alive until we tell it to close }
void C_GetTime::waitFor( void ) { do { if( _timeSlice == true ) processMessage(); } while( !timeUp() ); }
int Game::quiesce(ChessBoard *board, const int depth, int alpha, const int beta) { // before any evaluation, first check if time is up if(m_evalCount % 1000 == 0) { if(timeUp()) m_bInterrupted = true; } if(m_bInterrupted){ #if DEBUG_LEVEL & 1 DEBUG_PRINT("\nQ interrupted @%d\n", depth); #endif return alpha; // } #if DEBUG_LEVEL & 1 DEBUG_PRINT(" %d{", depth); #endif // administer evaluation count and get the board value m_evalCount++; const int boardValue = board->boardValueExtension(); int value; // at this point there can be a beta cutt-off unless we're in check if(!Move_isCheck(board->getMyMove())){ if(boardValue >= beta){ #if DEBUG_LEVEL & 1 DEBUG_PRINT("X%d,%d ", depth, value, beta); #endif return beta; } } if(depth == 0) // ok, max quiesce depth is reached; return this value { #if DEBUG_LEVEL & 1 //DEBUG_PRINT(".%d", boardValue); #endif // get the value of this node return boardValue; } if(boardValue >= beta) return beta; // futility pruning here? i.e. can alpha be improved // update lower bound if(boardValue > alpha) { alpha = boardValue; } int move; //int best = (-ChessBoard::VALUATION_MATE)-1; ChessBoard *nextBoard = m_boardFactory[MAX_DEPTH - depth]; board->scoreMoves(); while (board->hasMoreMoves()) { move = board->getNextScoredMove(); board->makeMove(move, nextBoard); // self check is illegal! if(nextBoard->checkInSelfCheck()){ // not valid, remove this move and continue board->removeMoveElementAt(); continue; } // generate the moves for this next board in order to validate the board // todo, reset this if attackedMoveSquares are working //nextBoard->genMoves(); if(nextBoard->checkInCheck()){ #if DEBUG_LEVEL & 1 DEBUG_PRINT("!", 0); #endif nextBoard->setMyMoveCheck(); move = Move_setCheck(move); } #if DEBUG_LEVEL & 1 char buf[20]; Move::toDbgString(move, buf); DEBUG_PRINT(" %s ", buf); #endif // quiescent search if(Move_isHIT(move) || Move_isCheck(move) || Move_isPromotionMove(move)){ // a valid and active move, so continue quiescent search // optimization; only generate when needed // todo back to before checkInCheck? nextBoard->genMoves(); value = -quiesce(nextBoard, depth-1,-beta,-alpha); if(value > alpha) { alpha = value; if(value >= beta){ #if DEBUG_LEVEL & 1 DEBUG_PRINT("B", 0); #endif return beta; //break; } } } } // no valid moves, so mate or stalemate if(board->getNumMoves() == 0){ //DEBUG_PRINT("@", 0); if(Move_isCheck(board->getMyMove())){ #if DEBUG_LEVEL & 1 DEBUG_PRINT("Q.#\n", 0); #endif return (-ChessBoard::VALUATION_MATE); } #if DEBUG_LEVEL & 1 DEBUG_PRINT("Q.$\n", 0); #endif return ChessBoard::VALUATION_DRAW; } #if DEBUG_LEVEL & 1 DEBUG_PRINT("|%d} %d @%d\n", board->getNumMoves(), alpha, depth); #endif return alpha; }
//////////////////////////////////////////////////////////////////////////////// // alphaBeta //////////////////////////////////////////////////////////////////////////////// int Game::alphaBeta(ChessBoard *board, const int depth, int alpha, const int beta) { if(m_evalCount % 1000 == 0) { if(timeUp()) m_bInterrupted = true; } if(m_bInterrupted){ return alpha; // } int value = 0; // 50 move rule and repetition check if(board->checkEnded()){ return ChessBoard::VALUATION_DRAW; } board->scoreMovesPV(m_arrPVMoves[m_searchDepth - depth]); int best = (-ChessBoard::VALUATION_MATE)-1; int move = 0; ChessBoard *nextBoard = m_boardFactory[depth]; while (board->hasMoreMoves()) { move = board->getNextScoredMove(); board->makeMove(move, nextBoard); // self check is illegal! if(nextBoard->checkInSelfCheck()){ // not valid, remove this move and continue //DEBUG_PRINT("#", 0); board->removeMoveElementAt(); continue; } // generate the moves for this next board in order to validate the board nextBoard->genMoves(); if(nextBoard->checkInCheck()){ //DEBUG_PRINT("+", 0); nextBoard->setMyMoveCheck(); move = Move_setCheck(move); } // ok valid move #if DEBUG_LEVEL & 1 char buf[20]; Move::toDbgString(move, buf); DEBUG_PRINT("\n%d %s > ", depth, buf); #endif // at depth one is at the leaves, so call quiescent search if(depth == 1) { value = -quiesce(nextBoard, QUIESCE_DEPTH, -beta, -alpha); } else { value = -alphaBeta(nextBoard, depth-1,-beta,-alpha); } if(value > best) { best = value; m_arrPVMoves[m_searchDepth - depth] = move; } if(best > alpha) { #if DEBUG_LEVEL & 1 DEBUG_PRINT("$ best > alpha %d > %d ", best, alpha); #endif alpha = best; } if(best >= beta){ #if DEBUG_LEVEL & 1 DEBUG_PRINT("$ best >= beta break, %d > %d\n", best, beta); #endif break; } } // no valid moves, so mate or stalemate if(board->getNumMoves() == 0){ //DEBUG_PRINT("@", 0); if(Move_isCheck(board->getMyMove())){ #if DEBUG_LEVEL & 1 DEBUG_PRINT("\nMate #\n", 0); #endif return (-ChessBoard::VALUATION_MATE); } #if DEBUG_LEVEL & 1 DEBUG_PRINT("\nStalemate\n", 0); #endif return ChessBoard::VALUATION_DRAW; } #if DEBUG_LEVEL & 4 DEBUG_PRINT("\n==> alphaBeta #moves: %d\n", board->getNumMoves()); #endif return best; }