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.
15 #include <boost/shared_ptr.hpp>
17 #include <QStringList>
22 #include "decoratedmove.h"
23 #include "interactiontype.h"
24 #include "turnpolicy.h"
29 class ActionCollection
;
33 * @brief Low level abstract classes used by the interface framework.
35 * This file includes definitions for abstract classes used from within
36 * the interface framework to access variant details using dynamic time
39 * One of the big problems with this approach is what I call 'lack of
40 * functional dependency support' for runtime polymorphism.
41 * What is missing is the ability to override a virtual member function
42 * in a derived class changing its signature. Or better, it is possible
43 * to only change its return type with a covariant type, while it would
44 * often be useful to change any of its argument types with covariant or
45 * even controvariant types.
46 * For controvariant types, I foresee no problems, and all type checking
47 * can be done statically (of course the method has to be dynamically
48 * bound) as they are now.
49 * When using covariant types, the compiler could insert appropriate
50 * dynamic cast's and type checks in the calling code, when there's no
51 * way to know the exact type of an argument at compile time.
53 * The file @ref tagua_wrapped.h tries to address this problem doing part
54 * of the compiler's work. One exception is that type checking code
55 * is inserted in a method wrapper, instead of the calling place, so
56 * even calling a method of a noncasted instance would cause some
57 * upcast / downcast overhead; this is negligible, though, because
58 * that scenario is rather unlikely, since wrapped classes are always
59 * kept in variables of abstract types.
64 * @brief A superclass for all the piece classes.
68 typedef boost::shared_ptr
<AbstractPiece
> Ptr
;
69 virtual ~AbstractPiece() { }
72 * Piece equality. Return false if the second pointer
75 virtual bool equals(const PiecePtr
& other
) const = 0;
78 * Return a unique key for the piece.
79 * Used to store pixmaps.
81 virtual QString
name() const = 0;
84 * Create a deep copy of the piece.
86 virtual PiecePtr
clone() const = 0;
91 * @brief A superclass for all the move classes.
93 * An abstract move is simply something that can serialize
94 * itself either as SAN notation, or as coordinate notation.
98 typedef boost::shared_ptr
<AbstractMove
> Ptr
;
99 virtual ~AbstractMove() { }
102 * Serialize the move.
103 * \param rep The type of representation to use.
104 * \param ref A reference position. Its meaning is defined by the serializer.
106 virtual QString
toString(const QString
& rep
, const PositionPtr
& ref
) const = 0;
109 * Convert the move to a normal user move. Used to
110 * perform move highlighting.
112 virtual NormalUserMove
toUserMove() const = 0;
115 * Checks if the two moves are equal.
117 virtual bool equals(const MovePtr
& other
) const = 0;
122 * @brief Superclass for pools
124 * A general interface for pools.
128 typedef boost::shared_ptr
<AbstractPool
> Ptr
;
129 virtual ~AbstractPool() {}
132 * \return the number of items in the pool
134 virtual int size() = 0;
137 * Inserts a piece in the pool, preferably at the position \a pref_index.
138 * But the pool can be unpredictable and the piece can be placed at an arbitrary position.
139 * \return the position at which the item was placed.
141 virtual int insert(int pref_index
, const PiecePtr
& piece
) = 0;
144 * Gets the piece at the position \a index in the pool.
146 virtual PiecePtr
get(int index
) = 0;
149 * Removes the piece at the position \a index in the pool.
150 * \return the removed piece.
152 virtual PiecePtr
take(int index
) = 0;
158 * @brief A superclass for all the position classes.
160 * A general interface for positions. It is used from within
161 * the graphical framework to validate external input wrt
162 * the played variant.
164 class AbstractPosition
{
166 typedef boost::shared_ptr
<AbstractPosition
> Ptr
;
167 typedef boost::shared_ptr
<AbstractPool
> PoolPtr
;
168 virtual ~AbstractPosition() { }
171 * Return board size in logical units. This could be
172 * static if C++ permitted virtual static functions.
174 virtual Point
size() const = 0;
177 * Returns the text to be used for the border surrounding this position in a board
179 virtual QStringList
borderCoords() const = 0;
182 * Create the starting piece setup.
184 virtual void setup() = 0;
187 * Retrieve the piece on square @a p.
188 * Return a null pointer if that square is empty.
190 virtual PiecePtr
get(const Point
& p
) const = 0;
193 * Set a piece on the board.
195 virtual void set(const Point
& p
, const PiecePtr
& piece
) = 0;
198 * \return an interface to modify the pool of the board relative to \a player
200 virtual PoolPtr
pool(int player
) = 0;
203 * Set a position pool, copying it from a given position.
205 virtual void copyPoolFrom(const PositionPtr
& pos
) = 0;
208 * \return 1 if the piece can be moved, -1 if could be moved in the future (premove), or else 0.
210 virtual InteractionType
movable(const TurnPolicy::Collection
& test
, const Point
& p
) const = 0;
213 * \return 1 if this pool can be dropped, -1 if could be dropped in the future (premove), or else 0.
215 virtual InteractionType
droppable(const TurnPolicy::Collection
& test
, int) const = 0;
218 * \return an id corresponding to the player
221 virtual int turn() const = 0;
224 * Sets the player on move
226 virtual void setTurn(int) = 0;
229 * \return the player that just moved
231 virtual int previousTurn() const = 0;
234 * Switch to the next player.
236 virtual void switchTurn() = 0;
239 * Check move legality. Set move fields as needed.
240 * Return whether the move is legal.
241 * This function should return immediately if \a m
242 * has already been tested.
244 virtual bool testMove(const MovePtr
& m
) const = 0;
247 * Execute move \a m. Assume that \a m is legal and tested.
249 virtual void move(const MovePtr
& m
) = 0;
252 * Create a deep copy of the position.
254 virtual PositionPtr
clone() const = 0;
257 * Make the position equal to the given one.
259 virtual void copyFrom(const PositionPtr
&) = 0;
262 * Tests if two positions are equal.
264 virtual bool equals(const PositionPtr
& p
) const = 0;
267 * Return a move from an algebraic notation, or a null pointer.
269 virtual MovePtr
getMove(const QString
&) const = 0;
272 * Return a string representing the current state of the position.
274 virtual QString
state() const = 0;
277 * A piece somehow representing or related to the move
278 * which has to be drawn by the interface.
280 * * for one click moves, the piece that would
281 * appear on the square.
282 * * for promotions in chesslike variants, the promoted
284 * @note Return a null pointer if the move has no valid
285 * piece hint defined.
287 virtual PiecePtr
moveHint(const MovePtr
&) const = 0;
290 * Variant introspection
292 virtual QString
variant() const = 0;
297 virtual void dump() const = 0;
300 class AnimationGroup
;
303 * @brief An abstract superclass for all animator classes.
305 * An animator is a class which is given a difference between
306 * a graphical and a logical position, and schedules an animation
307 * which graphically does an update.
308 * If the difference is due to a move, the animator tries to
309 * create a smooth animation which could possibly work for many
310 * chess-like variants, and then fills in the gaps with a direct
313 class AbstractAnimator
{
315 typedef boost::shared_ptr
<AbstractAnimator
> Ptr
;
316 virtual ~AbstractAnimator() { }
319 * Sync the graphical position with the given position.
321 virtual AnimationPtr
warp(const PositionPtr
&) = 0;
324 * Animate forward syncing to the given position.
326 virtual AnimationPtr
forward(const PositionPtr
&, const MovePtr
&) = 0;
329 * Animate back syncing to the given position.
331 virtual AnimationPtr
back(const PositionPtr
&, const MovePtr
&) = 0;
337 virtual ~VariantInfo() { }
338 virtual PositionPtr
createPosition() = 0;
339 virtual PositionPtr
createCustomPosition(const OptList
& l
) = 0;
340 virtual void forallPieces(class PieceFunction
&) = 0;
341 virtual int moveListLayout() const = 0;
342 virtual AnimatorPtr
createAnimator(GraphicalAPI
* graphical_api
) = 0;
343 virtual MovePtr
createNormalMove(const NormalUserMove
&) = 0;
344 virtual MovePtr
createDropMove(const DropUserMove
&) = 0;
345 virtual MovePtr
getVerboseMove(int turn
, const class VerboseNotation
&) const = 0;
348 * \return if moves are done by just clicking
350 virtual bool simpleMoves() = 0;
353 * \return the name of the variant
355 virtual QString
name() const = 0;
358 * \return the name of the theme proxy variant, ie the variant whose theme can be used
359 * for the current one (for instance crazyhouse can use chess themes).
361 virtual QString
themeProxy() const = 0;
364 * \return the (subvariant) options that can be specified for position creation, such as board size, etc
366 virtual OptList
positionOptions() const = 0;
369 * \return The ICS API for this variant, or a null pointer, if the variant does not support
370 * ICS (in that case a Dummy variant will be used).
372 virtual ICSAPIPtr
icsAPI() const = 0;
375 * Setup a list of variant specific actions to be displayed on a game toolbar
378 virtual ActionCollection
* actions() = 0;
381 * Add variant specific information (i.e. promotionType) to a move.
383 virtual void setupMove(NormalUserMove
& m
) const = 0;
387 class VariantFactory
{
389 virtual ~VariantFactory() { }
390 virtual VariantInfo
* createVariant() const = 0;
391 virtual QString
name() const = 0;
392 virtual QString
themeProxy() const = 0;
393 virtual bool hidden() const = 0;