summaryrefslogtreecommitdiff
path: root/noncore/games/backgammon/backgammon.cpp
Side-by-side diff
Diffstat (limited to 'noncore/games/backgammon/backgammon.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/backgammon/backgammon.cpp131
1 files changed, 31 insertions, 100 deletions
diff --git a/noncore/games/backgammon/backgammon.cpp b/noncore/games/backgammon/backgammon.cpp
index 38ccb49..c40f462 100644
--- a/noncore/games/backgammon/backgammon.cpp
+++ b/noncore/games/backgammon/backgammon.cpp
@@ -1,69 +1,49 @@
#include "backgammon.h"
#include "aidialog.h"
-#include "displaydialog.h"
#include "filedialog.h"
#include "playerdialog.h"
#include "rulesdialog.h"
#include "themedialog.h"
#include <qdatetime.h>
#include <qfile.h>
+#include <qlayout.h>
#include <qmessagebox.h>
#include <qstring.h>
#include <qtimer.h>
+#include <qmainwindow.h>
#include <qpe/qpeapplication.h>
#include <qpe/config.h>
#include <qpe/qpemenubar.h>
#include <qpe/resource.h>
#include <stdlib.h>
-BackGammonView::BackGammonView(QCanvas* canvas,QWidget* parent)
- :QCanvasView(canvas,parent)
-{
- //do nothing
-}
-
-
-
-BackGammonView::~BackGammonView()
-{
- //do nothing
-}
-
-
-
-void BackGammonView::contentsMousePressEvent(QMouseEvent* e)
-{
- int x=e->x();
- int y=e->y();
- emit mouse(x,y);
-}
BackGammon::BackGammon(QWidget* parent, const char* name, WFlags fl)
- : QWidget(parent, name, fl)
+ : QMainWindow(parent, name, fl)
{
if (!name) setName("BackGammon");
setCaption("Backgammon");
setIcon( Resource::loadPixmap( "backgammon" ) );
//general counter varaible
int a=0;
//the game engine
move=new MoveEngine();
//load the default theme
Config conf("backgammon");
if(!conf.isValid())
{
qDebug("config file does not exist");
conf.setGroup("general");
conf.writeEntry("theme","default");
conf.setGroup("rules");
conf.writeEntry("move_with_pieces_out",false);
conf.writeEntry("nice_dice",false);
conf.setGroup("display");
conf.writeEntry("small",false);
conf.writeEntry("warning",true);
conf.setGroup("ai");
conf.writeEntry("rescue",6);
@@ -75,63 +55,48 @@ BackGammon::BackGammon(QWidget* parent, const char* name, WFlags fl)
}
conf.setGroup("general");
theme_name=conf.readEntry("theme","default");
QString theme_file=QPEApplication::qpeDir()+"/backgammon/"+theme_name+".theme";
//the rules
conf.setGroup("rules");
rules.move_with_pieces_out=conf.readBoolEntry("move_with_pieces_out",false);
rules.generous_dice=conf.readBoolEntry("nice_dice",false);
move->setRules(rules);
//get the AI settings
AISettings ai;
conf.setGroup("ai");
ai.rescue=conf.readNumEntry("rescue",6);
ai.eliminate=conf.readNumEntry("eliminate",4);
ai.expose=conf.readNumEntry("expose",1);
ai.protect=conf.readNumEntry("protect",5);
ai.safe=conf.readNumEntry("safe",3);
ai.empty=conf.readNumEntry("empty",2);
move->setAISettings(ai);
- //non qte styles are smaller
- conf.setGroup("display");
- display.small=conf.readBoolEntry("small",false);
- display.warning=conf.readBoolEntry("warning",true);
- non_qte=display.small;
- if(display.warning)
- {
- Config test("qpe");
- test.setGroup("Appearance");
- if(test.readEntry("Style")!="QPE")
- {
- displaySettings();
- }
- }
- offset=(non_qte) ? 5 : 0;
//get the theme component names
Config theme(theme_file,Config::File);
if(!theme.isValid())
{
qDebug("theme file does not exist");
theme.setGroup("theme");
theme.writeEntry("board","casino_board_1");
theme.writeEntry("pieces1","casino_pieces_blue");
theme.writeEntry("pieces2","casino_pieces_white");
theme.writeEntry("dice1","casino_dice");
theme.writeEntry("dice2","casino_dice");
theme.writeEntry("table","casino_table_green");
theme.writeEntry("odds","casino_odds");
}
theme.setGroup("theme");
board_name=theme.readEntry("board","casino_board_1");
piecesA_name=theme.readEntry("pieces1","casino_pieces_blue");
piecesB_name=theme.readEntry("pieces2","casino_pieces_white");
diceA_name=theme.readEntry("dice1","casino_dice");
diceB_name=theme.readEntry("dice2","casino_dice");
table_name=theme.readEntry("table","casino_table_green");
odds_name=theme.readEntry("odds","casino_odds");
@@ -142,68 +107,63 @@ BackGammon::BackGammon(QWidget* parent, const char* name, WFlags fl)
QPopupMenu* gamemenu= new QPopupMenu(this);
gamemenu->insertItem("New",this,SLOT(newgame()));
gamemenu->insertSeparator();
gamemenu->insertItem("Load",this,SLOT(loadgame()));
gamemenu->insertItem("Save",this,SLOT(savegame()));
gamemenu->insertSeparator();
gamemenu->insertItem("Delete",this,SLOT(deletegame()));
menuBar->insertItem("Game",gamemenu);
QPopupMenu* thememenu= new QPopupMenu(this);
thememenu->insertItem("New",this,SLOT(newtheme()));
thememenu->insertSeparator();
thememenu->insertItem("Load",this,SLOT(loadtheme()));
thememenu->insertItem("Save",this,SLOT(savetheme()));
thememenu->insertSeparator();
thememenu->insertItem("Default",this,SLOT(themedefault()));
thememenu->insertItem("Delete",this,SLOT(deletetheme()));
menuBar->insertItem("Theme",thememenu);
QPopupMenu* optionmenu=new QPopupMenu(this);
optionmenu->insertItem("Player",this,SLOT(playerselect()));
optionmenu->insertSeparator();
optionmenu->insertItem("AI",this,SLOT(modify_AI()));
optionmenu->insertItem("Rules",this,SLOT(setrules()));
- optionmenu->insertItem("Display",this,SLOT(displaySettings()));
menuBar->insertItem("Options",optionmenu);
- //status bar
- message=new QLabel("<b>Backgammon</b>",this);
- message->setGeometry(0,260,237,20);
- message->setAlignment(AlignHCenter);
-
+ QWidget* mainarea=new QWidget(this);
+ setCentralWidget(mainarea);
//the main area
- area=new QCanvas(235-2*offset,235-2*offset);
- boardview=new BackGammonView(area,this);
+ QBoxLayout* layout=new QBoxLayout(mainarea,QBoxLayout::TopToBottom);
+ area=new QCanvas(235,235);
+ boardview=new BackGammonView(area,mainarea);
+ boardview->setMaximumHeight(240);
+ layout->addWidget(boardview);
connect(boardview,SIGNAL(mouse(int,int)),this,SLOT(mouse(int,int)));
- if(non_qte)
- {
- boardview->setGeometry(5,20,229,229);
- }
- else
- {
- boardview->setGeometry(1,20,237,237);
- }
+ //status bar
+ message=new QLabel("<b>Backgammon</b>",mainarea);
+ message->setAlignment(AlignHCenter);
+ layout->addWidget(message);
//the marker
marker_current=new QCanvasRectangle(area);
marker_current->setBrush(QColor(0,0,255));
marker_current->setSize(15,5);
marker_current->setZ(1);
for(a=0;a<4;a++)
{
marker_next[a]=new QCanvasRectangle(area);
marker_next[a]->setBrush(QColor(0,255,0));
marker_next[a]->setSize(15,5);
marker_next[a]->setZ(1);
}
connect(move,SIGNAL(done_dice1()),this,SLOT(done_dice1()));
connect(move,SIGNAL(done_dice2()),this,SLOT(done_dice2()));
connect(move,SIGNAL(done_dice3()),this,SLOT(done_dice3()));
connect(move,SIGNAL(done_dice4()),this,SLOT(done_dice4()));
connect(move,SIGNAL(nomove()),this,SLOT(nomove()));
connect(move,SIGNAL(player_finished(int)),this,SLOT(finished(int)));
//the pieces
p1=new CanvasImageItem*[15];
@@ -227,109 +187,101 @@ BackGammon::BackGammon(QWidget* parent, const char* name, WFlags fl)
p1_side[a]->setSize(15,5);
p2[a]=new CanvasImageItem(piece_2_front,area);
p2[a]->setSize(15,15);
p2_side[a]=new CanvasImageItem(piece_2_side,area);
p2_side[a]->setSize(15,5);
}
draw();
//the dice
QImage dicebgA_all(Resource::loadImage("backgammon/dice/"+diceA_name));
diceA1=new CanvasImageItem*[7];
diceA2=new CanvasImageItem*[7];
QImage dicebgB_all(Resource::loadImage("backgammon/dice/"+diceB_name));
diceB1=new CanvasImageItem*[7];
diceB2=new CanvasImageItem*[7];
QImage oddsbg_all=(Resource::loadImage("backgammon/odds/"+odds_name));
//oddsDice=new CanvasImageItem*[6];
for(a=0;a<7;a++)
{
QImage dicebgA=dicebgA_all.copy(a*25,0,25,25);
diceA1[a]=new CanvasImageItem(dicebgA,area);
- diceA1[a]->setX(5-offset);
- diceA1[a]->setY(205-2*offset);
+ diceA1[a]->setX(5);
+ diceA1[a]->setY(205-2);
diceA1[a]->setZ(1);
diceA1[a]->setSize(25,25);
diceA2[a]=new CanvasImageItem(dicebgA,area);
- diceA2[a]->setX(35-offset);
- diceA2[a]->setY(205-2*offset);
+ diceA2[a]->setX(35);
+ diceA2[a]->setY(205-2);
diceA2[a]->setZ(1);
diceA2[a]->setSize(25,25);
QImage dicebgB=dicebgB_all.copy(a*25,0,25,25);
diceB1[a]=new CanvasImageItem(dicebgB,area);
- diceB1[a]->setX(175-offset);
- diceB1[a]->setY(205-2*offset);
+ diceB1[a]->setX(175);
+ diceB1[a]->setY(205-2);
diceB1[a]->setZ(1);
diceB1[a]->setSize(25,25);
diceB2[a]=new CanvasImageItem(dicebgB,area);
- diceB2[a]->setX(205-offset);
- diceB2[a]->setY(205-2*offset);
+ diceB2[a]->setX(205);
+ diceB2[a]->setY(205-2);
diceB2[a]->setZ(1);
diceB2[a]->setSize(25,25);
/*
if(a<6)
{
QImage oddsbg=oddsbg_all.copy(a*15,0,15,15);
oddsDice[a]=new CanvasImageItem(oddsbg,area);
- oddsDice[a]->setX(110-offset);
- oddsDice[a]->setY(210-2*offset);
+ oddsDice[a]->setX(110);
+ oddsDice[a]->setY(210-2);
oddsDice[a]->setZ(1);
oddsDice[a]->setSize(15,15);
oddsDice[a]->hide();
}
*/
}
//oddsDice[0]->show();
//set the board
QImage boardbg(Resource::loadImage("backgammon/boards/"+board_name));
- if(non_qte)
- {
- boardbg=boardbg.copy(offset,offset,235-2*offset,200-2*offset);
- }
board=new CanvasImageItem(boardbg,area);
board->setX(0);
board->setY(0);
board->setZ(0);
- board->setSize(235-2*offset,200-2*offset);
+ board->setSize(235-2,200-2);
board->show();
//the table
QImage tablebg(Resource::loadImage("backgammon/table/"+table_name));
- if(non_qte)
- {
- tablebg=tablebg.copy(offset,0,235-offset,200);
- }
table=new CanvasImageItem(tablebg,area);
table->setX(0);
- table->setY(200-2*offset);
+ table->setY(200-2);
table->setZ(0);
- table->setSize(235-2*offset,20);
+ table->setSize(235-2,20);
table->show();
//the no move marker
QImage nomovebg(Resource::loadImage("backgammon/no_move"));
nomove_marker=new CanvasImageItem(nomovebg,area);
nomove_marker->setX(0);
nomove_marker->setY(200);
nomove_marker->setZ(2);
nomove_marker->hide();
//default human against computer
player1_auto=false;
player2_auto=true;
//start new game
newgame();
}
BackGammon::~BackGammon()
{
//DESTRUCTOR
}
void BackGammon::newgame()
{
@@ -578,66 +530,53 @@ void BackGammon::modify_AI()
Config conf("backgammon");
conf.setGroup("ai");
conf.writeEntry("rescue",ai.rescue);
conf.writeEntry("eliminate",ai.eliminate);
conf.writeEntry("expose",ai.expose);
conf.writeEntry("protect",ai.protect);
conf.writeEntry("safe",ai.safe);
conf.writeEntry("empty",ai.empty);
}
void BackGammon::setrules()
{
RulesDialog* rulesdialog=new RulesDialog(this,"Load Theme",".theme");
rulesdialog->setRules(rules);
if(!rulesdialog->exec())
return;
rules=rulesdialog->getRules();
Config conf("backgammon");
conf.setGroup("rules");
conf.writeEntry("move_with_pieces_out",rules.move_with_pieces_out);
conf.writeEntry("nice_dice",rules.generous_dice);
move->setRules(rules);
}
-void BackGammon::displaySettings()
-{
- DisplayDialog* displaydialog=new DisplayDialog(this);
- displaydialog->setDisplaySettings(display);
- if(!displaydialog->exec())
- return;
- display=displaydialog->getDisplaySettings();
- Config conf("backgammon");
- conf.setGroup("display");
- conf.writeEntry("small",display.small);
- conf.writeEntry("warning",display.warning);
- QMessageBox::warning(this,"Backgammon","changed display settings will\nonly take place after game has\nbeen restarted","OK");
-}
void BackGammon::draw()
{
Pieces pieces;
- move->position(pieces,non_qte);
+ move->position(pieces);
for(int a=0;a<15;a++)
{
if(!pieces.player1[a].side)
{
p1[a]->setX(pieces.player1[a].x);
p1[a]->setY(pieces.player1[a].y);
p1[a]->setZ(pieces.player1[a].z);
p1[a]->show();
p1_side[a]->hide();
}
else
{
p1_side[a]->setX(pieces.player1[a].x);
p1_side[a]->setY(pieces.player1[a].y);
p1_side[a]->setZ(pieces.player1[a].z);
p1_side[a]->show();
p1[a]->hide();
}
if(!pieces.player2[a].side)
{
p2[a]->setX(pieces.player2[a].x);
p2[a]->setY(pieces.player2[a].y);
p2[a]->setZ(pieces.player2[a].z);
@@ -648,65 +587,65 @@ void BackGammon::draw()
{
p2_side[a]->setX(pieces.player2[a].x);
p2_side[a]->setY(pieces.player2[a].y);
p2_side[a]->setZ(pieces.player2[a].z);
p2_side[a]->show();
p2[a]->hide();
}
}
}
void BackGammon::mouse(int x,int y)
{
if(gameFinished)
{
newgame();
return;
}
if(y<=200) //move pieces
{
if((player==1 && player1_auto) || (player==2 && player2_auto))
return;
Marker marker;
- move->boardpressed(x,y,marker,non_qte);
+ move->boardpressed(x,y,marker);
if(marker.visible_current)
{
- marker_current->setX(marker.x_current-offset);
+ marker_current->setX(marker.x_current);
marker_current->setY(marker.y_current);
marker_current->show();
}
else
{
marker_current->hide();
}
for(int a=0;a<4;a++)
{
if(marker.visible_next[a])
{
- marker_next[a]->setX(marker.x_next[a]-offset);
+ marker_next[a]->setX(marker.x_next[a]);
marker_next[a]->setY(marker.y_next[a]);
marker_next[a]->show();
}
else
{
marker_next[a]->hide();
}
}
area->update();
}
else //roll dice
{
if(x>=10 && x<=65 && player==1 && !dice_rolled)
{
dice1_played=false;
dice2_played=false;
dice3_played=false;
dice4_played=false;
dice_rolled=true;
srand(QTime::currentTime().msec());
diceA1_value=1+(int) (6.0*rand()/(RAND_MAX+1.0));
diceA2_value=1+(int) (6.0*rand()/(RAND_MAX+1.0));
if(diceA1_value==diceA2_value)
{
@@ -964,59 +903,51 @@ void BackGammon::setplayer()
}
else if(dice1_played && dice2_played && dice3_played && dice4_played && player==2)
{
message->setText("<b>P1 turn</b>");
dice_rolled=false;
player=1;
if(player1_auto)
QTimer::singleShot(2000,this,SLOT(autoroll_dice1()));
}
}
void BackGammon::autoroll_dice1()
{
mouse(20,210);
}
void BackGammon::autoroll_dice2()
{
mouse(170,210);
}
void BackGammon::applytheme()
{
QImage boardbg(Resource::loadImage("backgammon/boards/"+board_name));
- if(non_qte)
- {
- boardbg=boardbg.copy(offset,offset,235-2*offset,200-2*offset);
- }
board->setImage(boardbg);
QImage tablebg(Resource::loadImage("backgammon/table/"+table_name));
- if(non_qte)
- {
- tablebg=tablebg.copy(offset,0,235-offset,200);
- }
table->setImage(tablebg);
QImage piece_1_all(Resource::loadImage("backgammon/pieces/"+piecesA_name));
QImage piece_1_front=piece_1_all.copy(0,0,15,15);
QImage piece_1_side=piece_1_all.copy(0,15,15,5);
QImage piece_2_all(Resource::loadImage("backgammon/pieces/"+piecesB_name));
QImage piece_2_front=piece_2_all.copy(0,0,15,15);
QImage piece_2_side=piece_2_all.copy(0,15,15,5);
int a=0;
for(a=0;a<15;a++)
{
p1[a]->setImage(piece_1_front);
p1_side[a]->setImage(piece_1_side);
p2[a]->setImage(piece_2_front);
p2_side[a]->setImage(piece_2_side);
}
draw();
QImage dicebgA_all(Resource::loadImage("backgammon/dice/"+diceA_name));
QImage dicebgB_all(Resource::loadImage("backgammon/dice/"+diceB_name));
QImage oddsbg_all=(Resource::loadImage("backgammon/odds/"+odds_name));