Browse Source

more game logic (all moves), can move pieces in gui

master
Stefan Bühler 13 years ago
parent
commit
3490c540ac
  1. 5
      src/src.pro
  2. 62
      src/testgame.cpp
  3. 10
      src/testgame.h
  4. 78
      src/toruschess.cpp
  5. 2
      src/toruschess.h

5
src/src.pro

@ -5,7 +5,8 @@ TEMPLATE = app
CONFIG += warn_on \
thread \
qt \
opengl
opengl \
debug
TARGET = toruschess
DESTDIR = ../bin
RESOURCES += media.qrc
@ -14,3 +15,5 @@ QT += svg \
opengl
HEADERS += testgame.h \
toruschess.h
CONFIG -= release

62
src/testgame.cpp

@ -22,6 +22,7 @@
#include <QGridLayout>
#include <QPainter>
#include <QResizeEvent>
#include <QMouseEvent>
namespace toruschess {
@ -53,7 +54,6 @@ namespace toruschess {
m_buffers[i] = new QImage(QSize(m_width, m_height), QImage::Format_ARGB32_Premultiplied);
m_buffers[i]->fill(0x0);
QPainter p(m_buffers[i]);
p.setViewport(1, 1, m_width-1, m_height-1);
m_pieces[i]->render(&p);
}
}
@ -74,7 +74,8 @@ namespace toruschess {
QGridLayout *layout = new QGridLayout();
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
layout->addWidget(new TestPlace(this, m_lib, m_game->field(), Pos(x, y)), y, 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);
@ -82,28 +83,73 @@ namespace toruschess {
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);
}
}
TestPlace::TestPlace(QWidget *parent, const PieceLibrary *lib, const Field *field, const Pos &p)
: QWidget(parent), m_field(field), m_lib(lib), m_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(this->rect(), QBrush(QColor(0xD1, 0x8B, 0x47)));
pt.fillRect(rect(), QBrush(QColor(0xD1, 0x8B, 0x47)));
} else {
// #FFCE9E
pt.fillRect(this->rect(), QBrush(QColor(0xFF, 0xCE, 0x9E)));
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);
}
m_lib->paint(pt, m_field->place(m_pos));
}
}

10
src/testgame.h

@ -60,6 +60,9 @@ namespace toruschess {
virtual QSize sizeHint() const;
void showMark() { m_mark = true; update(); }
void hideMark() { m_mark = false; update(); }
protected:
virtual void paintEvent(QPaintEvent *event);
@ -67,6 +70,7 @@ namespace toruschess {
const Field *m_field;
const PieceLibrary *m_lib;
Pos m_pos;
bool m_mark;
};
class TestField : public QWidget {
@ -74,12 +78,18 @@ namespace toruschess {
public:
TestField(QWidget *parent, PieceLibrary *lib, Game *m_game);
void markMoves(const QList<Move> &moves);
protected:
virtual void resizeEvent(QResizeEvent *event);
virtual void mousePressEvent(QMouseEvent *event);
virtual void mouseReleaseEvent(QMouseEvent *event);
private:
PieceLibrary *m_lib;
Game *m_game;
TestPlace* m_places[8][8];
QList<Move> m_marked;
};
class TestGame : public QMainWindow {

78
src/toruschess.cpp

@ -39,55 +39,111 @@ namespace toruschess {
memcpy(m_places, start_field, sizeof(start_field));
}
void tryDirection(QList<Move> &moves, const Field *field, int thePiece, const Pos &from, int dx, int dy) {
void tryDirection(QList<Move> &moves, const Field *field, int thePlace, const Pos &from, int dx, int dy) {
for (int d = 1; d < 8; d++) {
Pos to = Pos(from.x() + d*dx, from.y() + d*dy);
int toPlace = field->place(to);
if (0 == toPlace) {
// qDebug("move 1, %i", d);
moves.push_back(Move(field, from, to)); /* move */
} else if (0 > toPlace * thePiece) {
moves.push_back(Move(field, from, to)); /* beat */
} else {
if (0 > toPlace * thePlace) {
// qDebug("beat 1, %i", d);
moves.push_back(Move(field, from, to)); /* beat */
} else {
// qDebug("hit friend, %i", d);
}
for (int c = 7; c > d; c--) {
to = Pos(from.x() + c*dx, from.y() + c*dy);
toPlace = field->place(to);
if (0 == toPlace) {
// qDebug("move 2, %i %i", d, c);
moves.push_back(Move(field, from, to)); /* move */
} else if (0 > toPlace * thePiece) {
} else if (0 > toPlace * thePlace) {
// qDebug("beat 2, %i %i", d, c);
moves.push_back(Move(field, from, to)); /* beat */
break;
} else {
// qDebug("hit friend, %i %i, (%i, %i) = %i", d, c, to.x(), to.y(), toPlace);
break;
}
}
break;
} else {
break;
}
}
}
/* 7 == -1 mod 8, 6 == -2: but positive values are needed for (a + b) % 8 >= 0 */
typedef struct { int x, y; } move_delta;
static const move_delta moves_pawn_walk[4] = { { 1, 0 }, { 0, 1 }, { 7, 0 }, { 0, 7 } };
static const move_delta moves_pawn_beat[4] = { { 1, 1 }, { 1, 7 }, { 7, 7 }, { 7, 1 } };
static const move_delta moves_knight[8] = { { 1, 2 }, { 1, 6 }, { 7, 2 }, { 7, 6 }, { 2, 1 }, { 2, 7 }, { 6, 1 }, { 6, 7 } };
static const move_delta moves_king[8] = { { 1, 1 }, { 1, 0 }, { 1, 7 }, { 0, 7 }, { 7, 7 }, { 7, 0 }, { 7, 1 }, { 0, 1 } };
static Pos operator + (const Pos &p, move_delta d) {
return Pos(p.x() + d.x, p.y() + d.y);
}
QList<Move> Field::validMoves(const Pos &from) const {
QList<Move> moves;
int thePlace = place(from);
Piece thePiece = (Piece) qAbs(thePlace);
qDebug("validMoves: %i", thePiece);
switch (thePiece) {
case NOPIECE:
break;
case PAWN:
for (int i = 0; i < 4; i++) {
if (0 == place(from + moves_pawn_walk[i])) {
moves.push_back(Move(this, from, from + moves_pawn_walk[i]));
}
if (0 > thePlace * place(from + moves_pawn_beat[i])) {
moves.push_back(Move(this, from, from + moves_pawn_beat[i]));
}
}
break;
case KNIGHT:
for (int i = 0; i < 8; i++) {
if (0 >= thePlace * place(from + moves_knight[i])) {
moves.push_back(Move(this, from, from + moves_knight[i]));
}
}
break;
case BISHOP:
tryDirection(moves, this, thePlace, from, 1, 1);
tryDirection(moves, this, thePlace, from, 1, 7);
break;
case ROOK:
tryDirection(moves, this, thePlace, from, 1, 0);
tryDirection(moves, this, thePlace, from, 0, 1);
break;
case QUEEN:
tryDirection(moves, this, thePiece, from, 1, 1);
tryDirection(moves, this, thePiece, from, 1, -1);
tryDirection(moves, this, thePiece, from, 1, 0);
tryDirection(moves, this, thePiece, from, 0, 1);
tryDirection(moves, this, thePlace, from, 1, 1);
tryDirection(moves, this, thePlace, from, 1, 7);
tryDirection(moves, this, thePlace, from, 1, 0);
tryDirection(moves, this, thePlace, from, 0, 1);
break;
case NOPIECE:
case KING:
for (int i = 0; i < 8; i++) {
if (0 >= thePlace * place(from + moves_king[i])) {
moves.push_back(Move(this, from, from + moves_king[i]));
}
}
break;
}
qDebug("validMoves: found %i", moves.size());
return moves;
}
bool Field::validMove(const Move &m) const {
QList<Move> moves = validMoves(m.from());
return moves.contains(m);
}
bool Field::move(const Move &m) {
if (!validMove(m)) return false;
place(m.from()) = 0;
place(m.to()) = m.prevFrom();
return true;
}
bool Field::undo(const Move &m) {

2
src/toruschess.h

@ -103,6 +103,8 @@ namespace toruschess {
const Field* field() const { return m_field; }
bool move(const Move &m) { return m_field->move(m); }
private:
Field *m_field;
};

Loading…
Cancel
Save