toruschess/src/toruschess.cpp

296 lines
8.9 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. *
***************************************************************************/
#include "toruschess.h"
namespace toruschess {
2009-01-09 11:44:39 +00:00
QString state2string(GameState state) {
switch (state) {
case TURN_WHITE: return "White's turn";
case TURN_BLACK: return "Black's turn";
case WON_WHITE: return "White is the winner";
case WON_BLACK: return "Black is the winner";
case DRAW: return "Draw game";
}
return "Invalid GameState";
}
2009-01-06 16:34:32 +00:00
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)) { }
2009-01-06 13:52:03 +00:00
static const int start_field[8][8] = {
{ -KNIGHT, -PAWN, -ROOK, 0, 0, 0, 0, 0 },
2009-01-06 16:34:32 +00:00
{ -BISHOP, -KING, -QUEEN, 0, 0, 0, 0, 0 },
{ -ROOK, -PAWN, -KNIGHT, 0, 0, 0, 0, 0 },
2009-01-06 13:52:03 +00:00
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, ROOK, PAWN, KNIGHT, 0 },
{ 0, 0, 0, 0, QUEEN, KING, BISHOP, 0 },
{ 0, 0, 0, 0, KNIGHT, PAWN, ROOK, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 }
};
Field::Field() {
memcpy(m_places, start_field, sizeof(start_field));
}
2009-01-06 16:34:32 +00:00
2009-01-09 11:44:39 +00:00
void Field::reset() {
memcpy(m_places, start_field, sizeof(start_field));
}
void tryDirection(QList<Move> &moves, const Field *field, int thePlace, const Pos &from, int dx, int dy) {
2009-01-06 16:41:49 +00:00
for (int d = 1; d < 8; d++) {
Pos to = Pos(from.x() + d*dx, from.y() + d*dy);
int toPlace = field->place(to);
if (0 == toPlace) {
// qDebug("move 1, %i", d);
2009-01-06 16:41:49 +00:00
moves.push_back(Move(field, from, to)); /* move */
} else {
if (0 > toPlace * thePlace) {
// qDebug("beat 1, %i", d);
moves.push_back(Move(field, from, to)); /* beat */
} else {
// qDebug("hit friend, %i", d);
}
2009-01-06 16:41:49 +00:00
for (int c = 7; c > d; c--) {
to = Pos(from.x() + c*dx, from.y() + c*dy);
toPlace = field->place(to);
if (0 == toPlace) {
// qDebug("move 2, %i %i", d, c);
2009-01-06 16:41:49 +00:00
moves.push_back(Move(field, from, to)); /* move */
} else if (0 > toPlace * thePlace) {
// qDebug("beat 2, %i %i", d, c);
2009-01-06 16:41:49 +00:00
moves.push_back(Move(field, from, to)); /* beat */
break;
} else {
// qDebug("hit friend, %i %i, (%i, %i) = %i", d, c, to.x(), to.y(), toPlace);
2009-01-06 16:41:49 +00:00
break;
}
}
break;
}
}
}
/* 7 == -1 mod 8, 6 == -2: but positive values are needed for (a + b) % 8 >= 0 */
typedef struct { int x, y; } move_delta;
static const move_delta moves_pawn_walk[4] = { { 1, 0 }, { 0, 1 }, { 7, 0 }, { 0, 7 } };
static const move_delta moves_pawn_beat[4] = { { 1, 1 }, { 1, 7 }, { 7, 7 }, { 7, 1 } };
static const move_delta moves_knight[8] = { { 1, 2 }, { 1, 6 }, { 7, 2 }, { 7, 6 }, { 2, 1 }, { 2, 7 }, { 6, 1 }, { 6, 7 } };
static const move_delta moves_king[8] = { { 1, 1 }, { 1, 0 }, { 1, 7 }, { 0, 7 }, { 7, 7 }, { 7, 0 }, { 7, 1 }, { 0, 1 } };
static Pos operator + (const Pos &p, move_delta d) {
return Pos(p.x() + d.x, p.y() + d.y);
}
2009-01-07 14:27:31 +00:00
QList<Move> Field::simpleValidMoves(const Pos &from) const {
2009-01-06 16:34:32 +00:00
QList<Move> moves;
2009-01-06 16:41:49 +00:00
int thePlace = place(from);
2009-01-06 16:34:32 +00:00
Piece thePiece = (Piece) qAbs(thePlace);
2009-01-07 14:27:31 +00:00
// qDebug("simpleValidMoves: %i", thePiece);
2009-01-06 16:34:32 +00:00
switch (thePiece) {
case NOPIECE:
break;
case PAWN:
for (int i = 0; i < 4; i++) {
if (0 == place(from + moves_pawn_walk[i])) {
moves.push_back(Move(this, from, from + moves_pawn_walk[i]));
}
if (0 > thePlace * place(from + moves_pawn_beat[i])) {
moves.push_back(Move(this, from, from + moves_pawn_beat[i]));
}
}
break;
case KNIGHT:
for (int i = 0; i < 8; i++) {
if (0 >= thePlace * place(from + moves_knight[i])) {
moves.push_back(Move(this, from, from + moves_knight[i]));
}
}
break;
case BISHOP:
tryDirection(moves, this, thePlace, from, 1, 1);
tryDirection(moves, this, thePlace, from, 1, 7);
break;
case ROOK:
tryDirection(moves, this, thePlace, from, 1, 0);
tryDirection(moves, this, thePlace, from, 0, 1);
break;
2009-01-06 16:41:49 +00:00
case QUEEN:
tryDirection(moves, this, thePlace, from, 1, 1);
tryDirection(moves, this, thePlace, from, 1, 7);
tryDirection(moves, this, thePlace, from, 1, 0);
tryDirection(moves, this, thePlace, from, 0, 1);
2009-01-06 16:41:49 +00:00
break;
case KING:
for (int i = 0; i < 8; i++) {
if (0 >= thePlace * place(from + moves_king[i])) {
moves.push_back(Move(this, from, from + moves_king[i]));
}
}
2009-01-06 16:34:32 +00:00
break;
}
2009-01-07 14:27:31 +00:00
// qDebug("simpleValidMoves: found %i", moves.size());
2009-01-06 16:34:32 +00:00
return moves;
}
2009-01-07 14:27:31 +00:00
QList<Move> Field::validMoves(const Pos &from) const {
QList<Move> moves = simpleValidMoves(from);
int thePlace = place(from);
Pos pking(0,0);
/* find king */
for (int x = 0; x < 8; x++) for (int y = 0; y < 8; y++) {
if (piece(Pos(x, y)) == KING && 0 < place(Pos(x, y)) * thePlace) {
pking = Pos(x, y);
goto foundking;
}
}
return QList<Move>(); /* error, no king found */
foundking:
QList<Move> resMoves;
Field testField(*this);
testField.place(from) = 0;
bool movedKing = (from == pking);
2009-01-09 11:44:39 +00:00
Player player = place2player(thePlace);
2009-01-07 14:27:31 +00:00
foreach(Move m, moves) {
testField.place(m.to()) = thePlace;
2009-01-09 11:44:39 +00:00
if (!testField.inCheck(player, movedKing ? m.to() : pking)) {
resMoves.push_back(m);
2009-01-07 14:27:31 +00:00
}
testField.place(m.to()) = m.prevTo();
}
return resMoves;
}
2009-01-06 16:41:49 +00:00
bool Field::validMove(const Move &m) const {
2009-01-06 16:34:32 +00:00
QList<Move> moves = validMoves(m.from());
return moves.contains(m);
}
2009-01-06 16:34:32 +00:00
bool Field::move(const Move &m) {
if (!validMove(m)) return false;
place(m.from()) = 0;
place(m.to()) = m.prevFrom();
2009-01-06 16:34:32 +00:00
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;
}
2009-01-09 11:44:39 +00:00
bool Field::inCheck(Player player, const Pos &pking) const {
for (int x = 0; x < 8; x++) for (int y = 0; y < 8; y++) {
Pos curp(x, y);
if (0 > place(curp) * player && simpleValidMoves(curp).contains(Move(this, curp, pking))) {
return true;
}
}
return false;
}
bool Field::inCheck(Player player) const {
/* find king */
for (int x = 0; x < 8; x++) for (int y = 0; y < 8; y++) {
Pos curp(x, y);
if (piece(curp) == KING && 0 < place(curp) * player) {
return inCheck(player, curp);
}
}
return false; /* error, no king found */
}
2009-01-06 16:34:32 +00:00
Game::Game(QObject *parent)
2009-01-09 11:44:39 +00:00
: QObject(parent), m_field(new Field()), m_state(TURN_WHITE) { }
2009-01-06 16:34:32 +00:00
Game::~Game() {
delete m_field;
}
2009-01-09 11:44:39 +00:00
bool Game::move(const Move &m) {
switch (m_state) {
case TURN_WHITE:
if (m.player() != WHITE) return false;
break;
case TURN_BLACK:
if (m.player() != BLACK) return false;
break;
default:
return false;
}
if (m_field->move(m)) {
m_moves.push_back(m);
if (m_state == TURN_WHITE) {
m_state = TURN_BLACK;
if (possibleMoves().empty()) {
if (m_field->inCheck(BLACK)) {
m_state = WON_WHITE;
} else {
m_state = DRAW;
}
}
} else {
m_state = TURN_WHITE;
if (possibleMoves().empty()) {
if (m_field->inCheck(WHITE)) {
m_state = WON_BLACK;
} else {
m_state = DRAW;
}
}
}
emit moved(m);
emit changed(m_state);
emit updated();
return true;
}
return false;
}
void Game::restart() {
m_state = TURN_WHITE;
m_field->reset();
emit changed(m_state);
emit started();
emit updated();
}
QList<Move> Game::possibleMoves() const {
Player player = NOPLAYER;
switch (m_state) {
case TURN_WHITE:
player = WHITE;
break;
case TURN_BLACK:
player = BLACK;
break;
default:
return QList<Move>();
}
QList<Move> moves;
for (int x = 0; x < 8; x++) for (int y = 0; y < 8; y++) {
Pos curp(x, y);
if (0 < m_field->place(curp) * player)
moves += m_field->validMoves(curp);
}
return moves;
}
2009-01-06 13:52:03 +00:00
}