Browse Source

added some game logic

master
Stefan Bühler 13 years ago
parent
commit
8bdaf0c5dd
  1. 45
      src/toruschess.cpp
  2. 41
      src/toruschess.h

45
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<Move> Field::validMoves(const Pos &p) {
QList<Move> moves;
int thePlace = place(p);
Piece thePiece = (Piece) qAbs(thePlace);
switch (thePiece) {
case NOPIECE:
break;
}
return moves;
}
bool Field::validMove(const Move &m) {
QList<Move> 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;
}
}

41
src/toruschess.h

@ -21,12 +21,17 @@
#define TORUSCHESSTORUSCHESS_H
#include <QObject>
#include <QList>
/**
@author Stefan Bühler <stbuehler@web.de>
*/
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<Move> 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];

Loading…
Cancel
Save