Debian Codegraph @

src/view.cc

raw symbols SCIP
1 /***************************************************************************
2 * Copyright (C) 2004-2005 Artur Wiebe *
3 * [email protected] *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #include <QLayout>
21 #include <QDate>
22 #include <QDebug>
23 #include <QTimer>
24
25 #include "pdn.h"
26 #include "echeckers.h"
27 #include "rcheckers.h"
28 #include "view.h"
29 #include "common.h"
30 #include "history.h"
31 #include "newgamedlg.h"
32 #include "toplevel.h"
33
34 #include "player.h"
35 #include "humanplayer.h"
36 #include "computerplayer.h"
37
38
39 #define MAX_CMD_LEN 80
40 #define MOVE_PAUSE 1000
41
42 // this class is used to note differencies between moves.
43 class myDiff {
44 public:
45 myDiff(int pos, int from, int to)
46 : m_pos(pos), m_from(from), m_to(to) {}
47 int m_pos;
48 int m_from;
49 int m_to;
50 };
51
52
53 myView::myView(myTopLevel* parent)
54 : QFrame(parent)
55 {
56 m_toplevel = parent;
57 /*
58 * board & info
59 */
60 m_board = new myBoard(this);
61 connect(m_board, SIGNAL(fieldClicked(int)),
62 this, SLOT(slot_click(int)));
63
64 /*
65 * it's the final layout.
66 */
67 QVBoxLayout* vb = new QVBoxLayout(this);
68 vb->addWidget(m_board);
69 //vb->setSizeConstraint(QLayout::SetFixedSize);
70
71 /*
72 * game init
73 */
74 m_player = m_current = 0;
75 }
76
77
78 myView::~myView()
79 {
80 if(m_player) {
81 delete m_player;
82 delete m_player->opponent();
83 }
84 }
85
86
87 void myView::setEnabled(bool b)
88 {
89 m_board->setEnabled(b);
90 if(b)
91 setCursor(Qt::ArrowCursor); // should be m_board bound.
92 else
93 setCursor(Qt::WaitCursor);
94 }
95
96
97 void myView::setTheme(const QString& path)
98 {
99 m_board->setTheme(path, m_player ? m_player->isWhite() : true);
100 //m_toplevel->get_history()->setFixedHeight(m_board->height());
101 }
102
103
104 void myView::newGame(int rules, bool freeplace,
105 const QString& name, bool is_white,
106 int opponent, const QString& opp_name, int skill)
107 {
108 m_freeplace_from = -1;
109
110 if(m_player) {
111 delete m_player;
112 delete m_player->opponent();
113 }
114
115 m_board->setColorWhite(is_white);
116
117 // create players
118 myPlayer* plr = new myHumanPlayer(name, is_white, false);
119 myPlayer* opp = 0;
120 if(opponent==HUMAN)
121 opp = new myHumanPlayer(opp_name, !is_white, true);
122 else
123 opp = new myComputerPlayer(opp_name, !is_white, skill);
124
125 emit working(true);
126
127
128 /*
129 * set up player stuff. slots/signals.
130 */
131 m_player = plr;
132
133 plr->setOpponent(opp);
134 opp->setOpponent(plr);
135
136 plr->disconnect();
137 opp->disconnect();
138
139 connect(plr, SIGNAL(moveDone(const QString&)),
140 this, SLOT(slot_move_done(const QString&)));
141
142 connect(opp, SIGNAL(moveDone(const QString&)),
143 this, SLOT(slot_move_done(const QString&)));
144
145
146 /*
147 * create game board.
148 */
149 m_board->setGame(rules);
150
151 m_board->reset();
152 m_toplevel->get_history()->clear();
153
154 begin_game(1, freeplace);
155 }
156
157
158 void myView::begin_game(unsigned int round, bool freeplace)
159 {
160 if(m_clear_log)
161 m_toplevel->clear_log();
162
163 m_board->adjustNotation(m_player->isWhite());
164
165 myHistory* history = m_toplevel->get_history();
166 history->newPdn(APPNAME" Game", freeplace);
167 history->setTag(PdnGame::Type, QString::number(m_board->type()));
168 history->setTag(PdnGame::Date,
169 QDate::currentDate().toString("yyyy.MM.dd"));
170 history->setTag(PdnGame::Result, "*");
171 history->setTag(PdnGame::Round, QString::number(round));
172
173
174 /*
175 * go!
176 */
177 myPlayer* last_player = get_first_player()->opponent();
178
179 m_game_over = false;
180 m_aborted = false;
181 m_current = last_player;
182
183 // setup names
184 if(m_player->isWhite()) {
185 history->setTag(PdnGame::White, m_player->name());
186 history->setTag(PdnGame::Black, m_player->opponent()->name());
187 } else {
188 history->setTag(PdnGame::White, m_player->opponent()->name());
189 history->setTag(PdnGame::Black, m_player->name());
190 }
191
192 if(history->isFreePlacement())
193 emit working(false);
194 else
195 slot_move_done(m_board->game()->toString(false));
196 }
197
198
199 bool myView::check_game_over()
200 {
201 if(m_game_over) // no further checks
202 return true;
203
204 m_game_over = true;
205
206 bool player_can = m_board->game()->checkMove1()
207 || m_board->game()->checkCapture1();
208 bool opp_can = m_board->game()->checkMove2()
209 || m_board->game()->checkCapture2();
210
211 // player cannot go but opponent can -> player lost.
212 if(/*FIXME*/m_player==m_current && !player_can && opp_can) {
213 you_won(false);
214 return m_game_over;
215 }
216 // player can go but opponent cannot -> player won.
217 if(/*FIXME*/m_player!=m_current && player_can && !opp_can) {
218 you_won(true);
219 return m_game_over;
220 }
221 // neither of the player can go -> draw.
222 if(!player_can && !opp_can) {
223 add_log(myView::System, tr("Drawn game."));
224 m_toplevel->get_history()->setTag(PdnGame::Result, "1/2-1/2");
225 return m_game_over;
226 }
227
228 m_game_over = false;
229 return m_game_over;
230 }
231
232
233 void myView::slot_click(int field_num)
234 {
235 if(m_game_over || m_aborted)
236 return;
237
238 myHistory* history = m_toplevel->get_history();
239 if(history->isPaused()) {
240 if(history->isFreePlacement()) {
241 // FIXME - hightlight fields
242 if(m_freeplace_from < 0) {
243 m_freeplace_from = field_num;
244 m_board->selectField(field_num, true);
245 } else {
246 m_board->selectField(m_freeplace_from, false);
247 m_board->doFreeMove(m_freeplace_from,
248 field_num);
249 m_freeplace_from = -1;
250 }
251 }
252 } else {
253 bool select = false;
254 QString err_msg;
255
256 if(!m_current->fieldClicked(field_num, &select, m_player->isWhite(), err_msg)) {
257 add_log(myView::Warning, m_current->name()+": "
258 + (err_msg.length()
259 ? err_msg
260 : tr("Invalid move.")));
261 } else {
262 m_board->selectField(field_num, select);
263 }
264 }
265 }
266
267
268 void myView::slotNextRound()
269 {
270 if(m_aborted)
271 return;
272
273 m_player->setWhite(!m_player->isWhite());
274 m_player->opponent()->setWhite(!m_player->isWhite());
275
276 m_board->setColorWhite(m_player->isWhite());
277 m_board->reset();
278
279 myHistory* history = m_toplevel->get_history();
280 unsigned int round = history->getTag(PdnGame::Round).toUInt() + 1;
281 begin_game(round, history->isFreePlacement());
282 }
283
284
285 void myView::slotStopGame()
286 {
287 m_player->stop();
288 m_player->opponent()->stop();
289 }
290
291
292 void myView::stop_game(const QString& msg)
293 {
294 m_game_over = true;
295 m_aborted = true;
296
297 QString text(tr("Game aborted.")+(!msg.isEmpty() ? "\n"+msg : ""));
298 add_log(myView::System, text);
299
300 emit working(false);
301 }
302
303
304 void myView::slot_move_done(const QString& board_str)
305 {
306 myHistory* history = m_toplevel->get_history();
307 if (history->isPaused()) // FIXME - ???
308 return;
309
310 perform_jumps(m_board->game()->toString(false), board_str);
311
312 // show who is next?
313 m_current = m_current->opponent();
314 history->setCurrent(m_current->name());
315
316 if(!m_current->isHuman()) {
317 emit working(true);
318 } else {
319 emit working(false);
320 }
321
322 if(m_current->opponent()->isHuman() && !m_current->isHuman())
323 QTimer::singleShot(MOVE_PAUSE, this,
324 SLOT(slot_move_done_step_two()));
325 else
326 slot_move_done_step_two();
327 }
328
329 void myView::slot_move_done_step_two()
330 {
331 //
332 m_current->yourTurn(m_board->game());
333
334 if(check_game_over())
335 emit working(false);
336 }
337
338
339 void myView::you_won(bool yes)
340 {
341 myHistory* history = m_toplevel->get_history();
342 if(yes&&m_player->isWhite() || !yes&&!m_player->isWhite()) {
343 history->setTag(PdnGame::Result, "1-0"); // white wins
344 add_log(myView::System, tr("White wins!"));
345 } else {
346 history->setTag(PdnGame::Result, "0-1"); // black wins
347 add_log(myView::System, tr("Black wins!"));
348 }
349
350 emit working(false);
351 }
352
353
354 bool myView::openPdn(const QString& fn)
355 {
356 emit working(false);
357
358 m_current->stop();
359
360 QString log_text;
361 if(!m_toplevel->get_history()->openPdn(fn, log_text)) {
362 return false;
363 }
364
365 if(log_text.length()) {
366 add_log(myView::System, tr("Opened:")+" "+fn);
367 add_log(myView::Error, log_text.trimmed());
368 add_log(myView::Warning, tr("Warning! Some errors occured."));
369 }
370
371 return true;
372 }
373
374
375 bool myView::savePdn(const QString& fn)
376 {
377 if(!m_toplevel->get_history()->savePdn(fn)) {
378 qDebug() << __PRETTY_FUNCTION__ << "failed.";
379 return false;
380 }
381 add_log(myView::System, tr("Saved:")+" "+fn);
382 return true;
383 }
384
385
386 void myView::slot_new_mode(bool paused, bool freeplace)
387 {
388 if(paused) {
389 if(freeplace)
390 m_board->setCursor(Qt::PointingHandCursor);
391 else
392 m_board->setCursor(Qt::ForbiddenCursor);
393 } else {
394 m_board->setCursor(Qt::ArrowCursor);
395 }
396
397 // resume game: ask info for who is next, black or white.XXX FIXME TODO
398 if(!paused) {
399 myPlayer* next = 0;
400 if(m_toplevel->get_history()->moveCount()%2==0)
401 next = get_first_player();
402 else
403 next = get_first_player()->opponent();
404
405 m_current = next->opponent();
406 slot_move_done(m_board->game()->toString(false));
407 }
408 }
409
410
411 void myView::slot_preview_game(int rules)
412 {
413 if(rules!=RUSSIAN && rules!=ENGLISH) {
414 qDebug() << __PRETTY_FUNCTION__ << rules << "Wrong game type.";
415 return;
416 }
417
418 m_board->setGame(rules);
419
420 myHistory* history = m_toplevel->get_history();
421 if(m_player->isWhite() && rules==RUSSIAN) {
422 m_player->setName(history->getTag(PdnGame::White));
423 m_player->opponent()->setName(history->getTag(PdnGame::Black));
424 } else {
425 m_player->setName(history->getTag(PdnGame::Black));
426 m_player->opponent()->setName(history->getTag(PdnGame::White));
427 }
428
429 // FIXME
430 m_player->setWhite(rules==RUSSIAN);// FIXME TODO
431 m_player->opponent()->setWhite(!m_player->isWhite());
432 m_board->setColorWhite(m_player->isWhite());
433 m_board->adjustNotation(m_player->isWhite());
434 }
435
436
437 void myView::slot_apply_moves(const QString& moves)
438 {
439 QStringList move_list= moves.split(MOVE_SPLIT, QString::SkipEmptyParts);
440
441 m_board->reset();
442
443 bool white_player = get_first_player()->isWhite();
444 foreach(QString move, move_list) {
445 m_board->doMove(move, white_player);
446 white_player = !white_player;
447 }
448
449 // set current player who pulls next. assume is white.
450 m_current = (m_player->isWhite() ? m_player : m_player->opponent());
451 if(!white_player)
452 m_current = m_current->opponent();
453 m_toplevel->get_history()->setCurrent(m_current->name());
454 }
455
456
457 void myView::add_log(enum LogType type, const QString& text)
458 {
459 QString str = text;
460 str = str.replace('<', "&lt;");
461 str = str.replace('>', "&gt;");
462
463 QString tag_b, tag_e;
464 switch(type) {
465 case Error: tag_b="<ul><pre>"; tag_e="</pre></ul>"; break;
466 case Warning: tag_b="<b>"; tag_e="</b>"; break;
467 case System: tag_b="<font color=\"blue\">"; tag_e="</font>"; break;
468 default: break;
469 }
470
471 m_toplevel->append_log(tag_b + str + tag_e);
472 }
473
474
475 void myView::perform_jumps(const QString& from_board, const QString& to_board)
476 {
477 if(from_board==to_board) {
478 return;
479 }
480
481 QString new_to_board = to_board;
482
483 //qDebug("F:%s\nT:%s", from_board.latin1(), new_to_board.latin1());
484
485 // diff
486 QList<myDiff*> diff_list;
487
488 // collect information
489 for(int i=0; i<32; i++) {
490 if(from_board[2*i]!=new_to_board[2*i]
491 || from_board[2*i+1]!=new_to_board[2*i+1]) {
492 myDiff* diff = new myDiff(i,
493 from_board.mid(2*i, 2).toInt(),
494 new_to_board.mid(2*i, 2).toInt());
495 diff_list.append(diff);
496
497 //qDebug(">%d: %d->%d", diff->m_pos, diff->m_from, diff->m_to);
498 }
499 }
500
501 int from_pos = -1;
502 int to_pos = -1;
503 bool captured = (diff_list.count()>2);
504
505 int man = -1;
506 // find the dest. first: so we have the man moved.
507 foreach(myDiff* diff, diff_list) {
508 if(diff->m_to!=FREE) {
509 man = diff->m_to;
510 to_pos = diff->m_pos;
511 break;
512 }
513 }
514
515 int king = -1;
516 switch(man) {
517 case MAN1: king=KING1; break;
518 case KING1: king=MAN1; break;
519 case MAN2: king=KING2; break;
520 case KING2: king=MAN2; break;
521 }
522 // find src.
523 foreach(myDiff* diff, diff_list) {
524 if(diff->m_to==FREE) {
525 if(diff->m_from==man || diff->m_from==king) {
526 from_pos = diff->m_pos;
527 break;
528 }
529 }
530 }
531
532 /*
533 qDebug(" to_pos=%d with man/king=%d from=%d", to_pos, man,
534 from_pos);
535 */
536
537 // finally - animate :)
538 QString move = m_board->doMove(from_pos, to_pos, m_current->isWhite());
539 m_toplevel->get_history()->appendMove(move.replace("?", captured ? "x" : "-" ), "");
540
541 qDeleteAll(diff_list);
542 }
543
544 void myView::setNotation(bool enabled, bool show_above)
545 {
546 // TODO - intermediate function - remove somehow!
547 m_board->setNotation(enabled, show_above);
548 }
549
550
551 void myView::setNotationFont(const QFont& f)
552 {
553 // TODO - intermediate function - remove somehow!
554 m_board->setNotationFont(f);
555 }
556
557
558 myPlayer* myView::get_first_player() const
559 {
560 bool white = m_board->type()==RUSSIAN ? true : false;
561 // it is white.
562 if((white && m_player->isWhite()) || (!white && !m_player->isWhite()))
563 return m_player;
564 return m_player->opponent();
565 }
566
567