added some game logic (move queen)

This commit is contained in:
Stefan Bühler 2009-01-06 17:41:49 +01:00
parent 8bdaf0c5dd
commit 29aadc0925
2 changed files with 38 additions and 5 deletions

View File

@ -39,17 +39,50 @@ namespace toruschess {
memcpy(m_places, start_field, sizeof(start_field));
}
QList<Move> Field::validMoves(const Pos &p) {
void tryDirection(QList<Move> &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<Move> Field::validMoves(const Pos &from) const {
QList<Move> 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<Move> moves = validMoves(m.from());
return moves.contains(m);
}

View File

@ -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<Move> validMoves(const Pos &p);
bool validMove(const Move &m);
QList<Move> validMoves(const Pos &from) const;
bool validMove(const Move &m) const;
bool move(const Move &m);
bool undo(const Move &m);