Starting to make things work again: dragging
[tagua.git] / src / index.h
blob5b76132766a0b3c9fe9ca1600a184b8a577fb108
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 <vector>
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(int v, int n) : variation(v), num_moves(n) {}
29 int variation;
30 int num_moves;
32 bool operator==(const Ref& r) const {
33 return r.variation == variation
34 && r.num_moves == num_moves;
36 bool operator!=(const Ref& r) const { return !((*this)==r); }
39 int num_moves;
40 std::vector<Ref> nested;
42 /** Constructor, you can contruct it from an integer that
43 is the number of moves played in the main line */
44 Index(int n) : num_moves(n) {}
46 operator QString() const;
47 int totalNumMoves() const;
48 bool atVariationStart() const;
49 Index flipVariation(const Index& vstart, int v_id) const;
50 Index next(int variation_id = -1, int num = 1) const;
51 Index prev(int _num = 1) const;
52 Index min(const Index& ix) const;
53 std::pair<int, int> stepsTo(const Index& ix) const;
54 int lastIndex();
56 /** True if this index refers to a position 'before' than the given one */
57 bool operator<(const Index& ix) const {
58 std::pair<int,int> s = stepsTo(ix);
59 return s.first == 0 && s.second>0;
62 /** True if this index refers to a position 'before or equal' than the given one */
63 bool operator<=(const Index& ix) const {
64 std::pair<int,int> s = stepsTo(ix);
65 return s.first == 0;
68 /** True if this index refers to a position 'after' than the given one */
69 bool operator>(const Index& ix) const {
70 return ix < *this;
73 /** True if this index refers to a position 'after or equal' than the given one */
74 bool operator>=(const Index& ix) const {
75 return ix <= *this;
78 /** True if this index refers to the same position of the given one */
79 bool operator==(const Index& ix) const {
80 std::pair<int,int> s = stepsTo(ix);
81 return s.first == 0 && s.second == 0;
84 /** True if this index refers to a different position of the given one */
85 bool operator!=(const Index& ix) const {
86 return !(*this == ix);
89 static Index fromString(const QString& s);
92 #endif //__INDEX_H__