00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <stdio.h>
00023
00024 #include <qwidget.h>
00025 #include <qpushbutton.h>
00026 #include <qpixmap.h>
00027 #include <qvbox.h>
00028 #include <qpopupmenu.h>
00029
00030 #include <kapplication.h>
00031 #include <kiconloader.h>
00032 #include <kdialogbase.h>
00033 #include <kscoredialog.h>
00034 #include <kstatusbar.h>
00035 #include <knuminput.h>
00036 #include <klocale.h>
00037 #include <knotifyclient.h>
00038 #include <kfiledialog.h>
00039 #include <kmessagebox.h>
00040 #include <kmenubar.h>
00041 #include <kconfig.h>
00042 #include <kstdaction.h>
00043 #include <kaction.h>
00044 #include <kdebug.h>
00045 #include <kkeydialog.h>
00046
00047 #include "KSameWidget.h"
00048 #include "StoneWidget.h"
00049 #include "version.h"
00050
00051 static int default_colors=3;
00052
00053 #define Board KScoreDialog::Custom1
00054
00055 KSameWidget::KSameWidget() : KMainWindow(0)
00056 {
00057 setCaption("");
00058 KStdAction::openNew(this, SLOT(m_new()), actionCollection(), "game_new");
00059 restart = new KAction(i18n("&Restart This Board"), CTRL+Key_R, this,
00060 SLOT(m_restart()), actionCollection(), "game_restart");
00061
00062
00063 new KAction(i18n("S&how Highscore"), CTRL+Key_H, this,
00064 SLOT(m_showhs()), actionCollection(), "game_highscores");
00065 KStdAction::quit(this, SLOT(m_quit()), actionCollection(), "game_quit");
00066 KStdAction::keyBindings( this, SLOT( slotConfigureKeys() ), actionCollection() );
00067 undo = KStdAction::undo(this, SLOT(m_undo()),
00068 actionCollection(), "edit_undo");
00069
00070 random = new KToggleAction(i18n("&Random Board"), 0, this,
00071 SLOT(m_tglboard()), actionCollection(),
00072 "random_board");
00073
00074 status=statusBar();
00075 status->insertItem(i18n("Colors: XX"),1,1);
00076 status->insertItem(i18n("Board: XXXXXX"),2,1);
00077 status->insertItem(i18n("Marked: XXXXXX"),3,1);
00078 status->insertItem(i18n("Score: XXXXXX"),4,1);
00079
00080 stone = new StoneWidget(this,15,10);
00081
00082 connect( stone, SIGNAL(s_gameover()), this, SLOT(gameover()));
00083
00084 connect( stone, SIGNAL(s_colors(int)), this, SLOT(setColors(int)));
00085 connect( stone, SIGNAL(s_board(int)), this, SLOT(setBoard(int)));
00086 connect( stone, SIGNAL(s_marked(int)), this, SLOT(setMarked(int)));
00087 connect( stone, SIGNAL(s_score(int)), this, SLOT(setScore(int)));
00088 connect( stone, SIGNAL(s_remove(int,int)), this, SLOT(stonesRemoved(int,int)));
00089
00090 connect(stone, SIGNAL(s_sizechanged()), this, SLOT(sizeChanged()));
00091
00092 sizeChanged();
00093 setCentralWidget(stone);
00094
00095 createGUI();
00096
00097 random->setChecked(true);
00098 setScore(0);
00099
00100 if (!kapp->isRestored()) newGame(kapp->random(),default_colors);
00101 kdDebug() << "test" << endl;
00102 }
00103
00104 KSameWidget::~KSameWidget() {
00105 }
00106
00107 void KSameWidget::slotConfigureKeys()
00108 {
00109 KKeyDialog::configure( actionCollection(), this );
00110 }
00111
00112 void KSameWidget::readProperties(KConfig *conf) {
00113 Q_ASSERT(conf);
00114 stone->readProperties(conf);
00115 }
00116
00117 void KSameWidget::saveProperties(KConfig *conf) {
00118 Q_ASSERT(conf);
00119 stone->saveProperties(conf);
00120 conf->sync();
00121 }
00122
00123 void KSameWidget::sizeChanged() {
00124 stone->setFixedSize(stone->sizeHint());
00125 }
00126
00127 void KSameWidget::newGame(unsigned int board,int colors) {
00128 while (board>=1000000) board-=1000000;
00129
00130 stone->newGame(board,colors);
00131 setScore(0);
00132 }
00133
00134 bool KSameWidget::confirmAbort() {
00135 return stone->isGameover() ||
00136 stone->isOriginalBoard() ||
00137 (KMessageBox::questionYesNo(this, i18n("Do you want to resign?"),
00138 i18n("New Game")) == KMessageBox::Yes);
00139 }
00140
00141 void KSameWidget::m_new() {
00142 if (random->isChecked()) {
00143 if (confirmAbort())
00144 newGame(kapp->random(),default_colors);
00145 } else {
00146 KDialogBase dlg(this, "boardchooser", true,
00147 i18n("Select Board"),
00148 KDialogBase::Ok | KDialogBase::Cancel,
00149 KDialogBase::Ok);
00150
00151 QVBox *page = dlg.makeVBoxMainWidget();
00152
00153 KIntNumInput bno(0, page);
00154 bno.setRange(0, 1000000, 1);
00155 bno.setLabel(i18n("Select a board:"));
00156 bno.setFocus();
00157 bno.setFixedSize(bno.sizeHint());
00158 bno.setValue(stone->board());
00159
00160 if (dlg.exec()) newGame(bno.value(),default_colors);
00161 }
00162 }
00163
00164 void KSameWidget::m_restart() {
00165 if (confirmAbort())
00166 newGame(stone->board(),default_colors);
00167 }
00168
00169 void KSameWidget::m_load() {
00170 kdDebug() << "menu load not supported" << endl;
00171 }
00172
00173 void KSameWidget::m_save() {
00174 kdDebug() << "menu save not supported" << endl;
00175 }
00176
00177 void KSameWidget::m_undo() {
00178 Q_ASSERT(stone);
00179 stone->undo();
00180 }
00181
00182
00183 void KSameWidget::m_showhs() {
00184 Q_ASSERT(stone);
00185 stone->unmark();
00186 KScoreDialog d(KScoreDialog::Name | KScoreDialog::Score, this);
00187 d.addField(Board, i18n("Board"), "Board");
00188 d.exec();
00189 }
00190
00191 void KSameWidget::m_quit() {
00192 Q_ASSERT(stone);
00193 stone->unmark();
00194 kapp->quit();
00195 delete this;
00196 }
00197
00198 void KSameWidget::m_tglboard() {
00199 kdDebug() << "toggled" << endl;
00200 }
00201
00202
00203 void KSameWidget::setColors(int colors) {
00204 status->changeItem(i18n("%1 Colors").arg(colors),1);
00205 }
00206
00207 void KSameWidget::setBoard(int board) {
00208 status->changeItem(i18n("Board: %1").arg(board, 6), 2);
00209 }
00210
00211 void KSameWidget::setMarked(int m) {
00212 status->changeItem(i18n("Marked: %1").arg(m, 6),3);
00213 }
00214
00215 void KSameWidget::stonesRemoved(int,int) {
00216 KNotifyClient::event("stones removed",
00217 i18n("%1 stones removed.").arg(stone->marked()));
00218 }
00219
00220 void KSameWidget::setScore(int score) {
00221 status->changeItem(i18n("Score: %1").arg(score, 6),4);
00222 undo->setEnabled(stone->undoPossible());
00223 restart->setEnabled(!stone->isOriginalBoard());
00224 }
00225
00226 void KSameWidget::gameover() {
00227 kdDebug() << "GameOver" << endl;
00228 if (stone->hasBonus()) {
00229 KNotifyClient::event("game won",
00230 i18n("You even removed the last stone, great job! "
00231 "This gave you a score of %1 in total.").arg(stone->score()));
00232 } else {
00233 KNotifyClient::event("game over",
00234 i18n("There are no more removeable stones. "
00235 "You got a score of %1 in total.").arg(stone->score()));
00236 }
00237 stone->unmark();
00238 KScoreDialog d(KScoreDialog::Name | KScoreDialog::Score, this);
00239 d.addField(Board, i18n("Board"), "Board");
00240
00241 KScoreDialog::FieldInfo scoreInfo;
00242 scoreInfo[Board].setNum(stone->board());
00243
00244 if (d.addScore(stone->score(), scoreInfo))
00245 d.exec();
00246 }
00247
00248 #include "KSameWidget.moc"