Rough PGN save support.
[tagua/yd.git] / src / index.h
blob4afa09507def8047584e808cf2329f00c3a9dea4
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 INDEX_H
12 #define INDEX_H
14 #include <QVector>
15 #include <QString>
16 #include "common.h"
17 #include <map>
19 /**
20 \class Index index.h <index.h>
21 \brief An index in a multi-variations game.
23 This is a class that you can use to refer to a position in a game.
25 class Index {
26 public:
27 class Ref {
28 public:
29 Ref() : variation(-1), num_moves(-1) {}
30 Ref(int v, int n) : variation(v), num_moves(n) {}
31 int variation;
32 int num_moves;
34 bool operator==(const Ref& r) const {
35 return r.variation == variation
36 && r.num_moves == num_moves;
38 bool operator!=(const Ref& r) const { return !((*this)==r); }
41 int num_moves;
42 QVector<Ref> nested;
44 /** Constructor, you can contruct it from an integer that
45 is the number of moves played in the main line */
46 Index(int n = -1) : num_moves(n) {}
48 /** return a string like "1_2.3_4.5" */
49 operator QString() const;
51 /** returns the number of moves from the start of the game */
52 int totalNumMoves() const;
54 /** \return true if this index is the first of a variation branch */
55 bool atVariationStart() const;
57 /** flip variation: returns the current index changed as if it was in the subvariation \a vid
58 at \a vstart instead of mainline and viceversa, ie at \a vstart the mainline becames
59 subvariation \a vid and viceversa */
60 Index flipVariation(const Index& vstart, int v_id) const;
62 /** Returns an index the point to the next position. If variation is != -1, you will be entering in the
63 sub-variation with this id instead of continuing in the current main line. You can also specify
64 the number of moves you want to go on (in the main line or in the specified variation) */
65 Index next(int variation_id = -1, int num = 1) const;
67 /** Returns an index pointing to the previous position (or to a position n moves back) */
68 Index prev(int _num = 1) const;
70 /** Returns an index that is the minimum of this index and the given one, ie the branch point
71 of the lines from start to the two indices. */
72 Index min(const Index& ix) const;
74 /** Returns the number of steps down and up you have to do to go from this index to the given one */
75 std::pair<int, int> stepsTo(const Index& ix) const;
77 /** returns the number of moves in the most nested variation */
78 int lastIndex();
80 /** True if this index refers to a position 'before' than the given one */
81 bool operator<(const Index& ix) const {
82 std::pair<int,int> s = stepsTo(ix);
83 return s.first == 0 && s.second>0;
86 /** True if this index refers to a position 'before or equal' than the given one */
87 bool operator<=(const Index& ix) const {
88 std::pair<int,int> s = stepsTo(ix);
89 return s.first == 0;
92 /** True if this index refers to a position 'after' than the given one */
93 bool operator>(const Index& ix) const {
94 return ix < *this;
97 /** True if this index refers to a position 'after or equal' than the given one */
98 bool operator>=(const Index& ix) const {
99 return ix <= *this;
102 /** True if this index refers to the same position of the given one */
103 bool operator==(const Index& ix) const {
104 std::pair<int,int> s = stepsTo(ix);
105 return s.first == 0 && s.second == 0;
108 /** True if this index refers to a different position of the given one */
109 bool operator!=(const Index& ix) const {
110 return !(*this == ix);
113 /** \return an index from a string like "1_2.3_4.5" */
114 static Index fromString(const QString& s);
116 /** \return whether this index points to the mainline */
117 bool mainLine() const;
120 #endif //INDEX_H