Debian Codegraph @

src/game_interfaces/qgoboard.cpp

raw symbols SCIP
1 /***************************************************************************
2 * Copyright (C) 2009 by The qGo Project *
3 * *
4 * This file is part of qGo. *
5 * *
6 * qGo 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, see <http://www.gnu.org/licenses/> *
18 * or write to the Free Software Foundation, Inc., *
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20 ***************************************************************************/
21
22
23 #include "qgoboard.h"
24 #include "boardwindow.h"
25 #include "tree.h"
26 #include "move.h"
27 #include "defines.h"
28 #include "audio.h"
29 #include "boarddispatch.h"
30 #include "messages.h"
31 #include "resultdialog.h"
32 #include "clockdisplay.h"
33 #include "gamedata.h"
34 #include "matrix.h"
35
36 qGoBoard::qGoBoard(BoardWindow *bw, Tree * t, GameData *gd) : QObject(bw)
37 {
38 isModified = false;
39 tree = t;
40 boardwindow = bw;
41
42 //data used for observing games, when getting the already played moves
43 stated_mv_count = -1;
44 lastMoveInGame = 0;
45
46 gameData = gd;
47
48 clickSound = new Sound("stone.wav");
49
50 dontCheckValidity = false;
51 }
52
53 qGoBoard::~qGoBoard()
54 {
55 delete clickSound;
56 }
57
58 /* FIXME: Make sure this isn't called from places it shouldn't be. Like
59 * subclass interfaces. */
60 void qGoBoard::setHandicap(int handicap)
61 {
62 GamePhase store = boardwindow->getGamePhase();
63 qDebug("setHandicap called\n");
64
65 /* Move should already be set to 0 by board handler and the placement
66 * of handicap stones won't change that */
67 boardwindow->setGamePhase(phaseEdit);
68 tree->getCurrent()->setMoveNumber(0);
69 if (tree->getCurrent()->getMatrix()->addHandicapStones(handicap))
70 {
71 tree->getCurrent()->setHandicapMove(true);
72 tree->getCurrent()->setX(-1);//-1
73 tree->getCurrent()->setY(-1);//-1
74 tree->getCurrent()->setColor(stoneBlack);
75 gameData->handicap = handicap;
76 }
77 boardwindow->updateMove(tree->getCurrent()); // Toggles window refresh
78
79 boardwindow->setGamePhase(store);
80 }
81
82 /*
83 * adds a mark to the current move's matrix
84 * This is used in edit phase to add mark on the board
85 */
86 void qGoBoard::addMark( int x, int y, MarkType t )
87 {
88 Matrix * mat = tree->getCurrent()->getMatrix();
89 if (mat->getMarkAt(x,y) != markNone)
90 mat->removeMark(x,y);
91
92 if (t == markText || t == markNumber)
93 {
94 QString txt = mat->getFirstTextAvailable(t);
95 mat->setMarkText(x,y,txt);
96 }
97
98 mat->insertMark(x,y,t);
99 /* FIXME this is a bit awkward here, but markTerr does not
100 * mark dead and they aren't ghosted immediately for some
101 * reason unless they're dead. Assuming this generally
102 * comes through qgoboard network code, we'll mark them
103 * dead here for now.
104 * I'm also still not certain if this is necessary, there
105 * was another bug elsewhere, but it can't hurt. */
106 if(t == markTerrBlack && mat->getStoneAt(x,y) == stoneWhite)
107 mat->markStoneDead(x,y);
108 else if(t == markTerrWhite && mat->getStoneAt(x,y) == stoneBlack)
109 mat->markStoneDead(x,y);
110
111 boardwindow->updateMove(tree->getCurrent());
112 setModified();
113 }
114
115 /*
116 * removes a mark to the current move's matrix
117 * This is used in edit phase to remove a mark on the board
118 */
119 void qGoBoard::removeMark( int x, int y)
120 {
121 if (tree->getCurrent()->getMatrix()->getMarkAt(x,y) != markNone)
122 tree->getCurrent()->getMatrix()->removeMark(x,y);
123
124 boardwindow->updateMove(tree->getCurrent());
125 setModified();
126 }
127
128 /*
129 * adds a stone to the current move's matrix
130 * This is used in edit phase to add stones (and not moves) on the board
131 */
132 void qGoBoard::addStone(StoneColor c, int x, int y)
133 {
134 tree->getCurrent()->addStone(c, x, y);
135 boardwindow->updateMove(tree->getCurrent());
136 setModified();
137 }
138
139 int qGoBoard::getMoveNumber(void)
140 {
141 return tree->getCurrent()->getMoveNumber();
142 }
143
144 /*
145 * 'Pass' button pressed
146 */
147 void qGoBoard::passRequest()
148 {
149 if((getBlackTurn() && boardwindow->getMyColorIsBlack()) ||
150 (!getBlackTurn() && boardwindow->getMyColorIsWhite()))
151 doPass();
152 }
153
154 /*
155 * 'Score' button toggled
156 */
157 void qGoBoard::slotScoreToggled(bool pressed)
158 {
159 if (pressed)
160 enterScoreMode();
161 else
162 leaveScoreMode();
163 }
164
165 void qGoBoard::setResult(GameResult & r)
166 {
167 if (tree->getCurrent() == NULL) //how can this happen???
168 {
169 qDebug("How can this possibly happen???");
170 return;
171 }
172 if(boardwindow->getGamePhase() == phaseEnded)
173 {
174 /* FIXME This is only okay if this is the only function
175 * setting the gamePhase to phaseEnded */
176 qDebug("Already received result");
177 return;
178 }
179 lastMoveInGame = tree->getCurrent();
180 if(r.result != GameResult::NOGAME)
181 kibitzReceived("\n" + r.shortMessage());
182 boardwindow->getGameData()->result = r.shortMessage();
183 /* This is all a little ugly. FIXME. The problem is that the
184 * QString result is used by the sgf loader, its sort of
185 * an easy way to store and load results through sgf files.
186 * The problem is that these aren't necessarily anything
187 * more than a string which means either we have to interpret
188 * them and allow partial GameResult records or this,
189 * which is to have two different results on the game data. And then
190 * all of this is just basically so the network code
191 * can get the margin when the client calulates it for
192 * a protocol. So yeah, find a better way. FIXME
193 * Really, I'd like to make the gamedata be one, single
194 * stand alone object instead of something we copy to different
195 * places, and then I'd like to do the same with the result
196 * as an add on to it. Just add whatever interpretation of
197 * SGF files is necessary.*/
198 boardwindow->getGameData()->fullresult = new GameResult(r);
199 //just one gamedata now, FIXME delete comment
200 BoardDispatch * boarddispatch = boardwindow->getBoardDispatch();
201 /*if(!boarddispatch)
202 {
203 qDebug("No board dispatch for game result");
204 return;
205 }
206 GameData * gr = boarddispatch->getGameData();
207 gr->fullresult = new GameResult(r);*/
208 /* This should be parent window modal, otherwise its annoying.
209 * I'm going to make it its own dialog so it could be extended
210 * later */
211 if(boarddispatch && r.result != GameResult::NOGAME)
212 {
213 ResultDialog * rd = new ResultDialog(boardwindow, boarddispatch, boardwindow->getId(), &r);
214 boarddispatch->setRematchDialog(rd); //necessary right?
215 rd->setWindowModality(Qt::WindowModal);
216 rd->show();
217 }
218 QSettings settings;
219 if( settings.value("AUTOSAVE").toBool())
220 boardwindow->doSave(boardwindow->getCandidateFileName(),true);
221
222 boardwindow->setGamePhase(phaseEnded);
223 /* FIXME: getting the result doesn't set the result
224 * in the toolbar for some reason. Like it doesn't
225 * show score or captures or anything else.
226 * this was an enterScoreMode thing, but there's more
227 * issues there*/
228 }
229
230 /*
231 * This handles the main envent with qGo : something has been clicked on the board
232 */
233 void qGoBoard::slotBoardClicked(bool , int x, int y , Qt::MouseButton mouseState)
234 {
235 bool blackToPlay = getBlackTurn();
236
237 switch (boardwindow->getGamePhase())
238 {
239 case phaseInit:
240 //should not happen
241 return ;
242
243 case phaseNavTo:
244 {
245 tree->findMoveByPos(x, y);
246 boardwindow->setGamePhase ( phaseOngoing );
247 return;
248 }
249
250 case phaseEdit:
251 {
252 switch (boardwindow->getEditMark())
253 {
254 case markNone:
255 {
256 if(tree->getCurrent()->getMatrix()->getStoneAt(x,y) == stoneNone)
257 addStone(mouseState == Qt::LeftButton ? stoneBlack : stoneWhite, x, y);
258 else
259 addStone(stoneErase, x, y);
260 setModified(true);
261 return;
262 }
263 default:
264 {
265 if (mouseState == Qt::LeftButton)
266 addMark(x,y, boardwindow->getEditMark());
267 else
268 removeMark(x,y);
269 setModified(true);
270 return;
271 }
272
273 }
274 return;
275 }
276
277 case phaseEnded:
278 return;
279
280 case phaseScore:
281 {
282 localMarkDeadRequest(x,y);
283 return;
284 }
285
286 case phaseOngoing:
287 if (blackToPlay && boardwindow->getMyColorIsBlack())
288 localMoveRequest(stoneBlack,x,y);
289
290 if (!blackToPlay && boardwindow->getMyColorIsWhite())
291 localMoveRequest(stoneWhite,x,y);
292 return;
293 }
294 }
295
296 /*
297 * This functions gets the request (from a board click)
298 * to mark a stone as dead/undead (score mode). All local but 'match'
299 */
300 void qGoBoard::localMarkDeadRequest(int x, int y)
301 {
302 toggleGroupAt(x,y);
303 /* FIXME maybe should be markDeadArea()? Maybe as option? */
304 }
305
306 /* We need to combine the below perhaps with the above sendPassToInterface */
307 /*
308 * This function adds a pass move to a game. there is no need to return anything
309 */
310 void qGoBoard::doPass(StoneColor c)
311 {
312 if (c == stoneNone)
313 c = (tree->getCurrent()->getColor() == stoneWhite) ? stoneBlack : stoneWhite;
314 tree->lastMoveInMainBranch = tree->getCurrent()->makeMove(c,PASS_XY,PASS_XY,true);
315 tree->setCurrent(tree->lastMoveInMainBranch);
316 setModified(true);
317 }
318
319
320 /*
321 * Returns true if black is to play
322 * FIXME shouldn't this be in board.cpp or some place?
323 * maybe not...
324 */
325 bool qGoBoard::getBlackTurn(bool time)
326 {
327 if (tree->getCurrent()->getPLinfo())
328 // color of next stone is same as current
329 return tree->getCurrent()->getPLnextMove() == stoneBlack;
330
331 //if (tree->getCurrent() == tree->getRoot())
332 // return true;
333
334 // the first handicap move bears number 0 as well
335 if (tree->getCurrent()->getMoveNumber() == 0)
336 {
337 // Handicap, so white starts
338 if (gameData->handicap >= 2)
339 return false;
340
341 return true;
342 }
343
344 // Normal mode
345 /* if (tree->getCurrent()->getGameMode() == modeNormal ||
346 tree->getCurrent()->getGameMode() == modeObserve ||
347 tree->getCurrent()->getGameMode() == modeMatch ||
348 tree->getCurrent()->getGameMode() == modeTeach ||
349 tree->getCurrent()->getGameMode() == modeComputer)
350 */
351 if (boardwindow->getGamePhase() != phaseEdit)
352 {
353 // change color
354 /* This is to prevent clock ticking for wrong player
355 * when looking back over moves in an observed game,
356 * for instance */
357 if(time)
358 return (tree->findLastMoveInMainBranch()->getColor() == stoneWhite);
359 else
360 return (tree->getCurrent()->getColor() == stoneWhite);
361 }
362 // Edit mode. Return color of parent move.
363 else if (tree->getCurrent()->parent != NULL)
364 return tree->getCurrent()->parent->getColor() == stoneWhite;
365
366 // Crap happened. 50% chance this is correct .)
367 qWarning("Oops, crap happened in BoardHandler::getBlackTurn() !");
368 return true;
369 }
370
371 /*
372 * This functions initialises the scoring mode
373 */
374 void qGoBoard::enterScoreMode()
375 {
376 if(boardwindow->getGamePhase() == phaseScore)
377 return;
378 qDebug("qgb::enterScoreMode()");
379 boardwindow->setGamePhase (phaseScore);
380 tree->countScore();
381 }
382
383 /*
384 * This functions leaves the scoring mode
385 */
386 void qGoBoard::leaveScoreMode()
387 {
388 if(boardwindow->getGamePhase() != phaseScore)
389 return;
390 qDebug("leaving score mode");
391 boardwindow->setGamePhase ( phaseOngoing );
392 tree->exitScore();
393 }
394
395 void qGoBoard::toggleGroupAt(int x, int y)
396 {
397 // is the click on a stone ?
398 if ( tree->getCurrent()->getMatrix()->getStoneAt(x, y) == stoneNone )
399 return;
400
401 tree->getCurrent()->getMatrix()->toggleGroupAt(x, y);
402 tree->countScore();
403 }
404
405 /*
406 * This functions is called in scoring phase when clicking on a dead group
407 */
408 void qGoBoard::markDeadStone(int x, int y)
409 {
410 // is the click on a stone ?
411 if ( tree->getCurrent()->getMatrix()->getStoneAt(x, y) == stoneNone )
412 return;
413
414 tree->getCurrent()->getMatrix()->markGroupDead(x, y);
415 tree->countScore();
416 }
417
418 void qGoBoard::markLiveStone(int x, int y)
419 {
420 // is the click on a stone ?
421 if ( tree->getCurrent()->getMatrix()->getStoneAt(x, y) == stoneNone )
422 return;
423
424 tree->getCurrent()->getMatrix()->markGroupAlive(x, y);
425 tree->countScore();
426 }
427
428 void qGoBoard::markDeadArea(int x, int y)
429 {
430 // is the click on a stone ?
431 if ( tree->getCurrent()->getMatrix()->getStoneAt(x, y) == stoneNone )
432 return;
433
434 tree->getCurrent()->getMatrix()->markAreaDead(x, y);
435 tree->countScore();
436 }
437
438 void qGoBoard::markLiveArea(int x, int y)
439 {
440 // is the click on a stone ?
441 if ( tree->getCurrent()->getMatrix()->getStoneAt(x, y) == stoneNone )
442 return;
443
444 tree->getCurrent()->getMatrix()->markAreaAlive(x, y);
445 tree->countScore();
446 }
447
448 void qGoBoard::slotUndoPressed(void)
449 {
450 /* Are we supposed to make a brother node or something ??? FIXME */
451 tree->deleteNode();
452 }
453
454 void qGoBoard::slotResignPressed(void)
455 {
456 GameResult g((getBlackTurn() ? stoneWhite : stoneBlack), GameResult::RESIGN);
457 setResult(g);
458 }
459
460 void qGoBoard::slotDonePressed(void)
461 {
462 GameResult g = tree->retrieveScore();
463 setResult(g);
464 }
465
466 /*
467 * kibitz was received. Tranfer it to move and comment window
468 */
469 void qGoBoard::kibitzReceived(const QString& text)
470 {
471
472 QString k = text;
473 // be nice to do this in bold or something
474 if(stated_mv_count != tree->findLastMoveInMainBranch()->getMoveNumber())
475 {
476 stated_mv_count = tree->findLastMoveInMainBranch()->getMoveNumber();
477 k.prepend( "(" + QString::number(stated_mv_count) + ") ");
478 }
479
480 boardwindow->displayComment(k);
481 }
482
483 // send regular time Info
484 void qGoBoard::timerEvent(QTimerEvent*)
485 {
486 // wait until first move
487 // if (mv_counter < 0 || id < 0 || game_paused)
488 if (boardwindow->getGamePhase() != phaseOngoing)
489 return;
490
491 boardwindow->getClockDisplay()->setTimeStep(getBlackTurn(true));
492 /*
493 // warn if I am within the last 10 seconds
494 if (gameMode == modeMatch)
495 {
496 if (myColorIsBlack && bt_i <= BY_timer && bt_i > -1 && mv_counter % 2) // ||
497 {
498 // each second we alternate button background color
499 if (bt_i %2)
500 win->getInterfaceHandler()->normalTools->pb_timeBlack->setPaletteBackgroundColor(Qt::red);
501 else
502 win->getInterfaceHandler()->normalTools->pb_timeBlack->setPaletteBackgroundColor(win->getInterfaceHandler()->normalTools->palette().color(QPalette::Active,QColorGroup::Button)) ;//setPaletteBackgroundColor(Qt::PaletteBase);
503 qgo->playTimeSound();
504 }
505
506 else if ( !myColorIsBlack && wt_i <= BY_timer && wt_i > -1 && (mv_counter % 2) == 0)
507 {
508 if (wt_i %2)
509 win->getInterfaceHandler()->normalTools->pb_timeWhite->setPaletteBackgroundColor(Qt::red);
510 else
511 win->getInterfaceHandler()->normalTools->pb_timeWhite->setPaletteBackgroundColor(win->getInterfaceHandler()->normalTools->palette().color(QPalette::Active,QColorGroup::Button)) ;//setPaletteBackgroundColor(Qt::PaletteBase);
512 qgo->playTimeSound();
513 }
514
515 //we have to reset the color when not anymore in warning period)
516 else if (win->getInterfaceHandler()->normalTools->pb_timeBlack->paletteBackgroundColor() == Qt::red)
517 win->getInterfaceHandler()->normalTools->pb_timeBlack->setPaletteBackgroundColor(win->getInterfaceHandler()->normalTools->palette().color(QPalette::Active,QColorGroup::Button)) ;
518
519
520 else if (win->getInterfaceHandler()->normalTools->pb_timeWhite->paletteBackgroundColor() == Qt::red)
521 win->getInterfaceHandler()->normalTools->pb_timeWhite->setPaletteBackgroundColor(win->getInterfaceHandler()->normalTools->palette().color(QPalette::Active,QColorGroup::Button)) ;
522
523
524
525 }
526 */
527 }
528
529 /* No pointer? Just an object?? FIXME */
530 TimeRecord qGoBoard::getOurTimeRecord(void)
531 {
532 return boardwindow->getClockDisplay()->getTimeRecord(boardwindow->getMyColorIsBlack());
533 }
534
535 TimeRecord qGoBoard::getTheirTimeRecord(void)
536 {
537 return boardwindow->getClockDisplay()->getTimeRecord(!boardwindow->getMyColorIsBlack());
538 }