00001 #include "backgammon.h"
00002 #include "aidialog.h"
00003 #include "filedialog.h"
00004 #include "playerdialog.h"
00005 #include "rulesdialog.h"
00006 #include "themedialog.h"
00007
00008
00009 #include <opie2/odebug.h>
00010 #include <opie2/oresource.h>
00011 #include <qpe/qpeapplication.h>
00012 #include <qpe/config.h>
00013 using namespace Opie::Core;
00014
00015
00016 #include <qfile.h>
00017 #include <qlayout.h>
00018 #include <qmessagebox.h>
00019 #include <qtimer.h>
00020 #include <qmenubar.h>
00021
00022 #include <stdlib.h>
00023
00024
00025 BackGammon::BackGammon(QWidget* parent, const char* name, WFlags fl)
00026 : QMainWindow(parent, name, fl)
00027 {
00028 if (!name) setName("BackGammon");
00029 setCaption(tr( "Backgammon") );
00030 setIcon( Opie::Core::OResource::loadPixmap( "backgammon" ) );
00031
00032 int a=0;
00033
00034 move=new MoveEngine();
00035
00036
00037 Config conf("backgammon");
00038 if(!conf.isValid())
00039 {
00040 odebug << "config file does not exist" << oendl;
00041 conf.setGroup("general");
00042 conf.writeEntry("theme","default");
00043 conf.setGroup("rules");
00044 conf.writeEntry("move_with_pieces_out",false);
00045 conf.writeEntry("nice_dice",false);
00046 conf.setGroup("display");
00047 conf.writeEntry("small",false);
00048 conf.writeEntry("warning",true);
00049 conf.setGroup("ai");
00050 conf.writeEntry("rescue",6);
00051 conf.writeEntry("eliminate",4);
00052 conf.writeEntry("expose",1);
00053 conf.writeEntry("protect",5);
00054 conf.writeEntry("safe",3);
00055 conf.writeEntry("empty",2);
00056
00057 }
00058 conf.setGroup("general");
00059 theme_name=conf.readEntry("theme","default");
00060 QString theme_file=QPEApplication::qpeDir()+"backgammon/"+theme_name+".theme";
00061
00062
00063 conf.setGroup("rules");
00064 rules.move_with_pieces_out=conf.readBoolEntry("move_with_pieces_out",false);
00065 rules.generous_dice=conf.readBoolEntry("nice_dice",false);
00066
00067 move->setRules(rules);
00068
00069
00070 AISettings ai;
00071 conf.setGroup("ai");
00072 ai.rescue=conf.readNumEntry("rescue",6);
00073 ai.eliminate=conf.readNumEntry("eliminate",4);
00074 ai.expose=conf.readNumEntry("expose",1);
00075 ai.protect=conf.readNumEntry("protect",5);
00076 ai.safe=conf.readNumEntry("safe",3);
00077 ai.empty=conf.readNumEntry("empty",2);
00078 move->setAISettings(ai);
00079
00080
00081
00082 Config theme(theme_file,Config::File);
00083 if(!theme.isValid())
00084 {
00085 odebug << "theme file does not exist" << oendl;
00086 theme.setGroup("theme");
00087 theme.writeEntry("board","casino_board_1");
00088 theme.writeEntry("pieces1","casino_pieces_blue");
00089 theme.writeEntry("pieces2","casino_pieces_white");
00090 theme.writeEntry("dice1","casino_dice");
00091 theme.writeEntry("dice2","casino_dice");
00092 theme.writeEntry("table","casino_table_green");
00093 theme.writeEntry("odds","casino_odds");
00094 }
00095 theme.setGroup("theme");
00096 board_name=theme.readEntry("board","casino_board_1");
00097 piecesA_name=theme.readEntry("pieces1","casino_pieces_blue");
00098 piecesB_name=theme.readEntry("pieces2","casino_pieces_white");
00099 diceA_name=theme.readEntry("dice1","casino_dice");
00100 diceB_name=theme.readEntry("dice2","casino_dice");
00101 table_name=theme.readEntry("table","casino_table_green");
00102 odds_name=theme.readEntry("odds","casino_odds");
00103
00104
00105
00106 QMenuBar* menuBar = new QMenuBar(this);
00107
00108 QPopupMenu* gamemenu= new QPopupMenu(this);
00109 gamemenu->insertItem(tr( "New" ),this,SLOT(newgame()));
00110 gamemenu->insertSeparator();
00111 gamemenu->insertItem(tr( "Load" ),this,SLOT(loadgame()));
00112 gamemenu->insertItem(tr( "Save" ),this,SLOT(savegame()));
00113 gamemenu->insertSeparator();
00114 gamemenu->insertItem(tr( "Delete" ),this,SLOT(deletegame()));
00115 menuBar->insertItem(tr( "Game" ),gamemenu);
00116
00117 QPopupMenu* thememenu= new QPopupMenu(this);
00118 thememenu->insertItem(tr( "New" ),this,SLOT(newtheme()));
00119 thememenu->insertSeparator();
00120 thememenu->insertItem(tr( "Load"),this,SLOT(loadtheme()));
00121 thememenu->insertItem(tr( "Save" ),this,SLOT(savetheme()));
00122 thememenu->insertSeparator();
00123 thememenu->insertItem(tr( "Default"),this,SLOT(themedefault()));
00124 thememenu->insertItem(tr( "Delete" ),this,SLOT(deletetheme()));
00125 menuBar->insertItem(tr( "Theme" ),thememenu);
00126
00127 QPopupMenu* optionmenu=new QPopupMenu(this);
00128 optionmenu->insertItem(tr( "Player" ),this,SLOT(playerselect()));
00129 optionmenu->insertSeparator();
00130 optionmenu->insertItem(tr( "AI" ),this,SLOT(modify_AI()));
00131 optionmenu->insertItem(tr( "Rules" ),this,SLOT(setrules()));
00132 menuBar->insertItem(tr( "Options"),optionmenu);
00133
00134 QWidget* mainarea=new QWidget(this);
00135 setCentralWidget(mainarea);
00136
00137 QBoxLayout* layout=new QBoxLayout(mainarea,QBoxLayout::TopToBottom);
00138 area=new QCanvas(235,235);
00139 boardview=new BackGammonView(area,mainarea);
00140 boardview->setMaximumHeight(240);
00141 layout->addWidget(boardview);
00142 connect(boardview,SIGNAL(mouse(int,int)),this,SLOT(mouse(int,int)));
00143
00144 message=new QLabel("<b>Backgammon</b>",mainarea);
00145 message->setAlignment(AlignHCenter);
00146 layout->addWidget(message);
00147
00148
00149 marker_current=new QCanvasRectangle(area);
00150 marker_current->setBrush(QColor(0,0,255));
00151 marker_current->setSize(15,5);
00152 marker_current->setZ(1);
00153
00154 for(a=0;a<4;a++)
00155 {
00156 marker_next[a]=new QCanvasRectangle(area);
00157 marker_next[a]->setBrush(QColor(0,255,0));
00158 marker_next[a]->setSize(15,5);
00159 marker_next[a]->setZ(1);
00160 }
00161
00162 connect(move,SIGNAL(done_dice1()),this,SLOT(done_dice1()));
00163 connect(move,SIGNAL(done_dice2()),this,SLOT(done_dice2()));
00164 connect(move,SIGNAL(done_dice3()),this,SLOT(done_dice3()));
00165 connect(move,SIGNAL(done_dice4()),this,SLOT(done_dice4()));
00166 connect(move,SIGNAL(nomove()),this,SLOT(nomove()));
00167 connect(move,SIGNAL(player_finished(int)),this,SLOT(finished(int)));
00168
00169
00170 p1=new CanvasImageItem*[15];
00171 p1_side=new CanvasImageItem*[15];
00172 QImage piece_1_all(Opie::Core::OResource::loadImage("backgammon/pieces/"+piecesA_name));
00173 QImage piece_1_front=piece_1_all.copy(0,0,15,15);
00174 QImage piece_1_side=piece_1_all.copy(0,15,15,5);
00175
00176 p2=new CanvasImageItem*[15];
00177 p2_side=new CanvasImageItem*[15];
00178 QImage piece_2_all(Opie::Core::OResource::loadImage("backgammon/pieces/"+piecesB_name));
00179 QImage piece_2_front=piece_2_all.copy(0,0,15,15);
00180 QImage piece_2_side=piece_2_all.copy(0,15,15,5);
00181
00182
00183 for(a=0;a<15;a++)
00184 {
00185 p1[a]=new CanvasImageItem(piece_1_front,area);
00186 p1[a]->setSize(15,15);
00187 p1_side[a]=new CanvasImageItem(piece_1_side,area);
00188 p1_side[a]->setSize(15,5);
00189
00190 p2[a]=new CanvasImageItem(piece_2_front,area);
00191 p2[a]->setSize(15,15);
00192 p2_side[a]=new CanvasImageItem(piece_2_side,area);
00193 p2_side[a]->setSize(15,5);
00194 }
00195 draw();
00196
00197
00198 QImage dicebgA_all(Opie::Core::OResource::loadImage("backgammon/dice/"+diceA_name));
00199 diceA1=new CanvasImageItem*[7];
00200 diceA2=new CanvasImageItem*[7];
00201 QImage dicebgB_all(Opie::Core::OResource::loadImage("backgammon/dice/"+diceB_name));
00202 diceB1=new CanvasImageItem*[7];
00203 diceB2=new CanvasImageItem*[7];
00204 QImage oddsbg_all=(Opie::Core::OResource::loadImage("backgammon/odds/"+odds_name));
00205
00206
00207
00208 for(a=0;a<7;a++)
00209 {
00210 QImage dicebgA=dicebgA_all.copy(a*25,0,25,25);
00211 diceA1[a]=new CanvasImageItem(dicebgA,area);
00212 diceA1[a]->setX(5);
00213 diceA1[a]->setY(205-2);
00214 diceA1[a]->setZ(1);
00215 diceA1[a]->setSize(25,25);
00216 diceA2[a]=new CanvasImageItem(dicebgA,area);
00217 diceA2[a]->setX(35);
00218 diceA2[a]->setY(205-2);
00219 diceA2[a]->setZ(1);
00220 diceA2[a]->setSize(25,25);
00221
00222 QImage dicebgB=dicebgB_all.copy(a*25,0,25,25);
00223 diceB1[a]=new CanvasImageItem(dicebgB,area);
00224 diceB1[a]->setX(175);
00225 diceB1[a]->setY(205-2);
00226 diceB1[a]->setZ(1);
00227 diceB1[a]->setSize(25,25);
00228 diceB2[a]=new CanvasImageItem(dicebgB,area);
00229 diceB2[a]->setX(205);
00230 diceB2[a]->setY(205-2);
00231 diceB2[a]->setZ(1);
00232 diceB2[a]->setSize(25,25);
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246 }
00247
00248
00249
00250 QImage boardbg(Opie::Core::OResource::loadImage("backgammon/boards/"+board_name));
00251 board=new CanvasImageItem(boardbg,area);
00252 board->setX(0);
00253 board->setY(0);
00254 board->setZ(0);
00255 board->setSize(235-2,200-2);
00256 board->show();
00257
00258
00259 QImage tablebg(Opie::Core::OResource::loadImage("backgammon/table/"+table_name));
00260 table=new CanvasImageItem(tablebg,area);
00261 table->setX(0);
00262 table->setY(200-2);
00263 table->setZ(0);
00264 table->setSize(235-2,20);
00265 table->show();
00266
00267
00268 QImage nomovebg(Opie::Core::OResource::loadImage("backgammon/no_move"));
00269 nomove_marker=new CanvasImageItem(nomovebg,area);
00270 nomove_marker->setX(0);
00271 nomove_marker->setY(200);
00272 nomove_marker->setZ(2);
00273 nomove_marker->hide();
00274
00275
00276 player1_auto=false;
00277 player2_auto=true;
00278
00279 newgame();
00280 }
00281
00282 BackGammon::~BackGammon()
00283 {
00284
00285 }
00286
00287 void BackGammon::newgame()
00288 {
00289 gameFinished=false;
00290 QDateTime now=QDateTime::currentDateTime();
00291 game_name=now.date().toString()+"_"+now.time().toString();
00292 move->reset();
00293 draw();
00294 diceA1_value=7;
00295 diceA2_value=7;
00296 diceA3_value=7;
00297 diceA4_value=7;
00298 diceB1_value=7;
00299 diceB2_value=7;
00300 diceB3_value=7;
00301 diceB4_value=7;
00302 showdice();
00303 player=2;
00304 dice1_played=true;
00305 dice2_played=true;
00306 dice3_played=true;
00307 dice4_played=true;
00308 dice_rolled=false;
00309 setplayer();
00310 area->update();
00311 }
00312
00313 void BackGammon::playerselect()
00314 {
00315 PlayerDialog* playerdialog=new PlayerDialog(this);
00316 playerdialog->setAuto1(player1_auto);
00317 playerdialog->setAuto2(player2_auto);
00318 if(!playerdialog->exec())
00319 return;
00320 player1_auto=playerdialog->getAuto1();
00321 player2_auto=playerdialog->getAuto2();
00322 }
00323
00324 void BackGammon::loadgame()
00325 {
00326 FileDialog* file=new FileDialog(this,"Load Game",".game");
00327 if(!file->exec())
00328 return;
00329
00330 game_name=file->filename();
00331 QString game_file=QPEApplication::qpeDir()+"backgammon/"+game_name+".game";
00332
00333 Config game(game_file,Config::File);
00334 game.setGroup("dice");
00335 diceA1_value=game.readNumEntry("diceA1_value");
00336 diceA2_value=game.readNumEntry("diceA2_value");
00337 diceA3_value=game.readNumEntry("diceA3_value");
00338 diceA4_value=game.readNumEntry("diceA4_value");
00339 diceB1_value=game.readNumEntry("diceB1_value");
00340 diceB2_value=game.readNumEntry("diceB2_value");
00341 diceB3_value=game.readNumEntry("diceB3_value");
00342 diceB4_value=game.readNumEntry("diceB4_value");
00343 player=game.readNumEntry("player");
00344 dice1_played=game.readBoolEntry("dice1_played");
00345 dice2_played=game.readBoolEntry("dice2_played");
00346 dice3_played=game.readBoolEntry("dice3_played");
00347 dice4_played=game.readBoolEntry("dice4_played");
00348 dice_rolled=game.readBoolEntry("dice_rolled");
00349 player1_auto=game.readBoolEntry("player1_auto");
00350 player2_auto=game.readBoolEntry("player2_auto");
00351
00352 game.setGroup("pieces");
00353 QString label;
00354 LoadSave load;
00355 for(int a=0;a<28;a++)
00356 {
00357 label.setNum(a);
00358 load.pop[a].total = game.readNumEntry(label,0);
00359 }
00360
00361 move->loadGame(load);
00362 setplayer();
00363 showdice();
00364 draw();
00365 area->update();
00366 }
00367
00368 void BackGammon::savegame()
00369 {
00370 QString game_file=QPEApplication::qpeDir()+"backgammon/"+game_name+".game";
00371
00372 Config game(game_file,Config::File);
00373 game.setGroup("dice");
00374 game.writeEntry("diceA1_value",diceA1_value);
00375 game.writeEntry("diceA2_value",diceA2_value);
00376 game.writeEntry("diceA3_value",diceA3_value);
00377 game.writeEntry("diceA4_value",diceA4_value);
00378 game.writeEntry("diceB1_value",diceB1_value);
00379 game.writeEntry("diceB2_value",diceB3_value);
00380 game.writeEntry("diceB3_value",diceB4_value);
00381 game.writeEntry("diceB4_value",diceB4_value);
00382 game.writeEntry("player",player);
00383 game.writeEntry("dice1_played",dice1_played);
00384 game.writeEntry("dice2_played",dice2_played);
00385 game.writeEntry("dice3_played",dice3_played);
00386 game.writeEntry("dice4_played",dice4_played);
00387 game.writeEntry("dice_rolled",dice_rolled);
00388 game.writeEntry("player1_auto",player1_auto);
00389 game.writeEntry("player2_auto",player2_auto);
00390
00391 game.setGroup("pieces");
00392 QString label;
00393 LoadSave save=move->saveGame();
00394 for(int a=0;a<28;a++)
00395 {
00396 label.setNum(a);
00397 game.writeEntry(label,save.pop[a].total);
00398 }
00399 QMessageBox::information(this,"Backgammon","Game saved","OK");
00400 }
00401
00402 void BackGammon::deletegame()
00403 {
00404 FileDialog* file=new FileDialog(this,"Delete Game",".game");
00405 if(!file->exec())
00406 return;
00407
00408 game_name=file->filename();
00409 QString game_file=QPEApplication::qpeDir()+"backgammon/"+game_name+".game";
00410
00411 if(!QMessageBox::warning(this,"Backgammon","deleted game\n"+game_name+" ?","OK","Cancel"))
00412 {
00413 QFile(game_file).remove();
00414 }
00415 }
00416
00417
00418 void BackGammon::newtheme()
00419 {
00420 ThemeDialog* theme=new ThemeDialog(this);
00421
00422 ImageNames names;
00423 names.theme=theme_name;
00424 names.board=board_name;
00425 names.pieces1=piecesA_name;
00426 names.pieces2=piecesB_name;
00427 names.dice1=diceA_name;
00428 names.dice2=diceB_name;
00429 names.odds=odds_name;
00430 names.table=table_name;
00431
00432 theme->setCurrent(names);
00433 if(!theme->exec())
00434 return;
00435
00436 names=theme->getNames();
00437 theme_name=names.theme;
00438 board_name=names.board;
00439 piecesA_name=names.pieces1;
00440 piecesB_name=names.pieces2;
00441 diceA_name=names.dice1;
00442 diceB_name=names.dice2;
00443 odds_name=names.odds;
00444 table_name=names.table;
00445
00446 applytheme();
00447 }
00448
00449 void BackGammon::loadtheme()
00450 {
00451 FileDialog* file=new FileDialog(this,"Load Theme",".theme");
00452 if(!file->exec())
00453 return;
00454
00455 theme_name=file->filename();
00456 QString theme_file=QPEApplication::qpeDir()+"backgammon/"+theme_name+".theme";
00457
00458 Config theme(theme_file,Config::File);
00459 theme.setGroup("theme");
00460 board_name=theme.readEntry("board","board_1");
00461 piecesA_name=theme.readEntry("pieces1","pieces_1");
00462 piecesB_name=theme.readEntry("pieces2","pieces_2");
00463 diceA_name=theme.readEntry("dice1","dice_1");
00464 diceB_name=theme.readEntry("dice2","dice_2");
00465 table_name=theme.readEntry("table","table_1");
00466 odds_name=theme.readEntry("odds","odds_1");
00467
00468 applytheme();
00469
00470 }
00471
00472 void BackGammon::savetheme()
00473 {
00474 if(theme_name=="default")
00475 {
00476 QMessageBox::information(this,"Backgammon","Sorry\nCannot overwrite default.theme","OK");
00477 return;
00478 }
00479 QString theme_file=QPEApplication::qpeDir()+"backgammon/"+theme_name+".theme";
00480 if(QMessageBox::information(this,"Backgammon","Save Theme\n"+theme_name,"Yes","No"))
00481 return;
00482
00483 Config theme(theme_file,Config::File);
00484 theme.setGroup("theme");
00485 theme.writeEntry("board",board_name);
00486 theme.writeEntry("pieces1",piecesA_name);
00487 theme.writeEntry("pieces2",piecesB_name);
00488 theme.writeEntry("dice1",diceA_name);
00489 theme.writeEntry("dice2",diceB_name);
00490 theme.writeEntry("table",table_name);
00491 theme.writeEntry("odds",odds_name);
00492
00493 }
00494
00495 void BackGammon::themedefault()
00496 {
00497 if(QMessageBox::information(this,"Backgammon","Make Theme\n"+theme_name+"\nthe default theme","Yes","No"))
00498 return;
00499
00500 Config conf("backgammon");
00501 conf.setGroup("general");
00502 conf.writeEntry("theme",theme_name);
00503 }
00504
00505 void BackGammon::deletetheme()
00506 {
00507 FileDialog* file=new FileDialog(this,"Delete Theme",".theme");
00508 if(!file->exec())
00509 return;
00510
00511 theme_name=file->filename();
00512 QString theme_file=QPEApplication::qpeDir()+"backgammon/"+theme_name+".theme";
00513
00514 if(!QMessageBox::warning(this,tr( "Backgammon" ),tr( "deleted theme %1?").arg(theme_name),tr( "OK" ),tr( "Cancel" )))
00515 {
00516 QFile(theme_file).remove();
00517 }
00518 }
00519
00520 void BackGammon::modify_AI()
00521 {
00522 AI_Dialog* ai_mod=new AI_Dialog(this,tr( "Load Theme" ),".theme");
00523 ai_mod->setAISettings(move->getAISettings());
00524 if(!ai_mod->exec())
00525 return;
00526
00527
00528 AISettings ai=ai_mod->getAISettings();
00529 move->setAISettings(ai);
00530
00531 Config conf("backgammon");
00532 conf.setGroup("ai");
00533 conf.writeEntry("rescue",ai.rescue);
00534 conf.writeEntry("eliminate",ai.eliminate);
00535 conf.writeEntry("expose",ai.expose);
00536 conf.writeEntry("protect",ai.protect);
00537 conf.writeEntry("safe",ai.safe);
00538 conf.writeEntry("empty",ai.empty);
00539 }
00540
00541 void BackGammon::setrules()
00542 {
00543 RulesDialog* rulesdialog=new RulesDialog(this,tr( "Load Theme" ),".theme");
00544 rulesdialog->setRules(rules);
00545 if(!rulesdialog->exec())
00546 return;
00547 rules=rulesdialog->getRules();
00548 Config conf("backgammon");
00549 conf.setGroup("rules");
00550 conf.writeEntry("move_with_pieces_out",rules.move_with_pieces_out);
00551 conf.writeEntry("nice_dice",rules.generous_dice);
00552 move->setRules(rules);
00553 }
00554
00555
00556 void BackGammon::draw()
00557 {
00558 Pieces pieces;
00559 move->position(pieces);
00560 for(int a=0;a<15;a++)
00561 {
00562 if(!pieces.player1[a].side)
00563 {
00564 p1[a]->setX(pieces.player1[a].x);
00565 p1[a]->setY(pieces.player1[a].y);
00566 p1[a]->setZ(pieces.player1[a].z);
00567 p1[a]->show();
00568 p1_side[a]->hide();
00569 }
00570 else
00571 {
00572 p1_side[a]->setX(pieces.player1[a].x);
00573 p1_side[a]->setY(pieces.player1[a].y);
00574 p1_side[a]->setZ(pieces.player1[a].z);
00575 p1_side[a]->show();
00576 p1[a]->hide();
00577 }
00578
00579 if(!pieces.player2[a].side)
00580 {
00581 p2[a]->setX(pieces.player2[a].x);
00582 p2[a]->setY(pieces.player2[a].y);
00583 p2[a]->setZ(pieces.player2[a].z);
00584 p2[a]->show();
00585 p2_side[a]->hide();
00586 }
00587 else
00588 {
00589 p2_side[a]->setX(pieces.player2[a].x);
00590 p2_side[a]->setY(pieces.player2[a].y);
00591 p2_side[a]->setZ(pieces.player2[a].z);
00592 p2_side[a]->show();
00593 p2[a]->hide();
00594 }
00595 }
00596 }
00597
00598 void BackGammon::mouse(int x,int y)
00599 {
00600 if(gameFinished)
00601 {
00602 newgame();
00603 return;
00604 }
00605 if(y<=200)
00606 {
00607 if((player==1 && player1_auto) || (player==2 && player2_auto))
00608 return;
00609
00610 Marker marker;
00611
00612 move->boardpressed(x,y,marker);
00613 if(marker.visible_current)
00614 {
00615 marker_current->setX(marker.x_current);
00616 marker_current->setY(marker.y_current);
00617 marker_current->show();
00618 }
00619 else
00620 {
00621 marker_current->hide();
00622 }
00623
00624 for(int a=0;a<4;a++)
00625 {
00626 if(marker.visible_next[a])
00627 {
00628 marker_next[a]->setX(marker.x_next[a]);
00629 marker_next[a]->setY(marker.y_next[a]);
00630 marker_next[a]->show();
00631 }
00632 else
00633 {
00634 marker_next[a]->hide();
00635 }
00636 }
00637 area->update();
00638 }
00639 else
00640 {
00641 if(x>=10 && x<=65 && player==1 && !dice_rolled)
00642 {
00643 dice1_played=false;
00644 dice2_played=false;
00645 dice3_played=false;
00646 dice4_played=false;
00647 dice_rolled=true;
00648 srand(QTime::currentTime().msec());
00649 diceA1_value=1+(int) (6.0*rand()/(RAND_MAX+1.0));
00650 diceA2_value=1+(int) (6.0*rand()/(RAND_MAX+1.0));
00651 if(diceA1_value==diceA2_value)
00652 {
00653 diceA3_value=diceA1_value;
00654 diceA4_value=diceA1_value;
00655 }
00656 else
00657 {
00658 diceA3_value=7;
00659 dice3_played=true;
00660 diceA4_value=7;
00661 dice4_played=true;
00662 }
00663 showdice();
00664 area->update();
00665 move->diceroll(1,diceA1_value,diceA2_value,diceA3_value,diceA4_value,player1_auto);
00666
00667 }
00668 else if(x>=160 && x<=225 && player==2 && !dice_rolled)
00669 {
00670 dice1_played=false;
00671 dice2_played=false;
00672 dice3_played=false;
00673 dice4_played=false;
00674 dice_rolled=true;
00675 srand(QTime::currentTime().msec());
00676 diceB1_value=1+(int) (6.0*rand()/(RAND_MAX+1.0));
00677 diceB2_value=1+(int) (6.0*rand()/(RAND_MAX+1.0));
00678 if(diceB1_value==diceB2_value)
00679 {
00680 diceB3_value=diceB1_value;
00681 diceB4_value=diceB1_value;
00682 }
00683 else
00684 {
00685 diceB3_value=7;
00686 dice3_played=true;
00687 diceB4_value=7;
00688 dice4_played=true;
00689 }
00690 showdice();
00691 area->update();
00692 move->diceroll(2,diceB1_value,diceB2_value,diceB3_value,diceB4_value,player2_auto);
00693 }
00694 }
00695 }
00696
00697 void BackGammon::done_dice1()
00698 {
00699 dice1_played=true;
00700 if(player==1)
00701 diceA1_value=7;
00702 else
00703 diceB1_value=7;
00704 setplayer();
00705 showdice();
00706 draw();
00707 area->update();
00708 if(!dice2_played || !dice3_played || !dice4_played)
00709 {
00710 if(player==1)
00711 {
00712 move->diceroll(1,diceA1_value,diceA2_value,diceA3_value,diceA4_value,player1_auto);
00713 }
00714 else
00715 {
00716 move->diceroll(2,diceB1_value,diceB2_value,diceB3_value,diceB4_value,player2_auto);
00717 }
00718 }
00719 }
00720
00721 void BackGammon::done_dice2()
00722 {
00723 dice2_played=true;
00724 if(player==1)
00725 diceA2_value=7;
00726 else
00727 diceB2_value=7;
00728 setplayer();
00729 showdice();
00730 draw();
00731 area->update();
00732 if(!dice1_played || !dice3_played || !dice4_played)
00733 {
00734 if(player==1)
00735 {
00736 move->diceroll(1,diceA1_value,diceA2_value,diceA3_value,diceA4_value,player1_auto);
00737 }
00738 else
00739 {
00740 move->diceroll(2,diceB1_value,diceB2_value,diceB3_value,diceB4_value,player2_auto);
00741 }
00742 }
00743 }
00744
00745
00746 void BackGammon::done_dice3()
00747 {
00748 dice3_played=true;
00749 if(player==1)
00750 diceA3_value=7;
00751 else
00752 diceB3_value=7;
00753 setplayer();
00754 showdice();
00755 draw();
00756 area->update();
00757 if(!dice1_played || !dice2_played || !dice4_played)
00758 {
00759 if(player==1)
00760 {
00761 move->diceroll(1,diceA1_value,diceA2_value,diceA3_value,diceA4_value,player1_auto);
00762 }
00763 else
00764 {
00765 move->diceroll(2,diceB1_value,diceB2_value,diceB3_value,diceB4_value,player2_auto);
00766 }
00767 }
00768 }
00769
00770
00771 void BackGammon::done_dice4()
00772 {
00773 dice4_played=true;
00774 if(player==1)
00775 diceA4_value=7;
00776 else
00777 diceB4_value=7;
00778 setplayer();
00779 showdice();
00780 draw();
00781 area->update();
00782 if(!dice1_played || !dice2_played || !dice3_played)
00783 {
00784 if(player==1)
00785 {
00786 move->diceroll(1,diceA1_value,diceA2_value,diceA3_value,diceA4_value,player1_auto);
00787 }
00788 else
00789 {
00790 move->diceroll(2,diceB1_value,diceB2_value,diceB3_value,diceB4_value,player2_auto);
00791 }
00792 }
00793 }
00794
00795
00796 void BackGammon::nomove()
00797 {
00798 if(player==1)
00799 nomove_marker->setX(0);
00800 else
00801 nomove_marker->setX(170);
00802 nomove_marker->show();
00803 message->setText(tr( "<b>no move</b>" ));
00804 dice1_played=true;
00805 dice2_played=true;
00806 dice3_played=true;
00807 dice4_played=true;
00808 if(player==1)
00809 {
00810 diceA1_value=7;
00811 diceA2_value=7;
00812 diceA3_value=7;
00813 diceA4_value=7;
00814 }
00815 else
00816 {
00817 diceB1_value=7;
00818 diceB2_value=7;
00819 diceB3_value=7;
00820 diceB4_value=7;
00821 }
00822 area->update();
00823 QTimer::singleShot(2000,this,SLOT(nomove2()));
00824 }
00825
00826 void BackGammon::nomove2()
00827 {
00828 nomove_marker->hide();
00829 setplayer();
00830 showdice();
00831 draw();
00832 area->update();
00833 }
00834
00835 void BackGammon::finished(int theplayer)
00836 {
00837 nomove_marker->hide();
00838 if(theplayer==1)
00839 message->setText(tr( "<b>Player 1 wins. Click on board for new game.</b>" ));
00840 else
00841 message->setText(tr( "<b>Player 2 wins. Click on board for new game.</b>" ));
00842 diceA1_value=7;
00843 diceA2_value=7;
00844 diceB1_value=7;
00845 diceB2_value=7;
00846 player=0;
00847 showdice();
00848 draw();
00849 area->update();
00850 gameFinished=true;
00851 }
00852
00853 void BackGammon::showdice()
00854 {
00855 int value_diceA1=diceA1_value-1;
00856 if(diceA1_value==7 && diceA3_value!=7)
00857 value_diceA1=diceA3_value-1;
00858
00859 int value_diceA2=diceA2_value-1;
00860 if(diceA2_value==7 && diceA4_value!=7)
00861 value_diceA2=diceA4_value-1;
00862
00863 int value_diceB1=diceB1_value-1;
00864 if(diceB1_value==7 && diceB3_value!=7)
00865 value_diceB1=diceB3_value-1;
00866
00867 int value_diceB2=diceB2_value-1;
00868 if(diceB2_value==7 && diceB4_value!=7)
00869 value_diceB2=diceB4_value-1;
00870
00871 for(int index=0;index<7;index++)
00872 {
00873 if(value_diceA1==index)
00874 diceA1[index]->show();
00875 else
00876 diceA1[index]->hide();
00877
00878 if(value_diceA2==index)
00879 diceA2[index]->show();
00880 else
00881 diceA2[index]->hide();
00882
00883 if(value_diceB1==index)
00884 diceB1[index]->show();
00885 else
00886 diceB1[index]->hide();
00887
00888 if(value_diceB2==index)
00889 diceB2[index]->show();
00890 else
00891 diceB2[index]->hide();
00892 }
00893 }
00894
00895 void BackGammon::setplayer()
00896 {
00897 if(dice1_played && dice2_played && dice3_played && dice4_played && player==1)
00898 {
00899 message->setText(tr( "<b>P2 turn</b>", "P means player" ));
00900 dice_rolled=false;
00901 player=2;
00902 if(player2_auto)
00903 QTimer::singleShot(2000,this,SLOT(autoroll_dice2()));
00904 }
00905 else if(dice1_played && dice2_played && dice3_played && dice4_played && player==2)
00906 {
00907 message->setText(tr( "<b>P1 turn</b>", "P means player" ));
00908 dice_rolled=false;
00909 player=1;
00910 if(player1_auto)
00911 QTimer::singleShot(2000,this,SLOT(autoroll_dice1()));
00912 }
00913 }
00914
00915 void BackGammon::autoroll_dice1()
00916 {
00917 mouse(20,210);
00918 }
00919
00920 void BackGammon::autoroll_dice2()
00921 {
00922 mouse(170,210);
00923 }
00924
00925 void BackGammon::applytheme()
00926 {
00927 QImage boardbg(Opie::Core::OResource::loadImage("backgammon/boards/"+board_name));
00928 board->setImage(boardbg);
00929
00930 QImage tablebg(Opie::Core::OResource::loadImage("backgammon/table/"+table_name));
00931 table->setImage(tablebg);
00932
00933 QImage piece_1_all(Opie::Core::OResource::loadImage("backgammon/pieces/"+piecesA_name));
00934 QImage piece_1_front=piece_1_all.copy(0,0,15,15);
00935 QImage piece_1_side=piece_1_all.copy(0,15,15,5);
00936
00937 QImage piece_2_all(Opie::Core::OResource::loadImage("backgammon/pieces/"+piecesB_name));
00938 QImage piece_2_front=piece_2_all.copy(0,0,15,15);
00939 QImage piece_2_side=piece_2_all.copy(0,15,15,5);
00940
00941 int a=0;
00942 for(a=0;a<15;a++)
00943 {
00944 p1[a]->setImage(piece_1_front);
00945 p1_side[a]->setImage(piece_1_side);
00946
00947 p2[a]->setImage(piece_2_front);
00948 p2_side[a]->setImage(piece_2_side);
00949 }
00950 draw();
00951
00952 QImage dicebgA_all(Opie::Core::OResource::loadImage("backgammon/dice/"+diceA_name));
00953 QImage dicebgB_all(Opie::Core::OResource::loadImage("backgammon/dice/"+diceB_name));
00954 QImage oddsbg_all=(Opie::Core::OResource::loadImage("backgammon/odds/"+odds_name));
00955
00956 for(a=0;a<7;a++)
00957 {
00958 QImage dicebgA=dicebgA_all.copy(a*25,0,25,25);
00959 diceA1[a]->setImage(dicebgA);
00960 diceA2[a]->setImage(dicebgA);
00961
00962 QImage dicebgB=dicebgB_all.copy(a*25,0,25,25);
00963 diceB1[a]->setImage(dicebgB);
00964 diceB2[a]->setImage(dicebgB);
00965
00966
00967
00968
00969
00970
00971
00972 }
00973 }
00974
00975