From 29aadc0925e41f19bc41e8861663b54edd268419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BChler?= Date: Tue, 6 Jan 2009 17:41:49 +0100 Subject: [PATCH] added some game logic (move queen) --- src/toruschess.cpp | 39 ++++++++++++++++++++++++++++++++++++--- src/toruschess.h | 4 ++-- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/toruschess.cpp b/src/toruschess.cpp index 4a897e4..d7b5521 100644 --- a/src/toruschess.cpp +++ b/src/toruschess.cpp @@ -39,17 +39,50 @@ namespace toruschess { memcpy(m_places, start_field, sizeof(start_field)); } - QList Field::validMoves(const Pos &p) { + void tryDirection(QList &moves, const Field *field, int thePiece, 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) { + moves.push_back(Move(field, from, to)); /* move */ + } else if (0 > toPlace * thePiece) { + moves.push_back(Move(field, from, to)); /* beat */ + for (int c = 7; c > d; c--) { + to = Pos(from.x() + c*dx, from.y() + c*dy); + toPlace = field->place(to); + if (0 == toPlace) { + moves.push_back(Move(field, from, to)); /* move */ + } else if (0 > toPlace * thePiece) { + moves.push_back(Move(field, from, to)); /* beat */ + break; + } else { + break; + } + } + break; + } else { + break; + } + } + } + + QList Field::validMoves(const Pos &from) const { QList moves; - int thePlace = place(p); + int thePlace = place(from); Piece thePiece = (Piece) qAbs(thePlace); switch (thePiece) { + 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); + break; case NOPIECE: break; } return moves; } - bool Field::validMove(const Move &m) { + bool Field::validMove(const Move &m) const { QList moves = validMoves(m.from()); return moves.contains(m); } diff --git a/src/toruschess.h b/src/toruschess.h index 49d3e84..bff9d60 100644 --- a/src/toruschess.h +++ b/src/toruschess.h @@ -86,8 +86,8 @@ namespace toruschess { int place(const Pos &p) const { return m_places[p.y()][p.x()]; } int& place(const Pos &p) { return m_places[p.y()][p.x()]; } - QList validMoves(const Pos &p); - bool validMove(const Move &m); + QList validMoves(const Pos &from) const; + bool validMove(const Move &m) const; bool move(const Move &m); bool undo(const Move &m);