~ubuntu-branches/ubuntu/maverick/qgo/maverick

« back to all changes in this revision

Viewing changes to src/move.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin A. Godisch
  • Date: 2005-01-01 23:07:10 UTC
  • Revision ID: james.westby@ubuntu.com-20050101230710-fhng6yidm47xlb2i
Tags: upstream-1.0.0-r2
ImportĀ upstreamĀ versionĀ 1.0.0-r2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* move.cpp
 
3
*/
 
4
 
 
5
#include "move.h"
 
6
#include "matrix.h"
 
7
 
 
8
Move::Move(int board_size)
 
9
{
 
10
        brother = NULL;
 
11
        son = NULL;
 
12
        parent = NULL;
 
13
        marker = NULL;
 
14
        stoneColor = stoneNone;
 
15
        x = y = -1;
 
16
        gameMode = modeNormal;
 
17
        moveNum = 0;
 
18
        terrMarked = false;
 
19
        capturesBlack = capturesWhite = 0;
 
20
        checked = true;
 
21
        fastLoadMarkDict = NULL;
 
22
        scored = false;
 
23
        scoreWhite = scoreBlack = 0;
 
24
        PLinfo = false;
 
25
        
 
26
        matrix = new Matrix(board_size);
 
27
}
 
28
 
 
29
Move::Move(StoneColor c, int mx, int my, int n, GameMode mode, const Matrix &mat, const QString &s)
 
30
: stoneColor(c), x(mx), y(my), moveNum(n), gameMode(mode), comment(s)
 
31
{
 
32
        brother = NULL;
 
33
        son = NULL;
 
34
        parent = NULL;
 
35
        marker = NULL;
 
36
        capturesBlack = capturesWhite = 0;
 
37
        terrMarked = false;
 
38
        checked = true;
 
39
        fastLoadMarkDict = NULL;
 
40
        scored = false;
 
41
        scoreWhite = scoreBlack = 0;
 
42
        PLinfo = false;
 
43
        
 
44
        matrix = new Matrix(mat);
 
45
        // Make all matrix values positive
 
46
        matrix->absMatrix();
 
47
}
 
48
 
 
49
Move::Move(StoneColor c, int mx, int my, int n, GameMode mode, const QString &s)
 
50
: stoneColor(c), x(mx), y(my), moveNum(n), gameMode(mode), comment(s)
 
51
{
 
52
        brother = NULL;
 
53
        son = NULL;
 
54
        parent = NULL;
 
55
        marker = NULL;
 
56
        capturesBlack = capturesWhite = 0;
 
57
        terrMarked = false;
 
58
        checked = false;
 
59
        matrix = NULL;
 
60
        fastLoadMarkDict = NULL;
 
61
        scored = false;
 
62
        scoreWhite = scoreBlack = 0;
 
63
        PLinfo = false;
 
64
}
 
65
 
 
66
Move::~Move()
 
67
{
 
68
        delete matrix;
 
69
        delete fastLoadMarkDict;
 
70
}
 
71
 
 
72
// We do not overwrite the operator == as well, as this is used to compare the
 
73
// pointers for faster operation.
 
74
bool Move::equals(Move *m)
 
75
{
 
76
        if (m == NULL)
 
77
                return false;
 
78
        
 
79
        if (x == m->getX() && y == m->getY() &&
 
80
                stoneColor == m->getColor() &&
 
81
                moveNum == m->getMoveNumber() &&
 
82
                gameMode == m->getGameMode())
 
83
                return true;
 
84
        
 
85
        return false;
 
86
}
 
87
 
 
88
const QString Move::saveMove(bool isRoot)
 
89
{
 
90
        QString str;
 
91
        
 
92
        if (!isRoot)
 
93
                str += ";"; //"\n;";
 
94
        
 
95
        if (x != -1 && y != -1 && gameMode != modeEdit)
 
96
        {
 
97
                // Write something like 'B[aa]'
 
98
                str += stoneColor == stoneBlack ? "B" : "W";
 
99
                str += "[" + Matrix::coordsToString(x-1, y-1) + "]";
 
100
        }
 
101
        
 
102
        // Save edited moves
 
103
        str += matrix->saveEditedMoves(parent != NULL ? parent->getMatrix() : 0);
 
104
        
 
105
        // Save marks
 
106
        str += matrix->saveMarks();
 
107
        
 
108
        // Add nodename, if we have one
 
109
        if (!nodeName.isNull() && !nodeName.isEmpty())
 
110
        {
 
111
                // simpletext
 
112
                str += "N[";
 
113
                str += nodeName;
 
114
                str += "]";
 
115
        }
 
116
 
 
117
        // Add next move's color
 
118
        if (PLinfo)
 
119
        {
 
120
                if (PLnextMove == stoneBlack)
 
121
                        str += "PL[B]";
 
122
                else
 
123
                        str += "PL[W]";
 
124
        }
 
125
 
 
126
        // Add comment, if we have one
 
127
        if (!comment.isNull() && !comment.isEmpty())
 
128
        {
 
129
                // text
 
130
                QString tmp = comment;
 
131
                int pos = 0;
 
132
                while ((pos = tmp.find("]", pos)) != -1 && static_cast<unsigned int>(pos) < tmp.length())
 
133
                {
 
134
                        tmp.replace(pos, 1, "\\]");
 
135
                        pos += 2;
 
136
                }
 
137
                str += "C[";
 
138
                str += tmp;
 
139
                str += "]";
 
140
        }
 
141
 
 
142
        // time info
 
143
        if (timeinfo && !isRoot && (int) timeLeft)
 
144
        {
 
145
                if (stoneColor == stoneBlack)
 
146
                        str += "BL[";
 
147
                else
 
148
                        str += "WL[";
 
149
                str += QString::number(timeLeft);
 
150
                str += "]";
 
151
 
 
152
                // open moves info
 
153
                if (openMoves > 0)
 
154
                {
 
155
                        if (stoneColor == stoneBlack)
 
156
                                str += "OB[";
 
157
                        else
 
158
                                str += "OW[";
 
159
                        str += QString::number(openMoves);
 
160
                        str += "]";
 
161
                }
 
162
        }
 
163
 
 
164
        // Add unknown properties, if we have some
 
165
        if (!unknownProperty.isNull() && !unknownProperty.isEmpty())
 
166
        {
 
167
                // complete property
 
168
                str += unknownProperty;
 
169
        }
 
170
        
 
171
        return str;
 
172
}
 
173
 
 
174
void Move::insertFastLoadMark(int x, int y, MarkType markType, const QString &txt)
 
175
{
 
176
        if (fastLoadMarkDict == NULL)
 
177
        {
 
178
                fastLoadMarkDict = new QIntDict<FastLoadMark>;
 
179
                fastLoadMarkDict->setAutoDelete(TRUE);
 
180
        }
 
181
        
 
182
        FastLoadMark *flm = new FastLoadMark;
 
183
        flm->x = x;
 
184
        flm->y = y;
 
185
        flm->t = markType;
 
186
        flm->txt = txt;
 
187
        
 
188
        fastLoadMarkDict->insert(Matrix::coordsToKey(x, y), flm);
 
189
}
 
190
 
 
191
bool Move::isPassMove()
 
192
{
 
193
  if ((x==20) && (y==20))
 
194
    return true;
 
195
 
 
196
  return false;
 
197
}