toruschess/src/toruschess.h

114 lines
3.4 KiB
C++

/***************************************************************************
* Copyright (C) 2009 by Stefan Bühler *
* stbuehler@web.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef TORUSCHESSTORUSCHESS_H
#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;
class Pos {
public:
Pos(int x, int y) : m_x(x % 8), m_y(y % 8) { }
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();
Player player(const Pos &p) const {
int pl = place(p);
return (pl < 0) ? BLACK : (pl > 0) ? WHITE : NOPLAYER;
}
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 &from) const;
bool validMove(const Move &m) const;
bool move(const Move &m);
bool undo(const Move &m);
private:
int m_places[8][8];
};
class Game : public QObject {
Q_OBJECT
public:
Game(QObject *parent = 0);
virtual ~Game();
const Field* field() const { return m_field; }
bool move(const Move &m) { return m_field->move(m); }
private:
Field *m_field;
};
}
#endif