summaryrefslogtreecommitdiff
authorimm <imm>2002-09-24 23:49:15 (UTC)
committer imm <imm>2002-09-24 23:49:15 (UTC)
commita390040768072b4b2b931ec0bcdaaa55daae0b4c (patch) (side-by-side diff)
tree1938c484842f0c35e40dc406e719c0d86e07091c
parentc280cda4ece4a4aa60f4ef2c632d7a37ac08b258 (diff)
downloadopie-a390040768072b4b2b931ec0bcdaaa55daae0b4c.zip
opie-a390040768072b4b2b931ec0bcdaaa55daae0b4c.tar.gz
opie-a390040768072b4b2b931ec0bcdaaa55daae0b4c.tar.bz2
pics dir changed
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/wordgame/wordgame.cpp4
1 files changed, 2 insertions, 2 deletions
diff --git a/noncore/games/wordgame/wordgame.cpp b/noncore/games/wordgame/wordgame.cpp
index 16d37b3..d3160f4 100644
--- a/noncore/games/wordgame/wordgame.cpp
+++ b/noncore/games/wordgame/wordgame.cpp
@@ -1,701 +1,701 @@
/**********************************************************************
** Copyright (C) 2000 Trolltech AS. All rights reserved.
**
** This file is part of Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
#include "wordgame.h"
#include <qpe/applnk.h>
#include <qpe/global.h>
#include <qpe/filemanager.h>
#include <qpe/resource.h>
#include <qpe/config.h>
#include <qapplication.h>
#include <qmessagebox.h>
#include <qcombobox.h>
#include <qdatetime.h>
#include <qfileinfo.h>
#include <qfile.h>
#include <qdir.h>
#include <qiconset.h>
#include <qlabel.h>
#include <qlineedit.h>
#include <qpushbutton.h>
#include <qtextstream.h>
#include <qtimer.h>
#include <qpe/qpetoolbar.h>
#include <qtoolbutton.h>
#include <qvbox.h>
#include <qwidgetstack.h>
#include <qpainter.h>
#include <qlayout.h>
#include <qregexp.h>
#include <stdlib.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>
enum RuleEffects {
Multiplier=15,
MultiplyAll=64,
Start=128
};
static int tile_smallw = 16;
static int tile_smallh = 16;
static int tile_bigw = 22;
static int tile_bigh = 22;
static int tile_stweak = -2;
static int tile_btweak = -1;
static const int rack_tiles=7;
const char* sampleWGR=
"wordgame_shapes\n"
"15 15\n"
"400001040100004\n"
"030000000000030\n"
"002002000200200\n"
"000300020003000\n"
"000020000020000\n"
"102001000100201\n"
"000000202000000\n"
"400200050002004\n"
"000000202000000\n"
"102001000100201\n"
"000020000020000\n"
"000300020003000\n"
"002002000200200\n"
"030000000000030\n"
"400001040100004\n"
"1 2 3 66 67 194 100 0\n"
"1 j 8\n"
"1 q 7\n"
"1 x 6\n"
"1 z 6\n"
"1 w 4\n"
"1 k 4\n"
"1 v 3\n"
"1 f 3\n"
"2 y 3\n"
"2 h 2\n"
"2 b 2\n"
"2 m 2\n"
"3 p 2\n"
"3 g 2\n"
"3 u 2\n"
"4 d 2\n"
"4 c 2\n"
"5 l 1\n"
"5 o 1\n"
"7 t 1\n"
"7 n 1\n"
"7 a 1\n"
"7 r 1\n"
"8 s 1\n"
"8 i 1\n"
"11 e 1\n"
"0\n";
WordGame::WordGame( QWidget* parent, const char* name, WFlags fl ) :
QMainWindow(parent, name, fl)
{
if ( qApp->desktop()->width() < 240 ) {
tile_smallw = 10;
tile_smallh = 10;
tile_bigw = 16;
tile_bigh = 16;
tile_stweak = 0;
tile_btweak = 0;
}
- setIcon( Resource::loadPixmap( "wordgame" ) );
+ setIcon( Resource::loadPixmap( "wordgame/WordGame.png" ) );
setCaption( tr("Word Game") );
setToolBarsMovable( FALSE );
vbox = new QVBox(this);
setCentralWidget(vbox);
toolbar = new QPEToolBar(this);
addToolBar(toolbar, Bottom);
reset = new QToolButton(Resource::loadPixmap("back"), tr("Back"), "", this, SLOT(resetTurn()), toolbar);
done = new QToolButton(Resource::loadPixmap("done"), tr("Done"), "", this, SLOT(endTurn()), toolbar);
scoreinfo = new ScoreInfo(toolbar);
scoreinfo->setFont(QFont("Helvetica",10));
new QToolButton(Resource::loadPixmap("finish"), tr("Close"), "", this, SLOT(endGame()), toolbar);
toolbar->setStretchableWidget(scoreinfo);
cpu = 0;
board = 0;
bag = 0;
racks = 0;
aiheart = new QTimer(this);
connect(aiheart, SIGNAL(timeout()), this, SLOT(think()));
readConfig();
}
WordGame::~WordGame()
{
writeConfig();
}
void WordGame::writeConfig()
{
Config cfg("WordGame");
cfg.setGroup("Game");
cfg.writeEntry("NameList",namelist,';');
cfg.writeEntry("CurrentPlayer",gameover ? 0 : player+1);
if ( !gameover ) {
cfg.writeEntry("Rules",rules);
bag->writeConfig(cfg);
board->writeConfig(cfg);
scoreinfo->writeConfig(cfg);
}
for (int p=0; p<nplayers; p++) {
cfg.setGroup("Player"+QString::number(p+1));
if ( gameover ) cfg.clearGroup(); else rack(p)->writeConfig(cfg);
}
}
void WordGame::readConfig()
{
Config cfg("WordGame");
cfg.setGroup("Game");
int currentplayer = cfg.readNumEntry("CurrentPlayer",0);
QStringList pnames = cfg.readListEntry("NameList",';');
if ( currentplayer ) {
gameover = FALSE;
rules = cfg.readEntry("Rules");
if ( rules.find("x-wordgamerules") >= 0 ) {
// rules files moved
rules = "Sample.rules";
}
if ( loadRules(rules) ) {
startGame(pnames);
bag->readConfig(cfg);
board->readConfig(cfg);
scoreinfo->readConfig(cfg);
for (int p=0; p<nplayers; p++) {
cfg.setGroup("Player"+QString::number(p+1));
rack(p)->readConfig(cfg);
}
player=currentplayer-1;
readyRack(player);
return;
}
}
// fall-back
openGameSelector(pnames);
}
void WordGame::openGameSelector(const QStringList& initnames)
{
toolbar->hide();
gameover = FALSE;
delete board;
board = 0;
delete racks;
racks = 0;
delete cpu;
cpu = 0;
newgame = new NewGame(vbox);
//Rules rules(this);
//connect(game.editrules, SIGNAL(clicked()), &rules, SLOT(editRules()));
//connect(&rules, SIGNAL(rulesChanged()), &game, SLOT(updateRuleSets()));
struct passwd* n = getpwuid(getuid());
QString playername = n ? n->pw_name : "";
if ( playername.isEmpty() ) {
playername = "Player";
}
newgame->player0->changeItem(playername,0);
newgame->player1->setCurrentItem(1);
newgame->updateRuleSets();
newgame->show();
connect(newgame->buttonOk, SIGNAL(clicked()), this, SLOT(startGame()));
}
void WordGame::startGame()
{
rules = newgame->ruleslist[newgame->rules->currentItem()];
if ( loadRules(rules) ) {
QStringList names;
names.append(newgame->player0->currentText());
names.append(newgame->player1->currentText());
names.append(newgame->player2->currentText());
names.append(newgame->player3->currentText());
names.append(newgame->player4->currentText());
names.append(newgame->player5->currentText());
delete newgame;
startGame(names);
} else {
// error...
delete newgame;
close();
}
}
void WordGame::startGame(const QStringList& playerlist)
{
toolbar->show();
racks = new QWidgetStack(vbox);
racks->setFixedHeight(TileItem::bigHeight()+2);
namelist.clear();
nplayers=0;
for (QStringList::ConstIterator it=playerlist.begin(); it!=playerlist.end(); ++it)
addPlayer(*it);
scoreinfo->init(namelist);
if ( nplayers ) {
player=0;
readyRack(player);
}
board->show();
racks->show();
}
bool WordGame::loadRules(const QString &name)
{
QString filename = Global::applicationFileName( "wordgame", name );
QFile file( filename );
if ( !file.open( IO_ReadOnly ) )
return FALSE;
QTextStream ts( &file );
QString title = name;
title.truncate( title.length() - 6 );
setCaption( title );
QString shapepixmap;
ts >> shapepixmap;
int htiles,vtiles;
ts >> htiles >> vtiles;
if ( htiles < 3 || vtiles < 3 )
return FALSE;
QString rule_shapes;
for (int i=0; i<vtiles; i++) {
QString line;
ts >> line;
rule_shapes += line;
}
static int rule_effects[12];
int re=0,e;
ts >> e;
while ( e && re < 10 ) {
rule_effects[re] = e;
if ( re++ < 10 ) ts >> e;
}
- QImage shim = Resource::loadImage(shapepixmap);
+ QImage shim = Resource::loadImage("wordgame/wordgame_shapes.xpm");
shim = shim.smoothScale((re-1)*TileItem::smallWidth(),TileItem::smallHeight());
QPixmap bgshapes;
bgshapes.convertFromImage(shim);
rule_effects[re++] = 100; // default bonus
board = new Board(bgshapes, htiles, vtiles, vbox);
board->setRules(rule_shapes, rule_effects);
connect(board, SIGNAL(temporaryScore(int)), scoreinfo, SLOT(showTemporaryScore(int)));
bag = new Bag;
int count;
ts >> count;
while ( count ) {
QString text;
int value;
ts >> text >> value;
if ( text == "_" )
text = "";
Tile t(text, value);
for (int n=count; n--; )
bag->add(t);
ts >> count;
}
return TRUE;
}
NewGame::NewGame(QWidget* parent) :
NewGameBase(parent)
{
}
void NewGame::updateRuleSets()
{
rules->clear();
QString rulesDir = Global::applicationFileName( "wordgame", "" );
QDir dir( rulesDir, "*.rules" );
ruleslist = dir.entryList();
if ( ruleslist.isEmpty() ) {
// Provide a sample
QFile file( rulesDir + "Sample.rules" );
if ( file.open( IO_WriteOnly ) ) {
file.writeBlock( sampleWGR, strlen(sampleWGR) );
file.close();
updateRuleSets();
}
return;
}
int newest=0;
int newest_age=INT_MAX;
QDateTime now = QDateTime::currentDateTime();
QStringList::Iterator it;
for ( it = ruleslist.begin(); it != ruleslist.end(); ++it ) {
QFileInfo fi((*it));
int age = fi.lastModified().secsTo(now);
QString name = *it;
name.truncate( name.length()-6 ); // remove extension
rules->insertItem( name );
if ( age < newest_age ) {
newest_age = age;
newest = rules->count()-1;
}
}
rules->setCurrentItem(newest);
}
Rules::Rules(QWidget* parent) :
RulesBase(parent,0,TRUE)
{
}
void Rules::editRules()
{
if ( exec() ) {
// ### create a new set of rules
emit rulesChanged();
}
}
void Rules::deleteRuleSet()
{
// ### delete existing rule set
emit rulesChanged();
}
void WordGame::addPlayer(const QString& name)
{
if ( !name.isEmpty() ) {
int colon = name.find(':');
int cpu = (colon >=0 && name.left(2) == "AI") ? name.mid(2,1).toInt() : 0;
addPlayer(name,cpu);
}
}
void WordGame::addPlayer(const QString& name, int cpu)
{
Rack* r = new Rack(rack_tiles,racks);
r->setPlayerName(name);
r->setComputerization(cpu);
racks->addWidget(r, nplayers);
refillRack(nplayers);
namelist.append(name);
++nplayers;
}
void WordGame::nextPlayer()
{
if ( !refillRack(player) ) {
endGame();
} else {
player = (player+1)%nplayers;
scoreinfo->setBoldOne(player);
readyRack(player);
}
}
bool WordGame::mayEndGame()
{
int out=-1;
int i;
for (i=0; i<nplayers; i++)
if ( !rack(i)->count() )
out = i;
if ( out<0 ) {
if ( QMessageBox::warning(this,tr("End game"),
tr("Do you want to end the game early?"),
tr("Yes"), tr("No") )!=0 )
{
return FALSE;
}
}
return TRUE;
}
void WordGame::endGame()
{
if ( gameover ) {
close();
return;
}
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);
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();
qDebug("%d,%d: %s (%d) for %d",current.x(),current.y(),st.latin1(),n,s);
}
*/
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;
}