Rough PGN save support.
[tagua/yd.git] / src / game.h
blob5de902ec81cf74ffd29b8c75bad9f0782412d1c4
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
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 */
11 #ifndef GAME_H
12 #define GAME_H
14 #include <boost/shared_ptr.hpp>
15 #include <boost/variant/variant_fwd.hpp>
16 #include <vector>
17 #include "fwd.h"
18 #include "index.h"
20 class PGN;
22 namespace GamePrivate {
24 class Entry;
25 typedef std::vector<Entry> History;
27 typedef boost::variant<class UndoAdd, class UndoPromote, class UndoTruncate,
28 class UndoRemove, class UndoClear, class UndoSetComment> UndoOp;
29 typedef std::vector<UndoOp> UndoHistory;
34 /**
35 \class Game game.h <game.h>
36 \brief A game with history and variations.
38 This template class encapsulates an editable game with history and undo editing.
40 class Game {
41 public:
42 GamePrivate::History history;
43 Index current;
45 GamePrivate::UndoHistory undo_history;
46 int undo_pos;
48 GamePrivate::Entry* fetch(const Index& ix);
49 const GamePrivate::Entry* fetch(const Index& ix) const;
50 GamePrivate::History* fetchRef(const Index& ix, int* idx);
51 const GamePrivate::History* fetchRef(const Index& ix, int* idx) const;
53 void testMove();
54 void testMove(const Index& ix);
55 void saveUndo(const GamePrivate::UndoOp& op);
57 QString variationPgn(const GamePrivate::History&, const GamePrivate::Entry&,
58 int start, const Index& _ix) const;
60 virtual void onAdded(const Index& i);
61 virtual void onRemoved(const Index& i);
62 virtual void onEntryChanged(const Index& at, int propagate=1);
63 virtual void onPromoteVariation(const Index& i, int v);
64 virtual void onSetComment(const Index& i, const QString& c);
65 virtual void onSetVComment(const Index& i, int v, const QString& c);
66 virtual void onCurrentIndexChanged(const Index& old = Index(-1));
67 virtual void onAvailableUndo(bool);
68 virtual void onAvailableRedo(bool);
70 public:
71 /** Constructor, creates an empty game*/
72 Game();
74 /** destructor */
75 virtual ~Game();
77 /** \return the index of the current position */
78 Index index() const;
80 /** \return an index opinting to the last position in the main line */
81 Index lastMainlineIndex() const;
83 /** \return true if the game contains the index */
84 bool containsIndex(const Index& index) const;
86 /** \return the current move */
87 MovePtr move() const;
89 /** \return the move at the given index */
90 MovePtr move(const Index& index) const;
92 /** \return the current position */
93 PositionPtr position() const;
95 /** \return the position at the given index */
96 PositionPtr position(const Index& index) const;
98 /** \return the current comment */
99 QString comment() const;
101 /** \return the comment at the given index */
102 QString comment(const Index& index) const;
104 /** clears the games, and puts \a pos as root position */
105 void reset(PositionPtr pos);
107 /** undo */
108 void undo();
110 /** redo */
111 void redo();
113 /** sets the comment in the current index */
114 void setComment(const QString& c);
116 /** sets the comment at the given index */
117 void setComment(const Index& index, const QString& c);
119 /** sets the variation comment at the given index/variation */
120 void setVComment(const Index& index, int v, const QString& c);
122 /** promotes the current position in the upper main line */
123 void promoteVariation();
125 /** promotes the given position in the upper main line */
126 void promoteVariation(const Index& index);
128 /** promotes the given variation in the upper main line */
129 void promoteVariation(const Index& index, int v);
131 /** removes the given variation in the current index */
132 void removeVariation(int v);
134 /** removes the given variation in the given index */
135 void removeVariation(const Index& index);
137 /** removes the given variation in the given index */
138 void removeVariation(const Index& index, int v);
140 /** removes the given variation in the current index */
141 void clearVariations();
143 /** removes the given variation in the given index */
144 void clearVariations(const Index& index);
146 /** removes all the successors of the current position */
147 void truncate();
149 /** removes all the successors of the given position */
150 void truncate(const Index& index);
152 /** adds a new move+position after the current one, on the main
153 line if possible, or else in a new variation */
154 void add(MovePtr move, PositionPtr pos);
156 /** forces a move+position at in certain index */
157 bool insert(MovePtr move, PositionPtr pos, const Index& index);
159 /** \return true if we cannot go forward */
160 bool lastPosition() const;
162 /** go back */
163 bool back();
165 /** go forward (in the current mainline) */
166 bool forward();
168 /** go to the root position */
169 void gotoFirst();
171 /** go to the last position (in the current mainline) */
172 void gotoLast();
174 /** go to a specified index */
175 bool goTo(const Index& index);
177 /** \return a pgn containing the whole game (with variations) */
178 QString pgn() const;
180 /** loads a pgn in the current game */
181 void load(PositionPtr, const PGN& pgn);
183 /** loads a pgn in the current game */
184 void load(const PGN& pgn);
187 #endif // GAME_H