2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
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.
16 * @brief Low level abstract classes used by the interface framework.
18 * This file includes definitions for abstract classes used from within
19 * the interface framework to access variant details using dynamic time
22 * One of the big problems with this approach is what I call 'lack of
23 * functional dependency support' for runtime polymorphism.
24 * What is missing is the ability to override a virtual member function
25 * in a derived class changing its signature. Or better, it is possible
26 * to only change its return type with a covariant type, while it would
27 * often be useful to change any of its argument types with covariant or
28 * even controvariant types.
29 * For controvariant types, I foresee no problems, and all type checking
30 * can be done statically (of course the method has to be dynamically
31 * bound) as they are now.
32 * When using covariant types, the compiler could insert appropriate
33 * dynamic cast's and type checks in the calling code, when there's no
34 * way to know the exact type of an argument at compile time.
36 * The file @ref highlevel.h tries to address this problem doing part
37 * of the compiler's work. One exception is that type checking code
38 * is inserted in a method wrapper, instead of the calling place, so
39 * even calling a method of a noncasted instance would cause some
40 * upcast / downcast overhead; this is negligible, though, because
41 * that scenario is rather unlikely, since wrapped classes are always
42 * kept in variables of abstract types.
46 #include <boost/shared_ptr.hpp>
48 #include <QStringList>
53 #include "decoratedmove.h"
54 #include "kboard_fwd.h"
57 * @brief A superclass for all the piece classes.
61 typedef boost::shared_ptr
<AbstractPiece
> Ptr
;
62 virtual ~AbstractPiece() { }
65 * Piece equality. Return false if the second pointer
68 virtual bool equals(AbstractPiece::Ptr other
) const = 0;
71 * Piece compare. Return true if less than other.
73 virtual bool less(AbstractPiece::Ptr other
) const = 0;
76 * Return the piece type id. It should be a nonnegative
77 * number, or -1 if the type concept for this piece
80 virtual int type() const = 0;
83 * Return the piece color id. It should be a nonnegative
84 * number, or -1 if the color concept for this piece
87 virtual int color() const = 0;
90 * Return a unique id for the piece
91 * (type, color) pair. Used to store
94 // virtual int id() const = 0;
97 * Return a unique key for the piece.
98 * Used to store pixmaps.
100 virtual QString
name() const = 0;
103 * Return the symbol used to identify the piece
106 // virtual QString typeSymbol() const = 0;
109 * Return the name used to load the
113 // virtual QString name() const = 0;
116 * Create a deep copy of the piece.
118 virtual AbstractPiece::Ptr
clone() const = 0;
122 * @brief A class to compare two pieces.
124 class AbstractPieceComparer
{
126 bool operator()(AbstractPiece::Ptr x
, AbstractPiece::Ptr y
) {
132 * @brief A superclass for all the move classes.
134 * An abstract move is simply something that can serialize
135 * itself either as SAN notation, or as coordinate notation.
139 typedef boost::shared_ptr
<AbstractMove
> Ptr
;
140 virtual ~AbstractMove() { }
143 * Return a compact SAN representation for the move.
145 virtual QString
SAN(boost::shared_ptr
<AbstractPosition
> ref
) const = 0;
148 * Return a decorated representation for the move.
150 virtual DecoratedMove
toDecoratedMove(boost::shared_ptr
<AbstractPosition
> ref
) const = 0;
153 * Return the move in coordinate notation.
155 virtual QString
toString(boost::shared_ptr
<AbstractPosition
> ref
) const = 0;
158 * Convert the move to a normal user move. Used to
159 * perform move highlighting.
161 virtual NormalUserMove
toUserMove() const = 0;
163 virtual bool equals(Ptr other
) const = 0;
168 * @brief A superclass for all the position classes.
170 * A general interface for positions. It is used from within
171 * the graphical framework to validate external input wrt
172 * the played variant.
174 class AbstractPosition
{
176 typedef boost::shared_ptr
<AbstractPosition
> Ptr
;
177 typedef std::map
<AbstractPiece::Ptr
, int, AbstractPieceComparer
> AbstractPool
;
178 typedef boost::shared_ptr
<AbstractPool
> PoolPtr
;
179 virtual ~AbstractPosition() { }
182 * Return board size in logical units. This could be
183 * static if C++ permitted virtual static functions.
185 virtual Point
size() const = 0;
188 * Returns the text to be used for the border surrounding this position in a board
190 virtual QStringList
borderCoords() const = 0;
193 * Create the starting piece setup.
195 virtual void setup() = 0;
198 * Retrieve the pieces in the piece pool.
200 virtual PoolPtr
pool() const = 0;
203 * Add a piece to the pool n times.
205 virtual void addToPool(AbstractPiece::Ptr piece
, int n
) = 0;
208 * Remove a piece from the pool n times.
210 virtual void removeFromPool(AbstractPiece::Ptr piece
, int n
) = 0;
213 * Copies the pool of a position.
215 virtual void copyPoolFrom(AbstractPosition::Ptr pos
) = 0;
218 * Sets the pool from a PoolPtr.
220 virtual void setPool(PoolPtr pool
) = 0;
223 * Retrieve the piece on square @a p.
224 * Return a null pointer if that square is empty.
226 virtual AbstractPiece::Ptr
get(const Point
& p
) const = 0;
229 * Set a piece on the board.
231 virtual void set(const Point
& p
, AbstractPiece::Ptr piece
) = 0;
234 * Return an id corresponding to the player
237 virtual int turn() const = 0;
238 virtual void setTurn(int) = 0;
240 virtual int previousTurn() const = 0;
243 * Switch to the next player.
245 virtual void switchTurn() = 0;
248 * Check move legality. Set move fields as needed.
249 * Return whether the move is legal.
250 * This function should return immediately if @a m
251 * has already been tested.
253 virtual bool testMove(AbstractMove::Ptr m
) const = 0;
256 * Execute move @a m. Assume that m is legal and tested.
258 virtual void move(AbstractMove::Ptr m
) = 0;
261 * Create a deep copy of the position.
263 virtual AbstractPosition::Ptr
clone() const = 0;
266 * Tests if two positions are equal.
268 virtual bool equal(AbstractPosition::Ptr p
) const = 0;
271 * Return a move from an algebraic notation, or a null pointer.
273 virtual AbstractMove::Ptr
getMove(const class AlgebraicNotation
&) const = 0;
276 * Return a move from an algebraic notation, or a null pointer.
278 virtual AbstractMove::Ptr
getMove(const QString
&) const = 0;
281 * Return a string representing the current state of the position.
283 virtual QString
state() const = 0;
286 * Return a FEN representation for the position, assuming
287 * @a halfmove as halfmove clock and @a fullmove as full move
290 virtual QString
fen(int halfmove
, int fullmove
) const = 0;
293 * A piece somehow representing or related to the move
294 * which has to be drawn by the interface.
296 * * for one click moves, the piece that would
297 * appear on the square.
298 * * for promotions in chesslike variants, the promoted
300 * @note Return a null pointer if the move has no valid
301 * piece hint defined.
303 virtual AbstractPiece::Ptr
moveHint(AbstractMove::Ptr
) const = 0;
306 * Variant introspection
308 virtual QString
variant() const = 0;
313 virtual void dump() const = 0;
316 class AnimationGroup
;
319 * @brief An abstract superclass for all animator classes.
321 * An animator is a class which is given a difference between
322 * a graphical and a logical position, and schedules an animation
323 * which graphically does an update.
324 * If the difference is due to a move, the animator tries to
325 * create a smooth animation which could possibly work for many
326 * chess-like variants, and then fills in the gaps with a direct
329 class AbstractAnimator
{
331 typedef boost::shared_ptr
<AbstractAnimator
> Ptr
;
332 typedef boost::shared_ptr
<AnimationGroup
> AnimationPtr
;
333 virtual ~AbstractAnimator() { }
336 * Sync the graphical position with the given position.
338 virtual AnimationPtr
warp(AbstractPosition::Ptr
) = 0;
341 * Animate forward syncing to the given position.
343 virtual AnimationPtr
forward(AbstractPosition::Ptr
, AbstractMove::Ptr
) = 0;
346 * Animate back syncing to the given position.
348 virtual AnimationPtr
back(AbstractPosition::Ptr
, AbstractMove::Ptr
) = 0;
351 class PointConverter
;
352 class GraphicalPosition
;
353 namespace PixmapLoader
{ class Info
; }
357 virtual ~VariantInfo() { }
358 virtual AbstractPosition::Ptr
createPosition() = 0;
359 virtual AbstractPosition::Ptr
createCustomPosition(const OptList
& l
) = 0;
360 virtual AbstractPosition::Ptr
createPositionFromFEN(const QString
& fen
) = 0;
361 virtual AbstractPosition::Ptr
createChessboard(int turn
, bool, bool, bool, bool, const Point
&) = 0;
362 virtual AbstractPiece::Ptr
createPiece(int color
, int type
) = 0;
363 virtual void forallPieces(class PieceFunction
&) = 0;
364 virtual int moveListLayout() const = 0;
365 virtual AbstractAnimator::Ptr
createAnimator(PointConverter
* converter
,
366 GraphicalPosition
* position
) = 0;
367 virtual AbstractMove::Ptr
createNormalMove(const NormalUserMove
&) = 0;
368 virtual AbstractMove::Ptr
createDropMove(const DropUserMove
&) = 0;
369 virtual AbstractMove::Ptr
getVerboseMove(int turn
, const class VerboseNotation
&) const = 0;
370 virtual int type(const QString
& x
) = 0;
371 virtual QString
typeSymbol(int type
) = 0;
372 virtual bool simpleMoves() = 0;
373 virtual QString
name() const = 0;
374 virtual QString
themeProxy() const = 0;
375 virtual OptList
positionOptions() const = 0;