toruschess/src/toruschess.h

195 lines
5.3 KiB
C
Raw Normal View History

2009-01-06 13:52:03 +00:00
/***************************************************************************
* 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>
2009-01-06 16:34:32 +00:00
#include <QList>
#include <QThread>
#include <QMetaType>
2009-01-06 13:52:03 +00:00
/**
@author Stefan Bühler <stbuehler@web.de>
*/
namespace toruschess {
2009-01-06 16:34:32 +00:00
class Pos;
class Field;
class Game;
class GameAIThread;
2009-01-06 13:52:03 +00:00
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;
2009-01-09 11:44:39 +00:00
typedef enum { TURN_WHITE, TURN_BLACK, WON_WHITE, WON_BLACK, DRAW } GameState;
typedef enum { HUMAN_HUMAN, HUMAN_COMPUTER, COMPUTER_HUMAN, COMPUTER_COMPUTER } GameMode;
2009-01-09 11:44:39 +00:00
inline Player place2player(int place) {
return (place < 0) ? BLACK : (place > 0) ? WHITE : NOPLAYER;
}
inline Piece place2piece(int place) {
return (Piece) qAbs(place);
}
QString state2string(GameState state);
2009-01-06 13:52:03 +00:00
class Pos {
public:
2009-01-09 11:44:39 +00:00
Pos() : m_x(0), m_y(0) { }
Pos(int x, int y) : m_x(x & 7), m_y(y & 7) { }
2009-01-06 13:52:03 +00:00
int x() const { return m_x; }
int y() const { return m_y; }
2009-01-06 16:34:32 +00:00
2009-01-07 14:27:31 +00:00
bool operator==(const Pos &other) const {
2009-01-06 16:34:32 +00:00
return m_x == other.m_x && m_y == other.m_y;
}
2009-01-06 13:52:03 +00:00
private:
int m_x, m_y;
};
2009-01-06 16:34:32 +00:00
class Move {
public:
2009-01-21 15:45:17 +00:00
Move();
2009-01-06 16:34:32 +00:00
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; }
2009-01-07 14:27:31 +00:00
bool operator==(const Move &other) const {
2009-01-06 16:34:32 +00:00
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 */
}
2009-01-21 15:45:17 +00:00
bool valid() const { return m_prevFrom != 0; }
2009-01-06 16:34:32 +00:00
private:
Pos m_from,m_to;
int m_prevFrom, m_prevTo;
Player m_player;
};
2009-01-06 13:52:03 +00:00
class Field {
public:
Field();
2009-01-09 11:44:39 +00:00
void reset();
Player player(const Pos &p) const { return place2player(place(p)); }
Piece piece(const Pos &p) const { return place2piece(place(p)); }
2009-01-06 13:52:03 +00:00
int place(const Pos &p) const { return m_places[p.y()][p.x()]; }
2009-01-06 16:34:32 +00:00
int& place(const Pos &p) { return m_places[p.y()][p.x()]; }
2009-01-06 16:41:49 +00:00
QList<Move> validMoves(const Pos &from) const;
2009-01-21 15:45:17 +00:00
bool validMove(const Move &m) const; /* does not check for inCheck */
2009-01-06 16:34:32 +00:00
bool move(const Move &m);
bool undo(const Move &m);
2009-01-06 13:52:03 +00:00
2009-01-09 11:44:39 +00:00
bool inCheck(Player player, const Pos &pking) const;
bool inCheck(Player player) const;
2009-01-21 15:45:17 +00:00
void move_unchecked(const Move &m);
void undo_unchecked(const Move &m);
2009-01-06 13:52:03 +00:00
private:
2009-01-07 14:27:31 +00:00
QList<Move> simpleValidMoves(const Pos &from) const;
2009-01-21 15:45:17 +00:00
Pos m_wking, m_bking;
2009-01-06 13:52:03 +00:00
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; }
2009-01-09 11:44:39 +00:00
GameState state() const { return m_state; }
QList<Move> moves() const { return m_moves; }
bool move(const Move &m);
void restart();
QList<Move> possibleMoves() const;
GameMode gameMode() const { return m_gameMode; }
void setGameMode(GameMode gm);
int aiStrength() const { return m_aiStrength; }
void setAiStrength(int s);
2009-01-09 11:44:39 +00:00
signals:
void moved(Move m);
void updated();
void undone(Move m);
void changed(GameState state);
void started();
void changedMode(GameMode mode);
void changedAIStrength(int aiStrength);
private slots:
void finishedAI(Move m);
2009-01-06 13:52:03 +00:00
private:
void checkAI();
2009-01-06 13:52:03 +00:00
Field *m_field;
2009-01-09 11:44:39 +00:00
GameState m_state;
GameMode m_gameMode;
int m_aiStrength;
2009-01-09 11:44:39 +00:00
QList<Move> m_moves;
GameAIThread *m_aiThread;
bool m_ignoreAI;
};
class GameAIThread : public QThread {
Q_OBJECT
public:
GameAIThread(const Field &field, Player curp, int depth, QObject *parent = 0);
void run();
signals:
void aiFinished(Move m);
private:
Field m_field;
Player m_curp;
int m_depth;
2009-01-06 13:52:03 +00:00
};
2009-01-06 13:52:03 +00:00
}
Q_DECLARE_METATYPE(toruschess::Move)
Q_DECLARE_METATYPE(toruschess::Pos)
2009-01-06 13:52:03 +00:00
#endif