File: boardmodel.cpp

package info (click to toggle)
psi-plugins 1.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 4,368 kB
  • sloc: cpp: 42,063; xml: 714; ansic: 84; makefile: 61; sh: 12
file content (254 lines) | stat: -rw-r--r-- 6,112 bytes parent folder | download
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 * boardmodel.cpp - Gomoku Game plugin
 * Copyright (C) 2011  Aleksey Andreev
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * You can also redistribute and/or modify this program under the
 * terms of the Psi License, specified in the accompanied COPYING
 * file, as published by the Psi Project; either dated January 1st,
 * 2005, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

#include "boardmodel.h"
#include "common.h"

using namespace GomokuGame;

BoardModel::BoardModel(QObject *parent) :
	QAbstractTableModel(parent),
	selectX(-1),
	selectY(-1),
	columnCount_(0),
	rowCount_(0),
	gameModel(NULL)
{
}

BoardModel::~BoardModel()
{
	if (gameModel)
		delete gameModel;
}

void BoardModel::init(GameModel *gm)
{
	if (gameModel)
		delete gameModel;
	gameModel = gm;
	selectX = -1;
	selectY = -1;
	setHeaders();
	beginResetModel();
	endResetModel();
	connect(gameModel, SIGNAL(statusUpdated(GameModel::GameStatus)), this, SIGNAL(changeGameStatus(GameModel::GameStatus)));
	emit changeGameStatus(gm->gameStatus());
}

void BoardModel::setHeaders()
{
	rowCount_ = gameModel->boardSizeY() + 4;
	columnCount_ = gameModel->boardSizeX() + 4;
}

Qt::ItemFlags BoardModel::flags(const QModelIndex &index) const
{
	Qt::ItemFlags flags = Qt::NoItemFlags | Qt::ItemIsEnabled;
	int row = index.row();
	if (row < 2 || row > rowCount_ - 2) {
		return flags;
	}
	int col = index.column();
	if (col < 2 || col > columnCount_ - 2) {
		return flags;
	}
	return (flags |  Qt::ItemIsSelectable);
}

QVariant BoardModel::headerData(int /*section*/, Qt::Orientation /*orientation*/, int /*role*/) const
{
	return QVariant();
}

QVariant BoardModel::data(const QModelIndex & /*index*/, int /*role*/) const
{
	return QVariant();
}

bool BoardModel::setData(const QModelIndex &index, const QVariant &/*value*/, int role)
{
	if (index.isValid() && role == Qt::DisplayRole) {
		emit dataChanged(index, index);
		return true;
	}
	return false;
}

int BoardModel::rowCount(const QModelIndex & /*parent*/) const
{
	return rowCount_;
}

int BoardModel::columnCount(const QModelIndex & /*parent*/) const
{
	return columnCount_;
}

const GameElement *BoardModel::getGameElement(int x, int y)
{
	return gameModel->getElement(x - 2, y - 2);
}

/**
 * Был щелчек мышью по доске
 */
bool BoardModel::clickToBoard(QModelIndex index)
{
	if (index.isValid()) {
		int x = index.column() - 2;
		int y = index.row() - 2;
		if (setElementToBoard(x, y, true)) {
			emit setupElement(x, y);
			return true;
		}
	}
	return false;
}

/**
 * Пришел ход от оппонента
 */
bool BoardModel::opponentTurn(int x, int y)
{
	if (setElementToBoard(x, y, false)) {
		// После хода оппонента в модели производится проверка на проигрыш и ничью.
		GameModel::GameStatus status = gameModel->gameStatus();
		if (status == GameModel::StatusLose)
			emit lose(); // Сообщаем оппоненту что мы проиграли
		else if (status == GameModel::StatusDraw)
			emit draw(); // Сообщаем оппоненту об ничьей
		return true;
	}
	gameModel->setErrorStatus();
	return false;
}

void BoardModel::setAccept()
{
	gameModel->accept();
}

void BoardModel::setError()
{
	gameModel->setErrorStatus();
}

void BoardModel::setClose()
{
	gameModel->breakGame();
}

void BoardModel::setWin()
{
	gameModel->setWin();
}

void BoardModel::opponentDraw()
{
	gameModel->setDraw();
}

/**
 * Мы сдались. Сообщаем оппоненту и самому себе.
 */
void BoardModel::setResign()
{
	emit lose();
	gameModel->setLose();
}

/**
 * Добавляет новый элемент (в данном случае камень) в игровую модель и обновляет игровую доску
 * Возвращает true в случае удачи.
 */
bool BoardModel::setElementToBoard(int x, int y, bool local)
{
	if (gameModel->doTurn(x, y, local)) {
		const QModelIndex mi = index(y + 2, x + 2); // Offset for margin
		emit dataChanged(mi, mi);
		return true;
	}
	const QString msg = gameModel->getLastError();
	if (!msg.isEmpty())
		emit doPopup(msg);
	return false;
}

/**
 * Возвращает номер  ожидаемого хода
 */
int BoardModel::turnNum()
{
	return (gameModel->turnsCount() + 1);
}

/**
 * Переключает цвет игрока (предусмотрено международными правилами)
 */
bool BoardModel::doSwitchColor(bool local)
{
	if (gameModel->doSwitchColor(local)) {
		if (local)
			emit switchColor();
		return true;
	}
	return false;
}

/**
 * Сохраняет состояние игры в строку
 */
QString BoardModel::saveToString() const
{
	return gameModel->toString();
}

/**
 * Устанавливает новые координаты подсвечиваемого объекта и дает команды на перисовку
 */
void BoardModel::setSelect(int x, int y)
{
	int old_x = selectX;
	int old_y = selectY;
	selectX = x + 2;
	selectY = y + 2;
	if (old_x != selectX || old_y != selectY) {
		if (old_x != -1 && old_y != -1) {
			QModelIndex idx = index(old_y, old_x);
			emit dataChanged(idx, idx);
		}
		QModelIndex idx = index(selectY, selectX);
		emit dataChanged(idx, idx);
	}
}

GameElement::ElementType BoardModel::myElementType() const
{
	if (gameModel) {
		return gameModel->myElementType();
	}
	return GameElement::TypeNone;
}