Diffstat (limited to 'noncore/games/zsame/KSameWidget.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/games/zsame/KSameWidget.cpp | 248 |
1 files changed, 248 insertions, 0 deletions
diff --git a/noncore/games/zsame/KSameWidget.cpp b/noncore/games/zsame/KSameWidget.cpp new file mode 100644 index 0000000..0db2385 --- a/dev/null +++ b/noncore/games/zsame/KSameWidget.cpp | |||
@@ -0,0 +1,248 @@ | |||
1 | /* Yo Emacs, this is -*- C++ -*- */ | ||
2 | /* | ||
3 | * ksame 0.4 - simple Game | ||
4 | * Copyright (C) 1997,1998 Marcus Kreutzberger | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation; either version 2 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * This program is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU General Public License | ||
17 | * along with this program; if not, write to the Free Software | ||
18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
19 | * | ||
20 | */ | ||
21 | |||
22 | #include <stdio.h> | ||
23 | |||
24 | #include <qwidget.h> | ||
25 | #include <qpushbutton.h> | ||
26 | #include <qpixmap.h> | ||
27 | #include <qvbox.h> | ||
28 | #include <qpopupmenu.h> | ||
29 | |||
30 | #include <kapplication.h> | ||
31 | #include <kiconloader.h> | ||
32 | #include <kdialogbase.h> | ||
33 | #include <kscoredialog.h> | ||
34 | #include <kstatusbar.h> | ||
35 | #include <knuminput.h> | ||
36 | #include <klocale.h> | ||
37 | #include <knotifyclient.h> | ||
38 | #include <kfiledialog.h> | ||
39 | #include <kmessagebox.h> | ||
40 | #include <kmenubar.h> | ||
41 | #include <kconfig.h> | ||
42 | #include <kstdaction.h> | ||
43 | #include <kaction.h> | ||
44 | #include <kdebug.h> | ||
45 | #include <kkeydialog.h> | ||
46 | |||
47 | #include "KSameWidget.h" | ||
48 | #include "StoneWidget.h" | ||
49 | #include "version.h" | ||
50 | |||
51 | static int default_colors=3; | ||
52 | |||
53 | #define Board KScoreDialog::Custom1 | ||
54 | |||
55 | KSameWidget::KSameWidget() : KMainWindow(0) | ||
56 | { | ||
57 | setCaption(""); | ||
58 | KStdAction::openNew(this, SLOT(m_new()), actionCollection(), "game_new"); | ||
59 | restart = new KAction(i18n("&Restart This Board"), CTRL+Key_R, this, | ||
60 | SLOT(m_restart()), actionCollection(), "game_restart"); | ||
61 | // KStdAction::open(this, SLOT(m_load()), actionCollection(), "game_load"); | ||
62 | // KStdAction::save(this, SLOT(m_save()), actionCollection(), "game_save"); | ||
63 | new KAction(i18n("S&how Highscore"), CTRL+Key_H, this, | ||
64 | SLOT(m_showhs()), actionCollection(), "game_highscores"); | ||
65 | KStdAction::quit(this, SLOT(m_quit()), actionCollection(), "game_quit"); | ||
66 | KStdAction::keyBindings( this, SLOT( slotConfigureKeys() ), actionCollection() ); | ||
67 | undo = KStdAction::undo(this, SLOT(m_undo()), | ||
68 | actionCollection(), "edit_undo"); | ||
69 | |||
70 | random = new KToggleAction(i18n("&Random Board"), 0, this, | ||
71 | SLOT(m_tglboard()), actionCollection(), | ||
72 | "random_board"); | ||
73 | |||
74 | status=statusBar(); | ||
75 | status->insertItem(i18n("Colors: XX"),1,1); | ||
76 | status->insertItem(i18n("Board: XXXXXX"),2,1); | ||
77 | status->insertItem(i18n("Marked: XXXXXX"),3,1); | ||
78 | status->insertItem(i18n("Score: XXXXXX"),4,1); | ||
79 | |||
80 | stone = new StoneWidget(this,15,10); | ||
81 | |||
82 | connect( stone, SIGNAL(s_gameover()), this, SLOT(gameover())); | ||
83 | |||
84 | connect( stone, SIGNAL(s_colors(int)), this, SLOT(setColors(int))); | ||
85 | connect( stone, SIGNAL(s_board(int)), this, SLOT(setBoard(int))); | ||
86 | connect( stone, SIGNAL(s_marked(int)), this, SLOT(setMarked(int))); | ||
87 | connect( stone, SIGNAL(s_score(int)), this, SLOT(setScore(int))); | ||
88 | connect( stone, SIGNAL(s_remove(int,int)), this, SLOT(stonesRemoved(int,int))); | ||
89 | |||
90 | connect(stone, SIGNAL(s_sizechanged()), this, SLOT(sizeChanged())); | ||
91 | |||
92 | sizeChanged(); | ||
93 | setCentralWidget(stone); | ||
94 | |||
95 | createGUI(); | ||
96 | |||
97 | random->setChecked(true); | ||
98 | setScore(0); | ||
99 | |||
100 | if (!kapp->isRestored()) newGame(kapp->random(),default_colors); | ||
101 | kdDebug() << "test" << endl; | ||
102 | } | ||
103 | |||
104 | KSameWidget::~KSameWidget() { | ||
105 | } | ||
106 | |||
107 | void KSameWidget::slotConfigureKeys() | ||
108 | { | ||
109 | KKeyDialog::configure( actionCollection(), this ); | ||
110 | } | ||
111 | |||
112 | void KSameWidget::readProperties(KConfig *conf) { | ||
113 | Q_ASSERT(conf); | ||
114 | stone->readProperties(conf); | ||
115 | } | ||
116 | |||
117 | void KSameWidget::saveProperties(KConfig *conf) { | ||
118 | Q_ASSERT(conf); | ||
119 | stone->saveProperties(conf); | ||
120 | conf->sync(); | ||
121 | } | ||
122 | |||
123 | void KSameWidget::sizeChanged() { | ||
124 | stone->setFixedSize(stone->sizeHint()); | ||
125 | } | ||
126 | |||
127 | void KSameWidget::newGame(unsigned int board,int colors) { | ||
128 | while (board>=1000000) board-=1000000; | ||
129 | // kdDebug() << "newgame board " << board << " colors " << colors << endl; | ||
130 | stone->newGame(board,colors); | ||
131 | setScore(0); | ||
132 | } | ||
133 | |||
134 | bool KSameWidget::confirmAbort() { | ||
135 | return stone->isGameover() || | ||
136 | stone->isOriginalBoard() || | ||
137 | (KMessageBox::questionYesNo(this, i18n("Do you want to resign?"), | ||
138 | i18n("New Game")) == KMessageBox::Yes); | ||
139 | } | ||
140 | |||
141 | void KSameWidget::m_new() { | ||
142 | if (random->isChecked()) { | ||
143 | if (confirmAbort()) | ||
144 | newGame(kapp->random(),default_colors); | ||
145 | } else { | ||
146 | KDialogBase dlg(this, "boardchooser", true, | ||
147 | i18n("Select Board"), | ||
148 | KDialogBase::Ok | KDialogBase::Cancel, | ||
149 | KDialogBase::Ok); | ||
150 | |||
151 | QVBox *page = dlg.makeVBoxMainWidget(); | ||
152 | |||
153 | KIntNumInput bno(0, page); | ||
154 | bno.setRange(0, 1000000, 1); | ||
155 | bno.setLabel(i18n("Select a board:")); | ||
156 | bno.setFocus(); | ||
157 | bno.setFixedSize(bno.sizeHint()); | ||
158 | bno.setValue(stone->board()); | ||
159 | |||
160 | if (dlg.exec()) newGame(bno.value(),default_colors); | ||
161 | } | ||
162 | } | ||
163 | |||
164 | void KSameWidget::m_restart() { | ||
165 | if (confirmAbort()) | ||
166 | newGame(stone->board(),default_colors); | ||
167 | } | ||
168 | |||
169 | void KSameWidget::m_load() { | ||
170 | kdDebug() << "menu load not supported" << endl; | ||
171 | } | ||
172 | |||
173 | void KSameWidget::m_save() { | ||
174 | kdDebug() << "menu save not supported" << endl; | ||
175 | } | ||
176 | |||
177 | void KSameWidget::m_undo() { | ||
178 | Q_ASSERT(stone); | ||
179 | stone->undo(); | ||
180 | } | ||
181 | |||
182 | |||
183 | void KSameWidget::m_showhs() { | ||
184 | Q_ASSERT(stone); | ||
185 | stone->unmark(); | ||
186 | KScoreDialog d(KScoreDialog::Name | KScoreDialog::Score, this); | ||
187 | d.addField(Board, i18n("Board"), "Board"); | ||
188 | d.exec(); | ||
189 | } | ||
190 | |||
191 | void KSameWidget::m_quit() { | ||
192 | Q_ASSERT(stone); | ||
193 | stone->unmark(); | ||
194 | kapp->quit(); | ||
195 | delete this; | ||
196 | } | ||
197 | |||
198 | void KSameWidget::m_tglboard() { | ||
199 | kdDebug() << "toggled" << endl; | ||
200 | } | ||
201 | |||
202 | |||
203 | void KSameWidget::setColors(int colors) { | ||
204 | status->changeItem(i18n("%1 Colors").arg(colors),1); | ||
205 | } | ||
206 | |||
207 | void KSameWidget::setBoard(int board) { | ||
208 | status->changeItem(i18n("Board: %1").arg(board, 6), 2); | ||
209 | } | ||
210 | |||
211 | void KSameWidget::setMarked(int m) { | ||
212 | status->changeItem(i18n("Marked: %1").arg(m, 6),3); | ||
213 | } | ||
214 | |||
215 | void KSameWidget::stonesRemoved(int,int) { | ||
216 | KNotifyClient::event("stones removed", | ||
217 | i18n("%1 stones removed.").arg(stone->marked())); | ||
218 | } | ||
219 | |||
220 | void KSameWidget::setScore(int score) { | ||
221 | status->changeItem(i18n("Score: %1").arg(score, 6),4); | ||
222 | undo->setEnabled(stone->undoPossible()); | ||
223 | restart->setEnabled(!stone->isOriginalBoard()); | ||
224 | } | ||
225 | |||
226 | void KSameWidget::gameover() { | ||
227 | kdDebug() << "GameOver" << endl; | ||
228 | if (stone->hasBonus()) { | ||
229 | KNotifyClient::event("game won", | ||
230 | i18n("You even removed the last stone, great job! " | ||
231 | "This gave you a score of %1 in total.").arg(stone->score())); | ||
232 | } else { | ||
233 | KNotifyClient::event("game over", | ||
234 | i18n("There are no more removeable stones. " | ||
235 | "You got a score of %1 in total.").arg(stone->score())); | ||
236 | } | ||
237 | stone->unmark(); | ||
238 | KScoreDialog d(KScoreDialog::Name | KScoreDialog::Score, this); | ||
239 | d.addField(Board, i18n("Board"), "Board"); | ||
240 | |||
241 | KScoreDialog::FieldInfo scoreInfo; | ||
242 | scoreInfo[Board].setNum(stone->board()); | ||
243 | |||
244 | if (d.addScore(stone->score(), scoreInfo)) | ||
245 | d.exec(); | ||
246 | } | ||
247 | |||
248 | #include "KSameWidget.moc" | ||