-rw-r--r-- | noncore/games/wordgame/wordgame.cpp | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/noncore/games/wordgame/wordgame.cpp b/noncore/games/wordgame/wordgame.cpp index 52e2be2..8cf92ef 100644 --- a/noncore/games/wordgame/wordgame.cpp +++ b/noncore/games/wordgame/wordgame.cpp @@ -463,257 +463,257 @@ void WordGame::endGame() if ( !mayEndGame() ) return; int out=-1; int totalleft=0; int i; for (i=0; i<nplayers; i++) { Rack* r = rack(i); int c = r->count(); if ( c ) { int lose=0; for ( int j=0; j<c; j++ ) lose += r->tileRef(j)->value(); totalleft += lose; scoreinfo->addScore(i,-lose); } else { out = i; } } int highest=0; int winner=0; for (i=0; i<nplayers; i++) { int s = scoreinfo->playerScore(i); if ( s > highest ) { highest = s; winner = i; } } if ( out >= 0 ) scoreinfo->addScore(out,totalleft); scoreinfo->setBoldOne(winner); gameover = TRUE; done->setEnabled(TRUE); reset->setEnabled(FALSE); } void WordGame::endTurn() { if ( gameover ) { openGameSelector(namelist); } else { if ( board->checkTurn() ) { if ( board->turnScore() >= 0 ) { scoreinfo->addScore(player,board->turnScore()); board->finalizeTurn(); } else { QApplication::beep(); } nextPlayer(); } } } void WordGame::resetTurn() { board->resetRack(); } void WordGame::passTurn() { // ######## trade? nextPlayer(); } bool WordGame::refillRack(int i) { Rack* r = rack(i); while ( !bag->isEmpty() && !r->isFull() ) { r->addTile(bag->takeRandom()); } return r->count() != 0; } void WordGame::readyRack(int i) { Rack* r = rack(i); racks->raiseWidget(i); board->setCurrentRack(r); done->setEnabled( !r->computerized() ); reset->setEnabled( !r->computerized() ); if ( r->computerized() ) { cpu = new ComputerPlayer(board, r); aiheart->start(0); } } Rack* WordGame::rack(int i) const { return (Rack*)racks->widget(i); } void WordGame::think() { if ( !cpu->step() ) { delete cpu; cpu = 0; aiheart->stop(); if ( board->turnScore() < 0 ) passTurn(); else endTurn(); } } ComputerPlayer::ComputerPlayer(Board* b, Rack* r) : board(b), rack(r), best(new const Tile*[rack_tiles]), best_blankvalues(new Tile[rack_tiles]) { best_score = -1; across=FALSE; dict=0; } ComputerPlayer::~ComputerPlayer() { delete [] best; delete [] best_blankvalues; } bool ComputerPlayer::step() { const QDawg::Node* root = dict ? Global::dawg("WordGame").root() : Global::fixedDawg().root(); QPoint d = across ? QPoint(1,0) : QPoint(0,1); const Tile* tiles[99]; // ### max board size uchar nletter[4095]; // QDawg only handles 0..4095 - memset(nletter,0,4096); + memset(nletter,0,4095); for (int i=0; i<rack->count(); i++) { const Tile* r = rack->tileRef(i); if ( r->isBlank() ) nletter[0]++; else nletter[r->text()[0].unicode()]++; } Tile blankvalues[99]; // ### max blanks findBest(current, d, root, 0, nletter, tiles, 0, blankvalues, 0); if ( ++current.rx() == board->xTiles() ) { current.rx() = 0; if ( ++current.ry() == board->yTiles() ) { if ( across ) { if ( dict == 1 ) { if ( best_score >= 0 ) { rack->arrangeTiles(best,best_n); rack->setBlanks(best_blankvalues); board->scoreTurn(best_start, best_n, best_dir); board->showTurn(); } return FALSE; } dict++; across = FALSE; current = QPoint(0,0); } else { across = TRUE; current = QPoint(0,0); } } } return TRUE; } void ComputerPlayer::findBest(QPoint at, const QPoint& d, const QDawg::Node* node, ulong used, uchar* nletter, const Tile** tiles, int n, Tile* blankvalues, int blused) { if ( !node ) return; QChar l = node->letter(); const Tile* cur = board->tile(at); if ( cur ) { if ( cur->text()[0] == l ) { bool nextok = board->contains(at+d); if ( node->isWord() && n && (!nextok || !board->tile(at+d)) ) noteChoice(tiles,n,d,blankvalues,blused); if ( nextok ) findBest(at+d, d, node->jump(), used, nletter, tiles, n, blankvalues, blused); // #### text()[1]... } } else { if ( nletter[l.unicode()] || nletter[0] ) { int rc = rack->count(); ulong msk = 1; for ( int x=0; x<rc; x++ ) { if ( !(used&msk) ) { const Tile* t = rack->tileRef(x); if ( t->isBlank() || t->text() == l ) { // #### multi-char value()s bool nextok = board->contains(at+d); tiles[n++] = t; if ( t->isBlank() ) blankvalues[blused++] = Tile(l,0); if ( node->isWord() && (!nextok || !board->tile(at+d)) ) noteChoice(tiles,n,d,blankvalues,blused); used |= msk; // mark nletter[t->text()[0].unicode()]--; if ( nextok ) findBest(at+d, d, node->jump(), used, nletter, tiles, n, blankvalues, blused); n--; nletter[t->text()[0].unicode()]++; if ( t->isBlank() ) { // keep looking blused--; used &= ~msk; // unmark } else { break; } } } msk <<= 1; } } // #### text()[1]... } findBest(at, d, node->next(), used, nletter, tiles, n, blankvalues, blused); } void ComputerPlayer::noteChoice(const Tile** tiles, int n, const QPoint& d, const Tile* blankvalues, int blused) { int s = board->score(current, tiles, n, blankvalues, d, TRUE, 0); /* if (s>0 || current==QPoint(5,1)){ QString st; for ( int i=0; i<n; i++ ) st += tiles[i]->text(); odebug << "" << current.x() << "," << current.y() << ": " << st.latin1() << " (" << n << ") for " << s << "" << oendl; } */ if ( s > best_score ) { int i; for ( i=0; i<n; i++ ) best[i] = tiles[i]; for ( i=0; i<blused; i++ ) best_blankvalues[i] = blankvalues[i]; best_n = n; best_blused = blused; best_score = s; best_dir = d; best_start = current; } } int TileItem::smallWidth() { return tile_smallw; } int TileItem::smallHeight() { return tile_smallh; } int TileItem::bigWidth() { return tile_bigw; } int TileItem::bigHeight() { |