summaryrefslogtreecommitdiff
path: root/noncore/games/backgammon/backgammon.cpp
authorwaspe <waspe>2003-03-30 11:30:54 (UTC)
committer waspe <waspe>2003-03-30 11:30:54 (UTC)
commit3065a1c13bec5d0b101bec48ff666d45a02ce8a6 (patch) (unidiff)
treef8fe8d3eba81ef4e371c12f530be9cd4debc895b /noncore/games/backgammon/backgammon.cpp
parent87ba3646367397e149979a8477a33239cb341e99 (diff)
downloadopie-3065a1c13bec5d0b101bec48ff666d45a02ce8a6.zip
opie-3065a1c13bec5d0b101bec48ff666d45a02ce8a6.tar.gz
opie-3065a1c13bec5d0b101bec48ff666d45a02ce8a6.tar.bz2
*** empty log message ***
Diffstat (limited to 'noncore/games/backgammon/backgammon.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/backgammon/backgammon.cpp1043
1 files changed, 1043 insertions, 0 deletions
diff --git a/noncore/games/backgammon/backgammon.cpp b/noncore/games/backgammon/backgammon.cpp
new file mode 100644
index 0000000..38ccb49
--- a/dev/null
+++ b/noncore/games/backgammon/backgammon.cpp
@@ -0,0 +1,1043 @@
1#include "backgammon.h"
2
3#include "aidialog.h"
4#include "displaydialog.h"
5#include "filedialog.h"
6#include "playerdialog.h"
7#include "rulesdialog.h"
8#include "themedialog.h"
9
10#include <qdatetime.h>
11#include <qfile.h>
12#include <qmessagebox.h>
13#include <qstring.h>
14#include <qtimer.h>
15#include <qpe/qpeapplication.h>
16#include <qpe/config.h>
17#include <qpe/qpemenubar.h>
18#include <qpe/resource.h>
19
20#include <stdlib.h>
21
22BackGammonView::BackGammonView(QCanvas* canvas,QWidget* parent)
23 :QCanvasView(canvas,parent)
24{
25 //do nothing
26}
27
28
29
30BackGammonView::~BackGammonView()
31{
32 //do nothing
33}
34
35
36
37void BackGammonView::contentsMousePressEvent(QMouseEvent* e)
38{
39 int x=e->x();
40 int y=e->y();
41 emit mouse(x,y);
42}
43
44BackGammon::BackGammon(QWidget* parent, const char* name, WFlags fl)
45 : QWidget(parent, name, fl)
46{
47 if (!name) setName("BackGammon");
48 setCaption("Backgammon");
49 setIcon( Resource::loadPixmap( "backgammon" ) );
50 //general counter varaible
51 int a=0;
52 //the game engine
53 move=new MoveEngine();
54
55 //load the default theme
56 Config conf("backgammon");
57 if(!conf.isValid())
58 {
59 qDebug("config file does not exist");
60 conf.setGroup("general");
61 conf.writeEntry("theme","default");
62 conf.setGroup("rules");
63 conf.writeEntry("move_with_pieces_out",false);
64 conf.writeEntry("nice_dice",false);
65 conf.setGroup("display");
66 conf.writeEntry("small",false);
67 conf.writeEntry("warning",true);
68 conf.setGroup("ai");
69 conf.writeEntry("rescue",6);
70 conf.writeEntry("eliminate",4);
71 conf.writeEntry("expose",1);
72 conf.writeEntry("protect",5);
73 conf.writeEntry("safe",3);
74 conf.writeEntry("empty",2);
75
76 }
77 conf.setGroup("general");
78 theme_name=conf.readEntry("theme","default");
79 QString theme_file=QPEApplication::qpeDir()+"/backgammon/"+theme_name+".theme";
80
81 //the rules
82 conf.setGroup("rules");
83 rules.move_with_pieces_out=conf.readBoolEntry("move_with_pieces_out",false);
84 rules.generous_dice=conf.readBoolEntry("nice_dice",false);
85
86 move->setRules(rules);
87
88 //get the AI settings
89 AISettings ai;
90 conf.setGroup("ai");
91 ai.rescue=conf.readNumEntry("rescue",6);
92 ai.eliminate=conf.readNumEntry("eliminate",4);
93 ai.expose=conf.readNumEntry("expose",1);
94 ai.protect=conf.readNumEntry("protect",5);
95 ai.safe=conf.readNumEntry("safe",3);
96 ai.empty=conf.readNumEntry("empty",2);
97 move->setAISettings(ai);
98
99 //non qte styles are smaller
100 conf.setGroup("display");
101 display.small=conf.readBoolEntry("small",false);
102 display.warning=conf.readBoolEntry("warning",true);
103 non_qte=display.small;
104 if(display.warning)
105 {
106 Config test("qpe");
107 test.setGroup("Appearance");
108 if(test.readEntry("Style")!="QPE")
109 {
110 displaySettings();
111 }
112 }
113 offset=(non_qte) ? 5 : 0;
114
115 //get the theme component names
116 Config theme(theme_file,Config::File);
117 if(!theme.isValid())
118 {
119 qDebug("theme file does not exist");
120 theme.setGroup("theme");
121 theme.writeEntry("board","casino_board_1");
122 theme.writeEntry("pieces1","casino_pieces_blue");
123 theme.writeEntry("pieces2","casino_pieces_white");
124 theme.writeEntry("dice1","casino_dice");
125 theme.writeEntry("dice2","casino_dice");
126 theme.writeEntry("table","casino_table_green");
127 theme.writeEntry("odds","casino_odds");
128 }
129 theme.setGroup("theme");
130 board_name=theme.readEntry("board","casino_board_1");
131 piecesA_name=theme.readEntry("pieces1","casino_pieces_blue");
132 piecesB_name=theme.readEntry("pieces2","casino_pieces_white");
133 diceA_name=theme.readEntry("dice1","casino_dice");
134 diceB_name=theme.readEntry("dice2","casino_dice");
135 table_name=theme.readEntry("table","casino_table_green");
136 odds_name=theme.readEntry("odds","casino_odds");
137
138
139 //the menu
140 QPEMenuBar* menuBar = new QPEMenuBar(this);
141
142 QPopupMenu* gamemenu= new QPopupMenu(this);
143 gamemenu->insertItem("New",this,SLOT(newgame()));
144 gamemenu->insertSeparator();
145 gamemenu->insertItem("Load",this,SLOT(loadgame()));
146 gamemenu->insertItem("Save",this,SLOT(savegame()));
147 gamemenu->insertSeparator();
148 gamemenu->insertItem("Delete",this,SLOT(deletegame()));
149 menuBar->insertItem("Game",gamemenu);
150
151 QPopupMenu* thememenu= new QPopupMenu(this);
152 thememenu->insertItem("New",this,SLOT(newtheme()));
153 thememenu->insertSeparator();
154 thememenu->insertItem("Load",this,SLOT(loadtheme()));
155 thememenu->insertItem("Save",this,SLOT(savetheme()));
156 thememenu->insertSeparator();
157 thememenu->insertItem("Default",this,SLOT(themedefault()));
158 thememenu->insertItem("Delete",this,SLOT(deletetheme()));
159 menuBar->insertItem("Theme",thememenu);
160
161 QPopupMenu* optionmenu=new QPopupMenu(this);
162 optionmenu->insertItem("Player",this,SLOT(playerselect()));
163 optionmenu->insertSeparator();
164 optionmenu->insertItem("AI",this,SLOT(modify_AI()));
165 optionmenu->insertItem("Rules",this,SLOT(setrules()));
166 optionmenu->insertItem("Display",this,SLOT(displaySettings()));
167 menuBar->insertItem("Options",optionmenu);
168
169 //status bar
170 message=new QLabel("<b>Backgammon</b>",this);
171 message->setGeometry(0,260,237,20);
172 message->setAlignment(AlignHCenter);
173
174 //the main area
175 area=new QCanvas(235-2*offset,235-2*offset);
176 boardview=new BackGammonView(area,this);
177 connect(boardview,SIGNAL(mouse(int,int)),this,SLOT(mouse(int,int)));
178 if(non_qte)
179 {
180 boardview->setGeometry(5,20,229,229);
181 }
182 else
183 {
184 boardview->setGeometry(1,20,237,237);
185 }
186
187 //the marker
188 marker_current=new QCanvasRectangle(area);
189 marker_current->setBrush(QColor(0,0,255));
190 marker_current->setSize(15,5);
191 marker_current->setZ(1);
192
193 for(a=0;a<4;a++)
194 {
195 marker_next[a]=new QCanvasRectangle(area);
196 marker_next[a]->setBrush(QColor(0,255,0));
197 marker_next[a]->setSize(15,5);
198 marker_next[a]->setZ(1);
199 }
200
201 connect(move,SIGNAL(done_dice1()),this,SLOT(done_dice1()));
202 connect(move,SIGNAL(done_dice2()),this,SLOT(done_dice2()));
203 connect(move,SIGNAL(done_dice3()),this,SLOT(done_dice3()));
204 connect(move,SIGNAL(done_dice4()),this,SLOT(done_dice4()));
205 connect(move,SIGNAL(nomove()),this,SLOT(nomove()));
206 connect(move,SIGNAL(player_finished(int)),this,SLOT(finished(int)));
207
208 //the pieces
209 p1=new CanvasImageItem*[15];
210 p1_side=new CanvasImageItem*[15];
211 QImage piece_1_all(Resource::loadImage("backgammon/pieces/"+piecesA_name));
212 QImage piece_1_front=piece_1_all.copy(0,0,15,15);
213 QImage piece_1_side=piece_1_all.copy(0,15,15,5);
214
215 p2=new CanvasImageItem*[15];
216 p2_side=new CanvasImageItem*[15];
217 QImage piece_2_all(Resource::loadImage("backgammon/pieces/"+piecesB_name));
218 QImage piece_2_front=piece_2_all.copy(0,0,15,15);
219 QImage piece_2_side=piece_2_all.copy(0,15,15,5);
220
221
222 for(a=0;a<15;a++)
223 {
224 p1[a]=new CanvasImageItem(piece_1_front,area);
225 p1[a]->setSize(15,15);
226 p1_side[a]=new CanvasImageItem(piece_1_side,area);
227 p1_side[a]->setSize(15,5);
228
229 p2[a]=new CanvasImageItem(piece_2_front,area);
230 p2[a]->setSize(15,15);
231 p2_side[a]=new CanvasImageItem(piece_2_side,area);
232 p2_side[a]->setSize(15,5);
233 }
234 draw();
235
236 //the dice
237 QImage dicebgA_all(Resource::loadImage("backgammon/dice/"+diceA_name));
238 diceA1=new CanvasImageItem*[7];
239 diceA2=new CanvasImageItem*[7];
240 QImage dicebgB_all(Resource::loadImage("backgammon/dice/"+diceB_name));
241 diceB1=new CanvasImageItem*[7];
242 diceB2=new CanvasImageItem*[7];
243 QImage oddsbg_all=(Resource::loadImage("backgammon/odds/"+odds_name));
244 //oddsDice=new CanvasImageItem*[6];
245
246
247 for(a=0;a<7;a++)
248 {
249 QImage dicebgA=dicebgA_all.copy(a*25,0,25,25);
250 diceA1[a]=new CanvasImageItem(dicebgA,area);
251 diceA1[a]->setX(5-offset);
252 diceA1[a]->setY(205-2*offset);
253 diceA1[a]->setZ(1);
254 diceA1[a]->setSize(25,25);
255 diceA2[a]=new CanvasImageItem(dicebgA,area);
256 diceA2[a]->setX(35-offset);
257 diceA2[a]->setY(205-2*offset);
258 diceA2[a]->setZ(1);
259 diceA2[a]->setSize(25,25);
260
261 QImage dicebgB=dicebgB_all.copy(a*25,0,25,25);
262 diceB1[a]=new CanvasImageItem(dicebgB,area);
263 diceB1[a]->setX(175-offset);
264 diceB1[a]->setY(205-2*offset);
265 diceB1[a]->setZ(1);
266 diceB1[a]->setSize(25,25);
267 diceB2[a]=new CanvasImageItem(dicebgB,area);
268 diceB2[a]->setX(205-offset);
269 diceB2[a]->setY(205-2*offset);
270 diceB2[a]->setZ(1);
271 diceB2[a]->setSize(25,25);
272
273 /*
274 if(a<6)
275 {
276 QImage oddsbg=oddsbg_all.copy(a*15,0,15,15);
277 oddsDice[a]=new CanvasImageItem(oddsbg,area);
278 oddsDice[a]->setX(110-offset);
279 oddsDice[a]->setY(210-2*offset);
280 oddsDice[a]->setZ(1);
281 oddsDice[a]->setSize(15,15);
282 oddsDice[a]->hide();
283 }
284 */
285 }
286 //oddsDice[0]->show();
287
288 //set the board
289 QImage boardbg(Resource::loadImage("backgammon/boards/"+board_name));
290 if(non_qte)
291 {
292 boardbg=boardbg.copy(offset,offset,235-2*offset,200-2*offset);
293 }
294 board=new CanvasImageItem(boardbg,area);
295 board->setX(0);
296 board->setY(0);
297 board->setZ(0);
298 board->setSize(235-2*offset,200-2*offset);
299 board->show();
300
301 //the table
302 QImage tablebg(Resource::loadImage("backgammon/table/"+table_name));
303 if(non_qte)
304 {
305 tablebg=tablebg.copy(offset,0,235-offset,200);
306 }
307 table=new CanvasImageItem(tablebg,area);
308 table->setX(0);
309 table->setY(200-2*offset);
310 table->setZ(0);
311 table->setSize(235-2*offset,20);
312 table->show();
313
314 //the no move marker
315 QImage nomovebg(Resource::loadImage("backgammon/no_move"));
316 nomove_marker=new CanvasImageItem(nomovebg,area);
317 nomove_marker->setX(0);
318 nomove_marker->setY(200);
319 nomove_marker->setZ(2);
320 nomove_marker->hide();
321
322 //default human against computer
323 player1_auto=false;
324 player2_auto=true;
325 //start new game
326 newgame();
327}
328
329BackGammon::~BackGammon()
330{
331 //DESTRUCTOR
332}
333
334void BackGammon::newgame()
335{
336 gameFinished=false;
337 QDateTime now=QDateTime::currentDateTime();
338 game_name=now.date().toString()+"_"+now.time().toString();
339 move->reset();
340 draw();
341 diceA1_value=7;
342 diceA2_value=7;
343 diceA3_value=7;
344 diceA4_value=7;
345 diceB1_value=7;
346 diceB2_value=7;
347 diceB3_value=7;
348 diceB4_value=7;
349 showdice();
350 player=2;
351 dice1_played=true;
352 dice2_played=true;
353 dice3_played=true;
354 dice4_played=true;
355 dice_rolled=false;
356 setplayer();
357 area->update();
358}
359
360void BackGammon::playerselect()
361{
362 PlayerDialog* playerdialog=new PlayerDialog(this);
363 playerdialog->setAuto1(player1_auto);
364 playerdialog->setAuto2(player2_auto);
365 if(!playerdialog->exec())
366 return;
367 player1_auto=playerdialog->getAuto1();
368 player2_auto=playerdialog->getAuto2();
369}
370
371void BackGammon::loadgame()
372{
373 FileDialog* file=new FileDialog(this,"Load Game",".game");
374 if(!file->exec())
375 return;
376
377 game_name=file->filename();
378 QString game_file=QPEApplication::qpeDir()+"/backgammon/"+game_name+".game";
379
380 Config game(game_file,Config::File);
381 game.setGroup("dice");
382 diceA1_value=game.readNumEntry("diceA1_value");
383 diceA2_value=game.readNumEntry("diceA2_value");
384 diceA3_value=game.readNumEntry("diceA3_value");
385 diceA4_value=game.readNumEntry("diceA4_value");
386 diceB1_value=game.readNumEntry("diceB1_value");
387 diceB2_value=game.readNumEntry("diceB2_value");
388 diceB3_value=game.readNumEntry("diceB3_value");
389 diceB4_value=game.readNumEntry("diceB4_value");
390 player=game.readNumEntry("player");
391 dice1_played=game.readBoolEntry("dice1_played");
392 dice2_played=game.readBoolEntry("dice2_played");
393 dice3_played=game.readBoolEntry("dice3_played");
394 dice4_played=game.readBoolEntry("dice4_played");
395 dice_rolled=game.readBoolEntry("dice_rolled");
396 player1_auto=game.readBoolEntry("player1_auto");
397 player2_auto=game.readBoolEntry("player2_auto");
398
399 game.setGroup("pieces");
400 QString label;
401 LoadSave load;
402 for(int a=0;a<28;a++)
403 {
404 label.setNum(a);
405 load.pop[a].total = game.readNumEntry(label,0);
406 }
407
408 move->loadGame(load);
409 setplayer();
410 showdice();
411 draw();
412 area->update();
413}
414
415void BackGammon::savegame()
416{
417 QString game_file=QPEApplication::qpeDir()+"/backgammon/"+game_name+".game";
418
419 Config game(game_file,Config::File);
420 game.setGroup("dice");
421 game.writeEntry("diceA1_value",diceA1_value);
422 game.writeEntry("diceA2_value",diceA2_value);
423 game.writeEntry("diceA3_value",diceA3_value);
424 game.writeEntry("diceA4_value",diceA4_value);
425 game.writeEntry("diceB1_value",diceB1_value);
426 game.writeEntry("diceB2_value",diceB3_value);
427 game.writeEntry("diceB3_value",diceB4_value);
428 game.writeEntry("diceB4_value",diceB4_value);
429 game.writeEntry("player",player);
430 game.writeEntry("dice1_played",dice1_played);
431 game.writeEntry("dice2_played",dice2_played);
432 game.writeEntry("dice3_played",dice3_played);
433 game.writeEntry("dice4_played",dice4_played);
434 game.writeEntry("dice_rolled",dice_rolled);
435 game.writeEntry("player1_auto",player1_auto);
436 game.writeEntry("player2_auto",player2_auto);
437
438 game.setGroup("pieces");
439 QString label;
440 LoadSave save=move->saveGame();
441 for(int a=0;a<28;a++)
442 {
443 label.setNum(a);
444 game.writeEntry(label,save.pop[a].total);
445 }
446 QMessageBox::information(this,"Backgammon","Game saved","OK");
447}
448
449void BackGammon::deletegame()
450{
451 FileDialog* file=new FileDialog(this,"Delete Game",".game");
452 if(!file->exec())
453 return;
454
455 game_name=file->filename();
456 QString game_file=QPEApplication::qpeDir()+"/backgammon/"+game_name+".game";
457
458 if(!QMessageBox::warning(this,"Backgammon","deleted game\n"+game_name+" ?","OK","Cancel"))
459 {
460 QFile(game_file).remove();
461 }
462}
463
464
465void BackGammon::newtheme()
466{
467 ThemeDialog* theme=new ThemeDialog(this);
468
469 ImageNames names;
470 names.theme=theme_name;
471 names.board=board_name;
472 names.pieces1=piecesA_name;
473 names.pieces2=piecesB_name;
474 names.dice1=diceA_name;
475 names.dice2=diceB_name;
476 names.odds=odds_name;
477 names.table=table_name;
478
479 theme->setCurrent(names);
480 if(!theme->exec())
481 return;
482
483 names=theme->getNames();
484 theme_name=names.theme;
485 board_name=names.board;
486 piecesA_name=names.pieces1;
487 piecesB_name=names.pieces2;
488 diceA_name=names.dice1;
489 diceB_name=names.dice2;
490 odds_name=names.odds;
491 table_name=names.table;
492
493 applytheme();
494}
495
496void BackGammon::loadtheme()
497{
498 FileDialog* file=new FileDialog(this,"Load Theme",".theme");
499 if(!file->exec())
500 return;
501
502 theme_name=file->filename();
503 QString theme_file=QPEApplication::qpeDir()+"/backgammon/"+theme_name+".theme";
504
505 Config theme(theme_file,Config::File);
506 theme.setGroup("theme");
507 board_name=theme.readEntry("board","board_1");
508 piecesA_name=theme.readEntry("pieces1","pieces_1");
509 piecesB_name=theme.readEntry("pieces2","pieces_2");
510 diceA_name=theme.readEntry("dice1","dice_1");
511 diceB_name=theme.readEntry("dice2","dice_2");
512 table_name=theme.readEntry("table","table_1");
513 odds_name=theme.readEntry("odds","odds_1");
514
515 applytheme();
516
517}
518
519void BackGammon::savetheme()
520{
521 if(theme_name=="default")
522 {
523 QMessageBox::information(this,"Backgammon","Sorry\nCannot overwrite default.theme","OK");
524 return;
525 }
526 QString theme_file=QPEApplication::qpeDir()+"/backgammon/"+theme_name+".theme";
527 if(QMessageBox::information(this,"Backgammon","Save Theme\n"+theme_name,"Yes","No"))
528 return;
529
530 Config theme(theme_file,Config::File);
531 theme.setGroup("theme");
532 theme.writeEntry("board",board_name);
533 theme.writeEntry("pieces1",piecesA_name);
534 theme.writeEntry("pieces2",piecesB_name);
535 theme.writeEntry("dice1",diceA_name);
536 theme.writeEntry("dice2",diceB_name);
537 theme.writeEntry("table",table_name);
538 theme.writeEntry("odds",odds_name);
539
540}
541
542void BackGammon::themedefault()
543{
544 if(QMessageBox::information(this,"Backgammon","Make Theme\n"+theme_name+"\nthe default theme","Yes","No"))
545 return;
546
547 Config conf("backgammon");
548 conf.setGroup("general");
549 conf.writeEntry("theme",theme_name);
550}
551
552void BackGammon::deletetheme()
553{
554 FileDialog* file=new FileDialog(this,"Delete Theme",".theme");
555 if(!file->exec())
556 return;
557
558 theme_name=file->filename();
559 QString theme_file=QPEApplication::qpeDir()+"/backgammon/"+theme_name+".theme";
560
561 if(!QMessageBox::warning(this,"Backgammon","deleted theme "+theme_name+" ?","OK","Cancel"))
562 {
563 QFile(theme_file).remove();
564 }
565}
566
567void BackGammon::modify_AI()
568{
569 AI_Dialog* ai_mod=new AI_Dialog(this,"Load Theme",".theme");
570 ai_mod->setAISettings(move->getAISettings());
571 if(!ai_mod->exec())
572 return;
573
574 //get the AI settings
575 AISettings ai=ai_mod->getAISettings();
576 move->setAISettings(ai);
577 //write new settings to conf file
578 Config conf("backgammon");
579 conf.setGroup("ai");
580 conf.writeEntry("rescue",ai.rescue);
581 conf.writeEntry("eliminate",ai.eliminate);
582 conf.writeEntry("expose",ai.expose);
583 conf.writeEntry("protect",ai.protect);
584 conf.writeEntry("safe",ai.safe);
585 conf.writeEntry("empty",ai.empty);
586}
587
588void BackGammon::setrules()
589{
590 RulesDialog* rulesdialog=new RulesDialog(this,"Load Theme",".theme");
591 rulesdialog->setRules(rules);
592 if(!rulesdialog->exec())
593 return;
594 rules=rulesdialog->getRules();
595 Config conf("backgammon");
596 conf.setGroup("rules");
597 conf.writeEntry("move_with_pieces_out",rules.move_with_pieces_out);
598 conf.writeEntry("nice_dice",rules.generous_dice);
599 move->setRules(rules);
600}
601
602void BackGammon::displaySettings()
603{
604 DisplayDialog* displaydialog=new DisplayDialog(this);
605 displaydialog->setDisplaySettings(display);
606 if(!displaydialog->exec())
607 return;
608 display=displaydialog->getDisplaySettings();
609 Config conf("backgammon");
610 conf.setGroup("display");
611 conf.writeEntry("small",display.small);
612 conf.writeEntry("warning",display.warning);
613 QMessageBox::warning(this,"Backgammon","changed display settings will\nonly take place after game has\nbeen restarted","OK");
614}
615
616void BackGammon::draw()
617{
618 Pieces pieces;
619 move->position(pieces,non_qte);
620 for(int a=0;a<15;a++)
621 {
622 if(!pieces.player1[a].side)
623 {
624 p1[a]->setX(pieces.player1[a].x);
625 p1[a]->setY(pieces.player1[a].y);
626 p1[a]->setZ(pieces.player1[a].z);
627 p1[a]->show();
628 p1_side[a]->hide();
629 }
630 else
631 {
632 p1_side[a]->setX(pieces.player1[a].x);
633 p1_side[a]->setY(pieces.player1[a].y);
634 p1_side[a]->setZ(pieces.player1[a].z);
635 p1_side[a]->show();
636 p1[a]->hide();
637 }
638
639 if(!pieces.player2[a].side)
640 {
641 p2[a]->setX(pieces.player2[a].x);
642 p2[a]->setY(pieces.player2[a].y);
643 p2[a]->setZ(pieces.player2[a].z);
644 p2[a]->show();
645 p2_side[a]->hide();
646 }
647 else
648 {
649 p2_side[a]->setX(pieces.player2[a].x);
650 p2_side[a]->setY(pieces.player2[a].y);
651 p2_side[a]->setZ(pieces.player2[a].z);
652 p2_side[a]->show();
653 p2[a]->hide();
654 }
655 }
656}
657
658void BackGammon::mouse(int x,int y)
659{
660 if(gameFinished)
661 {
662 newgame();
663 return;
664 }
665 if(y<=200) //move pieces
666 {
667 if((player==1 && player1_auto) || (player==2 && player2_auto))
668 return;
669
670 Marker marker;
671
672 move->boardpressed(x,y,marker,non_qte);
673 if(marker.visible_current)
674 {
675 marker_current->setX(marker.x_current-offset);
676 marker_current->setY(marker.y_current);
677 marker_current->show();
678 }
679 else
680 {
681 marker_current->hide();
682 }
683
684 for(int a=0;a<4;a++)
685 {
686 if(marker.visible_next[a])
687 {
688 marker_next[a]->setX(marker.x_next[a]-offset);
689 marker_next[a]->setY(marker.y_next[a]);
690 marker_next[a]->show();
691 }
692 else
693 {
694 marker_next[a]->hide();
695 }
696 }
697 area->update();
698 }
699 else //roll dice
700 {
701 if(x>=10 && x<=65 && player==1 && !dice_rolled)
702 {
703 dice1_played=false;
704 dice2_played=false;
705 dice3_played=false;
706 dice4_played=false;
707 dice_rolled=true;
708 srand(QTime::currentTime().msec());
709 diceA1_value=1+(int) (6.0*rand()/(RAND_MAX+1.0));
710 diceA2_value=1+(int) (6.0*rand()/(RAND_MAX+1.0));
711 if(diceA1_value==diceA2_value)
712 {
713 diceA3_value=diceA1_value;
714 diceA4_value=diceA1_value;
715 }
716 else
717 {
718 diceA3_value=7;
719 dice3_played=true;
720 diceA4_value=7;
721 dice4_played=true;
722 }
723 showdice();
724 area->update();
725 move->diceroll(1,diceA1_value,diceA2_value,diceA3_value,diceA4_value,player1_auto);
726
727 }
728 else if(x>=160 && x<=225 && player==2 && !dice_rolled)
729 {
730 dice1_played=false;
731 dice2_played=false;
732 dice3_played=false;
733 dice4_played=false;
734 dice_rolled=true;
735 srand(QTime::currentTime().msec());
736 diceB1_value=1+(int) (6.0*rand()/(RAND_MAX+1.0));
737 diceB2_value=1+(int) (6.0*rand()/(RAND_MAX+1.0));
738 if(diceB1_value==diceB2_value)
739 {
740 diceB3_value=diceB1_value;
741 diceB4_value=diceB1_value;
742 }
743 else
744 {
745 diceB3_value=7;
746 dice3_played=true;
747 diceB4_value=7;
748 dice4_played=true;
749 }
750 showdice();
751 area->update();
752 move->diceroll(2,diceB1_value,diceB2_value,diceB3_value,diceB4_value,player2_auto);
753 }
754 }
755}
756
757void BackGammon::done_dice1()
758{
759 dice1_played=true;
760 if(player==1)
761 diceA1_value=7;
762 else
763 diceB1_value=7;
764 setplayer();
765 showdice();
766 draw();
767 area->update();
768 if(!dice2_played || !dice3_played || !dice4_played)
769 {
770 if(player==1)
771 {
772 move->diceroll(1,diceA1_value,diceA2_value,diceA3_value,diceA4_value,player1_auto);
773 }
774 else
775 {
776 move->diceroll(2,diceB1_value,diceB2_value,diceB3_value,diceB4_value,player2_auto);
777 }
778 }
779}
780
781void BackGammon::done_dice2()
782{
783 dice2_played=true;
784 if(player==1)
785 diceA2_value=7;
786 else
787 diceB2_value=7;
788 setplayer();
789 showdice();
790 draw();
791 area->update();
792 if(!dice1_played || !dice3_played || !dice4_played)
793 {
794 if(player==1)
795 {
796 move->diceroll(1,diceA1_value,diceA2_value,diceA3_value,diceA4_value,player1_auto);
797 }
798 else
799 {
800 move->diceroll(2,diceB1_value,diceB2_value,diceB3_value,diceB4_value,player2_auto);
801 }
802 }
803}
804
805
806void BackGammon::done_dice3()
807{
808 dice3_played=true;
809 if(player==1)
810 diceA3_value=7;
811 else
812 diceB3_value=7;
813 setplayer();
814 showdice();
815 draw();
816 area->update();
817 if(!dice1_played || !dice2_played || !dice4_played)
818 {
819 if(player==1)
820 {
821 move->diceroll(1,diceA1_value,diceA2_value,diceA3_value,diceA4_value,player1_auto);
822 }
823 else
824 {
825 move->diceroll(2,diceB1_value,diceB2_value,diceB3_value,diceB4_value,player2_auto);
826 }
827 }
828}
829
830
831void BackGammon::done_dice4()
832{
833 dice4_played=true;
834 if(player==1)
835 diceA4_value=7;
836 else
837 diceB4_value=7;
838 setplayer();
839 showdice();
840 draw();
841 area->update();
842 if(!dice1_played || !dice2_played || !dice3_played)
843 {
844 if(player==1)
845 {
846 move->diceroll(1,diceA1_value,diceA2_value,diceA3_value,diceA4_value,player1_auto);
847 }
848 else
849 {
850 move->diceroll(2,diceB1_value,diceB2_value,diceB3_value,diceB4_value,player2_auto);
851 }
852 }
853}
854
855
856void BackGammon::nomove()
857{
858 if(player==1)
859 nomove_marker->setX(0);
860 else
861 nomove_marker->setX(170);
862 nomove_marker->show();
863 message->setText("<b>no move</b>");
864 dice1_played=true;
865 dice2_played=true;
866 dice3_played=true;
867 dice4_played=true;
868 if(player==1)
869 {
870 diceA1_value=7;
871 diceA2_value=7;
872 diceA3_value=7;
873 diceA4_value=7;
874 }
875 else
876 {
877 diceB1_value=7;
878 diceB2_value=7;
879 diceB3_value=7;
880 diceB4_value=7;
881 }
882 area->update();
883 QTimer::singleShot(2000,this,SLOT(nomove2()));
884}
885
886void BackGammon::nomove2()
887{
888 nomove_marker->hide();
889 setplayer();
890 showdice();
891 draw();
892 area->update();
893}
894
895void BackGammon::finished(int theplayer)
896{
897 nomove_marker->hide();
898 if(theplayer==1)
899 message->setText("<b>Player 1 wins. Click on board for new game.</b>");
900 else
901 message->setText("<b>Player 2 wins. Click on board for new game.</b>");
902 diceA1_value=7;
903 diceA2_value=7;
904 diceB1_value=7;
905 diceB2_value=7;
906 player=0;
907 showdice();
908 draw();
909 area->update();
910 gameFinished=true;
911}
912
913void BackGammon::showdice()
914{
915 int value_diceA1=diceA1_value-1;
916 if(diceA1_value==7 && diceA3_value!=7)
917 value_diceA1=diceA3_value-1;
918
919 int value_diceA2=diceA2_value-1;
920 if(diceA2_value==7 && diceA4_value!=7)
921 value_diceA2=diceA4_value-1;
922
923 int value_diceB1=diceB1_value-1;
924 if(diceB1_value==7 && diceB3_value!=7)
925 value_diceB1=diceB3_value-1;
926
927 int value_diceB2=diceB2_value-1;
928 if(diceB2_value==7 && diceB4_value!=7)
929 value_diceB2=diceB4_value-1;
930
931 for(int index=0;index<7;index++)
932 {
933 if(value_diceA1==index)
934 diceA1[index]->show();
935 else
936 diceA1[index]->hide();
937
938 if(value_diceA2==index)
939 diceA2[index]->show();
940 else
941 diceA2[index]->hide();
942
943 if(value_diceB1==index)
944 diceB1[index]->show();
945 else
946 diceB1[index]->hide();
947
948 if(value_diceB2==index)
949 diceB2[index]->show();
950 else
951 diceB2[index]->hide();
952 }
953}
954
955void BackGammon::setplayer()
956{
957 if(dice1_played && dice2_played && dice3_played && dice4_played && player==1)
958 {
959 message->setText("<b>P2 turn</b>");
960 dice_rolled=false;
961 player=2;
962 if(player2_auto)
963 QTimer::singleShot(2000,this,SLOT(autoroll_dice2()));
964 }
965 else if(dice1_played && dice2_played && dice3_played && dice4_played && player==2)
966 {
967 message->setText("<b>P1 turn</b>");
968 dice_rolled=false;
969 player=1;
970 if(player1_auto)
971 QTimer::singleShot(2000,this,SLOT(autoroll_dice1()));
972 }
973}
974
975void BackGammon::autoroll_dice1()
976{
977 mouse(20,210);
978}
979
980void BackGammon::autoroll_dice2()
981{
982 mouse(170,210);
983}
984
985void BackGammon::applytheme()
986{
987 QImage boardbg(Resource::loadImage("backgammon/boards/"+board_name));
988 if(non_qte)
989 {
990 boardbg=boardbg.copy(offset,offset,235-2*offset,200-2*offset);
991 }
992 board->setImage(boardbg);
993
994 QImage tablebg(Resource::loadImage("backgammon/table/"+table_name));
995 if(non_qte)
996 {
997 tablebg=tablebg.copy(offset,0,235-offset,200);
998 }
999 table->setImage(tablebg);
1000
1001 QImage piece_1_all(Resource::loadImage("backgammon/pieces/"+piecesA_name));
1002 QImage piece_1_front=piece_1_all.copy(0,0,15,15);
1003 QImage piece_1_side=piece_1_all.copy(0,15,15,5);
1004
1005 QImage piece_2_all(Resource::loadImage("backgammon/pieces/"+piecesB_name));
1006 QImage piece_2_front=piece_2_all.copy(0,0,15,15);
1007 QImage piece_2_side=piece_2_all.copy(0,15,15,5);
1008
1009 int a=0;
1010 for(a=0;a<15;a++)
1011 {
1012 p1[a]->setImage(piece_1_front);
1013 p1_side[a]->setImage(piece_1_side);
1014
1015 p2[a]->setImage(piece_2_front);
1016 p2_side[a]->setImage(piece_2_side);
1017 }
1018 draw();
1019
1020 QImage dicebgA_all(Resource::loadImage("backgammon/dice/"+diceA_name));
1021 QImage dicebgB_all(Resource::loadImage("backgammon/dice/"+diceB_name));
1022 QImage oddsbg_all=(Resource::loadImage("backgammon/odds/"+odds_name));
1023
1024 for(a=0;a<7;a++)
1025 {
1026 QImage dicebgA=dicebgA_all.copy(a*25,0,25,25);
1027 diceA1[a]->setImage(dicebgA);
1028 diceA2[a]->setImage(dicebgA);
1029
1030 QImage dicebgB=dicebgB_all.copy(a*25,0,25,25);
1031 diceB1[a]->setImage(dicebgB);
1032 diceB2[a]->setImage(dicebgB);
1033 /*
1034 if(a<6)
1035 {
1036 QImage oddsbg=oddsbg_all.copy(a*15,0,15,15);
1037 oddsDice[a]->setImage(oddsbg);
1038 }
1039 */
1040 }
1041}
1042
1043