Initial porting to the new component API.
[tagua/yd.git] / src / game.h
blob0fa36122f09ae4ce8152975be5a3a27baaf6b1a5
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 <core/move.h>
18 #include <core/state_fwd.h>
19 #include "index.h"
21 class IMoveSerializer;
22 class IValidator;
23 class Variant;
24 class PGN;
26 namespace GamePrivate {
28 class Entry;
29 typedef std::vector<Entry> History;
31 typedef boost::variant<class UndoAdd, class UndoPromote, class UndoTruncate,
32 class UndoRemove, class UndoClear, class UndoSetComment> UndoOp;
33 typedef std::vector<UndoOp> UndoHistory;
38 /**
39 \class Game game.h <game.h>
40 \brief A game with history and variations.
42 This template class encapsulates an editable game with history and undo editing.
44 class Game {
45 public:
46 GamePrivate::History history;
47 Index current;
49 GamePrivate::UndoHistory undo_history;
50 int undo_pos;
52 GamePrivate::Entry* fetch(const Index& ix);
53 const GamePrivate::Entry* fetch(const Index& ix) const;
54 GamePrivate::History* fetchRef(const Index& ix, int* idx);
55 const GamePrivate::History* fetchRef(const Index& ix, int* idx) const;
57 void testMove();
58 void testMove(const Index& ix);
59 void saveUndo(const GamePrivate::UndoOp& op);
61 QString variationPgn(const GamePrivate::History&, const GamePrivate::Entry&,
62 int start, const Index& _ix) const;
64 virtual void onAdded(const Index& i);
65 virtual void onRemoved(const Index& i);
66 virtual void onEntryChanged(const Index& at, int propagate=1);
67 virtual void onPromoteVariation(const Index& i, int v);
68 virtual void onSetComment(const Index& i, const QString& c);
69 virtual void onSetVComment(const Index& i, int v, const QString& c);
70 virtual void onCurrentIndexChanged(const Index& old = Index(-1));
71 virtual void onAvailableUndo(bool);
72 virtual void onAvailableRedo(bool);
74 IValidator* m_validator;
75 IMoveSerializer* m_serializer;
76 public:
77 /** Constructor, creates an empty game*/
78 explicit Game(Variant* variant);
80 /** destructor */
81 virtual ~Game();
83 /** \return the index of the current position */
84 Index index() const;
86 /** \return an index opinting to the last position in the main line */
87 Index lastMainlineIndex() const;
89 /** \return true if the game contains the index */
90 bool containsIndex(const Index& index) const;
92 /** \return the current move */
93 Move move() const;
95 /** \return the move at the given index */
96 Move move(const Index& index) const;
98 /** \return the current position */
99 StatePtr position() const;
101 /** \return the position at the given index */
102 StatePtr position(const Index& index) const;
104 /** \return the current comment */
105 QString comment() const;
107 /** \return the comment at the given index */
108 QString comment(const Index& index) const;
110 /** clears the games, and puts \a pos as root position */
111 void reset(StatePtr pos);
113 /** undo */
114 void undo();
116 /** redo */
117 void redo();
119 /** sets the comment in the current index */
120 void setComment(const QString& c);
122 /** sets the comment at the given index */
123 void setComment(const Index& index, const QString& c);
125 /** sets the variation comment at the given index/variation */
126 void setVComment(const Index& index, int v, const QString& c);
128 /** promotes the current position in the upper main line */
129 void promoteVariation();
131 /** promotes the given position in the upper main line */
132 void promoteVariation(const Index& index);
134 /** promotes the given variation in the upper main line */
135 void promoteVariation(const Index& index, int v);
137 /** removes the given variation in the current index */
138 void removeVariation(int v);
140 /** removes the given variation in the given index */
141 void removeVariation(const Index& index);
143 /** removes the given variation in the given index */
144 void removeVariation(const Index& index, int v);
146 /** removes the given variation in the current index */
147 void clearVariations();
149 /** removes the given variation in the given index */
150 void clearVariations(const Index& index);
152 /** removes all the successors of the current position */
153 void truncate();
155 /** removes all the successors of the given position */
156 void truncate(const Index& index);
158 /** adds a new move+position after the current one, on the main
159 line if possible, or else in a new variation */
160 void add(const Move& move, const StatePtr& pos);
162 /** forces a move+position at in certain index */
163 bool insert(const Move& move, const StatePtr& pos, const Index& index);
165 /** \return true if we cannot go forward */
166 bool lastPosition() const;
168 /** go back */
169 bool back();
171 /** go forward (in the current mainline) */
172 bool forward();
174 /** go to the root position */
175 void gotoFirst();
177 /** go to the last position (in the current mainline) */
178 void gotoLast();
180 /** go to a specified index */
181 bool goTo(const Index& index);
183 /** \return a pgn containing the whole game (with variations) */
184 QString pgn() const;
186 /** loads a pgn in the current game */
187 void load(StatePtr, const PGN& pgn);
189 /** loads a pgn in the current game */
190 void load(const PGN& pgn);
193 #endif // GAME_H