Add more assertions.
[tagua/yd.git] / src / game.h
blob124e28f049f426eedaf810336287b1293732619c
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 Components;
22 class PGN;
23 class Variant;
25 namespace GamePrivate {
27 class Entry;
28 typedef std::vector<Entry> History;
30 typedef boost::variant<class UndoAdd, class UndoPromote, class UndoTruncate,
31 class UndoRemove, class UndoClear, class UndoSetComment> UndoOp;
32 typedef std::vector<UndoOp> UndoHistory;
37 /**
38 \class Game game.h <game.h>
39 \brief A game with history and variations.
41 This template class encapsulates an editable game with history and undo editing.
43 class Game {
44 public:
45 GamePrivate::History history;
46 Index current;
48 GamePrivate::UndoHistory undo_history;
49 int undo_pos;
51 GamePrivate::Entry* fetch(const Index& ix);
52 const GamePrivate::Entry* fetch(const Index& ix) const;
53 GamePrivate::History* fetchRef(const Index& ix, int* idx);
54 const GamePrivate::History* fetchRef(const Index& ix, int* idx) const;
56 void testMove();
57 void testMove(const Index& ix);
58 void saveUndo(const GamePrivate::UndoOp& op);
60 QString variationPgn(const GamePrivate::History&, const GamePrivate::Entry&,
61 int start, const Index& _ix) const;
63 virtual void onAdded(const Index& i);
64 virtual void onRemoved(const Index& i);
65 virtual void onEntryChanged(const Index& at, int propagate=1);
66 virtual void onPromoteVariation(const Index& i, int v);
67 virtual void onSetComment(const Index& i, const QString& c);
68 virtual void onSetVComment(const Index& i, int v, const QString& c);
69 virtual void onCurrentIndexChanged(const Index& old = Index(-1));
70 virtual void onAvailableUndo(bool);
71 virtual void onAvailableRedo(bool);
73 Components* m_components;
74 public:
75 /** Constructor, creates an empty game*/
76 explicit Game(Components* components);
78 /** destructor */
79 virtual ~Game();
81 /** \return the index of the current position */
82 Index index() const;
84 /** \return an index opinting to the last position in the main line */
85 Index lastMainlineIndex() const;
87 /** \return true if the game contains the index */
88 bool containsIndex(const Index& index) const;
90 /** \return the current move */
91 Move move() const;
93 /** \return the move at the given index */
94 Move move(const Index& index) const;
96 /** \return the current position */
97 StatePtr position() const;
99 /** \return the position at the given index */
100 StatePtr position(const Index& index) const;
102 /** \return the current comment */
103 QString comment() const;
105 /** \return the comment at the given index */
106 QString comment(const Index& index) const;
108 /** clears the games, and puts \a pos as root position */
109 void reset(StatePtr pos);
111 /** undo */
112 void undo();
114 /** redo */
115 void redo();
117 /** sets the comment in the current index */
118 void setComment(const QString& c);
120 /** sets the comment at the given index */
121 void setComment(const Index& index, const QString& c);
123 /** sets the variation comment at the given index/variation */
124 void setVComment(const Index& index, int v, const QString& c);
126 /** promotes the current position in the upper main line */
127 void promoteVariation();
129 /** promotes the given position in the upper main line */
130 void promoteVariation(const Index& index);
132 /** promotes the given variation in the upper main line */
133 void promoteVariation(const Index& index, int v);
135 /** removes the given variation in the current index */
136 void removeVariation(int v);
138 /** removes the given variation in the given index */
139 void removeVariation(const Index& index);
141 /** removes the given variation in the given index */
142 void removeVariation(const Index& index, int v);
144 /** removes the given variation in the current index */
145 void clearVariations();
147 /** removes the given variation in the given index */
148 void clearVariations(const Index& index);
150 /** removes all the successors of the current position */
151 void truncate();
153 /** removes all the successors of the given position */
154 void truncate(const Index& index);
156 /** adds a new move+position after the current one, on the main
157 line if possible, or else in a new variation */
158 void add(const Move& move, const StatePtr& pos);
160 /** forces a move+position at in certain index */
161 bool insert(const Move& move, const StatePtr& pos, const Index& index);
163 /** \return true if we cannot go forward */
164 bool lastPosition() const;
166 /** go back */
167 bool back();
169 /** go forward (in the current mainline) */
170 bool forward();
172 /** go to the root position */
173 void gotoFirst();
175 /** go to the last position (in the current mainline) */
176 void gotoLast();
178 /** go to a specified index */
179 bool goTo(const Index& index);
181 /** \return a pgn containing the whole game (with variations) */
182 QString pgn() const;
184 /** loads a pgn in the current game */
185 void load(StatePtr, const PGN& pgn);
187 /** loads a pgn in the current game */
188 void load(const PGN& pgn);
191 #endif // GAME_H