added some game logic
This commit is contained in:
parent
77567f18c2
commit
8bdaf0c5dd
@ -21,17 +21,13 @@
|
|||||||
|
|
||||||
namespace toruschess {
|
namespace toruschess {
|
||||||
|
|
||||||
Game::Game(QObject *parent)
|
Move::Move(const Field* field, const Pos &from, const Pos &to)
|
||||||
: QObject(parent), m_field(new Field()) { }
|
: m_from(from), m_to(to), m_prevFrom(field->place(from)), m_prevTo(field->place(to)), m_player(field->player(from)) { }
|
||||||
|
|
||||||
Game::~Game() {
|
|
||||||
delete m_field;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const int start_field[8][8] = {
|
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 },
|
{ -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, 0, 0, 0, 0 },
|
||||||
{ 0, 0, 0, 0, ROOK, PAWN, KNIGHT, 0 },
|
{ 0, 0, 0, 0, ROOK, PAWN, KNIGHT, 0 },
|
||||||
{ 0, 0, 0, 0, QUEEN, KING, BISHOP, 0 },
|
{ 0, 0, 0, 0, QUEEN, KING, BISHOP, 0 },
|
||||||
@ -42,4 +38,37 @@ namespace toruschess {
|
|||||||
Field::Field() {
|
Field::Field() {
|
||||||
memcpy(m_places, start_field, sizeof(start_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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -21,12 +21,17 @@
|
|||||||
#define TORUSCHESSTORUSCHESS_H
|
#define TORUSCHESSTORUSCHESS_H
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
#include <QList>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@author Stefan Bühler <stbuehler@web.de>
|
@author Stefan Bühler <stbuehler@web.de>
|
||||||
*/
|
*/
|
||||||
namespace toruschess {
|
namespace toruschess {
|
||||||
|
|
||||||
|
class Pos;
|
||||||
|
class Field;
|
||||||
|
class Game;
|
||||||
|
|
||||||
typedef enum { BLACK = -1, NOPLAYER = 0, WHITE = 1 } Player;
|
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;
|
typedef enum { NOPIECE = 0, PAWN = 1, KNIGHT = 2, BISHOP = 3, ROOK = 4, QUEEN = 5, KING = 6 } Piece;
|
||||||
|
|
||||||
@ -37,10 +42,38 @@ namespace toruschess {
|
|||||||
int x() const { return m_x; }
|
int x() const { return m_x; }
|
||||||
int y() const { return m_y; }
|
int y() const { return m_y; }
|
||||||
|
|
||||||
|
bool operator==(const Pos &other) {
|
||||||
|
return m_x == other.m_x && m_y == other.m_y;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int m_x, m_y;
|
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 {
|
class Field {
|
||||||
public:
|
public:
|
||||||
Field();
|
Field();
|
||||||
@ -51,6 +84,12 @@ namespace toruschess {
|
|||||||
}
|
}
|
||||||
Piece piece(const Pos &p) const { return (Piece) qAbs(place(p)); }
|
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) 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:
|
private:
|
||||||
int m_places[8][8];
|
int m_places[8][8];
|
||||||
|
Loading…
Reference in New Issue
Block a user