-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.h
152 lines (123 loc) · 3.7 KB
/
common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#ifndef COMMON_H
#define COMMON_H
#include <QtCore/QVector>
#include <QtCore/QSize>
#include <QtCore/QHash>
#define VERSION_STRING "QGoban:0.1"
#define VERSION 0.1
enum Color { cVoid = 0x0, cBlack = 0x1, cWhite = 0x2, cBoth = 0x3};
enum Markup { mVoid = 0x0,
mCircle,
mCross,
mSquare,
mTriangle,
mSelection
};
enum LineStyle {
lsLine = 0x1,
lsArrow
};
// position at board
class Point {
public:
qint8 col;
qint8 row;
inline const Point operator+(const Point p)const { return Point(col+p.col, row+p.row); }
inline const Point operator-(const Point p)const { return Point(col-p.col, row-p.row); }
inline bool operator ==(const Point second)const { return second.col == col && second.row == row; }
inline bool isNull()const { return *this == null(); }
inline bool isPass()const { return *this == pass(); }
static inline Point null() { return Point(-2, -2); };
static inline Point pass() { return Point(-1, -1); };
inline Point(qint8 c, qint8 r) : col(c), row(r) { }
inline Point() : col(-2), row(-2) { }
};
inline uint qHash(Point p) { return qHash(QPair<qint8,qint8>(p.col, p.row)); }
// color and position
class Stone {
public:
Color color;
Point point;
inline bool operator ==(const Stone& s)const { return color == s.color && point == s.point; }
inline bool isNull() { return *this == null(); }
static inline Stone null() { return Stone(); }
inline Stone() : color(cVoid), point() { }
inline Stone(Color c, Point p) : color(c), point(p) { }
};
inline uint qHash(const Stone &p) { return qHash(QPair<Color,Point>(p.color, p.point)); }
// labels on board
class Mark {
public:
Markup mark;
Point pos;
inline bool operator == (const Mark& mrk)const { return mark==mrk.mark && pos==mrk.pos; }
inline Mark(): mark(), pos() {}
inline Mark(Markup mrk, Point ps) : mark(mrk), pos(ps) { }
};
// labels on board
class Label {
public:
QString text;
Point pos;
inline bool operator == (const Label& lbl)const { return text==lbl.text && pos==lbl.pos; }
inline Label(): text(), pos() {}
inline Label(QString txt, Point ps) : text(txt), pos(ps) { }
};
inline uint qHash(const Label &p) { return qHash(QPair<QString,Point>(p.text, p.pos)); }
// lines of markup
class Line {
public:
Point from;
Point to;
LineStyle style;
inline bool operator == (const Line& ln)const { return from==ln.from && to==ln.to && style==ln.style; }
inline Line() : from(), to(), style(lsLine) {}
inline Line(Point f, Point t, LineStyle ls) : from(f), to(t), style(ls) {}
};
inline uint qHash(const Line &p) { return qHash( QPair< QPair<Point,Point>, uint >(QPair<Point,Point>(p.from, p.to), uint(p.style)) ); }
template <typename T>
void resizeMatrix(QVector< QVector<T> > &v, QSize newSize, const T& defaultValue)
{
QSize oldSize(v.size(),
v.size()>0 ? v[0].size() : 0);
v.resize(newSize.height());
for (int i=0; i < std::min(oldSize.height(), newSize.height()); ++i)
{
v[i].resize(newSize.width());
for (int j=oldSize.width(); j<newSize.width(); ++j)
v[i][j] = defaultValue;
}
for (int i=oldSize.height(); i<newSize.height(); ++i)
v[i] = QVector <T> (newSize.width(), defaultValue);
}
inline Color invertColor(Color c)
{
return c == cBlack ? cWhite :
( c == cWhite ? cBlack : cVoid );
}
template <class T>
void removeFromVector(QVector <T> &v, const T &val)
{
typename QVector<T>::iterator it;
it = qFind(v.begin(), v.end(), val);
if (it != v.end())
{
std::swap(*it, v.last());
v.pop_back();
}
}
template <typename T>
bool insert(QVector<T>& v, const T& val)
{
if (v.contains(val))
return false;
else
{
v.push_back(val);
return true;
}
}
QVector <Point> getUpDownLeftRight();
extern const QHash <Markup, QString> markupNames;
extern const QHash <QString, Markup> namesMarkup;
#endif // COMMON_H