From 8bdaf0c5dd126e2432003a3dc9fe295c4e523c9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BChler?= Date: Tue, 6 Jan 2009 17:34:32 +0100 Subject: [PATCH] added some game logic --- src/toruschess.cpp | 45 +++++++++++++++++++++++++++++++++++++-------- src/toruschess.h | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 77 insertions(+), 9 deletions(-) diff --git a/src/toruschess.cpp b/src/toruschess.cpp index 3b08472..4a897e4 100644 --- a/src/toruschess.cpp +++ b/src/toruschess.cpp @@ -21,17 +21,13 @@ namespace toruschess { - Game::Game(QObject *parent) - : QObject(parent), m_field(new Field()) { } - - Game::~Game() { - delete m_field; - } + Move::Move(const Field* field, const Pos &from, const Pos &to) + : m_from(from), m_to(to), m_prevFrom(field->place(from)), m_prevTo(field->place(to)), m_player(field->player(from)) { } static const int start_field[8][8] = { - { -ROOK, -PAWN, -KNIGHT, 0, 0, 0, 0, 0 }, - { -BISHOP, -KING, -QUEEN, 0, 0, 0, 0, 0 }, { -KNIGHT, -PAWN, -ROOK, 0, 0, 0, 0, 0 }, + { -BISHOP, -KING, -QUEEN, 0, 0, 0, 0, 0 }, + { -ROOK, -PAWN, -KNIGHT, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, ROOK, PAWN, KNIGHT, 0 }, { 0, 0, 0, 0, QUEEN, KING, BISHOP, 0 }, @@ -42,4 +38,37 @@ namespace toruschess { Field::Field() { memcpy(m_places, start_field, sizeof(start_field)); } + + QList Field::validMoves(const Pos &p) { + QList moves; + int thePlace = place(p); + Piece thePiece = (Piece) qAbs(thePlace); + switch (thePiece) { + case NOPIECE: + break; + } + return moves; + } + bool Field::validMove(const Move &m) { + QList moves = validMoves(m.from()); + return moves.contains(m); + } + bool Field::move(const Move &m) { + if (!validMove(m)) return false; + return true; + } + bool Field::undo(const Move &m) { + if (place(m.from()) != 0 || place(m.to()) != m.prevFrom()) return false; + place(m.from()) = m.prevFrom(); + place(m.to()) = m.prevTo(); + return false; + } + + Game::Game(QObject *parent) + : QObject(parent), m_field(new Field()) { } + + Game::~Game() { + delete m_field; + } + } diff --git a/src/toruschess.h b/src/toruschess.h index f3c8645..49d3e84 100644 --- a/src/toruschess.h +++ b/src/toruschess.h @@ -21,12 +21,17 @@ #define TORUSCHESSTORUSCHESS_H #include +#include /** @author Stefan Bühler */ namespace toruschess { + class Pos; + class Field; + class Game; + typedef enum { BLACK = -1, NOPLAYER = 0, WHITE = 1 } Player; typedef enum { NOPIECE = 0, PAWN = 1, KNIGHT = 2, BISHOP = 3, ROOK = 4, QUEEN = 5, KING = 6 } Piece; @@ -36,11 +41,39 @@ namespace toruschess { int x() const { return m_x; } int y() const { return m_y; } - + + bool operator==(const Pos &other) { + return m_x == other.m_x && m_y == other.m_y; + } + private: int m_x, m_y; }; + class Move { + public: + Move(const Field* field, const Pos &from, const Pos &to); + + const Pos& from() const { return m_from; } + const Pos& to() const { return m_to; } + int prevFrom() const { return m_prevFrom; } + int prevTo() const { return m_prevTo; } + Player player() const { return m_player; } + + bool operator==(const Move &other) { + return m_from == other.m_from + && m_to == other.m_to + && m_prevFrom == other.m_prevFrom + && m_prevTo == other.m_prevTo; + /* player is included in m_prevFrom */ + } + + private: + Pos m_from,m_to; + int m_prevFrom, m_prevTo; + Player m_player; + }; + class Field { public: Field(); @@ -51,6 +84,12 @@ namespace toruschess { } Piece piece(const Pos &p) const { return (Piece) qAbs(place(p)); } 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); + bool move(const Move &m); + bool undo(const Move &m); private: int m_places[8][8];