Removed AlgebraicNotation from the variant API.
[tagua/yd.git] / src / index.h
blob5a3b4b17b4b0e6f86d32c412ecb2e249dd5579da
1 /*
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.
9 */
11 #ifndef INDEX_H
12 #define INDEX_H
14 #include <QVector>
15 #include <QString>
16 #include "common.h"
18 /**
19 \class Index index.h <index.h>
20 \brief An index in a multi-variations game.
22 This is a class that you can use to refer to a position in a game.
24 class Index {
25 public:
26 class Ref {
27 public:
28 Ref() : variation(-1), num_moves(-1) {}
29 Ref(int v, int n) : variation(v), num_moves(n) {}
30 int variation;
31 int num_moves;
33 bool operator==(const Ref& r) const {
34 return r.variation == variation
35 && r.num_moves == num_moves;
37 bool operator!=(const Ref& r) const { return !((*this)==r); }
40 int num_moves;
41 QVector<Ref> nested;
43 /** Constructor, you can contruct it from an integer that
44 is the number of moves played in the main line */
45 Index(int n = -1) : num_moves(n) {}
47 /** return a string like "1_2.3_4.5" */
48 operator QString() const;
50 /** returns the number of moves from the start of the game */
51 int totalNumMoves() const;
53 /** \return true if this index is the first of a variation branch */
54 bool atVariationStart() const;
56 /** flip variation: returns the current index changed as if it was in the subvariation \a vid
57 at \a vstart instead of mainline and viceversa, ie at \a vstart the mainline becames
58 subvariation \a vid and viceversa */
59 Index flipVariation(const Index& vstart, int v_id) const;
61 /** Returns an index the point to the next position. If variation is != -1, you will be entering in the
62 sub-variation with this id instead of continuing in the current main line. You can also specify
63 the number of moves you want to go on (in the main line or in the specified variation) */
64 Index next(int variation_id = -1, int num = 1) const;
66 /** Returns an index pointing to the previous position (or to a position n moves back) */
67 Index prev(int _num = 1) const;
69 /** Returns an index that is the minimum of this index and the given one, ie the branch point
70 of the lines from start to the two indices. */
71 Index min(const Index& ix) const;
73 /** Returns the number of steps down and up you have to do to go from this index to the given one */
74 std::pair<int, int> stepsTo(const Index& ix) const;
76 /** returns the number of moves in the most nested variation */
77 int lastIndex();
79 /** True if this index refers to a position 'before' than the given one */
80 bool operator<(const Index& ix) const {
81 std::pair<int,int> s = stepsTo(ix);
82 return s.first == 0 && s.second>0;
85 /** True if this index refers to a position 'before or equal' than the given one */
86 bool operator<=(const Index& ix) const {
87 std::pair<int,int> s = stepsTo(ix);
88 return s.first == 0;
91 /** True if this index refers to a position 'after' than the given one */
92 bool operator>(const Index& ix) const {
93 return ix < *this;
96 /** True if this index refers to a position 'after or equal' than the given one */
97 bool operator>=(const Index& ix) const {
98 return ix <= *this;
101 /** True if this index refers to the same position of the given one */
102 bool operator==(const Index& ix) const {
103 std::pair<int,int> s = stepsTo(ix);
104 return s.first == 0 && s.second == 0;
107 /** True if this index refers to a different position of the given one */
108 bool operator!=(const Index& ix) const {
109 return !(*this == ix);
112 /** \return an index from a string like "1_2.3_4.5" */
113 static Index fromString(const QString& s);
115 /** \return whether this index points to the mainline */
116 bool mainLine() const;
119 #endif //INDEX_H