1 /********************************************************************
3 * Copyright (C) 2008 Daniele Battaglia
5 * This file is part of GoMoku3D.
7 * GoMoku3D is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * GoMoku3D is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GoMoku3D. If not, see <http://www.gnu.org/licenses/>.
20 *******************************************************************/
23 #include "GameMatrix.h"
25 const int GameMatrix::EmptyPoint
= -1;
27 GameMatrix
*GameMatrix::_instance
= 0;
29 GameMatrix::GameMatrix(int d1
, int d2
, int numPlayers
)
34 for (int i
= 0; i
< numPlayers
; i
++) {
35 _lastRound
.append(Point());
38 int matrixSize
= (d1
* d2
) * (d1
* d2
) * (d1
* d2
);
39 _matrix
= new int[matrixSize
];
40 for (int i
= 0; i
< matrixSize
; i
++) {
41 _matrix
[i
] = EmptyPoint
;
44 _freeCounter
= matrixSize
;
47 GameMatrix::~GameMatrix()
53 GameMatrix
*GameMatrix::create(int d1
, int d2
, int numPlayers
)
56 _instance
= new GameMatrix(d1
, d2
, numPlayers
);
61 void GameMatrix::destroy()
70 GameMatrix
*GameMatrix::instance()
75 bool GameMatrix::check(Point point
)
77 return point
.isValid() && instance()->elementAt(point
) == EmptyPoint
;
80 bool GameMatrix::add(Move move
)
82 int pos
= move
.point().x() * (_d1
* _d2
) * (_d1
* _d2
) + move
.point().y() * (_d1
* _d2
) + move
.point().z();
83 _matrix
[pos
] = move
.playerId();
85 int start
= qMax(0, move
.point().x());
89 while (valid
&& start
<= move
.point().x()) {
90 while (valid
&& i
< _d1
) {
91 int id
= elementAt(start
+ i
, move
.point().y(), move
.point().z());
92 if (id
== move
.playerId()) {
110 start
= qMax(0, move
.point().y());
114 while (valid
&& start
<= move
.point().y()) {
115 while (valid
&& i
< _d1
) {
116 int id
= elementAt(move
.point().x(), start
+ i
, move
.point().z());
117 if (id
== move
.playerId()) {
135 start
= qMax(0, move
.point().z());
139 while (valid
&& start
<= move
.point().z()) {
140 while (valid
&& i
< _d1
) {
141 int id
= elementAt(move
.point().x(), move
.point().y(), start
+ i
);
142 if (id
== move
.playerId()) {
165 extractCC(move
.point(), move
.playerId(), set
);
168 int minX
= _d1
* _d2
;
176 minX
= qMin(minX
, e
.x());
177 minY
= qMin(minY
, e
.y());
178 minZ
= qMin(minZ
, e
.z());
179 maxX
= qMax(maxX
, e
.x());
180 maxY
= qMax(maxY
, e
.y());
181 maxZ
= qMax(maxZ
, e
.z());
183 return (minX
== 0 && maxX
== _d1
* _d2
-1) || (minY
== 0 && maxY
== _d1
* _d2
-1) || (minZ
== 0 && maxZ
== _d1
* _d2
-1);
186 void GameMatrix::clear(Point point
)
188 if (elementAt(point
) != EmptyPoint
) {
190 int pos
= point
.x() * (_d1
* _d2
) * (_d1
* _d2
) + point
.y() * (_d1
* _d2
) + point
.z();
191 _matrix
[pos
] = EmptyPoint
;
196 int GameMatrix::elementAt(int x
, int y
, int z
) const
198 if (Point(x
, y
, z
).isValid()) {
199 int pos
= x
* (_d1
* _d2
) * (_d1
* _d2
) + y
* (_d1
* _d2
) + z
;
205 int GameMatrix::elementAt(Point point
) const
207 return elementAt(point
.x(), point
.y(), point
.z());
210 bool GameMatrix::isFull() const
212 return _freeCounter
== 0;
215 int GameMatrix::numberOfPlayers() const
217 return _lastRound
.size();
220 void GameMatrix::setLastRound(QList
<Point
> round
)
226 void GameMatrix::extractCC(Point p
, int id
, QSet
<Point
> &set
)
230 QList
<Point
> adjacent
;
231 adjacent
.append(Point(p
.x() - 1, p
.y(), p
.z()));
232 adjacent
.append(Point(p
.x() + 1, p
.y(), p
.z()));
233 adjacent
.append(Point(p
.x(), p
.y() - 1, p
.z()));
234 adjacent
.append(Point(p
.x(), p
.y() + 1, p
.z()));
235 adjacent
.append(Point(p
.x(), p
.y(), p
.z() - 1));
236 adjacent
.append(Point(p
.x(), p
.y(), p
.z() + 1));
240 foreach (e
, adjacent
) {
241 if (id
== elementAt(e
) && set
.find(e
) == set
.end()) {
242 extractCC(e
, id
, set
);
248 int GameMatrix::d1() const
253 int GameMatrix::d2() const