Debian Codegraph @

src/game_tree/tree.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 "tree.h"
24 #include "move.h"
25 #include "matrix.h"
26 #include "group.h"
27 #include "messages.h"
28 #include "sgfparser.h"
29 #include "gamedata.h"
30
31 #include <vector>
32
33 #include <QtCore>
34
35 Tree::Tree(int board_size, float komi)
36 : boardSize(board_size), root(NULL), komi(komi)
37 {
38 init();
39 loadingSGF = false;
40 }
41
42 void Tree::init()
43 {
44 if(root)
45 delete root;
46 lastMoveInMainBranch = current = root = new Move(boardSize);
47 // node index used for IGS review
48 root->setNodeIndex(1);
49 }
50
51 Tree::~Tree()
52 {
53 if (root)
54 delete root;
55 }
56
57 /*
58 * Find a move starting from the given in the argument in the main branch, or at marked move.
59 */
60 Move* Tree::findMove(Move *start, int x, int y, bool checkmarker)
61 {
62 Move *t = start;
63 while (t && (t->getX() != x || t->getY() != y))
64 {
65 t = (checkmarker && t->marker ? t->marker : t->son);
66 }
67 return t;
68 }
69
70 /*
71 * Find a move starting from the given in the argument in the
72 * all following branches.
73 */
74 Move* Tree::findNode(Move *m, int node)
75 {
76 Q_CHECK_PTR(m);
77 QStack<Move*> stack;
78 Move *t = NULL;
79
80 // Traverse the tree and drop every node into stack result
81 stack.push(m);
82
83 while (!stack.isEmpty())
84 {
85 t = stack.pop();
86 if (t != NULL)
87 {
88 if (t->getNodeIndex() == node)
89 return t;
90 stack.push(t->brother);
91 stack.push(t->son);
92 }
93 }
94
95 // node index not found
96 return NULL;
97 }
98
99 void Tree::setCurrent(Move *m)
100 {
101 if (!m || (current == m))
102 return;
103
104 current = m;
105 emit currentMoveChanged(current);
106 }
107
108 Move *Tree::findLastMoveInMainBranch()
109 {
110 return lastMoveInMainBranch;
111 }
112
113 Move *Tree::findLastMoveInCurrentBranch()
114 {
115 Move *m = getCurrent();
116 if (m)
117 return m->getLastMove(true);
118 else
119 return NULL;
120 }
121
122 void Tree::addEmptyMove()
123 {
124 Move * node = new Move(current,stoneBlack, -1, -1);
125 node->setTimeinfo(false);
126 current = node;
127 if(current->isInMainBranch())
128 lastMoveInMainBranch = current;
129 }
130
131 /* FIXME double check and remove editMove, unnecessary */
132 void Tree::doPass(bool /*sgf*/, bool /*fastLoad*/)
133 {
134 /////////////////FIXME We may have a problem at first move with handicap games ...
135 StoneColor c = (current->getColor() == stoneWhite) ? stoneBlack : stoneWhite;
136 Move *result = current->makeMove(c, PASS_XY, PASS_XY);
137 setCurrent(result);
138 }
139
140 /*
141 * this deletes the current node an all its sons
142 */
143 void Tree::deleteNode()
144 {
145 if (current == root)
146 {
147 init();
148 return;
149 }
150
151 Move *remember = current->parent;
152 delete current;
153 setCurrent(remember);
154 if(remember->isInMainBranch())
155 lastMoveInMainBranch = remember;
156 }
157
158
159 /*
160 * Former BoardHandler slots
161 */
162 void Tree::slotNavForward()
163 {
164 if (root == NULL || current == NULL || current->son == NULL)
165 return;
166
167 if (current->marker)
168 setCurrent(current->marker);
169 else
170 setCurrent(current->son);
171
172 current->parent->marker = current; // Parents remembers this move we went to
173 }
174
175 void Tree::slotNavBackward()
176 {
177 if (root == NULL || current == NULL || current->parent == NULL)
178 return;
179
180 current->parent->marker = current; // Remember the son we came from
181 setCurrent(current->parent);
182 }
183
184 void Tree::slotNavFirst()
185 {
186 setCurrent(root);
187 }
188
189 void Tree::slotNavLast()
190 {
191 setCurrent(findLastMoveInCurrentBranch());
192 }
193
194 void Tree::slotNavPrevComment()
195 {
196 Move *m = current->parent;
197
198 while (m)
199 {
200 if (!(m->getComment().isEmpty()))
201 break;
202 m = m->parent;
203 }
204 setCurrent(m);
205 }
206
207 void Tree::slotNavNextComment()
208 {
209 Move *m = current->son;
210
211 while (m)
212 {
213 if (!(m->getComment().isEmpty()))
214 break;
215 m = m->son;
216 }
217 setCurrent(m);
218 }
219
220 void Tree::slotNthMove(int n)
221 {
222 if (n < 0)
223 return;
224
225 Move *m = current;
226
227 int currentMove = m->getMoveNumber();
228
229 while (m && ((currentMove = m->getMoveNumber()) != n))
230 {
231 if (m->parent)
232 m->parent->marker = m;
233 if (n > currentMove)
234 {
235 m = (m->marker ? m->marker : m->son);
236 }
237 else
238 {
239 m = m->parent;
240 }
241 }
242
243 setCurrent(m);
244 }
245
246 void Tree::slotNavNextVar()
247 {
248 if (current && current->brother)
249 setCurrent(current->brother);
250 }
251
252 void Tree::slotNavPrevVar()
253 {
254 if (current == NULL)
255 return;
256 setCurrent(current->getPrevBrother());
257 }
258
259 void Tree::slotNavStartVar()
260 {
261 Move *m = current->parent;
262 while (m && !(m->brother))
263 m = m->parent;
264 setCurrent(m ? m : root);
265 }
266
267 void Tree::slotNavNextBranch()
268 {
269 Move *m = current;
270 if (m == NULL)
271 return;
272
273 // Descend to the next branching point
274 while (m->son && !(m->brother))
275 m = m->son;
276
277 setCurrent(m);
278 }
279
280 /*
281 * This function resumes back to the first move in the main branch
282 */
283 void Tree::slotNavMainBranch()
284 {
285 Move *m = current;
286 Move *n = current;
287 while (m->parent)
288 {
289 if (m->parent->son != m)
290 n = m->parent;
291 m = m->parent;
292 }
293
294 if (n == m)
295 return;
296
297 n->marker = NULL;
298 setCurrent(n);
299 }
300
301 /*
302 * Called after the preceding (slot nav Intersection)
303 * When the intersection 'x/y' has been clicked on
304 */
305 void Tree::findMoveByPos(int x, int y)
306 {
307 Move *m = findMoveInMainBranch(x, y);
308 if(!m)
309 m=findMoveInCurrentBranch(x, y);
310
311 if (m)
312 setCurrent(m);
313 }
314
315 /*
316 * Called by qgoboard when score button is pressed up, leaving score mode
317 */
318 void Tree::exitScore()
319 {
320 // Remove territory marks
321 if (current->isTerritoryMarked())
322 {
323 current->getMatrix()->clearTerritoryMarks();
324 current->setTerritoryMarked(false);
325 current->setScored(false);
326 }
327
328 current->getMatrix()->absMatrix();
329 emit currentMoveChanged(current);
330 }
331
332 bool Tree::importSGFFile(QString filename)
333 {
334 SGFParser *sgfParser = new SGFParser(this);
335 QString SGFLoaded = sgfParser->loadFile(filename);
336 return sgfParser->doParse(SGFLoaded);
337 }
338
339 bool Tree::importSGFString(QString SGF)
340 {
341 SGFParser *sgfParser = new SGFParser(this);
342 return sgfParser->doParse(SGF);
343 }
344
345 QString Tree::exportSGFString(GameData *gameData)
346 {
347 QString sgf = "";
348 SGFParser *p = new SGFParser(this);
349 p->exportSGFtoClipB(&sgf, this, gameData);
350 delete p;
351 return sgf;
352 }
353
354 /*
355 * Performs all operations on the matrix of current move to display score marks
356 * and score informaton on the uI
357 */
358 void Tree::countScore(void)
359 {
360 current->getMatrix()->markTerritory();
361 current->setTerritoryMarked(true);
362 current->setScored(true);
363 emit currentMoveChanged(current); // Toggles window refresh
364
365 countMarked(current);
366 }
367
368 void Tree::countMarked(Move * mv)
369 {
370 deadWhite = 0;
371 deadBlack = 0;
372 terrWhite = 0;
373 terrBlack = 0;
374 capturesBlack = mv->getCapturesBlack();
375 capturesWhite = mv->getCapturesWhite();
376
377 mv->getMatrix()->count(terrBlack, terrWhite, deadBlack, deadWhite);
378 qDebug() << "Tree::countMarked():" << terrBlack << capturesBlack << deadWhite << terrWhite << capturesWhite << deadBlack;
379 emit scoreChanged(terrBlack, capturesBlack, deadWhite,
380 terrWhite, capturesWhite, deadBlack);
381 }
382
383 /* Not totally confident that this belongs here, but
384 * the score is counted here */
385 GameResult Tree::retrieveScore(void)
386 {
387 GameResult g;
388 g.result = GameResult::SCORE;
389 /* What about different scoring types? (chinese versus japanese)
390 * FIXME This basically confirms for me that this does not
391 * belong here */
392
393 float blackScore = terrBlack + capturesBlack + deadWhite;
394 float whiteScore = terrWhite + capturesWhite + deadBlack + komi;
395 if(whiteScore > blackScore)
396 {
397 g.winner_color = stoneWhite;
398 g.winner_score = whiteScore;
399 g.loser_score = blackScore;
400 }
401 else
402 {
403 g.winner_color = stoneBlack;
404 g.winner_score = blackScore;
405 g.loser_score = whiteScore;
406 }
407 return g;
408 }