1 /****************************************************************************
2
3 Copyright (C) 2002-2014 Gilles Debunne. All rights reserved.
4
5 This file is part of the QGLViewer library version 2.7.2.
6
7 http://www.libqglviewer.com - contact@libqglviewer.com
8
9 This file may be used under the terms of the GNU General Public License
10 versions 2.0 or 3.0 as published by the Free Software Foundation and
11 appearing in the LICENSE file included in the packaging of this file.
12 In addition, as a special exception, Gilles Debunne gives you certain
13 additional rights, described in the file GPL_EXCEPTION in this package.
14
15 libQGLViewer uses dual licensing. Commercial/proprietary software must
16 purchase a libQGLViewer Commercial License.
17
18 This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
19 WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20
21 *****************************************************************************/
22
23 #include "move.h"
24 #include "board.h"
25 #include "qregexp.h"
26 #include "qstringlist.h"
27
28 #include "QGLViewer/keyFrameInterpolator.h"
29 using namespace qglviewer;
30
Move(const QPoint & s,const QPoint & e)31 Move::Move(const QPoint &s, const QPoint &e) : start_(s), end_(e) {}
32
Move(const Board * const b,int s,int e)33 Move::Move(const Board *const b, int s, int e) {
34 start_ = b->pointFromInt(s);
35 end_ = b->pointFromInt(e);
36 }
37
Move(const QString text)38 Move::Move(const QString text) {
39 #if QT_VERSION < 0x040000
40 QStringList list = QStringList::split(QRegExp("\\D"), text);
41 #else
42 QStringList list = text.split(QRegExp("\\D"), QString::SkipEmptyParts);
43 #endif
44
45 start_ = QPoint(list[0].toInt(), list[1].toInt());
46 end_ = QPoint(list[2].toInt(), list[3].toInt());
47 }
48
isValid(const Board * const b) const49 bool Move::isValid(const Board *const b) const {
50 return (b->isValid(start()) && b->isValid(end()) &&
51 abs(start().x() - end().x()) <= 2 &&
52 abs(start().y() - end().y()) <= 2 && start() != end() &&
53 b->stateOf(start()) == Board::blueColor(b->bluePlays()) &&
54 b->stateOf(end()) == Board::EMPTY);
55 }
56
isClose() const57 bool Move::isClose() const {
58 QPoint delta = start() - end();
59 return (abs(delta.x()) < 2) && (abs(delta.y()) < 2);
60 }
61
numberOfNewPieces(const Board & b) const62 int Move::numberOfNewPieces(const Board &b) const {
63 int res = 0;
64
65 for (int i = -1; i <= 1; ++i)
66 for (int j = -1; j <= 1; ++j) {
67 const QPoint p(end().x() + i, end().y() + j);
68 if (b.isValid(p) && b.stateOf(p) == Board::blueColor(!b.bluePlays()))
69 res++;
70 }
71
72 if (isClose())
73 res++;
74
75 return res;
76 }
77
operator <<(std::ostream & out,const Move & m)78 std::ostream &operator<<(std::ostream &out, const Move &m) {
79 out << "(" << m.start().x() << "," << m.start().y() << ") -> (" << m.end().x()
80 << "," << m.end().y() << ")" << std::endl;
81 return out;
82 }
83