139 lines
4.7 KiB
C++
139 lines
4.7 KiB
C++
/***************************************************************************
|
|
* Copyright (C) 2009 by Stefan Bühler *
|
|
* stbuehler@web.de *
|
|
* *
|
|
* 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. *
|
|
* *
|
|
* 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 program; if not, write to the *
|
|
* Free Software Foundation, Inc., *
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
***************************************************************************/
|
|
#include "testgame.h"
|
|
|
|
#include <QGridLayout>
|
|
#include <QResizeEvent>
|
|
#include <QMouseEvent>
|
|
#include <QPainter>
|
|
#include <QStatusBar>
|
|
|
|
#include "field2d.h"
|
|
#include "field3d.h"
|
|
|
|
#include "ai.h"
|
|
|
|
namespace toruschess {
|
|
|
|
TestPlace::TestPlace(QWidget *parent, const PieceLibrary *lib, const Field *field, const Pos &p)
|
|
: QWidget(parent), m_field(field), m_lib(lib), m_pos(p), m_mark(false) {
|
|
setMinimumSize(m_lib->minPieceSize());
|
|
}
|
|
|
|
QSize TestPlace::sizeHint() const {
|
|
return m_lib->pieceSize();
|
|
}
|
|
|
|
void TestPlace::paintEvent(QPaintEvent *) {
|
|
QPainter pt(this);
|
|
int pl = m_field->place(m_pos);
|
|
if ((m_pos.x() + m_pos.y()) % 2) {
|
|
// #D18B47
|
|
pt.fillRect(rect(), QBrush(QColor(0xD1, 0x8B, 0x47)));
|
|
} else {
|
|
// #FFCE9E
|
|
pt.fillRect(rect(), QBrush(QColor(0xFF, 0xCE, 0x9E)));
|
|
}
|
|
m_lib->paint(pt, pl);
|
|
if (m_mark) {
|
|
int r = qMax(1, (rect().width() + rect().height()) / 36);
|
|
pt.setBrush(pl != 0 ? Qt::green : Qt::white);
|
|
pt.setPen(Qt::red);
|
|
pt.drawEllipse(QPoint(rect().width() / 2, rect().height() / 2), r, r);
|
|
}
|
|
}
|
|
|
|
TestField::TestField(QWidget *parent, PieceLibrary *lib, Game *game) : QWidget(parent), m_lib(lib), m_game(game) {
|
|
QGridLayout *layout = new QGridLayout();
|
|
for (int y = 0; y < 8; y++) {
|
|
for (int x = 0; x < 8; x++) {
|
|
m_places[x][y] = new TestPlace(this, m_lib, m_game->field(), Pos(x, y));
|
|
layout->addWidget(m_places[x][y], y, x);
|
|
}
|
|
}
|
|
layout->setSpacing(0);
|
|
setLayout(layout);
|
|
show();
|
|
}
|
|
|
|
void TestField::markMoves(const QList<Move> &moves) {
|
|
foreach(Move m, m_marked) m_places[m.to().x()][m.to().y()]->hideMark();
|
|
m_marked = moves;
|
|
foreach(Move m, m_marked) m_places[m.to().x()][m.to().y()]->showMark();
|
|
update();
|
|
}
|
|
|
|
void TestField::resizeEvent(QResizeEvent *event) {
|
|
m_lib->setSize(event->size().width()/8, event->size().height()/8);
|
|
}
|
|
|
|
void TestField::mousePressEvent(QMouseEvent *event) {
|
|
if (event->button() == Qt::LeftButton) {
|
|
QList<Move> moves;
|
|
int x = (event->x() * 8) / width();
|
|
int y = (event->y() * 8) / height();
|
|
if (x < 0 || x >= 8 || y < 0 || y >= 8) {
|
|
markMoves(moves);
|
|
} else {
|
|
moves = m_game->field()->validMoves(Pos(x, y));
|
|
markMoves(moves);
|
|
}
|
|
}
|
|
}
|
|
|
|
void TestField::mouseReleaseEvent(QMouseEvent *event) {
|
|
if (event->button() == Qt::LeftButton) {
|
|
if (m_marked.size() == 0) return;
|
|
Pos from = m_marked[0].from();
|
|
QList<Move> moves;
|
|
markMoves(QList<Move>());
|
|
int x = (event->x() * 8) / width();
|
|
int y = (event->y() * 8) / height();
|
|
if (x < 0 || x >= 8 || y < 0 || y >= 8) {
|
|
return;
|
|
}
|
|
Move m(m_game->field(), from, Pos(x, y));
|
|
m_game->move(m);
|
|
}
|
|
}
|
|
|
|
TestGame::TestGame(QWidget *parent) : QMainWindow(parent), m_game(new Game()) {
|
|
setWindowTitle("Torus Chess");
|
|
// PieceLibrary *lib = new PieceLibrary(this);
|
|
// setCentralWidget(new TestField(this, lib, m_game));
|
|
setCentralWidget(new Field2D(m_game, this));
|
|
statusBar()->show();
|
|
connect(m_game, SIGNAL(updated()), this, SLOT(gameUpdated()));
|
|
connect(m_game, SIGNAL(changed(GameState)), this, SLOT(gameChanged(GameState)));
|
|
m_game->restart();
|
|
}
|
|
|
|
void TestGame::gameUpdated() {
|
|
if (m_game->state() == TURN_BLACK) {
|
|
Move m = ai::getMove(m_game);
|
|
if (m.valid()) m_game->move(m);
|
|
}
|
|
}
|
|
|
|
void TestGame::gameChanged(GameState state) {
|
|
statusBar()->showMessage(state2string(state));
|
|
}
|
|
}
|