From fd8f95cd7f9d7302e9450babe002a1f6c55d4652 Mon Sep 17 00:00:00 2001 From: Paolo Capriotti Date: Wed, 8 Aug 2007 14:08:55 +0200 Subject: [PATCH] Removed all old variants. --- src/CMakeLists.txt | 10 +- src/algebraicnotation.cpp | 144 ------ src/algebraicnotation.h | 94 ---- src/entities/engineentity.cpp | 1 - src/entities/examinationentity.cpp | 5 +- src/entities/gameentity.cpp | 4 +- src/entities/icsentity.cpp | 3 - src/game.cpp | 2 +- src/hlvariant/animator.impl.h | 1 - src/icsconnection.cpp | 3 +- src/icsgamedata.cpp | 2 +- src/mainwindow.cpp | 6 +- src/moveserializer.impl.h | 1 - src/newgame.cpp | 2 +- src/pgnparser.cpp | 1 + src/pgnparser.h | 1 - src/poolinfo.cpp | 3 +- src/positioninfo.cpp | 9 +- src/pref_theme.cpp | 2 +- src/tagua_wrapped.h | 1 - src/ui.cpp | 17 - src/ui.h | 4 - src/{variants => }/variants.cpp | 14 - src/{variants => }/variants.h | 0 src/variants/atomic.cpp_ | 275 ---------- src/variants/atomic.h_ | 23 - src/variants/chainreaction.cpp_ | 479 ----------------- src/variants/chainreaction.h_ | 23 - src/variants/chess.cpp | 226 -------- src/variants/chess.h | 50 -- src/variants/connect4.cpp_ | 248 --------- src/variants/connect4.h_ | 23 - src/variants/crazyhouse.cpp | 276 ---------- src/variants/crazyhouse.h | 24 - src/variants/crazyhouse_p.h | 169 ------ src/variants/dummy.cpp_ | 175 ------- src/variants/dummy.h_ | 24 - src/variants/king4pawns.cpp_ | 111 ---- src/variants/king4pawns.h_ | 22 - src/variants/progressive.cpp_ | 90 ---- src/variants/progressive.h_ | 22 - src/variants/reversi.cpp_ | 410 --------------- src/variants/reversi.h_ | 23 - src/variants/rubyvariant.cpp_ | 569 -------------------- src/variants/rubyvariant.h_ | 24 - src/variants/shogi.cpp | 816 ----------------------------- src/variants/shogi.cpp_ | 743 --------------------------- src/variants/shogi.h | 23 - src/variants/shogi.h_ | 23 - src/variants/suicide.h__ | 13 - src/variants/tagua.rb | 91 ---- src/variants/tictactoe.rb | 110 ---- src/variants/trivialanimator.h_ | 22 - src/variants/ultima.cpp__ | 356 ------------- src/variants/ultima.h__ | 178 ------- src/variants/variants-autogen | 12 - src/variants/xchess/animator.h | 69 --- src/variants/xchess/animator.impl.h | 202 -------- src/variants/xchess/dropanimator.h | 26 - src/variants/xchess/dropanimator.impl.h | 102 ---- src/variants/xchess/generator.h | 175 ------- src/variants/xchess/move.cpp | 52 -- src/variants/xchess/move.h | 57 --- src/variants/xchess/piece.cpp | 201 -------- src/variants/xchess/piece.h | 195 ------- src/variants/xchess/piecetype.h | 30 -- src/variants/xchess/pool.h | 210 -------- src/variants/xchess/position.h | 883 -------------------------------- src/variants/xiangqi.cpp_ | 640 ----------------------- src/variants/xiangqi.h_ | 13 - 70 files changed, 18 insertions(+), 8840 deletions(-) delete mode 100644 src/algebraicnotation.cpp delete mode 100644 src/algebraicnotation.h rename src/{variants => }/variants.cpp (75%) rename src/{variants => }/variants.h (100%) delete mode 100644 src/variants/atomic.cpp_ delete mode 100644 src/variants/atomic.h_ delete mode 100644 src/variants/chainreaction.cpp_ delete mode 100644 src/variants/chainreaction.h_ delete mode 100644 src/variants/chess.cpp delete mode 100644 src/variants/chess.h delete mode 100644 src/variants/connect4.cpp_ delete mode 100644 src/variants/connect4.h_ delete mode 100644 src/variants/crazyhouse.cpp delete mode 100644 src/variants/crazyhouse.h delete mode 100644 src/variants/crazyhouse_p.h delete mode 100644 src/variants/dummy.cpp_ delete mode 100644 src/variants/dummy.h_ delete mode 100644 src/variants/king4pawns.cpp_ delete mode 100644 src/variants/king4pawns.h_ delete mode 100644 src/variants/progressive.cpp_ delete mode 100644 src/variants/progressive.h_ delete mode 100644 src/variants/reversi.cpp_ delete mode 100644 src/variants/reversi.h_ delete mode 100644 src/variants/rubyvariant.cpp_ delete mode 100644 src/variants/rubyvariant.h_ delete mode 100644 src/variants/shogi.cpp delete mode 100644 src/variants/shogi.cpp_ delete mode 100644 src/variants/shogi.h delete mode 100644 src/variants/shogi.h_ delete mode 100644 src/variants/suicide.h__ delete mode 100644 src/variants/tagua.rb delete mode 100644 src/variants/tictactoe.rb delete mode 100644 src/variants/trivialanimator.h_ delete mode 100644 src/variants/ultima.cpp__ delete mode 100644 src/variants/ultima.h__ delete mode 100644 src/variants/variants-autogen delete mode 100644 src/variants/xchess/animator.h delete mode 100644 src/variants/xchess/animator.impl.h delete mode 100644 src/variants/xchess/dropanimator.h delete mode 100755 src/variants/xchess/dropanimator.impl.h delete mode 100644 src/variants/xchess/generator.h delete mode 100644 src/variants/xchess/move.cpp delete mode 100644 src/variants/xchess/move.h delete mode 100644 src/variants/xchess/piece.cpp delete mode 100644 src/variants/xchess/piece.h delete mode 100644 src/variants/xchess/piecetype.h delete mode 100644 src/variants/xchess/pool.h delete mode 100644 src/variants/xchess/position.h delete mode 100644 src/variants/xiangqi.cpp_ delete mode 100644 src/variants/xiangqi.h_ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index cf5ad11..0f842ed 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -22,14 +22,6 @@ set(tagua_SRC entities/entity.cpp entities/engineentity.cpp entities/icsentity.cpp - - variants/variants.cpp - variants/xchess/move.cpp - variants/xchess/piece.cpp - - variants/chess.cpp - variants/crazyhouse.cpp - variants/shogi.cpp hlvariant/chess/variant.cpp hlvariant/chess/san.cpp @@ -78,7 +70,6 @@ set(tagua_SRC clock.cpp chesstable.cpp index.cpp - algebraicnotation.cpp mastersettings.cpp location.cpp hline.cpp @@ -112,6 +103,7 @@ set(tagua_SRC icsgamedata.cpp turnpolicy.cpp decoratedmove.cpp + variants.cpp ) # remove spurious xpressive warnings diff --git a/src/algebraicnotation.cpp b/src/algebraicnotation.cpp deleted file mode 100644 index 996dd6e..0000000 --- a/src/algebraicnotation.cpp +++ /dev/null @@ -1,144 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#include -#include -#include "algebraicnotation.h" -#include "variants/xchess/piece.h" -#include "variants/xchess/move.h" - -//BEGIN AlgebraicNotation - -// 1 2 3 -QRegExp AlgebraicNotation::pattern("^([PRNBKQ])?([a-wyzA-Z]?\\d*|x\\d+)([-x@])?" -// 4 5 6 - "([a-zA-Z]\\d+)(=?([RNBKQrnbkq]))?[+#]?[\?!]*"); -QRegExp AlgebraicNotation::kingCastlingPattern("^[oO0]-?[oO0][+#]?"); -QRegExp AlgebraicNotation::queenCastlingPattern("^[oO0]-?[oO0]-?[oO0][+#]?"); -QRegExp AlgebraicNotation::nonePattern("^none"); - -AlgebraicNotation::AlgebraicNotation() -: from(Point::invalid()) -, to(Point::invalid()) -, type(-1) -, promotion(-1) -, castling(NoCastling) -, drop(false) { -} - -AlgebraicNotation::AlgebraicNotation(const QString& str, int ysize) -: from(Point::invalid()) -, to(Point::invalid()) -, type(-1) -, promotion(-1) -, castling(NoCastling) -, drop(false) { - - int offset = 0; - init(str, offset, ysize); -} - -AlgebraicNotation::AlgebraicNotation(const QString& str, int& offset, int ysize) -: from(Point::invalid()) -, to(Point::invalid()) -, type(-1) -, promotion(-1) -, castling(NoCastling) -, drop(false) { - - init(str, offset, ysize); -} - -void AlgebraicNotation::init(const QString& str, int& offset, int ysize) { - if (nonePattern.indexIn(str, offset, QRegExp::CaretAtOffset) != -1) { - from = Point::invalid(); - to = Point::invalid(); - offset += nonePattern.matchedLength(); - } - else if (pattern.indexIn(str, offset, QRegExp::CaretAtOffset) != -1) { - type = ChessPiece::getType(pattern.cap(1)); - drop = pattern.cap(3) == "@"; - if (drop) - from = Point::invalid(); - else - from = Point(pattern.cap(2), ysize); - to = Point(pattern.cap(4), ysize); - promotion = pattern.cap(6).isEmpty() ? -1 : ChessPiece::getType(pattern.cap(6)); - castling = NoCastling; - offset += pattern.matchedLength(); - } - else if (queenCastlingPattern.indexIn(str, offset, QRegExp::CaretAtOffset) != -1) { - castling = QueenSide; - - offset += queenCastlingPattern.matchedLength(); - } - else if (kingCastlingPattern.indexIn(str, offset, QRegExp::CaretAtOffset) != -1) { - castling = KingSide; - - offset += kingCastlingPattern.matchedLength(); - } - else { - //std::cout << "error!!!! " << str.mid(offset) << std::endl; - to = Point::invalid(); - } -} - -std::ostream& operator<<(std::ostream& os, const AlgebraicNotation& move) { - if (move.castling == AlgebraicNotation::KingSide) - return os << "O-O"; - else if (move.castling == AlgebraicNotation::QueenSide) - return os << "O-O-O"; - else - return os << move.type << ": " << move.from << " -> " << move.to; -} - -//END AlgebraicNotation - - -//BEGIN VerboseNotation -// 1 2 3 5 -QRegExp VerboseNotation::pattern("([PRNBKQ])/([a-zA-Z]\\d+|@@)-([a-zA-Z]\\d+)(=([PRNBKQ]))?"); -QRegExp VerboseNotation::kingCastlingPattern("[oO0]-[oO0]"); -QRegExp VerboseNotation::queenCastlingPattern("[oO0]-[oO0]-[oO0]"); -QRegExp VerboseNotation::nonePattern("none"); - -VerboseNotation::VerboseNotation(const QString& str, int ysize) -: promotion(INVALID_TYPE) { - if (nonePattern.indexIn(str) == 0) { - from = Point::invalid(); - to = Point::invalid(); - } - else if (pattern.indexIn(str) == 0) { - if (pattern.cap(2) == "@@") - from = Point::invalid(); - else - from = Point(pattern.cap(2), ysize); - - to = Point(pattern.cap(3), ysize); - - type = ChessPiece::getType(pattern.cap(1)); - if (!pattern.cap(5).isEmpty()) - promotion = ChessPiece::getType(pattern.cap(6)); - else - promotion = INVALID_TYPE; - castling = AlgebraicNotation::NoCastling; - } - else if (queenCastlingPattern.indexIn(str) == 0) - castling = AlgebraicNotation::QueenSide; - else if (kingCastlingPattern.indexIn(str) == 0) - castling = AlgebraicNotation::KingSide; - else { - from = Point::invalid(); - to = Point::invalid(); - castling = AlgebraicNotation::NoCastling; - } -} - -//END VerboseNotation diff --git a/src/algebraicnotation.h b/src/algebraicnotation.h deleted file mode 100644 index ea6c547..0000000 --- a/src/algebraicnotation.h +++ /dev/null @@ -1,94 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#ifndef ALGEBRAICNOTATION_H -#define ALGEBRAICNOTATION_H - -#include -#include "common.h" -#include "point.h" - -class QRegExp; -class QString; - -class AlgebraicNotation { - friend std::ostream& operator<<(std::ostream& os, const AlgebraicNotation& move); - - static QRegExp pattern; - static QRegExp kingCastlingPattern; - static QRegExp queenCastlingPattern; - static QRegExp nonePattern; -public: - enum CastlingType { - NoCastling, - KingSide, - QueenSide - }; - - AlgebraicNotation(const QString&, int& offset, int ysize); - explicit AlgebraicNotation(const QString&, int ysize); - AlgebraicNotation(); - - void init(const QString&, int& offset, int ysize); - - Point from, to; - int type; - int promotion; - CastlingType castling; - bool drop; - - inline bool invalid() const { return (to == Point::invalid()) && (castling == NoCastling); } - inline bool valid() const { return !invalid(); } -}; - -class VerboseNotation { - static QRegExp pattern; - static QRegExp kingCastlingPattern; - static QRegExp queenCastlingPattern; - static QRegExp nonePattern; - -public: - explicit VerboseNotation(const QString&, int ysize); - Point from, to; - int type, promotion; - AlgebraicNotation::CastlingType castling; - inline bool invalid() const { return (from == Point::invalid() && to == Point::invalid()); } - inline bool valid() const { return !invalid(); } -}; - -// IMPLEMENTATION - - -#define TRY(x) ref.getMove(x, ok); if (ok) return; -template -void minimalNotation(AlgebraicNotation& san, const Pos& ref) { - Point from = san.from; - san.castling = AlgebraicNotation::NoCastling; - bool ok; - - // try notation without starting point - san.from = Point::invalid(); - TRY(san); - - // add column indication - san.from = Point(from.x, -1); - TRY(san); - - // add row indication - san.from = Point(-1, from.y); - TRY(san); - - // add complete starting point - san.from = from; -} -#undef TRY - - -#endif // ALGEBRAICNOTATION_H diff --git a/src/entities/engineentity.cpp b/src/entities/engineentity.cpp index 7bae3bc..4f2d764 100644 --- a/src/entities/engineentity.cpp +++ b/src/entities/engineentity.cpp @@ -10,7 +10,6 @@ #include "engineentity.h" #include "game.h" -#include "algebraicnotation.h" #include "engine.h" #include diff --git a/src/entities/examinationentity.cpp b/src/entities/examinationentity.cpp index 0fd80b4..3c525cd 100644 --- a/src/entities/examinationentity.cpp +++ b/src/entities/examinationentity.cpp @@ -12,9 +12,8 @@ #include "game.h" #include "icsconnection.h" #include "positioninfo.h" -#include "algebraicnotation.h" #include "pgnparser.h" -#include "variants/xchess/piecetype.h" +#include "hlvariant/chess/piece.h" #include "icsapi.h" #include @@ -22,7 +21,7 @@ ExaminationEntity::ExaminationEntity(VariantInfo* variant, const boost::shared_ptr& game, int game_number, const boost::shared_ptr& connection, AgentGroup* group) -: UserEntity(game, QUEEN) +: UserEntity(game, HLVariant::Chess::Piece::QUEEN) , m_variant(variant) , m_icsapi(variant->icsAPI()) , m_game_number(game_number) diff --git a/src/entities/gameentity.cpp b/src/entities/gameentity.cpp index 879ea48..0879150 100644 --- a/src/entities/gameentity.cpp +++ b/src/entities/gameentity.cpp @@ -13,13 +13,13 @@ #include "game.h" #include "board.h" #include "pgnparser.h" -#include "variants/xchess/piecetype.h" +#include "hlvariant/chess/piece.h" using namespace boost; GameEntity::GameEntity(VariantInfo* variant, const boost::shared_ptr& game, Board* chessboard, AgentGroup* group) -: UserEntity(game, QUEEN) +: UserEntity(game, HLVariant::Chess::Piece::QUEEN) , m_variant(variant) , m_chessboard(chessboard) , m_dispatcher(group, this) { diff --git a/src/entities/icsentity.cpp b/src/entities/icsentity.cpp index 8237e62..6ec86ab 100644 --- a/src/entities/icsentity.cpp +++ b/src/entities/icsentity.cpp @@ -10,7 +10,6 @@ #include "icsentity.h" #include "game.h" -#include "algebraicnotation.h" #include "icsconnection.h" #include "positioninfo.h" #include "poolinfo.h" @@ -126,8 +125,6 @@ void ICSEntity::notifyStyle12(const PositionInfo& style12, bool is_starting) { AbstractPosition::Ptr position = m_game->position(style12.index() - 1); if (position) { AbstractMove::Ptr mv = position->getMove(style12.lastMoveSAN); - std::cout << "mv = " << mv->toString(position) << std::endl; - std::cout << "last = " << last_move->toString(position) << std::endl; if (!mv || !mv->equals(last_move)) { std::cout << "[server inconsistency] SAN for last move is different from verbose notation" diff --git a/src/game.cpp b/src/game.cpp index 6bee74a..4e227f3 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -19,7 +19,7 @@ #include #endif #ifndef NO_PGN -#include "variants/variants.h" +#include "variants.h" #include "pgnparser.h" #include "tagua.h" #endif //NO_PGN diff --git a/src/hlvariant/animator.impl.h b/src/hlvariant/animator.impl.h index 4ab6255..a7f66d6 100644 --- a/src/hlvariant/animator.impl.h +++ b/src/hlvariant/animator.impl.h @@ -15,7 +15,6 @@ #include "animationfactory.h" #include "common.h" #include "namedsprite.h" -#include "variants/xchess/piecetype.h" namespace HLVariant { diff --git a/src/icsconnection.cpp b/src/icsconnection.cpp index 3e6bc62..1248b98 100644 --- a/src/icsconnection.cpp +++ b/src/icsconnection.cpp @@ -15,12 +15,11 @@ #include "icsconnection.h" #include "poolinfo.h" #include "positioninfo.h" -#include "algebraicnotation.h" #include "player.h" #include "gameinfo.h" #include "pgnparser.h" #include "icslistener.h" -#include "variants/variants.h" +#include "variants.h" using namespace boost; diff --git a/src/icsgamedata.cpp b/src/icsgamedata.cpp index 006f49e..2df4724 100644 --- a/src/icsgamedata.cpp +++ b/src/icsgamedata.cpp @@ -11,7 +11,7 @@ #include "icsgamedata.h" #include "tagua.h" #include "gameinfo.h" -#include "variants/variants.h" +#include "variants.h" ICSGameData::ICSGameData() : index(0) { diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index f089e50..bc9366b 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -31,9 +31,7 @@ #include "console.h" #include "clock.h" #include "newgame.h" -#include "variants/variants.h" -#include "variants/xchess/piecetype.h" -#include "variants/chess.h" +#include "variants.h" #include "gameinfo.h" #include "controllers/editgame.h" #include "controllers/editposition.h" @@ -362,7 +360,7 @@ void MainWindow::setupGame(const GameInfo* gameInfo, const PositionInfo& style12 void MainWindow::setupExaminedGame(const GameInfo* /*gameInfo*/, const PositionInfo& style12) { shared_ptr controller(new EditGameController( - table(), ChessVariant::info())); + table(), Variant::variant("chess"))); if (controller->setExaminationMode(style12.gameNumber, m_connection)) { table()->setPlayers(Player(style12.whitePlayer, -1), Player(style12.blackPlayer, -1)); diff --git a/src/moveserializer.impl.h b/src/moveserializer.impl.h index 6bfb483..e172f1f 100644 --- a/src/moveserializer.impl.h +++ b/src/moveserializer.impl.h @@ -12,7 +12,6 @@ #define MOVESERIALIZER_IMPL_H #include "moveserializer.h" -#include "algebraicnotation.h" /** * Assume the move has already been tested against @a ref. diff --git a/src/newgame.cpp b/src/newgame.cpp index 504d242..0cc3a4e 100644 --- a/src/newgame.cpp +++ b/src/newgame.cpp @@ -12,7 +12,7 @@ #include #include #include "common.h" -#include "variants/variants.h" +#include "variants.h" #include "tagua.h" #include "newgame.h" diff --git a/src/pgnparser.cpp b/src/pgnparser.cpp index 938039a..9d2e967 100644 --- a/src/pgnparser.cpp +++ b/src/pgnparser.cpp @@ -11,6 +11,7 @@ #include #include #include "pgnparser.h" +#include "common.h" QRegExp PGN::number("^(\\d+)(?:(?:\\.\\s+)?(\\.\\.\\.)|\\.?)?"); QRegExp PGN::begin_var("^\\("); diff --git a/src/pgnparser.h b/src/pgnparser.h index fcca880..49ffdbf 100644 --- a/src/pgnparser.h +++ b/src/pgnparser.h @@ -23,7 +23,6 @@ #include #endif #include -#include "algebraicnotation.h" class QRegExp; diff --git a/src/poolinfo.cpp b/src/poolinfo.cpp index e90d159..221e442 100644 --- a/src/poolinfo.cpp +++ b/src/poolinfo.cpp @@ -10,8 +10,7 @@ #include "poolinfo.h" #include "gameinfo.h" -#include "variants/chess.h" -#include "variants/variants.h" +#include "variants.h" QRegExp PoolInfo::s_pattern("^\\s+game\\s+(\\d+)" //game num "\\s+white\\s+\\[([QKBNRP]*)\\]" // white pieces diff --git a/src/positioninfo.cpp b/src/positioninfo.cpp index 4451ec6..65b050b 100644 --- a/src/positioninfo.cpp +++ b/src/positioninfo.cpp @@ -9,8 +9,7 @@ */ #include "positioninfo.h" -#include "variants/chess.h" -#include "variants/variants.h" +#include "variants.h" #include "gameinfo.h" #include "icsapi.h" @@ -68,7 +67,7 @@ PositionInfo::PositionRow::PositionRow(const ICSAPIPtr& icsapi, const QString& s */ int PositionInfo::index() const { int res = (moveIndex - 1) * 2; - if (turn == BLACK) res++; + if (turn == 1) res++; return res; } @@ -99,13 +98,13 @@ PositionInfo::PositionInfo(const std::map& games, const QStrin moveIndex = pattern.cap(CaptureIndexes::MoveOrdinal).toInt(); whitePlayer = pattern.cap(CaptureIndexes::WhitePlayer); blackPlayer = pattern.cap(CaptureIndexes::BlackPlayer); - turn = pattern.cap(CaptureIndexes::Turn) == "W"? WHITE : BLACK; + turn = pattern.cap(CaptureIndexes::Turn) == "W"? 0 : 1; int ep = pattern.cap(CaptureIndexes::EnPassant).toInt(); if (ep == -1) enPassantSquare = Point::invalid(); else - enPassantSquare = Point(ep, turn == WHITE? 2 : 5); + enPassantSquare = Point(ep, turn == 0? 2 : 5); bool wkCastle = pattern.cap(CaptureIndexes::WhiteKingCastle).toInt() == 1; bool wqCastle = pattern.cap(CaptureIndexes::WhiteQueenCastle).toInt() == 1; diff --git a/src/pref_theme.cpp b/src/pref_theme.cpp index 04cbd2e..8715888 100644 --- a/src/pref_theme.cpp +++ b/src/pref_theme.cpp @@ -17,7 +17,7 @@ #include "foreach.h" #include "mastersettings.h" #include "luaapi/loader.h" -#include "variants/variants.h" +#include "variants.h" #include "tagua.h" #include "pref_theme.h" diff --git a/src/tagua_wrapped.h b/src/tagua_wrapped.h index 919a7e3..9933fc5 100644 --- a/src/tagua_wrapped.h +++ b/src/tagua_wrapped.h @@ -20,7 +20,6 @@ #include "piecefactory.h" #include "moveserializer.h" #include "nopool.h" -#include "algebraicnotation.h" template class WrappedPiece; template class WrappedMove; diff --git a/src/ui.cpp b/src/ui.cpp index b8cf539..e557f46 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -13,7 +13,6 @@ #include #include "ui.h" #include "controllers/abstract.h" -#include "variants/xchess/piecetype.h" using namespace boost; @@ -102,22 +101,6 @@ void UI::pgnPaste(const QString&) { //controller()->loadPGN(pgn); } -void UI::promoteToQueen() { - controller()->setPromotionType(QUEEN); -} - -void UI::promoteToRook() { - controller()->setPromotionType(ROOK); -} - -void UI::promoteToBishop() { - controller()->setPromotionType(BISHOP); -} - -void UI::promoteToKnight() { - controller()->setPromotionType(KNIGHT); -} - int UI::promotionType() const { return controller()->promotionType(); } diff --git a/src/ui.h b/src/ui.h index abbef65..9609389 100644 --- a/src/ui.h +++ b/src/ui.h @@ -53,10 +53,6 @@ public Q_SLOTS: void pgnPaste(const QString&); // promotion type - void promoteToQueen(); - void promoteToRook(); - void promoteToBishop(); - void promoteToKnight(); int promotionType() const; // do promotion diff --git a/src/variants/variants.cpp b/src/variants.cpp similarity index 75% rename from src/variants/variants.cpp rename to src/variants.cpp index 03b02d9..ecbf0a9 100644 --- a/src/variants/variants.cpp +++ b/src/variants.cpp @@ -10,9 +10,6 @@ #include "variants.h" #include "tagua.h" -#include "variants/chess.h" -#include "variants/crazyhouse.h" -#include "variants/shogi.h" #include "hlvariant/chess/variant.h" #include "hlvariant/crazyhouse/variant.h" #include "hlvariant/dummy/variant.h" @@ -41,17 +38,6 @@ void Variant::register_variant() { } void Variant::initialize() { - register_variant(); - register_variant(); - // register_variant(); - // register_variant(); - // register_variant(); - // register_variant(); - // register_variant(); - // register_variant(); - // register_variant(); - register_variant(); - // register_variant(); register_variant(); register_variant(); register_variant(); diff --git a/src/variants/variants.h b/src/variants.h similarity index 100% rename from src/variants/variants.h rename to src/variants.h diff --git a/src/variants/atomic.cpp_ b/src/variants/atomic.cpp_ deleted file mode 100644 index a527304..0000000 --- a/src/variants/atomic.cpp_ +++ /dev/null @@ -1,275 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#include "atomic.h" -#include "highlevel.h" -#include "xchess/position.h" -#include "moveserializer.impl.h" -#include "xchess/animator.impl.h" -#include "graphicalposition.h" -#include "pointconverter.h" -#include "animation.h" - -#define USE_DELAYED_EXPLOSIONS - -using namespace boost; - -class AtomicPosition : public ChessPosition { -public: - typedef ChessPosition::Piece Piece; - AtomicPosition(); - AtomicPosition(const OptList& l); - AtomicPosition(const ChessPosition&); - AtomicPosition(Piece::Color turn, bool wk, bool wq, - bool bk, bool bq, const Point& ep); - virtual AtomicPosition* clone() const { return new AtomicPosition(*this); } - virtual bool attacks(Color, const Point& destination) const; - virtual bool attacks(Color, const Point& destination, Point& source) const; - virtual void move(const ChessMove&); - virtual bool testMove(ChessMove&) const; - virtual bool pseudolegal(ChessMove&) const; - virtual QString type() const { return "atomic"; } -}; - -AtomicPosition::AtomicPosition() { } - -AtomicPosition::AtomicPosition(const OptList&) { } - -AtomicPosition::AtomicPosition(const ChessPosition& other) -: ChessPosition(other) { } - -AtomicPosition::AtomicPosition(Piece::Color turn, bool wk, bool wq, - bool bk, bool bq, const Point& ep) -: ChessPosition(turn, wk, wq, bk, bq, ep) { } - -bool AtomicPosition::attacks(Color color, const Point& destination) const { - Point source; - if (ChessPosition::attacks(color, destination)) { - if (!m_board[destination]) return true; - - // a piece does not attack occupied squares near its own king - Piece king(color, KING); - for (int i = -1; i <= 1; i++) - for (int j = -1; j <= 1; j++) { - Point p = destination + Point(i,j); - if (valid(p) && king == m_board[p]) return false; - } - - return true; - } - else return false; -} - -bool AtomicPosition::attacks(Color color, const Point& destination, Point& source) const { - bool b = ChessPosition::attacks(color, destination, source); - printf("VAL: %d!\n",b); - return b; -} - -void AtomicPosition::move(const ChessMove& mv) { - bool cap = m_board[mv.to]; - ChessPosition::move(mv); - - if (!cap) return; - - for (int i = -1; i <= 1; i++) - for (int j = -1; j <= 1; j++) { - Point p = mv.to + Point(i,j); - if (!valid(p)) continue; - Piece piece = m_board[p]; - if (piece && (piece.type() != PAWN || - (i == 0 && j == 0))) { - m_board[p] = Piece(); - } - } -} - -bool AtomicPosition::pseudolegal(ChessMove& mv) const { - if (!ChessPosition::pseudolegal(mv)) return false; - - // a move that causes the explosion of one's king - // is not pseudolegal - Piece::Color color = m_board[mv.from].color(); - - { std::auto_ptr temp(clone()); - - temp->move(mv); - if (temp->findPiece(color, KING) == Point::invalid()) - { - printf("CUSP!\n"); - return false; - } - } - - return true; -} - -bool AtomicPosition::testMove(ChessMove& move) const { - if (move.status == ChessMove::Untested) { - if (pseudolegal(move)) { - ChessPiece::Color turn = moveTurn(move); - - // check king safety - std::auto_ptr tempPosition(clone()); - std::cout << "[atomic_position] cloned" << std::endl; - tempPosition->move(move); - std::cout << "[atomic_position] move tried" << std::endl; - Point kingPos = tempPosition->findPiece(turn, KING); - std::cout << "[atomic_position] found king: " << kingPos << std::endl; - Point kingPos2 = tempPosition->findPiece(ChessPiece::oppositeColor(turn), KING); - std::cout << "[atomic_position] found king: " << kingPos << std::endl; - - if (kingPos == Point::invalid()) - move.status = ChessMove::Illegal; - else if (kingPos2 == Point::invalid()) - move.status = ChessMove::Legal; - else if (tempPosition->attacks(ChessPiece::oppositeColor(turn), kingPos)) - move.status = ChessMove::Illegal; - else - move.status = ChessMove::Legal; - } - else move.status = ChessMove::Illegal; - } - - return move.status == ChessMove::Legal ? true : false; -} - -#if 0 -class AtomicAnimator : public ChessAnimator { -public: - AtomicAnimator(PointConverter* converter, GraphicalPosition* position) - : ChessAnimator(converter, position) { } - - virtual shared_ptr createCapture( - const Point& point, - const Element& piece, - const Element& captured, - AbstractPosition::Ptr pos) { - if(m_anim_explode) { -#ifdef USE_DELAYED_EXPLOSIONS - shared_ptr res(new DelayedAnimationSet(m_random)); -#else // USE_DELAYED_EXPLOSIONS -#define addAnimation addPreAnimation - AnimationPtr res(new AnimationGroup); -#endif // USE_DELAYED_EXPLOSIONS - - res->addAnimation(ChessAnimator::createCapture(point, piece, captured, pos)); - - if(captured) { - // add explosions - for (int i = -1; i <= 1; i++) - for (int j = -1; j <= 1; j++) { - if (i == 0 && j == 0) continue; - Point p = point + Point(i,j); - Element element = m_position->getElement(p); - if (!pos->get(p) && element) { - m_position->removeElement(p); - res->addAnimation(shared_ptr( - new ExplodeAnimation(element.sprite(), m_random))); - } - } - - // explode the capturing piece too :) - res->addAnimation(shared_ptr( - new ExplodeAnimation(piece.sprite(), m_random))); - } - - return res; - } - else { - AnimationPtr res(new AnimationGroup); - - res->addPreAnimation(ChessAnimator::createCapture(point, piece, captured, pos)); - - if(captured) { - // add explosions - for (int i = -1; i <= 1; i++) - for (int j = -1; j <= 1; j++) { - if (i == 0 && j == 0) continue; - Point p = point + Point(i,j); - Element element = m_position->getElement(p); - if (!pos->get(p) && element) { - m_position->removeElement(p); - res->addPreAnimation( - m_anim_fade - ? shared_ptr(new FadeAnimation(element.sprite(), - m_converter->toReal(p), 255, 0)) - : shared_ptr(new CaptureAnimation(element.sprite())) - ); - } - } - - // explode the capturing piece too :) - res->addPreAnimation( - m_anim_fade - ? shared_ptr(new FadeAnimation(piece.sprite(), - m_converter->toReal(point), 255, 0)) - : shared_ptr(new CaptureAnimation(piece.sprite())) - ); - } - - return res; - } - } - - virtual void finalizeBackAnimation(AnimationPtr animation, - AbstractPosition::Ptr pos, - const ChessMove& move) { - // undo explosions - for (int i = -1; i <= 1; i++) - for (int j = -1; j <= 1; j++) { - if (i == 0 && j == 0) continue; - Point p = move.to + Point(i,j); - Element element = m_position->getElement(p); - if (pos->get(p) && !element) { - QPoint real = m_converter->toReal(p); - animation->addPreAnimation( - m_anim_fade - ? shared_ptr(new FadeAnimation( - m_position->setPiece(p, pos->get(p)), real, 0, 255)) - : shared_ptr(new DropAnimation( - m_position->setPiece(p, pos->get(p)))) - ); - } - } - } -}; -#endif - -class AtomicVariantInfo { -public: - typedef AtomicPosition Position; - typedef Position::Move Move; - typedef Position::Piece Piece; - typedef SimpleAnimator Animator; - static const bool m_simple_moves = false; - static void forallPieces(PieceFunction& f); - static int moveListLayout(){ return 0; } - static const char *m_name; - static const char *m_theme_proxy; - static OptList positionOptions() { return OptList(); } -}; - -const char *AtomicVariantInfo::m_name = "Atomic"; -const char *AtomicVariantInfo::m_theme_proxy = "Chess"; - -void AtomicVariantInfo::forallPieces(PieceFunction& f) { - ChessVariant::forallPieces(f); -} - - -VariantInfo* AtomicVariant::static_atomic_variant = 0; - -VariantInfo* AtomicVariant::info() { - if (!static_atomic_variant) - static_atomic_variant = new WrappedVariantInfo; - return static_atomic_variant; -} - diff --git a/src/variants/atomic.h_ b/src/variants/atomic.h_ deleted file mode 100644 index 5b4a7bf..0000000 --- a/src/variants/atomic.h_ +++ /dev/null @@ -1,23 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#ifndef ATOMICVARIANT_H -#define ATOMICVARIANT_H - -#include "chess.h" - -class AtomicVariant { -private: - static VariantInfo* static_atomic_variant; -public: - static VariantInfo* info(); -}; - -#endif // ATOMICVARIANT_H diff --git a/src/variants/chainreaction.cpp_ b/src/variants/chainreaction.cpp_ deleted file mode 100644 index 1e0e0df..0000000 --- a/src/variants/chainreaction.cpp_ +++ /dev/null @@ -1,479 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#include "chainreaction.h" -#include -#include "xchess/animator.impl.h" -#include "xchess/piece.h" -#include "xchess/move.h" -#include "piecefunction.h" -#include "highlevel.h" -#include "algebraicnotation.h" -#include "graphicalposition.h" -#include "pointconverter.h" -#include "piecegrid.h" - -using namespace boost; - -class ChainReactionPiece { -public: - typedef PieceColor Color; - typedef int Type; -private: - Color m_color; - int m_type; -public: - ChainReactionPiece() - : m_color(static_cast(-1)), m_type(static_cast(-1)) { } - ChainReactionPiece(Color color, Type t) - : m_color(color), m_type(t) { } - Color color() const { return m_color; } - Type type() const { return m_type; } - int id() const { return static_cast(m_color); } - QString name() const { - return (m_color == WHITE ? "white_" - : m_color == BLACK ? "black_" - : m_color == 2 ? "neutral_" : "unknown") - +(m_type>0 ? QString::number(m_type) : QString("add")); } - static Color oppositeColor(Color color) { - return color == WHITE ? BLACK : WHITE; - } - static int getType(const QString&) { return -1; } - static QString typeSymbol(int) { return QString(); } - - bool operator==(const ChainReactionPiece& other) const { - return m_color == other.m_color && m_type == other.m_type; - } - bool operator!=(const ChainReactionPiece& other) const { - return !(*this == other); - } - bool operator<(const ChainReactionPiece& other) const { - return m_color < other.m_color || (m_color == other.m_color && m_type < other.m_type); - } - bool equals(const ChainReactionPiece* other) const { - return other && *this == *other; - } - operator bool() const { - return m_type >= 1 && m_color != -1; - } -}; - -typedef Point ChainReactionMove; - -class ChainReactionPosition { -public: - typedef ChainReactionPiece Piece; - typedef ChainReactionMove Move; - typedef std::map Pool; -private: - Pool m_pool; - Grid m_board; - Piece::Color m_turn; - -public: - static bool explodeStep(const Grid& board, Piece::Color turn, Grid& target); - - ChainReactionPosition(); - ChainReactionPosition(const OptList& l); - ChainReactionPosition(Piece::Color turn, bool wk, bool wq, - bool bk, bool bq, const Point& ep); - - Pool& pool() { return m_pool; } - const Pool& pool() const { return m_pool; } - void addToPool(const Piece&, int) { } - void removeFromPool(const Piece&, int) { } - - Piece::Color turn() const { return m_turn; } - void setTurn(Piece::Color turn) { m_turn = turn; } - void switchTurn(); - Piece::Color previousTurn() const { return Piece::oppositeColor(m_turn); } - - void fromFEN(const QString&, bool& ok) { ok = true;} - QString fen(int,int) const { return QString(); } - - static Move getVerboseMove(int turn, const VerboseNotation& m); - Move getMove(const AlgebraicNotation& m, bool& ok) const; - - bool testMove(Move& p) const; - void move(const Move& p); - - Point size() const { return m_board.getSize(); } - bool valid(const Point& p) const { return m_board.valid(p); } - void setup(); - - const Piece* get(const Point& p) const { - return valid(p) && m_board[p] ? &m_board[p] : 0; - } - Piece* get(const Point& p) { - return valid(p) && m_board[p] ? &m_board[p] : 0; - } - const Piece* operator[](const Point& p) const { return get(p); } - void set(const Point& p, Piece* piece) { - if (!valid(p)) - return; - m_board[p] = piece ? *piece : Piece(); - } - void removePiece(const Point& p) { set(p, 0); } - - bool operator==(const ChainReactionPosition& other) const; - - shared_ptr moveHint(const Move& m) const; - - void dump() const { } - - QStringList borderCoords() const; -}; - -ChainReactionPosition::ChainReactionPosition() -: m_board(9,9) -, m_turn(WHITE) { } - -ChainReactionPosition::ChainReactionPosition(const OptList& l) -: m_board(options_list_find(l, "width", 9), - options_list_find(l, "height", 9)) -, m_turn(WHITE) { } - -ChainReactionPosition::ChainReactionPosition(Piece::Color turn, bool, bool, bool, bool, const Point&) -: m_board(9,9) -, m_turn(turn) { } - -QStringList ChainReactionPosition::borderCoords() const -{ - QStringList retv; - Point p = m_board.getSize(); - for(int i=0; i(2), static_cast(1) ); - } -} - -bool ChainReactionPosition::explodeStep(const Grid& board, Piece::Color turn, Grid& target) { - bool all_this_color = true; - for(Point p = board.first(); p <= board.last(); p = board.next(p)) - if(target[p].color() != turn) { - all_this_color = false; - break; - } - if(all_this_color) - return false; - - target = board; - bool exploding = false; - - for(Point p = board.first(); p <= board.last(); p = board.next(p)) { - int max = 0; - Point pts[] = { p+Point(0,-1), p+Point(0,1), p+Point(1,0), p+Point(-1,0) }; - for(int i=0;i<4;i++) - if(board.valid(pts[i])) - max++; - if(board[p].type()>max) { - exploding = true; - for(int i=0;i<4;i++) - if(board.valid(pts[i])) - target[pts[i]] = Piece(turn, static_cast(target[pts[i]].type()+1)); - target[p] = Piece(turn, static_cast(target[p].type()-max)); - } - } - - return exploding; -} - -void ChainReactionPosition::switchTurn() { - m_turn = Piece::oppositeColor(m_turn); -} - -bool ChainReactionPosition::testMove(Move& p) const { - if (!valid(p) || m_board[p].color()==Piece::oppositeColor(m_turn)) - return false; - - return true; -} - -void ChainReactionPosition::move(const Move& p) { - m_board[p] = Piece(m_turn, static_cast(m_board[p].type()+1)); - Grid tmp = m_board; - while(explodeStep(m_board, m_turn, tmp)) - m_board = tmp; - switchTurn(); -} - -bool ChainReactionPosition::operator==(const ChainReactionPosition& other) const { - return m_board == other.m_board - && m_turn == other.m_turn; -} - -shared_ptr ChainReactionPosition::moveHint(const Move& /*p*/) const { - return shared_ptr(new Piece(m_turn, static_cast(-1))); -} - -#if 0 -//BEGIN ChainReactionAnimator --------------------------------------------------------------------- - -class ChainReactionAnimator { - typedef boost::shared_ptr AnimationPtr; - typedef ChainReactionPiece Piece; - typedef ChainReactionPosition Position; - - PointConverter* m_converter; - GraphicalPosition* m_position; - Random m_random; - - bool m_anim_movement; - bool m_anim_explode; - bool m_anim_fade; - bool m_anim_rotate; -public: - ChainReactionAnimator(PointConverter* converter, GraphicalPosition* position); - AnimationPtr warp(AbstractPosition::Ptr); - AnimationPtr forward(AbstractPosition::Ptr, const ChainReactionMove& move); - AnimationPtr back(AbstractPosition::Ptr, const ChainReactionMove& move); -}; - -ChainReactionAnimator::ChainReactionAnimator(PointConverter* converter, GraphicalPosition* position) -: m_converter(converter) -, m_position(position) -, m_anim_movement(false) -, m_anim_explode(false) -, m_anim_fade(false) -, m_anim_rotate(false) { - if(position->getBoolSetting("animations", true)) { - m_anim_movement = (bool)position->getBoolSetting("animations.movement", true); - m_anim_explode = (bool)position->getBoolSetting("animations.explode", true); - m_anim_fade = (bool)position->getBoolSetting("animations.fading", true); - m_anim_rotate = (bool)position->getBoolSetting("animations.transform", true); - } -} - -ChainReactionAnimator::AnimationPtr ChainReactionAnimator::warp(AbstractPosition::Ptr final) { - AnimationPtr res(new AnimationGroup); - for (Point i = m_position->first(); i <= m_position->last(); i = m_position->next(i)) { - QPoint real = m_converter->toReal(i); - Element p = m_position->getElement(i); - AbstractPiece::Ptr q = final->get(i); - shared_ptr a; - - if (p) { - shared_ptr sprite = p.sprite(); - Q_ASSERT(sprite); - - if (!p.piece()->equals(q)) { - shared_ptr sprite = p.sprite(); - - if (q) { - a = shared_ptr(new PromotionAnimation( sprite, - m_position->setPiece(i, q, false, false) )); - } - else { - // remove it - m_position->removeElement(i); - a = shared_ptr(new CaptureAnimation(sprite)); - } - } - } - else if (q) { - a = shared_ptr(new DropAnimation( m_position->setPiece(i, q, false, false) )); - } - - if (a) res->addPreAnimation(a); - } - - return res; -} - -ChainReactionAnimator::AnimationPtr ChainReactionAnimator::forward(AbstractPosition::Ptr final, - const ChainReactionMove& m) { - - AnimationPtr retv(new AnimationGroup); - AnimationPtr curr = retv; - - Grid board(final->size().x,final->size().y); - for (Point i = m_position->first(); i <= m_position->last(); i = m_position->next(i)) { - Element p = m_position->getElement(i); - if(p.piece()) - board[i] = Piece(static_cast(p.piece()->color()), - static_cast(p.piece()->type())); - } - Grid exploded(board.getSize().x, board.getSize().y); - Grid target = board; - Piece::Color turn = Piece::oppositeColor(static_cast(final->turn())); - target[m] = Piece(turn, static_cast(target[m].type()+1)); - - do { - bool added = false; - - for(Point i = board.first(); i <= board.last(); i = board.next(i)) { - int max = 0; - Point pts[] = { i+Point(0,-1), i+Point(0,1), i+Point(1,0), i+Point(-1,0) }; - for(int j=0;j<4;j++) - if(board.valid(pts[j])) - max++; - exploded[i] = false; - if(board[i].type() > max) { - if(shared_ptr sprite = m_position->getSprite(i)) { - curr->addPreAnimation(shared_ptr( - new ExplodeAnimation(sprite, m_random))); - exploded[i] = true; - } - } - } - -// {AnimationPtr next(new AnimationGroup); -// curr->addPostAnimation(next); -// curr = next;} - - for(Point i = target.first(); i <= target.last(); i = target.next(i)) { - Element p = m_position->getElement(i); - if(!p.piece() || p.piece()->color() != target[i].color() - || p.piece()->type() != target[i].type()) { - if(p.sprite() && !exploded[i]) - curr->addPreAnimation(shared_ptr( - new CaptureAnimation(p.sprite()))); - AbstractPiece::Ptr piece = ChainReactionVariant::info()->createPiece( - target[i].color(), target[i].type() ); - shared_ptr s = m_position->setPiece(i, piece, false, false); - curr->addPreAnimation(shared_ptr( - new DropAnimation(s))); - added = true; - } - } - - board = target; - -#if 1 - AnimationPtr delay(new AnimationGroup); - delay->addPreAnimation(shared_ptr(new DelayAnimation(200))); - curr->addPreAnimation(delay); - curr = delay; -#endif - AnimationPtr next(new AnimationGroup); - curr->addPostAnimation(next); - curr = next; - } - while(Position::explodeStep(board, turn, target)); - - curr->addPostAnimation(warp(final)); - - return retv; -} - -ChainReactionAnimator::AnimationPtr ChainReactionAnimator::back(AbstractPosition::Ptr final, - const ChainReactionMove&) { - return warp(final); -} - -//END ChainReactionAnimator ----------------------------------------------------------------------- - -#endif - -class ChainReactionVariantInfo { -public: - typedef ChainReactionPosition Position; - typedef Position::Move Move; - typedef Position::Piece Piece; - typedef class ChainReactionAnimator Animator; - - static const bool m_simple_moves = true; - static const char *m_name; - static const char *m_theme_proxy; - - static void forallPieces(PieceFunction& f); - static int moveListLayout() { return 0; } - static OptList positionOptions() { return OptList() - << OptPtr(new IntOpt("width", "Board width:", 9, 2, 40)) - << OptPtr(new IntOpt("height", "Board height:", 9, 2, 40)); } -}; - -const char *ChainReactionVariantInfo::m_name = "ChainReaction"; -const char *ChainReactionVariantInfo::m_theme_proxy = "ChainReaction"; - -void ChainReactionVariantInfo::forallPieces(PieceFunction& f) { - f(WHITE, 0); - f(BLACK, 0); - f(WHITE, 1); - f(BLACK, 1); - f(WHITE, 2); - f(BLACK, 2); - f(WHITE, 3); - f(BLACK, 3); - f(WHITE, 4); - f(BLACK, 4); -} - -VariantInfo* ChainReactionVariant::static_chainreaction_variant = 0; - -VariantInfo* ChainReactionVariant::info() { - if (!static_chainreaction_variant) - static_chainreaction_variant = new WrappedVariantInfo; - return static_chainreaction_variant; -} - -template <> -struct MoveFactory { - static ChainReactionMove createNormalMove(const NormalUserMove& move) { - return move.to; - } - static ChainReactionMove createDropMove(const ChainReactionPiece& /*piece*/, const Point& to) { - return to; - } - static NormalUserMove toNormal(const ChainReactionMove& m) { - return NormalUserMove(Point::invalid(), m); - } -}; - -template <> -class MoveSerializer : public AbstractMoveSerializer { - ChainReactionMove m_move; - const ChainReactionPosition& m_ref; -public: - MoveSerializer(const ChainReactionMove& m, const ChainReactionPosition& ref) - : m_move(m), m_ref(ref) { } - - DecoratedMove toDecoratedMove() const { - return DecoratedMove() << SAN(); - } - - virtual QString SAN() const { - return m_move.toString(m_ref.size().y); - } -}; - -class ChainReactionAnimator : public SimpleAnimator { - typedef SimpleAnimator Base; - typedef Base::Position Position; - typedef Base::Move Move; - typedef Base::GPosition GPosition; -public: - ChainReactionAnimator(PointConverter* converter, const boost::shared_ptr& position) - : Base(converter, position) { } - - AnimationPtr forward(const Position& final, const Move&) { - return warp(final); - } - - AnimationPtr back(const Position& final, const Move&) { - return warp(final); - } -}; diff --git a/src/variants/chainreaction.h_ b/src/variants/chainreaction.h_ deleted file mode 100644 index b088d6c..0000000 --- a/src/variants/chainreaction.h_ +++ /dev/null @@ -1,23 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#ifndef VARIANTS__CHAINREACTION_H -#define VARIANTS__CHAINREACTION_H - -#include "tagua.h" - -class ChainReactionVariant { -private: - static VariantInfo* static_chainreaction_variant; -public: - static VariantInfo* info(); -}; - -#endif //VARIANTS__CHAINREACTION_H diff --git a/src/variants/chess.cpp b/src/variants/chess.cpp deleted file mode 100644 index bab064e..0000000 --- a/src/variants/chess.cpp +++ /dev/null @@ -1,226 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#include -#include -#include "variants/chess.h" -#include "common.h" -#include "tagua_wrapped.h" -#include "icsapi.impl.h" -#include "moveserializer.impl.h" -#include "xchess/animator.impl.h" -#include "piecefunction.h" -#include "animation.h" - -using namespace boost; -typedef boost::shared_ptr AnimationPtr; - -const char *ChessVariant::m_name = "Chess_OLD"; -const char *ChessVariant::m_theme_proxy = "Chess"; -VariantInfo* ChessVariant::static_chess_variant = 0; - -#if 0 -class ChessAnimator { - ChessGraphicalAPI::Ptr m_cinterface; -public: - ChessAnimator(ChessGraphicalAPI::Ptr cinterface) - : m_cinterface(cinterface) { - } - - AnimationGroupPtr warp(const ChessPosition& final) { - const ChessPosition* current = m_cinterface->position(); - AnimationFactory res(m_cinterface->inner()); - - for (Point i = current->first(); i <= current->last(); i = current->next(i)) { - ChessPiece c = current->get(i); - ChessPiece f = final.get(i); - - if( !c && f ) { - //current->set(i, f); - NamedSprite sprite = m_cinterface->setPiece(i, f, false); - res.addPreAnimation(Animate::appear(sprite), Animate::Instant); - } - else if (c && !f) { - NamedSprite old_sprite = m_cinterface->takeSprite(i); - res.addPreAnimation(Animate::disappear(old_sprite), Animate::Instant); - } - else if(c && f && !(c == f) ) { - NamedSprite old_sprite = m_cinterface->takeSprite(i); - NamedSprite sprite = m_cinterface->setPiece(i, f, false); - res.addPreAnimation(Animate::morph(old_sprite, sprite), Animate::Instant); - } - } - - //BROKEN: implement pool update - - - return res; - } - - boost::shared_ptr forward(const ChessPosition& final, const ChessMove& move) { - AnimationFactory res(m_cinterface->inner()); - - NamedSprite piece = m_cinterface->takeSprite(move.from); - NamedSprite captured = m_cinterface->takeSprite(move.to); - m_cinterface->setSprite(move.to, piece); - - if (piece) { - bool knight = m_cinterface->position()->get(move.from).type() == KNIGHT; - int mtype = knight - ? Animate::move::LShaped | Animate::move::Rotating - : Animate::move::Straight; - res.addPreAnimation(Animate::move(piece, move.to, mtype)); - } - else - ERROR("Bug!!!"); - - if (captured) - res.addPostAnimation(Animate::destroy(captured)); - - if (move.type() == ChessMove::EnPassantCapture) { - Point phantom(move.to.x, move.from.y); - NamedSprite capturedPawn = m_cinterface->takeSprite(phantom); - - if (capturedPawn) { - QPoint real = m_cinterface->converter()->toReal(phantom); - res.addPostAnimation(Animate::disappear(capturedPawn)); - } - else - ERROR("Bug!!!"); - } - else if (move.type() == ChessMove::Promotion) { - ChessPiece promoted = final.get(move.to); - - if (promoted) { - QPoint real = m_cinterface->converter()->toReal(move.to); - NamedSprite old_sprite = m_cinterface->getSprite(move.to); - NamedSprite new_sprite = m_cinterface->setPiece(move.to, promoted, /*false,*/ false); - - res.addPostAnimation(Animate::morph(old_sprite, new_sprite)); - } - else - ERROR("Bug!!!"); - } - else if (move.type() == ChessMove::KingSideCastling) { - Point rookSquare = move.to + Point(1,0); - Point rookDestination = move.from + Point(1,0); - - NamedSprite rook = m_cinterface->takeSprite(rookSquare); - m_cinterface->setSprite(rookDestination, rook); - res.addPreAnimation(Animate::move(rook, rookDestination)); - } - else if (move.type() == ChessMove::QueenSideCastling) { - Point rookSquare = move.to + Point(-2,0); - Point rookDestination = move.from + Point(-1,0); - - NamedSprite rook = m_cinterface->takeSprite(rookSquare); - m_cinterface->setSprite(rookDestination, rook); - res.addPreAnimation(Animate::move(rook, rookDestination)); - } - - return res; - } - - boost::shared_ptr back(const ChessPosition& final, const ChessMove& move) { - AnimationFactory res(m_cinterface->inner()); - - NamedSprite piece = m_cinterface->takeSprite(move.to); - NamedSprite captured; - if (ChessPiece captured_piece = final.get(move.to)) { - captured = m_cinterface->setPiece(move.to, captured_piece, false); - res.addPreAnimation(Animate::appear(captured)); - } - - if (!piece) { - piece = m_cinterface->createPiece(move.to, final.get(move.from), false); - res.addPreAnimation(Animate::appear(piece)); - } - - m_cinterface->setSprite(move.from, piece); - - - if (move.type() == ChessMove::EnPassantCapture) { - Point phantom(move.to.x, move.from.y); - - if (ChessPiece pawn_piece = final.get(phantom)) { - NamedSprite captured_pawn = m_cinterface->setPiece(phantom, pawn_piece, false); - res.addPreAnimation(Animate::appear(captured_pawn)); - } - } - else if (move.type() == ChessMove::Promotion) { - ChessPiece pawn_piece = final.get(move.from); - if (pawn_piece) { - NamedSprite pawn = m_cinterface->createPiece(move.to, pawn_piece, false); - res.addPreAnimation(Animate::morph(piece, pawn)); - // replace piece with pawn - m_cinterface->setSprite(move.from, pawn); - piece = pawn; - } - } - else if (move.type() == ChessMove::KingSideCastling) { - Point rookSquare = move.to + Point(1,0); - Point rookDestination = move.from + Point(1,0); - - NamedSprite rook = m_cinterface->takeSprite(rookDestination); - m_cinterface->setSprite(rookSquare, rook); - - res.addPreAnimation(Animate::move(rook, rookSquare)); - } - else if (move.type() == ChessMove::QueenSideCastling) { - Point rookSquare = move.to + Point(-2,0); - Point rookDestination = move.from + Point(-1,0); - - NamedSprite rook = m_cinterface->takeSprite(rookDestination); - m_cinterface->setSprite(rookSquare, rook); - - res.addPreAnimation(Animate::move(rook, rookSquare)); - } - - { - bool knight = m_cinterface->position()->get(move.to).type() == KNIGHT; - int mtype = knight - ? Animate::move::LShaped | Animate::move::Rotating - : Animate::move::Straight; - res.addPreAnimation(Animate::move(piece, move.from, mtype)); - } - return res; - } -}; -#endif - -void ChessVariant::forallPieces(PieceFunction& f) { - f(WHITE, KING); - f(WHITE, QUEEN); - f(WHITE, ROOK); - f(WHITE, BISHOP); - f(WHITE, KNIGHT); - f(WHITE, PAWN); - f(BLACK, KING); - f(BLACK, QUEEN); - f(BLACK, ROOK); - f(BLACK, BISHOP); - f(BLACK, KNIGHT); - f(BLACK, PAWN); -} - -VariantInfo* ChessVariant::info() { - if (!static_chess_variant) - static_chess_variant = new WrappedVariantInfo; - return static_chess_variant; -} - -// piece factory -template <> -class PieceFactory { -public: - static ChessPiece createPiece(const QString& description) { - return ChessPiece::fromDescription(description); - } -}; diff --git a/src/variants/chess.h b/src/variants/chess.h deleted file mode 100644 index 635be70..0000000 --- a/src/variants/chess.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#ifndef CHESS_H -#define CHESS_H - -#include "variants/xchess/position.h" -#include "variants/xchess/animator.h" -#include "variants/xchess/piece.h" -#include "variants/xchess/move.h" -#include "option.h" -#include "nopool.h" -#include "piecegrid.h" - -class ChessPiece; -class ChessMove; -class VariantInfo; -template class PointerGrid; -template class Position; -typedef Grid PieceGrid; -typedef Position ChessPosition; - -struct ChessVariant { - typedef ChessPosition Position; - typedef Position::Move Move; - typedef Position::Piece Piece; - typedef SimpleAnimator Animator; - typedef NoPool Pool; - - static const bool hasICS = true; - static const bool m_simple_moves = false; - static const char *m_name; - static const char *m_theme_proxy; - static void forallPieces(class PieceFunction& f); - static int moveListLayout(){ return 0; } - - static VariantInfo* static_chess_variant; - static VariantInfo* info(); - static OptList positionOptions() { return OptList(); } -}; - - -#endif // CHESS_H diff --git a/src/variants/connect4.cpp_ b/src/variants/connect4.cpp_ deleted file mode 100644 index bc8cac1..0000000 --- a/src/variants/connect4.cpp_ +++ /dev/null @@ -1,248 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#include "connect4.h" -#include -#include "xchess/animator.h" -#include "xchess/piece.h" -#include "xchess/move.h" -#include "piecefunction.h" -#include "highlevel.h" -#include "algebraicnotation.h" -#include "graphicalposition.h" -#include "pointconverter.h" -#include "piecegrid.h" -#include "xchess/animator.impl.h" -#include "trivialanimator.h" - -using namespace boost; - -class Connect4Piece { -public: - typedef PieceColor Color; - typedef int Type; -private: - Color m_color; -public: - Connect4Piece(Color color, Type) - : m_color(color) { } - Connect4Piece(Color color) - : m_color(color) { } - Color color() const { return m_color; } - Type type() const { return -1; } - int id() const { return static_cast(m_color); } - QString name() const { return m_color == WHITE ? "white_stone" - : m_color == BLACK ? "black_stone" : "unknown"; } - static Color oppositeColor(Color color) { - return color == WHITE ? BLACK : WHITE; - } - static int getType(const QString&) { return -1; } - static QString typeSymbol(int) { return ""; } - - bool operator==(const Connect4Piece& other) const { - return m_color == other.m_color; - } - bool operator<(const Connect4Piece& other) const { - return m_color < other.m_color; - } - bool equals(const Connect4Piece* other) const { - return other && *this == *other; - } -}; - -typedef Point Connect4Move; - -class Connect4Position { -public: - typedef Connect4Piece Piece; - typedef Connect4Move Move; - typedef std::map Pool; -private: - Pool m_pool; - PointerGrid m_board; - Piece::Color m_turn; - -public: - Connect4Position(); - Connect4Position(const OptList&); - Connect4Position(Piece::Color turn, bool wk, bool wq, - bool bk, bool bq, const Point& ep); - - Pool& pool() { return m_pool; } - const Pool& pool() const { return m_pool; } - void addToPool(const Piece&, int) { } - void removeFromPool(const Piece&, int) { } - - Piece::Color turn() const { return m_turn; } - void setTurn(Piece::Color turn) { m_turn = turn; } - void switchTurn(); - Piece::Color previousTurn() const { return Piece::oppositeColor(m_turn); } - - void fromFEN(const QString&, bool& ok) { ok = true;} - QString fen(int,int) const { return ""; } - - static Move getVerboseMove(int turn, const VerboseNotation& m); - Move getMove(const AlgebraicNotation& m, bool& ok) const; - - bool testMove(Move& p) const; - void move(const Move& p); - - Point size() const { return m_board.getSize(); } - bool valid(const Point& p) const { return m_board.valid(p); } - void setup(); - - const Piece* get(const Point& p) const { - return valid(p) ? m_board[p] : 0; - } - Piece* get(const Point& p) { - return valid(p) ? m_board[p] : 0; - } - const Piece* operator[](const Point& p) const { return get(p); } - void set(const Point& p, Piece* piece) { - if (!valid(p)) - return; - delete m_board[p]; - m_board[p] = piece; - } - void removePiece(const Point& p) { set(p, 0); } - - bool operator==(const Connect4Position& other) const; - - shared_ptr moveHint(const Move& m) const; - void dump() const { } - QStringList borderCoords() const; -}; - -Connect4Position::Connect4Position() -: m_board(7,7) -, m_turn(WHITE) { } - -Connect4Position::Connect4Position(const OptList&) -: m_board(7,7) -, m_turn(WHITE) { } - -Connect4Position::Connect4Position(Piece::Color turn, bool, bool, bool, bool, const Point&) -: m_board(7,7) -, m_turn(turn) { } - -QStringList Connect4Position::borderCoords() const -{ - QStringList retv; - Point p = m_board.getSize(); - for(int i=0; i=1;i--) - if(!m_board[Point(x,i)]) { - set(Point(x,i), new Piece(m_turn)); - switchTurn(); - return; - } -} - -bool Connect4Position::operator==(const Connect4Position& other) const { - return m_board == other.m_board - && m_turn == other.m_turn; -} - -shared_ptr Connect4Position::moveHint(const Connect4Move& /*m*/) const { - return shared_ptr(new Piece(m_turn)); -} - -class Connect4VariantInfo { -public: - typedef Connect4Position Position; - typedef Position::Move Move; - typedef Position::Piece Piece; - typedef TrivialAnimator Animator; - - static const bool m_simple_moves = true; - static const char *m_name; - static const char *m_theme_proxy; - - static void forallPieces(PieceFunction& f); - static int moveListLayout() { return 0; } - static OptList positionOptions() { return OptList(); } -}; - -const char *Connect4VariantInfo::m_name = "Connect4"; -const char *Connect4VariantInfo::m_theme_proxy = "Connect4"; - -void Connect4VariantInfo::forallPieces(PieceFunction& f) { - f(WHITE, -1); - f(BLACK, -1); -} - -VariantInfo* Connect4Variant::static_connect4_variant = 0; - -VariantInfo* Connect4Variant::info() { - if (!static_connect4_variant) - static_connect4_variant = new WrappedVariantInfo; - return static_connect4_variant; -} - -template <> -struct MoveFactory { - static Connect4Move createNormalMove(const NormalUserMove& move) { - return move.to; - } - static Connect4Move createDropMove(const Connect4Piece& /*piece*/, const Point& to) { - return to; - } - static NormalUserMove toNormal(const Connect4Move& m) { - return NormalUserMove(Point::invalid(), m); - } -}; - -template <> -class MoveSerializer : public AbstractMoveSerializer { - Connect4Move m_move; - const Connect4Position& m_ref; -public: - MoveSerializer(const Connect4Move& m, const Connect4Position& ref) - : m_move(m), m_ref(ref) { } - - DecoratedMove toDecoratedMove() const { - return DecoratedMove() << SAN(); - } - - virtual QString SAN() const { - return m_move.toString(m_ref.size().y); - } -}; - diff --git a/src/variants/connect4.h_ b/src/variants/connect4.h_ deleted file mode 100644 index ae743b9..0000000 --- a/src/variants/connect4.h_ +++ /dev/null @@ -1,23 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#ifndef VARIANTS__CONNECT4_H -#define VARIANTS__CONNECT4_H - -#include "tagua.h" - -class Connect4Variant { -private: - static VariantInfo* static_connect4_variant; -public: - static VariantInfo* info(); -}; - -#endif //VARIANTS__CONNECT4_H diff --git a/src/variants/crazyhouse.cpp b/src/variants/crazyhouse.cpp deleted file mode 100644 index 1333c7c..0000000 --- a/src/variants/crazyhouse.cpp +++ /dev/null @@ -1,276 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#include "crazyhouse_p.h" -#include "crazyhouse.h" -#include "tagua_wrapped.h" -#include "icsapi.impl.h" -#include "xchess/dropanimator.impl.h" -#include "animation.h" -#include "animationfactory.h" -#include "moveserializer.impl.h" - -class CrazyhouseVariantInfo { -public: - typedef CrazyhousePosition Position; - typedef Position::Move Move; - typedef Position::Piece Piece; - typedef Position::PoolReference Pool; - - typedef DropAnimatorMixin > Animator; - - static const bool hasICS = true; - static const bool m_simple_moves = false; - static void forallPieces(PieceFunction& f); - static int moveListLayout(){ return 0; } - static const char *m_name; - static const char *m_theme_proxy; - static OptList positionOptions() { return OptList(); } -}; - -//BEGIN CrazyhousePiece - -CrazyhousePiece::CrazyhousePiece(ChessPiece::Color color, ChessPiece::Type type, bool promoted) -: ChessPiece(color, type) -, m_promoted(promoted) { } - -CrazyhousePiece::CrazyhousePiece(const CrazyhousePiece& other) -: ChessPiece(other) -, m_promoted(other.m_promoted) { } - -CrazyhousePiece::CrazyhousePiece(const ChessPiece& other) -: ChessPiece(other) -, m_promoted(false) { } - -//END CrazyhousePiece - -//BEGIN CrazyhouseMove - -CrazyhouseMove::CrazyhouseMove(const ChessMove& m) -: ChessMove(m) -, m_drop(INVALID_COLOR, INVALID_TYPE) -, m_pool(-1) -, m_pool_index(-1) { } - -CrazyhouseMove::CrazyhouseMove(const Point& from, const Point& to, PieceType promotionType) -: ChessMove(from, to, promotionType) -, m_drop(INVALID_COLOR, INVALID_TYPE) -, m_pool(-1) -, m_pool_index(-1) { } - -CrazyhouseMove::CrazyhouseMove(const CrazyhouseMove& other) -: ChessMove(other) -, m_drop(other.m_drop) -, m_pool(other.m_pool) -, m_pool_index(other.m_pool_index) { } - -CrazyhouseMove::CrazyhouseMove(const CrazyhousePiece& pc, const Point& to) -: ChessMove(Point::invalid(), to) -, m_drop(pc) -, m_pool(-1) -, m_pool_index(-1) { } - -CrazyhouseMove::CrazyhouseMove(int pool, int pool_index, const Point& to) -: ChessMove(Point::invalid(), to) -, m_drop(INVALID_COLOR, INVALID_TYPE) -, m_pool(pool) -, m_pool_index(pool_index) { } - -//END CrazyhouseMove - -//BEGIN CrazyhousePosition - -CrazyhousePosition::CrazyhousePosition() { -} - -CrazyhousePosition::CrazyhousePosition(const OptList&) { -} - -CrazyhousePosition::CrazyhousePosition(const CrazyhousePosition& other) -: Base(other) -, m_pool(other.m_pool) { -} - -CrazyhousePosition::CrazyhousePosition(const ChessPosition& other) -: Base(other) { -} - -CrazyhousePosition::CrazyhousePosition(CrazyhousePiece::Color turn, bool wk, bool wq, - bool bk, bool bq, const Point& ep) -: Base(turn, wk, wq, bk, bq, ep) { -} - -CrazyhousePosition* CrazyhousePosition::clone() const { - return new CrazyhousePosition(*this); -} - -CrazyhousePiece::Color CrazyhousePosition::moveTurn(const Move& move) const { - if (move.drop()) - return move.drop().color(); - else - return Base::moveTurn(move); -} - -boost::shared_ptr > -CrazyhousePosition::createLegalGenerator() const { - return boost::shared_ptr >( - new MoveGenerator >(*this)); -} - -CrazyhousePosition::PoolReference CrazyhousePosition::pool(int index) { - Color c = static_cast(index); - return PoolReference(&m_pool[c], c); -} - -CrazyhousePosition::PoolConstReference CrazyhousePosition::pool(int index) const { - Color c = static_cast(index); - return PoolConstReference(&m_pool[c], c); -} - -CrazyhousePosition::PlayerPool& CrazyhousePosition::rawPool(Piece::Color color) { - return m_pool[color]; -} - -const CrazyhousePosition::PlayerPool& CrazyhousePosition::rawPool(Piece::Color color) const { - return m_pool[color]; -} - -bool CrazyhousePosition::pseudolegal(Move& move) const { - - if (!move.drop() && move.pool() != -1 && move.poolIndex() != -1) { - move.setDrop(pool(move.pool()).get(move.poolIndex())); - dump(); - std::cout << move.drop() << " " << move.pool() << " " << move.poolIndex() << " " << - pool(move.pool()).size() << std::endl; - } - - if (move.drop()) { - Q_ASSERT(valid(move.to)); - - // cannot drop on occupied squares - if (m_board[move.to]) return false; - - // cannot drop pawns in first or eighth rank - if (move.drop().type() == PAWN && - (move.to.y == 0 || move.to.y == 7)) - return false; - - return true; - } - // normal move - else - return Base::pseudolegal(move); -} - -void CrazyhousePosition::move(const Move& move) { - // drop - if (move.drop()) { - Q_ASSERT(m_pool[move.drop().color()].count(move.drop().type())); - Q_ASSERT(!m_board[move.to]); - - basicDropPiece(new Piece(move.drop()), move.to); - if(!--m_pool[move.drop().color()][move.drop().type()]) - m_pool[move.drop().color()].erase(move.drop().type()); - } - else { - // normal move - Base::move(move); - - // set promoted flag - if (move.type() == Move::Promotion) { - m_board[move.to].setPromoted(true); - } - } - -#if 0 - for(Pool::iterator i = m_pool.begin(); i != m_pool.end(); ++i) - std::cout << i->first.color() << "." << i->first.type() << " " << i->second <second.begin(); i != j->second.end(); ++i) - std::cout << j->first << "." << i->first << " " << i->second < - - -// piece factory -template <> -class PieceFactory { -public: - static CrazyhousePiece createPiece(const QString& description) { - return ChessPiece::fromDescription(description); - } -}; - -//END PieceFactory - - -//BEGIN CrazyhouseVariant - - -const char *CrazyhouseVariantInfo::m_name = "Crazyhouse_OLD"; -const char *CrazyhouseVariantInfo::m_theme_proxy = "Chess"; - -VariantInfo* CrazyhouseVariant::static_crazyhouse_variant = 0; - -void CrazyhouseVariantInfo::forallPieces(PieceFunction& f) { - return ChessVariant::forallPieces(f); -} - -VariantInfo* CrazyhouseVariant::info() { - if (!static_crazyhouse_variant) - static_crazyhouse_variant = new WrappedVariantInfo; - return static_crazyhouse_variant; -} - -//END CrazyhouseVariant diff --git a/src/variants/crazyhouse.h b/src/variants/crazyhouse.h deleted file mode 100644 index c37ad2e..0000000 --- a/src/variants/crazyhouse.h +++ /dev/null @@ -1,24 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#ifndef CRAZYHOUSE_H -#define CRAZYHOUSE_H - -#include "variants/variants.h" - -class CrazyhouseVariant { -private: - static VariantInfo* static_crazyhouse_variant; - -public: - static VariantInfo* info(); -}; - -#endif // CRAZYHOUSE_H diff --git a/src/variants/crazyhouse_p.h b/src/variants/crazyhouse_p.h deleted file mode 100644 index abd63ca..0000000 --- a/src/variants/crazyhouse_p.h +++ /dev/null @@ -1,169 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#ifndef CRAZYHOUSE_P_H -#define CRAZYHOUSE_P_H - -#include -#include "chess.h" -#include "xchess/pool.h" - -class CrazyhousePiece : public ChessPiece { - bool m_promoted; - -public: - CrazyhousePiece(ChessPiece::Color = INVALID_COLOR, ChessPiece::Type = INVALID_TYPE, - bool promoted = false); - CrazyhousePiece(const CrazyhousePiece&); - CrazyhousePiece(const ChessPiece&); - - inline bool promoted() const { return m_promoted; } - inline void setPromoted(bool value) { m_promoted = value; } - - bool operator<(const CrazyhousePiece& p) const { - return m_promoted != p.m_promoted ? - m_promoted < p.m_promoted : - this->ChessPiece::operator<(p); - } -}; - -class CrazyhouseMove : public ChessMove { - CrazyhousePiece m_drop; - int m_pool; - int m_pool_index; -public: - CrazyhouseMove(const ChessMove& move); - CrazyhouseMove(const Point& from, const Point& to, PieceType promotionType = INVALID_TYPE); - CrazyhouseMove(const CrazyhousePiece& p, const Point& to); - CrazyhouseMove(int pool, int m_pool_index, const Point& to); - CrazyhouseMove(const CrazyhouseMove&); - - QString toString(int xsize, int ysize) const { - if(m_drop.valid()) - return CrazyhousePiece::typeSymbol(m_drop.type()) + "@" + to.toString(ysize); - return ChessMove::toString(xsize, ysize); - } - static CrazyhouseMove createDropMove(int pool, int m_pool_index, const Point& to) { - return CrazyhouseMove(pool, m_pool_index, to); - } - - CrazyhousePiece drop() const { return m_drop; } - void setDrop(const CrazyhousePiece& piece) { m_drop = piece; } - int pool() const { return m_pool; } - int poolIndex() const { return m_pool_index; } -}; - -class CrazyhousePosition : public Position > { -public: - typedef CrazyhouseMove Move; - typedef CrazyhousePiece Piece; - typedef Position > Base; - typedef PoolReference PoolReference; - typedef PoolConstReference PoolConstReference; - typedef PoolReference::Pool Pool; - typedef PoolReference::PlayerPool PlayerPool; - - CrazyhousePosition(); - CrazyhousePosition(const OptList& l); - CrazyhousePosition(const CrazyhousePosition&); - CrazyhousePosition(const ChessPosition&); - CrazyhousePosition(CrazyhousePiece::Color turn, bool wk, bool wq, - bool bk, bool bq, const Point& ep); - virtual CrazyhousePosition* clone() const; - - - Pool m_pool; -public: - virtual CrazyhousePiece::Color moveTurn(const Move&) const; - virtual bool pseudolegal(Move&) const; - virtual void move(const Move&); - virtual void executeCaptureOn(const Point& point); - virtual boost::shared_ptr > createLegalGenerator() const; - - virtual Move getMove(const AlgebraicNotation& san, bool& ok) const; - - virtual bool operator==(const CrazyhousePosition& other) const; - - static Move getVerboseMove(Color turn, const VerboseNotation& m) { - Move retv = ChessPosition::getVerboseMove(turn, m); - if(retv.from == Point::invalid()) - retv.setDrop(CrazyhousePiece(turn, static_cast(m.type))); - else - retv.setDrop(CrazyhousePiece(INVALID_COLOR, INVALID_TYPE)); - return retv; - } - - void dump() const; - - PoolReference pool(int index); - PoolConstReference pool(int index) const; - - PlayerPool& rawPool(Piece::Color color); - const PlayerPool& rawPool(Piece::Color color) const; - - void setRawPool(const Pool& p) { m_pool = p; } - Pool& rawPool() { return m_pool; } - const Pool& rawPool() const { return m_pool; } -}; - -template -class MoveGenerator - : public Generator { - typedef Generator Base; - using Base::m_pos; - using Base::m_test; - using Base::m_moves; -public: - MoveGenerator(const CrazyhousePosition& pos) - : Base(pos) { } - - std::vector& generate() { - generateDrops(); - return Base::generate(); - } - -private: - void generateDrops() { - const CrazyhousePosition::PlayerPool& pp = m_pos.rawPool()[m_pos.turn()]; - for (CrazyhousePosition::PlayerPool::const_iterator it = pp.begin(); - it != pp.end(); ++it) { - for (Point to = m_pos.first(); - to <= m_pos.last(); - to = m_pos.next(to)) { - CrazyhouseMove move(CrazyhousePiece(m_pos.turn(),it->first), to); - if (m_test(move)) m_moves.push_back(move); - } - } - } -}; - -template <> -class MoveSerializer : public MoveSerializerBase { - typedef CrazyhousePosition Position; - typedef CrazyhouseMove Move; - typedef CrazyhousePiece Piece; - typedef MoveSerializerBase Base; -public: - MoveSerializer(const Move& move, const Position& ref) - : MoveSerializerBase(move, ref) { } - - QString SAN() const { - if (m_move.drop()) { - - return QString("%1@%2") - .arg(CrazyhousePiece::typeSymbol(m_move.drop().type())) - .arg(m_move.to.toString(m_ref.size().y)) + checkSuffix(); - } - else - return Base::SAN(); - } -}; - -#endif // CRAZYHOUSE_P_H diff --git a/src/variants/dummy.cpp_ b/src/variants/dummy.cpp_ deleted file mode 100644 index 0bdb344..0000000 --- a/src/variants/dummy.cpp_ +++ /dev/null @@ -1,175 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#include "dummy.h" -#include "piecesprite.h" -#include "crazyhouse_p.h" -#include "xchess/animator.impl.h" - -typedef CrazyhousePiece DummyPiece; -typedef CrazyhouseMove DummyMove; - -//BEGIN DummyPosition - -class DummyPosition : public CrazyhousePosition { -public: - friend class MoveSerializer; - typedef DummyMove Move; - typedef DummyPiece Piece; - typedef CrazyhousePosition Base; - typedef std::map Pool; - - DummyPosition(); - DummyPosition(const OptList& l); - DummyPosition(const DummyPosition&); - DummyPosition(const ChessPosition&); - DummyPosition(DummyPiece::Color turn, bool wk, bool wq, - bool bk, bool bq, const Point& ep); - virtual DummyPosition* clone() const; - -public: - virtual bool pseudolegal(Move&) const; - virtual bool testMove(Move&) const; - virtual void move(const Move&); - virtual void executeCaptureOn(const Point& point) { - this->CrazyhousePosition::Base::executeCaptureOn(point); - } - - virtual bool operator==(const DummyPosition& other) const; - - static Move getVerboseMove(Color turn, const VerboseNotation& m) { - Move retv = ChessPosition::getVerboseMove(turn, m); - if(retv.from == Point::invalid()) - retv.m_drop = DummyPiece(turn, static_cast(m.type) ); - else - retv.m_drop = DummyPiece(INVALID_COLOR, INVALID_TYPE); - return retv; - } -}; - -DummyPosition::DummyPosition() { -} - -DummyPosition::DummyPosition(const OptList&) { -} - -DummyPosition::DummyPosition(const DummyPosition& other) -: Base(other) { -} - -DummyPosition::DummyPosition(const ChessPosition& other) -: Base(other) { -} - -DummyPosition::DummyPosition(DummyPiece::Color turn, bool wk, bool wq, - bool bk, bool bq, const Point& ep) -: Base(turn, wk, wq, bk, bq, ep) { -} - -DummyPosition* DummyPosition::clone() const { - return new DummyPosition(*this); -} - -bool DummyPosition::pseudolegal(Move& move) const { - if (move.m_drop.valid()) - return valid(move.to); - else - return valid(move.from) && valid(move.to); -} - -bool DummyPosition::testMove(Move& move) const { - if (move.status == Move::Untested) - move.status = pseudolegal(move) ? Move::Legal : Move::Illegal; - return move.status == Move::Legal; -} - -void DummyPosition::move(const Move& move) { - if (move.m_drop.valid()) { - basicDropPiece(new Piece(move.m_drop), move.to); - if(--m_pool[move.m_drop] <= 0) - m_pool.erase(move.m_drop); - } - else { - executeCaptureOn(move.to); - basicMovePiece(move); - switchTurn(); - } -} - -bool DummyPosition::operator==(const DummyPosition& pos) const { - return pool() == pos.pool() && Base::operator==(pos); -} - -//END DummyPosition - - -//BEGIN DummyVariant - - -#include "highlevel.h" -#include "moveserializer.impl.h" - -template <> -class MoveSerializer : public AbstractMoveSerializer { - const DummyMove& m_move; - const DummyPosition& m_ref; -public: - MoveSerializer(const DummyMove& m, const DummyPosition& r) - : m_move(m), m_ref(r) { } - - DecoratedMove toDecoratedMove() const { - return DecoratedMove() << SAN(); - } - - virtual QString SAN() const { - if(m_move.m_drop.valid()) - return CrazyhousePiece::typeSymbol(m_move.m_drop.type()) + "@" + m_move.to.toString(8); - else { - QString res = m_move.from.toString(m_ref.size().y) + m_move.to.toString(m_ref.size().y); - if (m_move.m_type == DummyMove::Promotion) - res = res + "=" + m_move.promotionSymbol(); - if(m_ref.m_board.valid(m_move.from) && m_ref.m_board[m_move.from] - && m_ref.get(m_move.from)->type() != PAWN) - res = DummyPiece::typeSymbol(m_ref.get(m_move.from)->type()) + res; - return res; - } - } -}; - -class DummyVariantInfo { -public: - typedef DummyPosition Position; - typedef Position::Move Move; - typedef Position::Piece Piece; - typedef SimpleAnimator Animator; - static const bool m_simple_moves = false; - static void forallPieces(PieceFunction& f); - static int moveListLayout(){ return 0; } - static const char *m_name; - static const char *m_theme_proxy; - static OptList positionOptions() { return OptList(); } -}; - -const char *DummyVariantInfo::m_name = "Dummy"; -const char *DummyVariantInfo::m_theme_proxy = "Chess"; - -VariantInfo* DummyVariant::static_dummy_variant = 0; - -void DummyVariantInfo::forallPieces(PieceFunction& f) { - return ChessVariant::forallPieces(f); -} - -VariantInfo* DummyVariant::info() { - if (!static_dummy_variant) - static_dummy_variant = new WrappedVariantInfo; - return static_dummy_variant; -} - -//END DummyVariant diff --git a/src/variants/dummy.h_ b/src/variants/dummy.h_ deleted file mode 100644 index 4ef766d..0000000 --- a/src/variants/dummy.h_ +++ /dev/null @@ -1,24 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#ifndef VARIANTS__DUMMY_H -#define VARIANTS__DUMMY_H - -class VariantInfo; - -class DummyVariant { -private: - static VariantInfo* static_dummy_variant; - -public: - static VariantInfo* info(); -}; - -#endif // VARIANTS__DUMMY_H diff --git a/src/variants/king4pawns.cpp_ b/src/variants/king4pawns.cpp_ deleted file mode 100644 index d576b57..0000000 --- a/src/variants/king4pawns.cpp_ +++ /dev/null @@ -1,111 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#include "king4pawns.h" -#include "tagua.h" -#include "variants/chess.h" -#include "variants/xchess/position.h" -#include "moveserializer.impl.h" -#include "xchess/animator.impl.h" -#include "highlevel.h" - -class King4PawnsPosition : public ChessPosition { - int m_move_ordinal; -public: - King4PawnsPosition(); - King4PawnsPosition(const OptList& l); - King4PawnsPosition(const ChessPosition&); - King4PawnsPosition(Piece::Color turn, bool wk, bool wq, - bool bk, bool bq, const Point& ep); - virtual King4PawnsPosition* clone() const { return new King4PawnsPosition(*this); } - -#define SET_PIECE(i,j, color, type) m_board[Point(i,j)] = Piece(color, type) - virtual void setup() { - // 4 pawns - for (int i = 2; i <= 5; i++) - SET_PIECE(i, 6, WHITE, PAWN); - - // 1 king - SET_PIECE(4,7, WHITE, KING); - - // all black's army - for (int i = 0; i < 8; i++) - SET_PIECE(i, 1, BLACK, PAWN); - - SET_PIECE(0,0, BLACK, ROOK); - SET_PIECE(1,0, BLACK, KNIGHT); - SET_PIECE(2,0, BLACK, BISHOP); - SET_PIECE(3,0, BLACK, QUEEN); - SET_PIECE(4,0, BLACK, KING); - SET_PIECE(5,0, BLACK, BISHOP); - SET_PIECE(6,0, BLACK, KNIGHT); - SET_PIECE(7,0, BLACK, ROOK); - } -#undef SET_PIECE - - virtual void switchTurn() { - if (turn() == WHITE && m_move_ordinal == 0) - m_move_ordinal++; - else { - ChessPosition::switchTurn(); - m_move_ordinal = 0; - } - } - - virtual void setTurn(Piece::Color turn) { - ChessPosition::setTurn(turn); - m_move_ordinal = 0; - } - - virtual bool testMove(Move& move) const { - return pseudolegal(move); - } -}; - -King4PawnsPosition::King4PawnsPosition() -: m_move_ordinal(0) { } -King4PawnsPosition::King4PawnsPosition(const OptList&) -: m_move_ordinal(0) { } -King4PawnsPosition::King4PawnsPosition(const ChessPosition& other) -: ChessPosition(other) -, m_move_ordinal(0) { } -King4PawnsPosition::King4PawnsPosition(Piece::Color turn, bool wk, bool wq, - bool bk, bool bq, const Point& ep) -: ChessPosition(turn, wk, wq, bk, bq, ep) -, m_move_ordinal(0) { } - -class King4PawnsVariantInfo { -public: - typedef King4PawnsPosition Position; - typedef Position::Piece Piece; - typedef Position::Move Move; - typedef SimpleAnimator Animator; - static const bool m_simple_moves = false; - static void forallPieces(PieceFunction& f); - static int moveListLayout(){ return 2; } - static const char *m_name; - static const char *m_theme_proxy; - static OptList positionOptions() { return OptList(); } -}; - -const char *King4PawnsVariantInfo::m_name = "King4Pawns"; -const char *King4PawnsVariantInfo::m_theme_proxy = "Chess"; - -void King4PawnsVariantInfo::forallPieces(PieceFunction& f) { - ChessVariant::forallPieces(f); -} - -VariantInfo* King4PawnsVariant::static_variant_info = 0; - -VariantInfo* King4PawnsVariant::info() { - if (!static_variant_info) - static_variant_info = new WrappedVariantInfo; - return static_variant_info; -} diff --git a/src/variants/king4pawns.h_ b/src/variants/king4pawns.h_ deleted file mode 100644 index e8a2d13..0000000 --- a/src/variants/king4pawns.h_ +++ /dev/null @@ -1,22 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#ifndef KING4PAWNS_H -#define KING4PAWNS_H - -class VariantInfo; - -class King4PawnsVariant { - static VariantInfo* static_variant_info; -public: - static VariantInfo* info(); -}; - -#endif // KING4PAWNS_H diff --git a/src/variants/progressive.cpp_ b/src/variants/progressive.cpp_ deleted file mode 100644 index ec14b39..0000000 --- a/src/variants/progressive.cpp_ +++ /dev/null @@ -1,90 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#include "variants/progressive.h" -#include "tagua.h" -#include "variants/chess.h" -#include "variants/xchess/position.h" -#include "moveserializer.impl.h" -#include "xchess/animator.impl.h" -#include "highlevel.h" - -class ProgressivePosition : public ChessPosition { - int m_progression; - int m_move_ordinal; -public: - ProgressivePosition(); - ProgressivePosition(const OptList& l); - ProgressivePosition(const ChessPosition&); - ProgressivePosition(Piece::Color turn, bool wk, bool wq, - bool bk, bool bq, const Point& ep); - virtual ProgressivePosition* clone() const { return new ProgressivePosition(*this); } - - virtual void switchTurn() { - m_move_ordinal++; - - if(m_move_ordinal >= m_progression || check(Piece::oppositeColor(turn()))) { - m_progression++; - m_move_ordinal = 0; - ChessPosition::switchTurn(); - } - } - - virtual void setTurn(Piece::Color turn) { - ChessPosition::setTurn(turn); - m_progression = 1; - m_move_ordinal = 0; - } -}; - -ProgressivePosition::ProgressivePosition() -: m_progression(1) -, m_move_ordinal(0) { } -ProgressivePosition::ProgressivePosition(const OptList& /*l*/) -: m_progression(1) -, m_move_ordinal(0) { } -ProgressivePosition::ProgressivePosition(const ChessPosition& other) -: ChessPosition(other) -, m_progression(1) -, m_move_ordinal(0) { } -ProgressivePosition::ProgressivePosition(Piece::Color turn, bool wk, bool wq, - bool bk, bool bq, const Point& ep) -: ChessPosition(turn, wk, wq, bk, bq, ep) -, m_progression(1) -, m_move_ordinal(0) { } - -class ProgressiveVariantInfo { -public: - typedef ProgressivePosition Position; - typedef Position::Piece Piece; - typedef Position::Move Move; - typedef SimpleAnimator Animator; - static const bool m_simple_moves = false; - static void forallPieces(PieceFunction& f); - static int moveListLayout(){ return 4; } - static const char *m_name; - static const char *m_theme_proxy; - static OptList positionOptions() { return OptList(); } -}; - -const char *ProgressiveVariantInfo::m_name = "Progressive"; -const char *ProgressiveVariantInfo::m_theme_proxy = "Chess"; - -void ProgressiveVariantInfo::forallPieces(PieceFunction& f) { - ChessVariant::forallPieces(f); -} - -VariantInfo* ProgressiveVariant::static_variant_info = 0; - -VariantInfo* ProgressiveVariant::info() { - if (!static_variant_info) - static_variant_info = new WrappedVariantInfo; - return static_variant_info; -} diff --git a/src/variants/progressive.h_ b/src/variants/progressive.h_ deleted file mode 100644 index f81e7f5..0000000 --- a/src/variants/progressive.h_ +++ /dev/null @@ -1,22 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#ifndef PROGRESSIVE_H -#define PROGRESSIVE_H - -class VariantInfo; - -class ProgressiveVariant { - static VariantInfo* static_variant_info; -public: - static VariantInfo* info(); -}; - -#endif // PROGRESSIVE_H diff --git a/src/variants/reversi.cpp_ b/src/variants/reversi.cpp_ deleted file mode 100644 index f60c74b..0000000 --- a/src/variants/reversi.cpp_ +++ /dev/null @@ -1,410 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#include "reversi.h" -#include -#include "xchess/animator.impl.h" -#include "xchess/piece.h" -#include "xchess/move.h" -#include "piecefunction.h" -#include "highlevel.h" -#include "algebraicnotation.h" -#include "graphicalposition.h" -#include "pointconverter.h" -#include "piecegrid.h" - -using namespace boost; - -class ReversiPiece { -public: - typedef PieceColor Color; - typedef int Type; -private: - Color m_color; -public: - ReversiPiece(Color color, Type) - : m_color(color) { } - ReversiPiece(Color color) - : m_color(color) { } - Color color() const { return m_color; } - Type type() const { return -1; } - int id() const { return static_cast(m_color); } - QString name() const { return m_color == WHITE ? "white_stone" - : m_color == BLACK ? "black_stone" : "unknown"; } - static Color oppositeColor(Color color) { - return color == WHITE ? BLACK : WHITE; - } - static int getType(const QString&) { return -1; } - static QString typeSymbol(int) { return ""; } - - bool operator==(const ReversiPiece& other) const { - return m_color == other.m_color; - } - bool operator<(const ReversiPiece& other) const { - return m_color < other.m_color; - } - bool equals(const ReversiPiece* other) const { - return other && *this == *other; - } -}; - -typedef Point ReversiMove; - -class ReversiPosition { -public: - typedef ReversiPiece Piece; - typedef ReversiMove Move; - typedef std::map Pool; -private: - Pool m_pool; - PointerGrid m_board; - Piece::Color m_turn; - - int checkDirection(const Point& p, const Point& dir) const; - void flipDirection(Point p, const Point& dir); - void flip(const Point& p); -public: - ReversiPosition(); - ReversiPosition(const OptList& l); - ReversiPosition(Piece::Color turn, bool wk, bool wq, - bool bk, bool bq, const Point& ep); - - Pool& pool() { return m_pool; } - const Pool& pool() const { return m_pool; } - void addToPool(const Piece&, int) { } - void removeFromPool(const Piece&, int) { } - - Piece::Color turn() const { return m_turn; } - void setTurn(Piece::Color turn) { m_turn = turn; } - void switchTurn(); - Piece::Color previousTurn() const { return Piece::oppositeColor(m_turn); } - - void fromFEN(const QString&, bool& ok) { ok = true;} - QString fen(int,int) const { return ""; } - - static Move getVerboseMove(int turn, const VerboseNotation& m); - Move getMove(const AlgebraicNotation& m, bool& ok) const; - - bool testMove(Move& p) const; - void move(const Move& p); - - Point size() const { return m_board.getSize(); } - bool valid(const Point& p) const { return m_board.valid(p); } - void setup(); - - const Piece* get(const Point& p) const { - return valid(p) ? m_board[p] : 0; - } - Piece* get(const Point& p) { - return valid(p) ? m_board[p] : 0; - } - const Piece* operator[](const Point& p) const { return get(p); } - void set(const Point& p, Piece* piece) { - if (!valid(p)) - return; - delete m_board[p]; - m_board[p] = piece; - } - void removePiece(const Point& p) { set(p, 0); } - - bool operator==(const ReversiPosition& other) const; - - shared_ptr moveHint(const Move& m) const; - void dump() const { } - QStringList borderCoords() const; -}; - -ReversiPosition::ReversiPosition() -: m_board(8,8) -, m_turn(WHITE) { } - -ReversiPosition::ReversiPosition(const OptList&) -: m_board(8,8) -, m_turn(WHITE) { } - -ReversiPosition::ReversiPosition(Piece::Color turn, bool, bool, bool, bool, const Point&) -: m_board(8,8) -, m_turn(turn) { } - -QStringList ReversiPosition::borderCoords() const -{ - QStringList retv; - Point p = m_board.getSize(); - for(int i=0; icolor() != m_turn) - n++; - else return n; - pos += dir; - } - - return 0; -} - -void ReversiPosition::flipDirection(Point p, const Point& dir) { - const int n = checkDirection(p, dir); - for (int i = 0; i < n; i++) - flip(p += dir); -} - -void ReversiPosition::flip(const Point& p) { - Q_ASSERT(m_board[p]); - set(p, new Piece(Piece::oppositeColor(m_board[p]->color()))); -} - -bool ReversiPosition::testMove(Move& p) const { - if (!valid(p) || m_board[p]) return false; - - for (int i = -1; i <= 1; i++) - for (int j = -1; j <= 1; j++) { - if (i == 0 && j == 0) continue; - if (checkDirection(p, Point(i, j)) > 0) return true; - } - - return false; -} - -void ReversiPosition::move(const Move& p) { - set(p, new Piece(m_turn)); - - for (int i = -1; i <= 1; i++) - for (int j = -1; j <= 1; j++) { - if (i == 0 && j == 0) continue; - flipDirection(p, Point(i, j)); - } - - switchTurn(); -} - -bool ReversiPosition::operator==(const ReversiPosition& other) const { - return m_board == other.m_board - && m_turn == other.m_turn; -} - -shared_ptr ReversiPosition::moveHint(const ReversiMove& /*m*/) const { - return shared_ptr(new Piece(m_turn)); -} - - -//BEGIN ReversiAnimator --------------------------------------------------------------------- - -#if 0 - -class ReversiAnimator { - typedef boost::shared_ptr AnimationPtr; - - - PointConverter* m_converter; - GraphicalPosition* m_position; - Random m_random; - - bool m_anim_movement; - bool m_anim_explode; - bool m_anim_fade; - bool m_anim_rotate; -public: - ReversiAnimator(PointConverter* converter, GraphicalPosition* position); - AnimationPtr warp(AbstractPosition::Ptr); - AnimationPtr forward(AbstractPosition::Ptr, const ReversiMove& move); - AnimationPtr back(AbstractPosition::Ptr, const ReversiMove& move); -}; - -ReversiAnimator::ReversiAnimator(PointConverter* converter, GraphicalPosition* position) -: m_converter(converter) -, m_position(position) -, m_anim_movement(false) -, m_anim_explode(false) -, m_anim_fade(false) -, m_anim_rotate(false) { - if (position->getBoolSetting("animations", true)) { - m_anim_movement = (bool)position->getBoolSetting("animations.movement", true); - m_anim_explode = (bool)position->getBoolSetting("animations.explode", true); - m_anim_fade = (bool)position->getBoolSetting("animations.fading", true); - m_anim_rotate = (bool)position->getBoolSetting("animations.transform", true); - } -} - -ReversiAnimator::AnimationPtr ReversiAnimator::warp(AbstractPosition::Ptr final) { - AnimationPtr res(new AnimationGroup); - for (Point i = m_position->first(); i <= m_position->last(); i = m_position->next(i)) { - QPoint real = m_converter->toReal(i); - Element p = m_position->getElement(i); - AbstractPiece::Ptr q = final->get(i); - shared_ptr a; - - if (p) { - shared_ptr sprite = p.sprite(); - Q_ASSERT(sprite); - - if (!p.piece()->equals(q)) { - shared_ptr sprite = p.sprite(); - - if (q) { - if((m_anim_explode||m_anim_fade) && m_anim_rotate) { - shared_ptr new_sprite = m_position->setPiece(i, q, false, false); - shared_ptr g(new AnimationGroup); - g->addPreAnimation(shared_ptr(new GrowAnimation(new_sprite))); - if(0)/*m_anim_explode)*/ - g->addPreAnimation(shared_ptr(new ExplodeAnimation(sprite, m_random))); - else - g->addPreAnimation(shared_ptr(new FadeAnimation(sprite, real, 255, 0))); - a = g; - } - else - a = shared_ptr(new PromotionAnimation( sprite, - m_position->setPiece(i, q, false, false) )); - } - else { - // remove it - m_position->removeElement(i); - if(m_anim_fade) - a = shared_ptr(new FadeAnimation(sprite, real, 255, 0)); - else - a = shared_ptr(new CaptureAnimation(sprite)); - } - } - } - else if (q) { - a = shared_ptr(new DropAnimation( m_position->setPiece(i, q, false, false) )); - } - - if (a) res->addPreAnimation(a); - } - - return res; -} - -ReversiAnimator::AnimationPtr ReversiAnimator::forward(AbstractPosition::Ptr final, const ReversiMove&) { - return warp(final); -} - -ReversiAnimator::AnimationPtr ReversiAnimator::back(AbstractPosition::Ptr final, const ReversiMove&) { - return warp(final); -} - -#endif - -//END ReversiAnimator ----------------------------------------------------------------------- - -class ReversiVariantInfo { -public: - typedef ReversiPosition Position; - typedef Position::Move Move; - typedef Position::Piece Piece; - typedef class ReversiAnimator Animator; - - static const bool m_simple_moves = true; - static const char *m_name; - static const char *m_theme_proxy; - - static void forallPieces(PieceFunction& f); - static int moveListLayout() { return 0; } - static OptList positionOptions() { return OptList(); } -}; - -const char *ReversiVariantInfo::m_name = "Reversi"; -const char *ReversiVariantInfo::m_theme_proxy = "Reversi"; - -void ReversiVariantInfo::forallPieces(PieceFunction& f) { - f(WHITE, -1); - f(BLACK, -1); -} - -VariantInfo* ReversiVariant::static_reversi_variant = 0; - -VariantInfo* ReversiVariant::info() { - if (!static_reversi_variant) - static_reversi_variant = new WrappedVariantInfo; - return static_reversi_variant; -} - -template <> -struct MoveFactory { - static ReversiMove createNormalMove(const NormalUserMove& move) { - return move.to; - } - static ReversiMove createDropMove(const ReversiPiece& /*p*/, const Point& to) { - return to; - } - static NormalUserMove toNormal(const ReversiMove& m) { - return NormalUserMove(Point::invalid(), m); - } -}; - -template <> -class MoveSerializer : public AbstractMoveSerializer { - ReversiMove m_move; - const ReversiPosition& m_ref; -public: - MoveSerializer(const ReversiMove& m, const ReversiPosition& ref) - : m_move(m), m_ref(ref) { } - - DecoratedMove toDecoratedMove() const { - return DecoratedMove() << SAN(); - } - - virtual QString SAN() const { - return m_move.toString(m_ref.size().y); - } -}; - -class ReversiAnimator : public SimpleAnimator { - typedef SimpleAnimator Base; - typedef Base::Position Position; - typedef Base::Move Move; - typedef Base::GPosition GPosition; -public: - ReversiAnimator(PointConverter* converter, const boost::shared_ptr& position) - : Base(converter, position) { } - - AnimationPtr forward(const Position& final, const Move&) { - return warp(final); - } - - AnimationPtr back(const Position& final, const Move&) { - return warp(final); - } -}; - diff --git a/src/variants/reversi.h_ b/src/variants/reversi.h_ deleted file mode 100644 index 13ce3a5..0000000 --- a/src/variants/reversi.h_ +++ /dev/null @@ -1,23 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#ifndef REVERSIVARIANT_H -#define REVERSIVARIANT_H - -#include "tagua.h" - -class ReversiVariant { -private: - static VariantInfo* static_reversi_variant; -public: - static VariantInfo* info(); -}; - -#endif // REVERSIVARIANT_H diff --git a/src/variants/rubyvariant.cpp_ b/src/variants/rubyvariant.cpp_ deleted file mode 100644 index a3368db..0000000 --- a/src/variants/rubyvariant.cpp_ +++ /dev/null @@ -1,569 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - - -#include "rubyutils.h" -#include "rubyvariant.h" -#include "tagua.h" -#include "xchess/animator.h" -#include "xchess/piece.h" -#include "xchess/move.h" -#include "highlevel.h" -#include "moveserializer.impl.h" -#include "crazyhouse_p.h" -#include "piecefunction.h" - -class RubyPosition; - -int value2int(VALUE val) { - if (rb_respond_to(val, rb_intern("to_i"))) - return NUM2INT(rb_funcall(val, rb_intern("to_i"), 0)); - else - return static_cast(SYM2ID(val)); -} - -class RubyPiece : public AbstractPiece { - VALUE m_piece; -public: - RubyPiece(VALUE); - - VALUE inner() const { return m_piece; } - - virtual bool equals(AbstractPiece::Ptr other) const; - virtual bool less(AbstractPiece::Ptr other) const; - virtual QString name() const; - virtual AbstractPiece::Ptr clone() const; - virtual int color() const; - virtual int type() const; -}; - -class RubyMove : public AbstractMove { - VALUE m_move; -public: - RubyMove(VALUE move) : m_move(move) { } - - VALUE inner() const { return m_move; } - - virtual QString SAN(AbstractPosition::Ptr ref) const; - virtual DecoratedMove toDecoratedMove(AbstractPosition::Ptr) const; - virtual QString toString(AbstractPosition::Ptr) const; - virtual NormalUserMove toUserMove() const; - virtual bool equals(AbstractMove::Ptr) const; -}; - -class RubyPool { -public: - RubyPool(VALUE); -}; - -class RubyVariantInfo : public VariantInfo { - VALUE m_type; - VALUE m_color; - VALUE m_piece; - VALUE m_move; - VALUE m_position; - - QString m_name; -public: - VALUE typeValue(int type); - VALUE colorValue(int type); - - virtual AbstractPosition::Ptr createPosition(); - virtual AbstractPosition::Ptr createCustomPosition(const OptList& l); - - virtual AbstractPosition::Ptr createPositionFromFEN(const QString& fen); - - virtual AbstractPosition::Ptr createChessboard(int turn, - bool wk, bool wq, bool bk, bool bq, - const Point& ep); - - virtual AbstractPiece::Ptr createPiece(int color, int type); - virtual void forallPieces(class PieceFunction& f); - virtual int moveListLayout() const; - virtual QStringList borderCoords() const; - - virtual AbstractAnimator::Ptr createAnimator(PointConverter* converter, - GraphicalPosition* position); - virtual AbstractMove::Ptr createNormalMove(const NormalUserMove& move); - virtual AbstractMove::Ptr createDropMove(const DropUserMove& move); - - virtual AbstractMove::Ptr getVerboseMove(int turn, const VerboseNotation& m) const; - virtual int type(const QString& str); - virtual QString typeSymbol(int type); - virtual bool simpleMoves(); - virtual QString name() const; - virtual QString themeProxy() const; - virtual OptList positionOptions() const; -}; - -class RubyAnimator : public AbstractAnimator { - typedef boost::shared_ptr AnimationPtr; -public: - RubyAnimator() { } - virtual AnimationPtr warp(AbstractPosition::Ptr) { return AnimationPtr(); } - - virtual AnimationPtr forward(AbstractPosition::Ptr, AbstractMove::Ptr) { return AnimationPtr(); } - virtual AnimationPtr back(AbstractPosition::Ptr, AbstractMove::Ptr) { return AnimationPtr(); } -}; - -static VALUE pool_set_helper(VALUE arg, VALUE _pool) { - AbstractPosition::AbstractPool* pool; - Data_Get_Struct(_pool, AbstractPosition::AbstractPool, pool); - VALUE piece = rb_ary_entry(arg, 0); - int count = NUM2INT(rb_ary_entry(arg, 1)); - (*pool)[AbstractPiece::Ptr(new RubyPiece(piece))] = count; - - return Qnil; -} - -static VALUE execute_piece_function(VALUE arg, VALUE functor) { - PieceFunction* f; - Data_Get_Struct(functor, PieceFunction, f); - RubyPiece piece(arg); - (*f)(piece.color(), piece.type()); - return Qnil; -} - - -RubyPiece::RubyPiece(VALUE piece) -: m_piece(piece) { } - -bool RubyPiece::equals(AbstractPiece::Ptr _other) const { - if (!_other) return false; - RubyPiece* other = dynamic_cast(_other.get()); - - if (other) - return RTEST(rb_funcall(m_piece, rb_intern("=="), 1, other->inner())); - else { - MISMATCH(*_other.get(),RubyPiece); - return false; - } -} - -bool RubyPiece::less(AbstractPiece::Ptr _other) const { - if (!_other) return false; - RubyPiece* other = dynamic_cast(_other.get()); - - if (other) - return RTEST(rb_funcall(m_piece, rb_intern("<="), 1, other->inner())); - else { - MISMATCH(*_other.get(),RubyPiece); - return false; - } -} - -AbstractPiece::Ptr RubyPiece::clone() const { - VALUE cl = rb_funcall(m_piece, rb_intern("clone"), 0); - return AbstractPiece::Ptr(new RubyPiece(cl)); -} - -QString RubyPiece::name() const { - VALUE name_value = rb_funcall(m_piece, rb_intern("to_s"), 0); - return StringValuePtr(name_value); -} - -int RubyPiece::color() const { - VALUE color_value = rb_funcall(m_piece, rb_intern("color"), 0); - return value2int(color_value); -} - -int RubyPiece::type() const { - if (rb_respond_to(m_piece, rb_intern("type"))) { - VALUE type_value = rb_funcall(m_piece, rb_intern("type"), 0); - return value2int(type_value); - } - else return -1; -} - -class RubyPosition : public AbstractPosition { - typedef RubyMove Move; - typedef RubyPiece Piece; - - VALUE m_pos; - RubyVariantInfo* m_variant; -public: - VALUE& inner() { return m_pos; } - const VALUE& inner() const { return m_pos; } - - RubyPosition(const VALUE& pos, RubyVariantInfo* variant) - : m_pos(pos) - , m_variant(variant) { } - - virtual QString variant() const { - return m_variant->name(); - } - - virtual Point size() const { - VALUE res = rb_funcall(m_pos, rb_intern("size"), 0); - return Ruby::value2point(res); - } - - virtual PoolPtr pool() const { - AbstractPool* p = new AbstractPool; - VALUE pool = rb_funcall(m_pos, rb_intern("pool"), 0); - VALUE c_pool = Data_Wrap_Struct(rb_cObject, 0, 0, p); - rb_iterate(rb_each, pool, (VALUE(*)(...))pool_set_helper, c_pool); - return PoolPtr(p); - } - - virtual void addToPool(AbstractPiece::Ptr _piece, int n) { - RubyPiece* piece = dynamic_cast(_piece.get()); - VALUE pool = rb_funcall(m_pos, rb_intern("pool"), 0); - - if (piece) - rb_funcall(pool, rb_intern("[]="), 2, piece->inner(), INT2FIX(n)); - else - MISMATCH(*_piece.get(),RubyPiece); - } - - virtual void removeFromPool(AbstractPiece::Ptr _piece, int n) { - RubyPiece* piece = dynamic_cast(_piece.get()); - VALUE pool = rb_funcall(m_pos, rb_intern("pool"), 0); - - if (piece) { - VALUE count_value = rb_funcall(pool, rb_intern("[]"), 1, piece->inner()); - int count = 0; - if (RTEST(count_value)) - count = NUM2INT(count_value) - n; - if (count <= 0) - rb_funcall(pool, rb_intern("delete"), 1, piece->inner()); - else - rb_funcall(pool, rb_intern("[]="), 2, piece->inner(), count); - } - else - MISMATCH(*_piece.get(),RubyPiece); - } - - virtual void copyPoolFrom(AbstractPosition::Ptr _other) { - RubyPosition* other = dynamic_cast(_other.get()); - - if (other) { - VALUE other_pool = rb_funcall(other->inner(), rb_intern("pool"), 0); - rb_funcall(m_pos, rb_intern("pool="), 1, other_pool); - } - else - MISMATCH(*_other.get(), RubyPosition); - } - - virtual void setPool(PoolPtr new_pool) { - VALUE pool = rb_funcall(m_pos, rb_intern("pool"), 0); - rb_funcall(pool, rb_intern("clear"), 0); - - for (AbstractPool::iterator it = new_pool->begin(); it != new_pool->end(); ++it) { - RubyPiece* piece = dynamic_cast(it->first.get()); - - if (piece) - rb_funcall(pool, rb_intern("[]="), 2, piece->inner(), NUM2INT(it->second)); - else { - MISMATCH(*it->first.get(), RubyPiece); - return; - } - } - } - - virtual AbstractPiece::Ptr get(const Point& p) const { - VALUE piece = rb_funcall(m_pos, rb_intern("[]"), 1, Ruby::point2value(p)); - if (piece) - return AbstractPiece::Ptr(new RubyPiece(piece)); - else - return AbstractPiece::Ptr(); - } - - virtual void set(const Point& p, AbstractPiece::Ptr _piece) { - if (!_piece) - rb_funcall(m_pos, rb_intern("[]="), 2, Ruby::point2value(p), Qnil); - else { - RubyPiece* piece = dynamic_cast(_piece.get()); - - if (piece) - rb_funcall(m_pos, rb_intern("[]="), 2, Ruby::point2value(p), piece->inner()); - else - MISMATCH(*_piece.get(),RubyPiece); - } - } - - virtual int turn() const { - VALUE turn_value = rb_funcall(m_pos, rb_intern("turn"), 0); - return value2int(turn_value); - } - - virtual void setTurn(int turn) { - VALUE turn_value = m_variant->colorValue(turn); - rb_funcall(m_pos, rb_intern("turn="), 1, turn_value); - } - - virtual int previousTurn() const { - VALUE turn_value = rb_funcall(m_pos, rb_intern("previous_turn"), 0); - return value2int(turn_value); - } - - virtual void switchTurn() { - rb_funcall(m_pos, rb_intern("switch_turn"), 0); - } - - virtual bool testMove(AbstractMove::Ptr _move) const { - RubyMove* move = dynamic_cast(_move.get()); - - if (move) - return RTEST(rb_funcall(m_pos, rb_intern("test_move"), 1, move->inner())); - else { - MISMATCH(*_move.get(),RubyMove); - return false; - } - } - - virtual void move(AbstractMove::Ptr _move) { - RubyMove* move = dynamic_cast(_move.get()); - - if (move) - rb_funcall(m_pos, rb_intern("move"), 1, move->inner()); - else - MISMATCH(*_move.get(),RubyMove); - } - - virtual void setup() { - rb_funcall(m_pos, rb_intern("setup"), 0); - } - - virtual AbstractPosition::Ptr clone() const { - VALUE cl = rb_funcall(m_pos, rb_intern("clone"), 0); - return AbstractPosition::Ptr(new RubyPosition(cl, m_variant)); - } - - virtual bool equal(AbstractPosition::Ptr _other) const { - RubyPosition* other = dynamic_cast(_other.get()); - - if(other) - return RTEST(rb_funcall(m_pos, rb_intern("=="), 1, other->inner())); - else { - MISMATCH(*_other.get(),RubyPosition); - return false; - } - } - - virtual AbstractMove::Ptr getMove(const AlgebraicNotation&) const { - return AbstractMove::Ptr(); - } - - virtual AbstractMove::Ptr getMove(const QString&) const { - return AbstractMove::Ptr(); - } - - virtual QString state() const { - return ""; // TODO - } - - virtual QStringList borderCoords() const { - return QStringList(); //TODO - } - - virtual QString fen(int /*halfmove*/, int /*fullmove*/) const { - return ""; - } - - virtual AbstractPiece::Ptr moveHint(AbstractMove::Ptr) const { - // TODO - return AbstractPiece::Ptr(); - } - - virtual void dump() const { - if (rb_respond_to(m_pos, rb_intern("to_s"))) { - VALUE dumped_value = rb_funcall(m_pos, rb_intern("to_s"), 0); - const char* dumped = StringValuePtr(dumped_value); - std::cout << wrap_cptr(dumped) << std::endl; - } - } -}; - - -QString RubyMove::SAN(AbstractPosition::Ptr ref) const { - if (RubyPosition* pos = dynamic_cast(ref.get())) { - VALUE res = rb_funcall(m_move, rb_intern("san"), 1, pos->inner()); - return StringValuePtr(res); - } - else { - MISMATCH(*ref.get(), RubyPosition); - return ""; - } -} - -DecoratedMove RubyMove::toDecoratedMove(AbstractPosition::Ptr ref) const { - // TODO - DecoratedMove res; - QString san = SAN(ref); - res.push_back(MovePart(san)); - return res; -} - -QString RubyMove::toString(AbstractPosition::Ptr ref) const { - if (RubyPosition* pos = dynamic_cast(ref.get())) { - VALUE res = rb_funcall(m_move, rb_intern("to_s"), 1, pos->inner()); - return StringValuePtr(res); - } - else { - MISMATCH(*ref.get(), RubyPosition); - return ""; - } -} - -NormalUserMove RubyMove::toUserMove() const { - VALUE res = rb_funcall(m_move, rb_intern("to_user"), 0); - Point from = Ruby::value2point(rb_funcall(res, rb_intern("from"), 0)); - Point to = Ruby::value2point(rb_funcall(res, rb_intern("to"), 0)); - return NormalUserMove(from, to); -} - -bool RubyMove::equals(AbstractMove::Ptr other) const { - if (RubyMove* mv = dynamic_cast(other.get())) { - return RTEST(rb_funcall(m_move, rb_intern("=="), 1, mv->inner())); - } - else { - MISMATCH(*other, RubyMove); - return false; - } -} - - -VALUE RubyVariantInfo::typeValue(int type) { - if (rb_respond_to(m_type, rb_intern("from_i"))) - return rb_funcall(m_type, rb_intern("from_i"), 1, type); - else - return ID2SYM(static_cast(type)); -} - -VALUE RubyVariantInfo::colorValue(int color) { - if (rb_respond_to(m_type, rb_intern("from_i"))) - return rb_funcall(m_type, rb_intern("from_i"), 1, color); - else - return ID2SYM(static_cast(color)); -} - -AbstractPosition::Ptr RubyVariantInfo::createPosition() { - VALUE pos = rb_funcall(m_position, rb_intern("new"), 0); - return AbstractPosition::Ptr( - new RubyPosition(pos, this)); -} - -AbstractPosition::Ptr RubyVariantInfo::createCustomPosition(const OptList&) { - // TODO - return createPosition(); -} - -AbstractPosition::Ptr RubyVariantInfo::createPositionFromFEN(const QString& fen) { - if (rb_respond_to(m_position, rb_intern("from_fen"))) { -// int ok; - VALUE pos = rb_funcall(m_position, rb_intern("from_fen"), 1, rb_str_new2(fen.toAscii())); -// if (!ok) return AbstractPosition::Ptr(); - return AbstractPosition::Ptr(new RubyPosition(pos, this)); - } - - return AbstractPosition::Ptr(); -} - -AbstractPosition::Ptr RubyVariantInfo::createChessboard(int /*turn*/, - bool /*wk*/, bool /*wq*/, bool /*bk*/, bool /*bq*/, - const Point& /*ep*/) { - return AbstractPosition::Ptr(); -} - -AbstractPiece::Ptr RubyVariantInfo::createPiece(int color, int type) { - if (rb_respond_to(m_piece, rb_intern("from_color_type"))) { - VALUE piece = rb_funcall(m_piece, rb_intern("from_color_type"), 2, - colorValue(color), typeValue(type)); - return AbstractPiece::Ptr(new RubyPiece(piece)); - } - else - return AbstractPiece::Ptr(); -} - -void RubyVariantInfo::forallPieces(PieceFunction& f) { - VALUE functor = Data_Wrap_Struct(rb_cObject, 0, 0, &f); - VALUE piece_class = rb_funcall(m_piece, rb_intern("class"), 0); - rb_iterate(rb_each, piece_class, (VALUE(*)(...))execute_piece_function, functor); -} - -int RubyVariantInfo::moveListLayout() const { -// if (rb_respond_to(m_variant, rb_intern("movelist_layout"))) -// return NUM2INT(rb_funcall(m_variant, rb_intern("movelist_layout"))); -// else - return 0; -} - -QStringList RubyVariantInfo::borderCoords() const { - // TODO - return QStringList() << "a" << "b" << "c" << "d" << "e" << "f" << "g" << "h" - << "1" << "2" << "3" << "4" << "5" << "6" << "7" << "8"; -} - -AbstractAnimator::Ptr RubyVariantInfo::createAnimator(PointConverter* /*converter*/, - GraphicalPosition* /*position*/) { - // TODO - return AbstractAnimator::Ptr(new RubyAnimator); -} - -AbstractMove::Ptr RubyVariantInfo::createNormalMove(const NormalUserMove& move) { - VALUE mv = rb_funcall(m_move, rb_intern("from_normal"), Ruby::point2value(move.from), Ruby::point2value(move.to)); - return AbstractMove::Ptr(new RubyMove(mv)); -} - -AbstractMove::Ptr RubyVariantInfo::createDropMove(const DropUserMove& move) { - RubyPiece* piece = dynamic_cast(move.m_piece.get()); - if (piece) { - VALUE mv = rb_funcall(m_move, rb_intern("from_drop"), piece->inner(), Ruby::point2value(move.m_to)); - return AbstractMove::Ptr(new RubyMove(mv)); - } - else { - MISMATCH(move.m_piece.get(), RubyPiece); - return AbstractMove::Ptr(); - } -} - -AbstractMove::Ptr RubyVariantInfo::getVerboseMove(int /*turn*/, const VerboseNotation& /*m*/) const { -// Move res = Position::getVerboseMove(static_cast(turn), m); -// return AbstractMove::Ptr(new RubyMove(res)); - // TODO - return AbstractMove::Ptr(new RubyMove(Qnil)); -} - -int RubyVariantInfo::type(const QString& str) { - VALUE type_str = rb_str_new2(str.toAscii()); - if (rb_respond_to(m_type, rb_intern("from_s"))) { - VALUE res = rb_funcall(m_type, rb_intern("from_s"), type_str); - return value2int(res); - } - else - return -1; -} - -QString RubyVariantInfo::typeSymbol(int type) { - VALUE type_value = typeValue(type); - if (rb_respond_to(type_value, rb_intern("symbol"))) { - VALUE res = rb_funcall(type_value, rb_intern("symbol"), 0); - return StringValuePtr(res); - } - else - return ""; -} - -bool RubyVariantInfo::simpleMoves() { - //TODO - return false; -} - -QString RubyVariantInfo::name() const { - return m_name; -} -QString RubyVariantInfo::themeProxy() const { - return ""; -} -OptList RubyVariantInfo::positionOptions() const { - return OptList(); -} - diff --git a/src/variants/rubyvariant.h_ b/src/variants/rubyvariant.h_ deleted file mode 100644 index 9d56189..0000000 --- a/src/variants/rubyvariant.h_ +++ /dev/null @@ -1,24 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#ifndef VARIANTS__RUBY_H -#define VARIANTS__RUBY_H - -#include "variants.h" - -class RubyVariant { -private: - static VariantInfo* static_ruby_variant; -public: - static VariantInfo* info(); -}; - - -#endif // VARIANTS__RUBY_H diff --git a/src/variants/shogi.cpp b/src/variants/shogi.cpp deleted file mode 100644 index 435d070..0000000 --- a/src/variants/shogi.cpp +++ /dev/null @@ -1,816 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#include "shogi.h" -#include "xchess/animator.h" -#include "xchess/piece.h" -#include "xchess/move.h" -#include "moveserializer.impl.h" -#include "xchess/dropanimator.impl.h" -#include "crazyhouse_p.h" -#include "interactiontype.h" -#include "turnpolicy.h" -#include "tagua_wrapped.h" -#include "icsapi.impl.h" - -class ShogiPiece { -public: - enum Type { - KING, - GOLD, - SILVER, - KNIGHT, - LANCE, - ROOK, - BISHOP, - PAWN, - INVALID_TYPE - }; - enum Color { - BLACK, - WHITE, - INVALID_COLOR - }; - typedef bool PromotionType; -private: - Color m_color; - Type m_type; - bool m_promoted; -public: - ShogiPiece() - : m_color(INVALID_COLOR) - , m_type(INVALID_TYPE) - , m_promoted(false) { } - ShogiPiece(ShogiPiece::Color color, ShogiPiece::Type type, bool promoted = false); - ShogiPiece(const ShogiPiece& other); - - void promote() { m_promoted = true; } - bool promoted() { return m_promoted; } - - bool operator<(const ShogiPiece& p) const { - if (m_promoted == p.m_promoted) - if (m_color == p.m_color) - return m_type < p.m_type; - else - return m_color < p.m_color; - else - return m_promoted < p.m_promoted; - } - - QString name() const; - static QString typeName(ShogiPiece::Type t); - bool valid() const { return m_color != INVALID_COLOR && m_type != INVALID_TYPE; } - operator bool() const { return valid(); } - bool operator!() const { return !valid(); } - - bool equals(const ShogiPiece* p) const { - if (valid()) { - if (p) - return (*this) == (*p); - else - return false; - } - else - return !p; - } - - bool operator==(const ShogiPiece& p) const { - return m_promoted == p.m_promoted && - m_color == p.m_color && - m_type == p.m_type; - } - - bool operator!=(const ShogiPiece& p) const { - return !(operator==(p)); - } - - static Type getType(const QString& t); - static QString typeSymbol(ShogiPiece::Type t); - - bool canMove(const class ShogiPosition&, const Point&, const Point&) const; - Color color() const { return m_color; } - Type type() const { return m_type; } - - static Color oppositeColor(Color c) { return c == WHITE ? BLACK : WHITE; } - Point direction() const { return Point(0, m_color == WHITE ? 1 : -1); } -}; - -ShogiPiece::ShogiPiece(ShogiPiece::Color color, ShogiPiece::Type type, bool promoted) -: m_color(color) -, m_type(type) -, m_promoted(promoted) { } - -ShogiPiece::ShogiPiece(const ShogiPiece& other) -: m_color(other.m_color) -, m_type(other.m_type) -, m_promoted(other.m_promoted) { } - -QString ShogiPiece::name() const { - QString res = m_color == WHITE ? "white_" : "black_"; - if (m_promoted) - res += "p_"; - res += typeName(m_type); - return res; -} - -QString ShogiPiece::typeName(ShogiPiece::Type t) { - switch (t) { - case KING: - return "king"; - case GOLD: - return "gold"; - case SILVER: - return "silver"; - case KNIGHT: - return "knight"; - case LANCE: - return "lance"; - case ROOK: - return "rook"; - case BISHOP: - return "bishop"; - case PAWN: - return "pawn"; - default: - return "unknown"; - } -} - -ShogiPiece::Type ShogiPiece::getType(const QString&) { - return KING; // FIXME -} - -QString ShogiPiece::typeSymbol(ShogiPiece::Type t) { - switch (t) { - case KING: - return "K"; - case GOLD: - return "G"; - case SILVER: - return "S"; - case KNIGHT: - return "N"; - case LANCE: - return "L"; - case ROOK: - return "R"; - case BISHOP: - return "B"; - case PAWN: - return "P"; - default: - return "?"; - } -} - -// ------------------------------ - -class ShogiMove { - ShogiPiece m_drop; - - int m_pool; - int m_pool_index; - - bool m_promote; - template friend class MoveSerializer; -public: - Point from; - Point to; - - ShogiMove(); - ShogiMove(const Point& from, const Point& to, bool promote); - ShogiMove(int pool, int pool_index, const Point& to); - - QString toString(int, int) const; - - bool operator==(const ShogiMove& other) const; - - const ShogiPiece& drop() const { return m_drop; } - void setDrop(const ShogiPiece& piece) { m_drop = piece; } - int pool() const { return m_pool; } - int poolIndex() const { return m_pool_index; } - - bool promote() const { return m_promote; } - bool valid() const { return to.valid(); } -}; - -ShogiMove::ShogiMove() -: m_pool(-1) -, m_pool_index(-1) -, m_promote(true) -, from(Point::invalid()) -, to(Point::invalid()) { } - -ShogiMove::ShogiMove(const Point& from, const Point& to, bool promote) -: m_pool(-1) -, m_pool_index(-1) -, m_promote(promote) -, from(from) -, to(to) { } - -ShogiMove::ShogiMove(int pool, int pool_index, const Point& to) -: m_pool(pool) -, m_pool_index(pool_index) -, m_promote(false) -, from(Point::invalid()) -, to(to) { } - -QString ShogiMove::toString(int xsize, int ysize) const { - QString res = from.numcol(xsize) + from.alpharow() - + to.numcol(xsize) + to.alpharow(); - //if (m_promote) res = res + "+"; - - return res; -} - -bool ShogiMove::operator==(const ShogiMove& other) const { - if (m_drop) - return m_drop == other.m_drop - && to == other.to; - else - return m_promote == other.m_promote - && to == other.to - && from == other.from; -} - -// ------------------------------ - -class ShogiPosition { -public: - typedef ShogiPiece Piece; - typedef ShogiMove Move; - - typedef PoolReference PoolReference; - typedef PoolConstReference PoolConstReference; - typedef PoolReference::Pool Pool; - typedef PoolReference::PlayerPool PlayerPool; -private: - Piece::Color m_turn; - Grid m_board; - Pool m_pool; -public: - template friend class MoveSerializer; - ShogiPosition(); - ShogiPosition(const ShogiPosition&); - ShogiPosition(Piece::Color turn, bool wk, bool wq, - bool bk, bool bq, const Point& ep); - ShogiPosition(const QList >& opts); - virtual ShogiPosition* clone() const { return new ShogiPosition(*this); } - virtual ~ShogiPosition() { } - - virtual void setup(); - - bool testMove(Move&) const; - bool pseudolegal(Move& m) const; - - PoolReference pool(int); - PoolConstReference pool(int) const; - - PlayerPool& rawPool(Piece::Color color) { return m_pool[color]; } - const PlayerPool& rawPool(Piece::Color color) const { return const_cast(m_pool)[color]; } - - virtual const Point first() const { return m_board.first(); } - virtual const Point last() const { return m_board.last(); } - virtual Point next(const Point& p) const { return m_board.next(p); } - inline bool valid(const Point& p) const { return m_board.valid(p); } - - ShogiPiece get(const Point& p) const; - void set(const Point& p, const Piece& piece); - - ShogiPiece operator[](const Point& p) const { return m_board[p]; } - - Piece::Color turn() const { return m_turn; } - void setTurn(Piece::Color turn) { m_turn = turn; } - Piece::Color previousTurn() const { return Piece::oppositeColor(m_turn); } - void switchTurn() { m_turn = Piece::oppositeColor(m_turn); } - - InteractionType movable(const TurnTest& test, const Point& p) const { - if (!valid(p) || !m_board[p] || !test(m_board[p].color())) - return NoAction; - return m_board[p].color() == m_turn ? Moving : Premoving; - } - InteractionType droppable(const TurnTest& test, int p) const { - if (!test(m_turn)) - return NoAction; - ShogiPiece::Color c = static_cast(p); - return c == m_turn ? Moving : Premoving; - } - void move(const ShogiMove& m); - - void fromFEN(const QString&, bool& ok) { ok = false; } - QString fen(int, int) const { return ""; } - bool operator==(const ShogiPosition& p) const; - - static Move getVerboseMove(Piece::Color turn, const VerboseNotation& m); - Move getMove(const AlgebraicNotation&, bool& ok) const; - boost::shared_ptr moveHint(const ShogiMove& m) const; - - Point size() const { return Point(9,9); } - void dump() const { } - - static bool promotionZone(Piece::Color color, const Point& p); - static bool stuckPiece(const Piece& piece, const Point& to); - PathInfo path(const Point& from, const Point& to) const { return m_board.path(from, to); } - QStringList borderCoords() const; -}; - -ShogiPosition::ShogiPosition() -: m_turn(ShogiPiece::BLACK) -, m_board(9,9) { } - -ShogiPosition::ShogiPosition(const ShogiPosition& other) -: m_turn(other.m_turn) -, m_board(other.m_board) -, m_pool(other.m_pool) { } - -ShogiPosition::ShogiPosition(Piece::Color turn, bool, bool, bool, bool, const Point&) -: m_turn(turn) -, m_board(9, 9) { } - -ShogiPosition::ShogiPosition(const QList >&) -: m_turn(ShogiPiece::BLACK) -, m_board(9,9) { } - -QStringList ShogiPosition::borderCoords() const -{ - QStringList retv; - for(int i=m_board.getSize().y; i>0; i--) - retv += QString::number(i); - retv << QChar(0x4e5d) << QChar(0x516b) << QChar(0x4e03) << QChar(0x516d) - << QChar(0x4e94) << QChar(0x56db) << QChar(0x4e09) << QChar(0x4e8c) << QChar(0x4e00); - return retv + retv; -} - -bool ShogiPiece::canMove(const ShogiPosition& pos, - const Point& from, const Point& to) const { - if (!from.valid()) return false; - if (!to.valid()) return false; - if (from == to) return false; - if (pos[to].color() == m_color) return false; - Point delta = to - from; - - if (!m_promoted) { - switch (m_type) { - case KING: - return abs(delta.x) <= 1 && abs(delta.y) <= 1; - case GOLD: - return (delta.x == 0 && abs(delta.y) == 1) - || (delta.y == 0 && abs(delta.x) == 1) - || (delta.y == direction().y && abs(delta.x) <= 1); - case SILVER: - return (abs(delta.x) == abs(delta.y) && abs(delta.x) == 1) - || (delta.y == direction().y && abs(delta.x) <= 1); - case ROOK: - { - PathInfo path = pos.path(from, to); - return path.parallel() && path.clear(); - } - case BISHOP: - { - PathInfo path = pos.path(from, to); - return path.diagonal() && path.clear(); - } - case KNIGHT: - { - return abs(delta.x) == 1 && delta.y == direction().y * 2; - } - case LANCE: - { - PathInfo path = pos.path(from, to); - return delta.x == 0 && path.clear() && (delta.y * direction().y > 0); - } - case PAWN: - return delta.x == 0 && delta.y == direction().y; - default: - return false; - } - } - else { - switch (m_type) { - case SILVER: - case PAWN: - case LANCE: - case KNIGHT: - return (delta.x == 0 && abs(delta.y) == 1) - || (delta.y == 0 && abs(delta.x) == 1) - || (delta.y == direction().y && abs(delta.x) <= 1); - case ROOK: - { - if (abs(delta.x) <= 1 && abs(delta.y) <= 1) return true; - PathInfo path = pos.path(from, to); - return path.parallel() && path.clear(); - } - case BISHOP: - { - if (abs(delta.x) <= 1 && abs(delta.y) <= 1) return true; - PathInfo path = pos.path(from, to); - return path.diagonal() && path.clear(); - } - default: - return false; - } - } -} - - -ShogiPosition::Move ShogiPosition::getVerboseMove(Piece::Color, const VerboseNotation&) { - return Move(); -} - -ShogiPosition::Move ShogiPosition::getMove(const AlgebraicNotation&, bool& ok) const { - ok = false; - return Move(); -} - -ShogiPiece ShogiPosition::get(const Point& p) const { - if (m_board.valid(p)) { - return m_board[p]; - } - else { - return ShogiPiece(); - } -} - -void ShogiPosition::set(const Point& p, const ShogiPiece& piece) { - if (m_board.valid(p)) { - m_board[p] = piece; - } -} - -ShogiPosition::PoolReference ShogiPosition::pool(int index) { - ShogiPiece::Color color = static_cast(index); - return PoolReference(&m_pool[color], color); -} - -ShogiPosition::PoolConstReference ShogiPosition::pool(int index) const { - ShogiPiece::Color color = static_cast(index); - return PoolConstReference(&m_pool[color], color); -} - -bool ShogiPosition::operator==(const ShogiPosition& p) const { - return m_turn == p.m_turn - && m_board == p.m_board; -} - -boost::shared_ptr ShogiPosition::moveHint(const ShogiMove& m) const { - if (m.drop()) return boost::shared_ptr(new ShogiPiece(m.drop())); - else return boost::shared_ptr(); -} - -bool ShogiPosition::promotionZone(Piece::Color color, const Point& p) { - return color == ShogiPiece::WHITE ? p.y >= 6 : p.y <= 2; -} - -#define SET_PIECE(i,j, color, type) m_board[Point(i,j)] = Piece(ShogiPiece::color, ShogiPiece::type) -void ShogiPosition::setup() { - for (int i = 0; i < m_board.getSize().x; i++) { - SET_PIECE(i, 2, WHITE, PAWN); - SET_PIECE(i, 6, BLACK, PAWN); - } - - SET_PIECE(0,0, WHITE, LANCE); - SET_PIECE(1,0, WHITE, KNIGHT); - SET_PIECE(2,0, WHITE, SILVER); - SET_PIECE(3,0, WHITE, GOLD); - SET_PIECE(4,0, WHITE, KING); - SET_PIECE(5,0, WHITE, GOLD); - SET_PIECE(6,0, WHITE, SILVER); - SET_PIECE(7,0, WHITE, KNIGHT); - SET_PIECE(8,0, WHITE, LANCE); - SET_PIECE(1,1, WHITE, ROOK); - SET_PIECE(7,1, WHITE, BISHOP); - - SET_PIECE(0,8, BLACK, LANCE); - SET_PIECE(1,8, BLACK, KNIGHT); - SET_PIECE(2,8, BLACK, SILVER); - SET_PIECE(3,8, BLACK, GOLD); - SET_PIECE(4,8, BLACK, KING); - SET_PIECE(5,8, BLACK, GOLD); - SET_PIECE(6,8, BLACK, SILVER); - SET_PIECE(7,8, BLACK, KNIGHT); - SET_PIECE(8,8, BLACK, LANCE); - SET_PIECE(1,7, BLACK, BISHOP); - SET_PIECE(7,7, BLACK, ROOK); - - - m_turn = ShogiPiece::BLACK; -} -#undef SET_PIECE - -bool ShogiPosition::testMove(Move& m) const { - if (!pseudolegal(m)) - return false; - - ShogiPosition tmp(*this); - tmp.move(m); - - // find king position - Point king_pos = Point::invalid(); - ShogiPiece king(m_turn, ShogiPiece::KING); - for (Point i = tmp.m_board.first(); i <= tmp.m_board.last(); i = tmp.m_board.next(i)) { - if (ShogiPiece p = tmp[i]) - if (p == king) { - king_pos = i; - break; - } - } - if (!king_pos.valid()) return false; - - // check if the king can be captured - for (Point i = tmp.m_board.first(); i <= tmp.m_board.last(); i = tmp.m_board.next(i)) { - if (ShogiPiece p = tmp[i]) - if (p.color() == tmp.turn() && p.canMove(tmp, i, king_pos)) return false; - } - - return true; -} - -bool ShogiPosition::stuckPiece(const ShogiPiece& piece, const Point& p) { - if (piece.type() == Piece::PAWN) { - if (p.y == (piece.color() == Piece::WHITE ? 8 : 0)) - return true; - } - else if (piece.type() == Piece::KNIGHT) { - if (piece.color() == Piece::WHITE) { - if (p.y >= 7) - return true; - } - else { - if (p.y <= 1) - return true; - } - } - return false; -} - -bool ShogiPosition::pseudolegal(Move& m) const { - if (!m.drop() && m.pool() != -1 && m.poolIndex() != -1) { - m.setDrop(pool(m.pool()).get(m.poolIndex())); - } - - if (ShogiPiece dropped = m.drop()) { - if (m_board[m.to]) return false; - if (stuckPiece(dropped, m.to)) return false; - if (dropped.type() == Piece::PAWN) { - for (int i = 0; i < m_board.getSize().y; i++) - if (ShogiPiece other = m_board[Point(m.to.x, i)]) - if (other.color() == m_turn && other.type() == Piece::PAWN && !other.promoted()) return false; - } - return true; - } - else { - const Piece& p = m_board[m.from]; - return p && p.canMove(*this, m.from, m.to); - } -} - -void ShogiPosition::move(const ShogiMove& m) { - if (Piece dropped = m.drop()) { - m_board[m.to] = dropped; - if (!--rawPool(dropped.color())[dropped.type()]) - rawPool(dropped.color()).erase(dropped.type()); - } - else { - if (Piece captured = m_board[m.to]) { - rawPool(Piece::oppositeColor(captured.color()))[captured.type()]++; - } - - m_board[m.to] = m_board[m.from]; - m_board[m.from] = Piece(); - } - - if (promotionZone(m_turn, m.to) || promotionZone(m_turn, m.from)) { - if (m.promote() || stuckPiece(m_board[m.to], m.to)) { - Piece::Type type = m_board[m.to].type(); - if (type != ShogiPiece::KING && type != ShogiPiece::GOLD) - m_board[m.to].promote(); - } - } - - switchTurn(); -} - -class ShogiAnimatorBase; - -class ShogiVariantInfo { -public: - static QStringList getNumbers() { - return QStringList() << QChar(0x4e5d) << QChar(0x516b) << QChar(0x4e03) - << QChar(0x516d) << QChar(0x4e94) << QChar(0x56db) - << QChar(0x4e09) << QChar(0x4e8c) << QChar(0x4e00); - } - - typedef ShogiPosition Position; - typedef Position::Move Move; - typedef Position::Piece Piece; - typedef DropAnimatorMixin Animator; - typedef NoPool Pool; - - static const bool hasICS = false; - static const bool m_simple_moves = false; - static void forallPieces(PieceFunction& f); - static int moveListLayout() { return 0; } - static OptList positionOptions() { return OptList(); } - static const char *m_name; - static const char *m_theme_proxy; -}; - -const char *ShogiVariantInfo::m_name = "Shogi_OLD"; -const char *ShogiVariantInfo::m_theme_proxy = "Shogi"; - - -void ShogiVariantInfo::forallPieces(PieceFunction& f) { - ChessVariant::forallPieces(f); -} - -VariantInfo* ShogiVariant::static_shogi_variant = 0; - -VariantInfo* ShogiVariant::info() { - if (!static_shogi_variant) - static_shogi_variant = new WrappedVariantInfo; - return static_shogi_variant; -} - -class ShogiAnimatorBase : public BaseAnimator { -public: - typedef ShogiVariantInfo Variant; -protected: - typedef BaseAnimator Base; - typedef Base::API API; -public: - ShogiAnimatorBase(API cinterface) - : Base(cinterface) { } - - virtual AnimationGroupPtr forward(const ShogiPosition& final, const ShogiMove& move) { - AnimationFactory res(m_cinterface->inner()); - - NamedSprite piece = m_cinterface->takeSprite(move.from); - NamedSprite captured = m_cinterface->takeSprite(move.to); - m_cinterface->setSprite(move.to, piece); - - if (piece) - res.addPreAnimation(Animate::move(piece, move.to)); - - if (captured) - res.addPostAnimation(Animate::destroy(captured)); - - if (final.get(move.to) != m_cinterface->position()->get(move.from)) { - Piece promoted = final.get(move.to); - - if (promoted) { - QPoint real = m_cinterface->converter()->toReal(move.to); - NamedSprite old_sprite = m_cinterface->getSprite(move.to); - NamedSprite new_sprite = m_cinterface->setPiece(move.to, promoted, false); - - res.addPostAnimation(Animate::morph(old_sprite, new_sprite)); - } - } - - return res; - } - - virtual AnimationGroupPtr back(const ShogiPosition& final, const ShogiMove& move) { - AnimationFactory res(m_cinterface->inner()); - - NamedSprite piece = m_cinterface->takeSprite(move.to); - NamedSprite captured; - if (Piece captured_piece = final.get(move.to)) { - captured = m_cinterface->setPiece(move.to, captured_piece, false); - res.addPreAnimation(Animate::appear(captured)); - } - - if (!piece) { - piece = m_cinterface->createPiece(move.to, final.get(move.from), false); - res.addPreAnimation(Animate::appear(piece)); - } - - m_cinterface->setSprite(move.from, piece); - - if (final.get(move.from) != m_cinterface->position()->get(move.to)) { - Piece old_piece = final.get(move.from); - if (old_piece) { - NamedSprite old = m_cinterface->createPiece(move.to, old_piece, false); - res.addPreAnimation(Animate::morph(piece, old)); - - // replace piece with pawn - m_cinterface->setSprite(move.from, old); - piece = old; - } - } - - res.addPreAnimation(Animate::move(piece, move.from)); - - return res; - - } -}; - -template <> -class MoveSerializer { - const ShogiMove& m_move; - const ShogiPosition& m_ref; - bool isAmbiguous() const { - ShogiPiece p = m_move.drop() ? m_move.drop() : m_ref.m_board[m_move.from]; - bool ambiguous = false; - if (!m_move.drop()) - for (Point i = m_ref.m_board.first(); i <= m_ref.m_board.last(); i = m_ref.m_board.next(i) ) { - if (i==m_move.from || m_ref.m_board[i] != p) - continue; - ShogiMove mv(i, m_move.to, false); - if (m_ref.testMove(mv)) { - ambiguous = true; - break; - } - } - return ambiguous; - } -public: - MoveSerializer(const ShogiMove& m, const ShogiPosition& r) - : m_move(m), m_ref(r) { } - - QString SAN() const { - ShogiPiece p = m_move.drop() ? m_move.drop() : m_ref.m_board[m_move.from]; - bool ambiguous = isAmbiguous(); - QString retv; - if (p.promoted()) - retv += "+"; - retv += ShogiPiece::typeSymbol(p.type()); - if (ambiguous) { - retv += QString::number(m_ref.m_board.getSize().x-m_move.from.x); - retv += QString(m_move.from.y+'a'); - } - if (m_move.drop()) - retv += "*"; - else if (m_ref.m_board[m_move.to]) - retv += "x"; - else - retv += "-"; - retv += QString::number(m_ref.m_board.getSize().x-m_move.to.x); - retv += QString(m_move.to.y+'a'); - if (!p.promoted() && !m_move.drop() && - ShogiPosition::promotionZone(m_ref.turn(), m_move.to)) { - if (m_move.m_promote) - retv += "+"; - else - retv += "="; - } - return retv; - } - - DecoratedMove toDecoratedMove() const { - ShogiPiece p = m_move.drop() ? m_move.drop() : m_ref.m_board[m_move.from]; - bool ambiguous = isAmbiguous(); - DecoratedMove retv; - if(p.type() == ShogiPiece::KING) - retv.push_back(MovePart(p.color() == ShogiPiece::BLACK?"king1":"king2", MovePart::Figurine)); - else - retv.push_back(MovePart((p.promoted() ? "p_" : "") + ShogiPiece::typeName(p.type()), MovePart::Figurine)); - if (ambiguous) { - retv.push_back(MovePart(QString::number(m_ref.m_board.getSize().x-m_move.from.x))); - retv.push_back(MovePart("num_"+QString::number(m_move.from.y+1), MovePart::Figurine)); - } - QString mmm; - if (m_move.drop()) - mmm += "*"; - else if (m_ref.m_board[m_move.to]) - mmm += "x"; - else - mmm += "-"; - mmm += QString::number(m_ref.m_board.getSize().x-m_move.to.x); - retv.push_back(mmm); - retv.push_back(MovePart("num_"+QString::number(m_move.to.y+1), MovePart::Figurine)); - if (!p.promoted() && !m_move.drop() && - ShogiPosition::promotionZone(m_ref.turn(), m_move.to)) { - if (m_move.m_promote) - retv.push_back(MovePart("+")); - else - retv.push_back(MovePart("=")); - } - return retv; - } -}; - - -template <> -struct MoveFactory { - static ShogiMove createNormalMove(const NormalUserMove& move) { - return ShogiMove(move.from, move.to, move.promotionType >= 0); - } - static ShogiMove createDropMove(const DropUserMove& move) { - return ShogiMove(move.m_pool, move.m_piece_index, move.m_to); - } - - static NormalUserMove toNormal(const ShogiMove& move) { - return NormalUserMove(move.from, move.to); - } -}; - - - diff --git a/src/variants/shogi.cpp_ b/src/variants/shogi.cpp_ deleted file mode 100644 index 0ba78de..0000000 --- a/src/variants/shogi.cpp_ +++ /dev/null @@ -1,743 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#include "shogi.h" -#include "xchess/animator.h" -#include "xchess/piece.h" -#include "xchess/move.h" -#include "highlevel.h" -#include "moveserializer.impl.h" -#include "xchess/animator.impl.h" -#include "crazyhouse_p.h" - -class ShogiPiece { -public: - enum Type { - KING, - GOLD, - SILVER, - KNIGHT, - LANCE, - ROOK, - BISHOP, - PAWN, - INVALID_TYPE - }; - enum Color { - BLACK, - WHITE, - INVALID_COLOR - }; - typedef bool PromotionType; -private: - Color m_color; - Type m_type; - bool m_promoted; -public: - ShogiPiece() - : m_color(INVALID_COLOR) - , m_type(INVALID_TYPE) - , m_promoted(false) { } - ShogiPiece(ShogiPiece::Color color, ShogiPiece::Type type, bool promoted = false); - ShogiPiece(const ShogiPiece& other); - - void promote() { m_promoted = true; } - bool promoted() { return m_promoted; } - - bool operator<(const ShogiPiece& p) const { - if (m_promoted == p.m_promoted) - if (m_color == p.m_color) - return m_type < p.m_type; - else - return m_color < p.m_color; - else - return m_promoted < p.m_promoted; - } - - QString name() const; - static QString typeName(ShogiPiece::Type t); - bool valid() const { return m_color != INVALID_COLOR && m_type != INVALID_TYPE; } - operator bool() const { return valid(); } - bool operator!() const { return !valid(); } - - bool equals(const ShogiPiece* p) const { - if (valid()) { - if (p) - return (*this) == (*p); - else - return false; - } - else - return !p; - } - - bool operator==(const ShogiPiece& p) const { - return m_promoted == p.m_promoted && - m_color == p.m_color && - m_type == p.m_type; - } - - bool operator!=(const ShogiPiece& p) const { - return !(operator==(p)); - } - - static Type getType(const QString& t); - static QString typeSymbol(ShogiPiece::Type t); - - bool canMove(const class ShogiPosition&, const Point&, const Point&) const; - Color color() const { return m_color; } - Type type() const { return m_type; } - - static Color oppositeColor(Color c) { return c == WHITE ? BLACK : WHITE; } - Point direction() const { return Point(0, m_color == WHITE ? 1 : -1); } -}; - -ShogiPiece::ShogiPiece(ShogiPiece::Color color, ShogiPiece::Type type, bool promoted) -: m_color(color) -, m_type(type) -, m_promoted(promoted) { } - -ShogiPiece::ShogiPiece(const ShogiPiece& other) -: m_color(other.m_color) -, m_type(other.m_type) -, m_promoted(other.m_promoted) { } - -QString ShogiPiece::name() const { - QString res = m_color == WHITE ? "white_" : "black_"; - if (m_promoted) - res += "p_"; - res += typeName(m_type); - return res; -} - -QString ShogiPiece::typeName(ShogiPiece::Type t) { - switch (t) { - case KING: - return "king"; - case GOLD: - return "gold"; - case SILVER: - return "silver"; - case KNIGHT: - return "knight"; - case LANCE: - return "lance"; - case ROOK: - return "rook"; - case BISHOP: - return "bishop"; - case PAWN: - return "pawn"; - default: - return "unknown"; - } -} - -ShogiPiece::Type ShogiPiece::getType(const QString&) { - return KING; // FIXME -} - -QString ShogiPiece::typeSymbol(ShogiPiece::Type t) { - switch (t) { - case KING: - return "K"; - case GOLD: - return "G"; - case SILVER: - return "S"; - case KNIGHT: - return "N"; - case LANCE: - return "L"; - case ROOK: - return "R"; - case BISHOP: - return "B"; - case PAWN: - return "P"; - default: - return "?"; - } -} - -// ------------------------------ - -class ShogiMove { - ShogiPiece m_dropped; - bool m_promote; - template friend class MoveSerializer; -public: - Point from; - Point to; - - ShogiMove(); - ShogiMove(const Point& from, const Point& to, bool promote); - ShogiMove(const ShogiPiece& piece, const Point& to); - - static ShogiMove createDropMove(const ShogiPiece& piece, const Point& to); - QString toString(int) const; - - bool operator==(const ShogiMove& other) const; - - const ShogiPiece& dropped() const { return m_dropped; } - bool promote() const { return m_promote; } - bool valid() const { return to.valid(); } -}; - -ShogiMove::ShogiMove() -: m_promote(true) -, from(Point::invalid()) -, to(Point::invalid()) { } - -ShogiMove::ShogiMove(const Point& from, const Point& to, bool promote) -: m_promote(promote) -, from(from) -, to(to) { } - -ShogiMove::ShogiMove(const ShogiPiece& piece, const Point& to) -: m_dropped(piece) -, m_promote(false) -, from(Point::invalid()) -, to(to) { } - -ShogiMove ShogiMove::createDropMove(const ShogiPiece& piece, const Point& to) { - return ShogiMove(piece, to); -} - -QString ShogiMove::toString(int) const { - return ""; -} - -bool ShogiMove::operator==(const ShogiMove& other) const { - if (m_dropped) - return m_dropped == other.m_dropped - && to == other.to; - else - return m_promote == other.m_promote - && to == other.to - && from == other.from; -} - -// ------------------------------ - -class ShogiPosition { -public: - typedef ShogiPiece Piece; - typedef ShogiMove Move; - typedef std::map Pool; -private: - Piece::Color m_turn; - Grid m_board; - Pool m_pool; -public: - template friend class MoveSerializer; - ShogiPosition(); - ShogiPosition(const ShogiPosition&); - ShogiPosition(Piece::Color turn, bool wk, bool wq, - bool bk, bool bq, const Point& ep); - ShogiPosition(const QList >& opts); - virtual ShogiPosition* clone() const { return new ShogiPosition(*this); } - virtual ~ShogiPosition() { } - - virtual void setup(); - - bool testMove(Move&) const; - bool pseudolegal(Move& m) const; - - virtual void addToPool(const Piece& p, int n) { m_pool[p] += n; } - virtual void removeFromPool(const Piece& p, int n) { - if((m_pool[p] -= n) <= 0) - m_pool.erase(p); - } - Pool& pool() { return m_pool; } - const Pool& pool() const { return m_pool; } - - const ShogiPiece* get(const Point& p) const; - ShogiPiece* get(const Point& p); - void set(const Point& p, Piece* piece); - - ShogiPiece operator[](const Point& p) const { return m_board[p]; } - - Piece::Color turn() const { return m_turn; } - void setTurn(Piece::Color turn) { m_turn = turn; } - Piece::Color previousTurn() const { return Piece::oppositeColor(m_turn); } - void switchTurn() { m_turn = Piece::oppositeColor(m_turn); } - - void move(const ShogiMove& m); - - void fromFEN(const QString&, bool& ok) { ok = false; } - QString fen(int, int) const { return ""; } - bool operator==(const ShogiPosition& p) const; - - static Move getVerboseMove(Piece::Color turn, const VerboseNotation& m); - Move getMove(const AlgebraicNotation&, bool& ok) const; - boost::shared_ptr moveHint(const ShogiMove& m) const; - - Point size() const { return Point(9,9); } - void dump() const { } - - static bool promotionZone(Piece::Color color, const Point& p); - static bool stuckPiece(const Piece& piece, const Point& to); - PathInfo path(const Point& from, const Point& to) const { return m_board.path(from, to); } - QStringList borderCoords() const; -}; - -ShogiPosition::ShogiPosition() -: m_turn(ShogiPiece::BLACK) -, m_board(9,9) { } - -ShogiPosition::ShogiPosition(const ShogiPosition& other) -: m_turn(other.m_turn) -, m_board(other.m_board) -, m_pool(other.m_pool) { } - -ShogiPosition::ShogiPosition(Piece::Color turn, bool, bool, bool, bool, const Point&) -: m_turn(turn) -, m_board(9, 9) { } - -ShogiPosition::ShogiPosition(const QList >&) -: m_turn(ShogiPiece::BLACK) -, m_board(9,9) { } - -QStringList ShogiPosition::borderCoords() const -{ - QStringList retv; - for(int i=9; i>0; i--) - retv += QString::number(i); - retv << QChar(0x4e5d) << QChar(0x516b) << QChar(0x4e03) << QChar(0x516d) - << QChar(0x4e94) << QChar(0x56db) << QChar(0x4e09) << QChar(0x4e8c) << QChar(0x4e00); - return retv + retv; -} - -bool ShogiPiece::canMove(const ShogiPosition& pos, - const Point& from, const Point& to) const { - if (!from.valid()) return false; - if (!to.valid()) return false; - if (from == to) return false; - if (pos[to].color() == m_color) return false; - Point delta = to - from; - - if (!m_promoted) { - switch (m_type) { - case KING: - return abs(delta.x) <= 1 && abs(delta.y) <= 1; - case GOLD: - return (delta.x == 0 && abs(delta.y) == 1) - || (delta.y == 0 && abs(delta.x) == 1) - || (delta.y == direction().y && abs(delta.x) <= 1); - case SILVER: - return (abs(delta.x) == abs(delta.y) && abs(delta.x) == 1) - || (delta.y == direction().y && abs(delta.x) <= 1); - case ROOK: - { - PathInfo path = pos.path(from, to); - return path.parallel() && path.clear(); - } - case BISHOP: - { - PathInfo path = pos.path(from, to); - return path.diagonal() && path.clear(); - } - case KNIGHT: - { - return abs(delta.x) == 1 && delta.y == direction().y * 2; - } - case LANCE: - { - PathInfo path = pos.path(from, to); - return delta.x == 0 && path.clear() && (delta.y * direction().y > 0); - } - case PAWN: - return delta.x == 0 && delta.y == direction().y; - default: - return false; - } - } - else { - switch (m_type) { - case SILVER: - case PAWN: - case LANCE: - case KNIGHT: - return (delta.x == 0 && abs(delta.y) == 1) - || (delta.y == 0 && abs(delta.x) == 1) - || (delta.y == direction().y && abs(delta.x) <= 1); - case ROOK: - { - if (abs(delta.x) <= 1 && abs(delta.y) <= 1) return true; - PathInfo path = pos.path(from, to); - return path.parallel() && path.clear(); - } - case BISHOP: - { - if (abs(delta.x) <= 1 && abs(delta.y) <= 1) return true; - PathInfo path = pos.path(from, to); - return path.diagonal() && path.clear(); - } - default: - return false; - } - } -} - - -ShogiPosition::Move ShogiPosition::getVerboseMove(Piece::Color, const VerboseNotation&) { - return Move(); -} - -ShogiPosition::Move ShogiPosition::getMove(const AlgebraicNotation&, bool& ok) const { - ok = false; - return Move(); -} - -const ShogiPiece* ShogiPosition::get(const Point& p) const { - return m_board.valid(p) && m_board[p] ? &m_board[p] : 0; -} - -ShogiPiece* ShogiPosition::get(const Point& p) { - return m_board.valid(p) && m_board[p] ? &m_board[p] : 0; -} - -void ShogiPosition::set(const Point& p, ShogiPiece* piece) { - if (!m_board.valid(p)) return; - if (piece) - m_board[p] = *piece; - else - m_board[p] = Piece(); -} - -bool ShogiPosition::operator==(const ShogiPosition& p) const { - return m_turn == p.m_turn - && m_board == p.m_board; -} - -boost::shared_ptr ShogiPosition::moveHint(const ShogiMove& m) const { - if (m.dropped()) return boost::shared_ptr(new ShogiPiece(m.dropped())); - else return boost::shared_ptr(); -} - -bool ShogiPosition::promotionZone(Piece::Color color, const Point& p) { - return color == ShogiPiece::WHITE ? p.y >= 6 : p.y <= 2; -} - -#define SET_PIECE(i,j, color, type) m_board[Point(i,j)] = Piece(ShogiPiece::color, ShogiPiece::type) -void ShogiPosition::setup() { - for (int i = 0; i < 9; i++) { - SET_PIECE(i, 2, WHITE, PAWN); - SET_PIECE(i, 6, BLACK, PAWN); - } - - SET_PIECE(0,0, WHITE, LANCE); - SET_PIECE(1,0, WHITE, KNIGHT); - SET_PIECE(2,0, WHITE, SILVER); - SET_PIECE(3,0, WHITE, GOLD); - SET_PIECE(4,0, WHITE, KING); - SET_PIECE(5,0, WHITE, GOLD); - SET_PIECE(6,0, WHITE, SILVER); - SET_PIECE(7,0, WHITE, KNIGHT); - SET_PIECE(8,0, WHITE, LANCE); - SET_PIECE(1,1, WHITE, ROOK); - SET_PIECE(7,1, WHITE, BISHOP); - - SET_PIECE(0,8, BLACK, LANCE); - SET_PIECE(1,8, BLACK, KNIGHT); - SET_PIECE(2,8, BLACK, SILVER); - SET_PIECE(3,8, BLACK, GOLD); - SET_PIECE(4,8, BLACK, KING); - SET_PIECE(5,8, BLACK, GOLD); - SET_PIECE(6,8, BLACK, SILVER); - SET_PIECE(7,8, BLACK, KNIGHT); - SET_PIECE(8,8, BLACK, LANCE); - SET_PIECE(1,7, BLACK, BISHOP); - SET_PIECE(7,7, BLACK, ROOK); - - - m_turn = ShogiPiece::BLACK; -} -#undef SET_PIECE - -bool ShogiPosition::testMove(Move& m) const { - if (!pseudolegal(m)) - return false; - - ShogiPosition tmp(*this); - tmp.move(m); - - // find king position - Point king_pos = Point::invalid(); - ShogiPiece king(m_turn, ShogiPiece::KING); - for (Point i = tmp.m_board.first(); i <= tmp.m_board.last(); i = tmp.m_board.next(i)) { - if (ShogiPiece p = tmp[i]) - if (p == king) { - king_pos = i; - break; - } - } - if (!king_pos.valid()) return false; - - // check if the king can be captured - for (Point i = tmp.m_board.first(); i <= tmp.m_board.last(); i = tmp.m_board.next(i)) { - if (ShogiPiece p = tmp[i]) - if (p.color() == tmp.turn() && p.canMove(tmp, i, king_pos)) return false; - } - - return true; -} - -bool ShogiPosition::stuckPiece(const ShogiPiece& piece, const Point& p) { - if (piece.type() == Piece::PAWN) { - if (p.y == (piece.color() == Piece::WHITE ? 8 : 0)) - return true; - } - else if (piece.type() == Piece::KNIGHT) { - if (piece.color() == Piece::WHITE) { - if (p.y >= 7) - return true; - } - else { - if (p.y <= 1) - return true; - } - } - return false; -} - -bool ShogiPosition::pseudolegal(Move& m) const { - if (ShogiPiece dropped = m.dropped()) { - if (m_board[m.to]) return false; - if (stuckPiece(dropped, m.to)) return false; - if (dropped.type() == Piece::PAWN) { - for (int i = 0; i < 9; i++) - if (ShogiPiece other = m_board[Point(m.to.x, i)]) - if (other.color() == m_turn && other.type() == Piece::PAWN && !other.promoted()) return false; - } - return true; - } - else { - const Piece& p = m_board[m.from]; - return p && p.canMove(*this, m.from, m.to); - } -} - -void ShogiPosition::move(const ShogiMove& m) { - if (m.dropped()) { - Q_ASSERT(m_pool.count(m.dropped())); - Q_ASSERT(!m_board[m.to]); - - m_board[m.to] = m.dropped(); - if(!--m_pool[m.dropped()]) - m_pool.erase(m.dropped()); - } - else { - if (Piece captured = m_board[m.to]) { - addToPool(Piece(Piece::oppositeColor(captured.color()), - captured.type(), false), 1); - } - - m_board[m.to] = m_board[m.from]; - m_board[m.from] = Piece(); - } - - if (promotionZone(m_turn, m.to) | promotionZone(m_turn, m.from)) { - if (m.promote() || stuckPiece(m_board[m.to], m.to)) { - Piece::Type type = m_board[m.to].type(); - if (type != ShogiPiece::KING && type != ShogiPiece::GOLD) - m_board[m.to].promote(); - } - } - - switchTurn(); -} - -#if 0 -class ShogiAnimator : protected CrazyhouseAnimator { -protected: - typedef boost::shared_ptr AnimationPtr; - virtual boost::shared_ptr - createMovementAnimation(const Element& element, const QPoint& destination); -public: - ShogiAnimator(PointConverter* converter, GraphicalPosition* position); - virtual ~ShogiAnimator(){} - virtual AnimationPtr warp(AbstractPosition::Ptr); - virtual AnimationPtr forward(AbstractPosition::Ptr, const ShogiMove& move); - virtual AnimationPtr back(AbstractPosition::Ptr, const ShogiMove& move); -}; - -boost::shared_ptr -ShogiAnimator::createMovementAnimation(const Element& element, const QPoint& destination) { - if (element.piece()->type() == static_cast(ShogiPiece::KNIGHT)) - return boost::shared_ptr(new KnightMovementAnimation(element.sprite(), - destination, m_anim_rotate, 1.0)); - else - return boost::shared_ptr(new MovementAnimation(element.sprite(), - destination, 1.0)); -} - -ShogiAnimator::ShogiAnimator(PointConverter* converter, GraphicalPosition* position) -: CrazyhouseAnimator(converter, position) { } - -ShogiAnimator::AnimationPtr ShogiAnimator::warp(AbstractPosition::Ptr pos) { - return CrazyhouseAnimator::warp(pos); -} - -ShogiAnimator::AnimationPtr ShogiAnimator::forward(AbstractPosition::Ptr pos, const ShogiMove& move) { - if (move.dropped()) - return CrazyhouseAnimator::forward(pos, CrazyhouseMove(CrazyhousePiece(WHITE, KING), move.to)); - else - return CrazyhouseAnimator::forward(pos, CrazyhouseMove(move.from, move.to)); -} - -ShogiAnimator::AnimationPtr ShogiAnimator::back(AbstractPosition::Ptr pos, const ShogiMove& move) { - if (move.dropped()) - return CrazyhouseAnimator::back(pos, CrazyhouseMove(CrazyhousePiece(WHITE, KING), move.to)); - else - return CrazyhouseAnimator::back(pos, CrazyhouseMove(move.from, move.to)); -} -#endif - -class ShogiVariantInfo { -public: - static QStringList getNumbers() { - return QStringList() << QChar(0x4e5d) << QChar(0x516b) << QChar(0x4e03) - << QChar(0x516d) << QChar(0x4e94) << QChar(0x56db) - << QChar(0x4e09) << QChar(0x4e8c) << QChar(0x4e00); - } - - typedef ShogiPosition Position; - typedef Position::Move Move; - typedef Position::Piece Piece; - typedef SimpleAnimator Animator; - static const bool m_simple_moves = false; - static void forallPieces(PieceFunction& f); - static int moveListLayout() { return 0; } - static OptList positionOptions() { return OptList(); } - static const char *m_name; - static const char *m_theme_proxy; -}; - -const char *ShogiVariantInfo::m_name = "Shogi"; -const char *ShogiVariantInfo::m_theme_proxy = "Shogi"; - - -void ShogiVariantInfo::forallPieces(PieceFunction& f) { - ChessVariant::forallPieces(f); -} - -VariantInfo* ShogiVariant::static_shogi_variant = 0; - -VariantInfo* ShogiVariant::info() { - if (!static_shogi_variant) - static_shogi_variant = new WrappedVariantInfo; - return static_shogi_variant; -} - - -template <> -class MoveSerializer { - const ShogiMove& m_move; - const ShogiPosition& m_ref; - bool isAmbiguous() const { - ShogiPiece p = m_move.dropped() ? m_move.dropped() : m_ref.m_board[m_move.from]; - bool ambiguous = false; - if (!m_move.m_dropped) - for (Point i = m_ref.m_board.first(); i <= m_ref.m_board.last(); i = m_ref.m_board.next(i) ) { - if (i==m_move.from || m_ref.m_board[i] != p) - continue; - ShogiMove mv(i, m_move.to, false); - if (m_ref.testMove(mv)) { - ambiguous = true; - break; - } - } - return ambiguous; - } -public: - MoveSerializer(const ShogiMove& m, const ShogiPosition& r) - : m_move(m), m_ref(r) { } - - QString SAN() const { - ShogiPiece p = m_move.dropped() ? m_move.dropped() : m_ref.m_board[m_move.from]; - bool ambiguous = isAmbiguous(); - QString retv; - if (p.promoted()) - retv += "+"; - retv += ShogiPiece::typeSymbol(p.type()); - if (ambiguous) { - retv += QString::number(9-m_move.from.x); - retv += QString(m_move.from.y+'a'); - } - if (m_move.m_dropped) - retv += "*"; - else if (m_ref.m_board[m_move.to]) - retv += "x"; - else - retv += "-"; - retv += QString::number(9-m_move.to.x); - retv += QString(m_move.to.y+'a'); - if (!p.promoted() && !m_move.dropped() && - ShogiPosition::promotionZone(m_ref.turn(), m_move.to)) { - if (m_move.m_promote) - retv += "+"; - else - retv += "="; - } - return retv; - } - - DecoratedMove toDecoratedMove() const { - ShogiPiece p = m_move.dropped() ? m_move.dropped() : m_ref.m_board[m_move.from]; - bool ambiguous = isAmbiguous(); - DecoratedMove retv; - if(p.type() == ShogiPiece::KING) - retv += MovePart(p.color() == ShogiPiece::BLACK?"king1":"king2", MovePart::Figurine); - else - retv += MovePart((p.promoted() ? "p_" : "") + ShogiPiece::typeName(p.type()), MovePart::Figurine); - if (ambiguous) { - retv += MovePart(QString::number(9-m_move.from.x)); - retv += MovePart("num_"+QString::number(m_move.from.y+1), MovePart::Figurine); - } - QString mmm; - if (m_move.m_dropped) - mmm += "*"; - else if (m_ref.m_board[m_move.to]) - mmm += "x"; - else - mmm += "-"; - mmm += QString::number(9-m_move.to.x); - retv += MovePart(mmm); - retv += MovePart("num_"+QString::number(m_move.to.y+1), MovePart::Figurine); - if (!p.promoted() && !m_move.dropped() && - ShogiPosition::promotionZone(m_ref.turn(), m_move.to)) { - if (m_move.m_promote) - retv += MovePart("+"); - else - retv += MovePart("="); - } - return retv; - } -}; - - -template <> -struct MoveFactory { - static ShogiMove createNormalMove(const NormalUserMove& move) { - return ShogiMove(move.from, move.to, move.promotionType >= 0); - } - static ShogiMove createDropMove(const ShogiPiece& dropped, const Point& to) { - return ShogiMove(dropped, to); - } - - static NormalUserMove toNormal(const ShogiMove& move) { - return NormalUserMove(move.from, move.to); - } -}; - - - diff --git a/src/variants/shogi.h b/src/variants/shogi.h deleted file mode 100644 index e0c48ff..0000000 --- a/src/variants/shogi.h +++ /dev/null @@ -1,23 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#ifndef SHOGIVARIANT_H -#define SHOGIVARIANT_H - -#include "chess.h" - -class ShogiVariant { -private: - static VariantInfo* static_shogi_variant; -public: - static VariantInfo* info(); -}; - -#endif // SHOGIVARIANT_H diff --git a/src/variants/shogi.h_ b/src/variants/shogi.h_ deleted file mode 100644 index e0c48ff..0000000 --- a/src/variants/shogi.h_ +++ /dev/null @@ -1,23 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#ifndef SHOGIVARIANT_H -#define SHOGIVARIANT_H - -#include "chess.h" - -class ShogiVariant { -private: - static VariantInfo* static_shogi_variant; -public: - static VariantInfo* info(); -}; - -#endif // SHOGIVARIANT_H diff --git a/src/variants/suicide.h__ b/src/variants/suicide.h__ deleted file mode 100644 index fd60024..0000000 --- a/src/variants/suicide.h__ +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef SUICIDE_H -#define SUICIDE_H - -class SuicidePosition : public ChessPosition { -public: - SuicidePosition(); - SuicidePosition(const SuicidePosition&); - virtual SuicidePosition* clone() const; - - -}; - -#endif // SUICIDE_H \ No newline at end of file diff --git a/src/variants/tagua.rb b/src/variants/tagua.rb deleted file mode 100644 index 6800f03..0000000 --- a/src/variants/tagua.rb +++ /dev/null @@ -1,91 +0,0 @@ -module Tagua - -class Point - attr_reader :x, :y - - def initialize(x, y) - @x = x - @y = y - end - - def +(other) - Point.new(@x + other.x, @y + other.y) - end - - def -(other) - Point.new(@x - other.x, @y - other.y) - end - - def *(k) - Point.new(@x * k, @y * k) - end - - def self.shift(v) - x = v.shift - if x.respond_to? :x and x.respond_to? :y - x - else - new(x, v.shift) - end - end -end - -class Grid - attr_reader :size - - def initialize(size) - @size = size - @grid = Array.new(size.x * size.y) - end - - def [](*args) - p = Point.shift(args) - @grid[p.x + p.y * size.x] - end - - def []=(*args) - p = Point.shift(args) - val = args.shift - @grid[p.x + p.y * size.x] = val - end - - def clone - res = self.class.new(@size) - res.grid = @grid.dup - res - end - - def ==(other) - return false unless @size == other.size - @grid.each do |i| - return false unless self[i] == other[i] - end - true - end - - def each_square - @grid.each {|i| yield Point.new(i % size.x, i / size.x) } - end - -private - attr_accessor :grid -end - -module GridAccess - def [](*args) - p = Point.shift(args) - @board[p] - end - - def []=(*args) - p = Point.shift(args) - val = args.shift - @board[p] = val - end -end - -def self.register(name, var) - -end - -end diff --git a/src/variants/tictactoe.rb b/src/variants/tictactoe.rb deleted file mode 100644 index 4a4e566..0000000 --- a/src/variants/tictactoe.rb +++ /dev/null @@ -1,110 +0,0 @@ -require 'tagua' - -module TicTacToe - -class Piece - attr_reader :color - - def initialize(color) - @color = color - end - - def to_s - "#{@color}_man" - end -end - -Move = Tagua::Point - -class Pool - def clear - end -end - -class Position - include GridAccess - attr_accessor :turn - def previous_turn - if @turn == :white - :black - else - :white - end - end - - def initialize - @turn = :white - @board = Grid.new(size) - end - - def size - Point.new(3,3) - end - - def setup - end - - def test_move(m) - not @board[m] - end - - def move(m) - @board[m] = Piece.new(@turn) - switch_turn - end - - def switch_turn - @turn = if @turn == :white - :black - else - :white - end - end - - def status - each_line do |line| - res = check_line(line) - return "#{res} wins" if res - end - @grid.each_square do |i| - return "in play" unless @grid[i] - end - "draw" - end - - def clone - res = self.class.new - res.turn = @turn - res.board = board.clone - res - end - - def ==(other) - @turn == other.turn and @board == other.board - end -protected - def each_line - (0..2).each do |i| - yield (0..2).map{|y| Point.new(i, y) } - yield (0..2).map{|x| Point.new(x, i) } - end - - yield (0..2).map{|x| Point.new(x, x) } - yield (0..2).map{|x| Point.new(x, 2 - x) } - end - - def check_line(line) - col = false - line.each do |val| - return false if col and col != val.color - col = val.color - end - col - end - - attr_accessor :board -end - -end - -Tagua.register "Tic Tac Toe", TicTacToe diff --git a/src/variants/trivialanimator.h_ b/src/variants/trivialanimator.h_ deleted file mode 100644 index 80ff933..0000000 --- a/src/variants/trivialanimator.h_ +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef TRIVIALANIMATOR_H -#define TRIVIALANIMATOR_H - -template -class TrivialAnimator : public SimpleAnimator { -protected: - typedef SimpleAnimator Base; - typedef typename Base::AnimationPtr AnimationPtr; - typedef typename Base::GPosition GPosition; - typedef typename Base::GElement GElement; - typedef typename Base::Position Position; - typedef typename Base::Piece Piece; - typedef typename Base::Move Move; -public: - TrivialAnimator(PointConverter* converter, const boost::shared_ptr& position) - : Base(converter, position) { } - virtual ~TrivialAnimator() { } - virtual AnimationPtr forward(const Position& pos, const Move&) { return warp(pos); } - virtual AnimationPtr back(const Position& pos, const Move&) { return warp(pos); } -}; - -#endif // TRIVIALANIMATOR_H diff --git a/src/variants/ultima.cpp__ b/src/variants/ultima.cpp__ deleted file mode 100644 index 1692637..0000000 --- a/src/variants/ultima.cpp__ +++ /dev/null @@ -1,356 +0,0 @@ -#include "ultima.h" -#include "controllers/game.impl.h" -#include "controllers/examination.h" -#include "controllers/localexamination.h" -#include "controllers/editposition.impl.h" -#include "controllers/observedgame.h" -#include "controllers/editgame.impl.h" -#include "piecepoolwidget.h" -#include "variant.impl.h" -#include "graphicalinfo.impl.h" -#include "poolgraphicalinfo.impl.h" -#include "animator.impl.h" -#include "boardmanager.impl.h" -#include "moveserializer.impl.h" -#include - -using namespace std; - -QString UltimaPiece::typeName() const { - switch (m_type) { - case King: - return "king"; - case Withdrawer: - return "withdrawer"; - case Coordinator: - return "coordinator"; - case Chameleon: - return "chameleon"; - case LongLeaper: - return "longleaper"; - case Pawn: - return "pawn"; - case Immobilizer: - return "immobilizer"; - default: - return ""; - } -} - -QString UltimaPiece::typeSymbol(Type type) { - switch (type) { - case King: - return "K"; - case Withdrawer: - return "W"; - case Coordinator: - return "C"; - case Chameleon: - return "X"; - case LongLeaper: - return "L"; - case Pawn: - return "P"; - case Immobilizer: - return "I"; - default: - return ""; - } -} - -bool UltimaPiece::canMove(UltimaMove& m, const UltimaPosition& position) const { - if (m_type == Chameleon) { - typedef map > Map; - Map possible_targets; - - bool valid = canMoveAs(m, position, m.targets, UltimaPiece::Chameleon); - - for (int i = 0; i < 7; i++) { - Type t = static_cast(i); - if (t != Chameleon) { - possible_targets[t] = vector(); - bool keep_this = true; - if (!canMoveAs(m, position, possible_targets[t], t)) - keep_this = false; - else { - if (possible_targets[t].size() == 0) { - if (position[m.to]) { - keep_this = true; - possible_targets[t].push_back(m.to); - } - else - keep_this = false; - } - else { - for (uint j = 0; j < possible_targets[t].size(); j++) { - Q_ASSERT(position[possible_targets[t][j]]); - if (position[possible_targets[t][j]]->type() != t) { - keep_this = false; - break; - } - } - } - } - - if (!keep_this) { - std::cout << "removing possible targets of type " << typeSymbol(t) << std::endl; - possible_targets.erase(t); - } - } - } - - for (Map::iterator i = possible_targets.begin(); - i != possible_targets.end(); ++i) { - std::cout << "remaining targets of type " << typeSymbol(i->first) << std::endl; - valid = true; - copy(i->second.begin(), - i->second.end(), - back_insert_iterator >(m.targets)); - } - - return valid; - } - else - return canMoveAs(m, position, m.targets, m_type); -} - -bool UltimaPiece::checkPawnCapture(const UltimaPosition& position, const Point& p, - const Point& direction, vector& targets) const { - Point opp = p + direction; - Point f = opp + direction; - - if (!position.valid(f)) return false; - if (position[opp] && position[opp]->color() != color() - && position[f] && position[f]->color() == color()) { - targets.push_back(opp); - return true; - } - else return false; -} - -bool UltimaPiece::canMoveAs(const UltimaMove& m, const UltimaPosition& position, - vector& targets, Type actualType) const { - Point delta = m.to - m.from; - if (actualType == King) - // the king moves exactly like a chess king - return abs(delta.x) <= 1 && abs(delta.y) <= 1 && !sameColor(position[m.to]); - else { - PathInfo path = position.path(m.from, m.to); - if (path.valid() && !position[m.to]) { - - // a long leaper is the only one that can jump - if (actualType == LongLeaper) { - Point step = delta.normalizeInfinity(); - // a long leaper move is pseudolegal if - // between from and to there are only - // opponent pieces, and they are not - // consecutive - bool last = false; - Point p = m.from; - while ((p += step) != m.to) { - if (position[p]) { - if (last || position[p]->color() == color()) return false; - targets.push_back(p); - last = true; - } - else last = false; - } - return true; - } - else { - if (!path.clear()) return false; - - switch (actualType) { - case Pawn: - // move like a rook - if (!path.parallel()) return false; - - // called specialized capture checks for each direction - checkPawnCapture(position, m.to, Point(1,0), targets); - checkPawnCapture(position, m.to, Point(0,1), targets); - checkPawnCapture(position, m.to, Point(-1,0), targets); - checkPawnCapture(position, m.to, Point(0,-1), targets); - break; - - case Coordinator: - { - // find king position - Point kingPos = position.findKing(m_color); - Q_ASSERT(kingPos != Point::invalid()); - - // capture on the other vertices of the rectangle - // determined by m.to and kingPos - Point t1(m.to.x, kingPos.y); - Point t2(kingPos.x, m.to.y); - - if (position[t1] && position[t1]->color() != m_color) - targets.push_back(t1); - if (position[t2] && position[t2]->color() != m_color) - targets.push_back(t2); - } - break; - case Immobilizer: - // the immobilizer cannot capture - break; - case Withdrawer: - { - Point dir = delta.normalizeInfinity(); - Point opp = m.from - dir; - if (position.valid(opp) && position[opp] && position[opp]->color() != color()) - targets.push_back(opp); - } - break; - case Chameleon: - break; - default: - return false; - - } - - return true; - } - - } - - return false; - } -} - -bool UltimaPiece::equals(const UltimaPiece* other) const { - return other && m_color == other->m_color && m_type == other->m_type; -} - - -bool UltimaPiece::sameColor(const UltimaPiece* other) const { - return other && m_color == other->m_color; -} - -UltimaPosition::UltimaPosition() -: m_turn(WHITE) -, m_board(8, 8) { }; - -UltimaPosition::~UltimaPosition() { } - -#define SET_PIECE(i, j, color, type) \ - m_board[Point(i,j)] = new Piece(color, UltimaPiece::type) -void UltimaPosition::setup() { - for (int i = 0; i < 8; i++) { - SET_PIECE(i, 1, BLACK, Pawn); - SET_PIECE(i, 6, WHITE, Pawn); - } - - SET_PIECE(0,0, BLACK, Coordinator); - SET_PIECE(1,0, BLACK, LongLeaper); - SET_PIECE(2,0, BLACK, Chameleon); - SET_PIECE(3,0, BLACK, Withdrawer); - SET_PIECE(4,0, BLACK, King); - SET_PIECE(5,0, BLACK, Chameleon); - SET_PIECE(6,0, BLACK, LongLeaper); - SET_PIECE(7,0, BLACK, Immobilizer); - - SET_PIECE(0,7, WHITE, Immobilizer); - SET_PIECE(1,7, WHITE, LongLeaper); - SET_PIECE(2,7, WHITE, Chameleon); - SET_PIECE(3,7, WHITE, King); - SET_PIECE(4,7, WHITE, Withdrawer); - SET_PIECE(5,7, WHITE, Chameleon); - SET_PIECE(6,7, WHITE, LongLeaper); - SET_PIECE(7,7, WHITE, Coordinator); - - m_turn = WHITE; -} -#undef SET_PIECE - -bool UltimaPosition::operator==(const UltimaPosition& other) const { - return m_turn == other.m_turn - && m_board == other.m_board; -} - -UltimaPiece* UltimaPosition::operator[](const Point& p) const { - return m_board[p]; -} - -void UltimaPosition::setPiece(const Point& p, Piece* piece) { - delete m_board[p]; - m_board[p] = piece; -} - -Point UltimaPosition::findKing(Piece::Color color) const { - return m_board.find(UltimaPiece(color, UltimaPiece::King)); -} - -void UltimaPosition::dump() const { } - - -bool UltimaPosition::valid(const Point& p) const { - return m_board.valid(p); -} - -bool UltimaPosition::testMove(Move& m) const { - return pseudolegal(m); -} - -bool UltimaPosition::pseudolegal(Move& m) const { - if (!m.valid()) return false; - Q_ASSERT(valid(m.from)); - Piece* piece = m_board[m.from]; - Q_ASSERT(piece); - - if (m.from == m.to) return false; - - // a piece cannot move if it is near an opponent immobilizer - // or near a chameleon if it is an immobilizer - bool immobilized = false; - for (int i = -1; i <= 1; ++i) - for (int j = -1; j <= 1; ++j) { - Point p = m.from + Point(i,j); - if (!valid(p)) continue; - if (m_board[p] - && m_board[p]->color() != piece->color()) { - Piece::Type type = m_board[p]->type(); - if (type == Piece::Immobilizer || - (piece->type() == Piece::Immobilizer && type == Piece::Chameleon)) { - immobilized = true; - break; - } - } - } - if (immobilized) { - // an immobilized piece can move - // if an only if the move is a suicide - return m.to == Point::invalid(); - } - - if (piece->canMove(m, *this)) { - return true; - } - else return false; -} - -void UltimaPosition::move(const Move& m) { - Q_ASSERT(m.valid()); - - // remove targets - for (uint i = 0; i < m.targets.size(); i++) - if (m.targets[i] != Point::invalid()) setPiece(m.targets[i], 0); - - if (m.to == Point::invalid()) { - // suicide -// setPiece(m.from, 0); - std::cout << "suicide on " << m.from << std::endl; - } - else { - // normal move - delete m_board[m.to]; - m_board[m.to] = m_board[m.from]; - m_board[m.from] = 0; - } - - switchTurn(); -} - -void UltimaPosition::switchTurn() { - m_turn = ChessPiece::oppositeColor(m_turn); -} - -template class EditGameController; diff --git a/src/variants/ultima.h__ b/src/variants/ultima.h__ deleted file mode 100644 index 02e8218..0000000 --- a/src/variants/ultima.h__ +++ /dev/null @@ -1,178 +0,0 @@ -#ifndef ULTIMA_H -#define ULTIMA_H - -#include -#include "common.h" -#include "position.h" -#include "spriteloader.h" -#include "serverreference.h" - -class UltimaMove; -class UltimaPosition; - -class UltimaPiece { -public: - enum Type { - InvalidType = -1, - Pawn, - LongLeaper, - Chameleon, - Coordinator, - Withdrawer, - King, - Immobilizer - }; - typedef PieceColor Color; -private: - Color m_color; - Type m_type; - - bool checkPawnCapture(const UltimaPosition& position, const Point& pos, const Point& direction, std::vector& targets) const; -public: - UltimaPiece(const Color& color, const Type& type) - : m_color(color), m_type(type) { } - UltimaPiece& operator=(const UltimaPiece& other) { - m_color = other.m_color; - m_type = other.m_type; - return *this; - } - bool operator==(const UltimaPiece& other) const { - return equals(&other); - } - - Color color() const { return m_color; } - Type type() const { return m_type; } - QString typeName() const; - - bool equals(const UltimaPiece* other) const; - bool sameColor(const UltimaPiece* other) const; - static QString typeSymbol(Type type); - - bool canMove(UltimaMove& m, const UltimaPosition& pos) const; - bool canMoveAs(const UltimaMove& m, const UltimaPosition& pos, - std::vector& targets, Type actualType) const; -}; - -class UltimaMove { -public: - Point from; - Point to; - std::vector targets; - - UltimaMove(const Point& from, const Point& to, PieceType /* ignored */ = INVALID_TYPE) - : from(from) - , to(to) { } - - static UltimaMove invalid() { - return UltimaMove(Point::invalid(), Point::invalid()); - } - - bool valid() const { - return from != Point::invalid(); - } - - ChessMove::Type type() const { return ChessMove::Normal; } -}; - -class UltimaPosition { -public: - typedef UltimaPiece Piece; - typedef UltimaMove Move; - typedef ChessPosition::State State; - - UltimaPosition(); - virtual ~UltimaPosition(); - virtual UltimaPosition* clone() const { return new UltimaPosition(*this); } - -private: - Piece::Color m_turn; - PointerGrid m_board; - -public: - virtual void setup(); - Piece::Color turn() const { return m_turn; } - Piece* operator[](const Point& p) const; - void setPiece(const Point& p, Piece* piece); - bool operator==(const UltimaPosition& other) const; - - Point findKing(Piece::Color color) const; - PathInfo path(const Point& from, const Point& to) const { - return m_board.path(from, to); - } - - bool valid(const Point& p) const; - Move getMove(const AlgebraicNotation&, bool&) const { - Q_ASSERT(!"unimplemented"); return *(Move*)0; } - bool testMove(Move&) const; - bool pseudolegal(Move&) const; - void move(const Move& mv); - void switchTurn(); - State state() const { return ChessPosition::BlackWins; } - - void dump() const; -}; - -template <> -class LoadPieces : public LoadPiecesBase { - typedef LoadPiecesBase Base; - typedef UltimaPiece Piece; -public: - LoadPieces(InnerSpriteLoader& data, InnerSpriteLoader::Pixmaps& cache, int size) - : Base(data, cache, size) { } - - #define LOAD(color, type) \ - load(Piece(color, Piece::type)) - virtual void apply() { - LOAD(WHITE, King); - LOAD(WHITE, Withdrawer); - LOAD(WHITE, Coordinator); - LOAD(WHITE, Chameleon); - LOAD(WHITE, LongLeaper); - LOAD(WHITE, Pawn); - LOAD(BLACK, King); - LOAD(BLACK, Withdrawer); - LOAD(BLACK, Coordinator); - LOAD(BLACK, Chameleon); - LOAD(BLACK, LongLeaper); - LOAD(BLACK, Pawn); - - loadUpsideDownRook(WHITE); - loadUpsideDownRook(BLACK); - } - #undef LOAD - - void loadUpsideDownRook(Piece::Color color) { - Piece coo(color, Piece::Coordinator); - Piece imm(color, Piece::Immobilizer); - - SpriteStore::Token rookToken = PieceToken::get(&coo); - SpriteStore::Token immToken = PieceToken::get(&imm); - QImage rookImage = m_data.store->get(rookToken); - QImage res = rookImage.transformed( - QMatrix(1, 0, 0, -1, 0, rookImage.height())); - m_data.store->add(immToken, res); - scale(immToken); - } -}; - - -template <> -class MoveSerializer { - typedef UltimaPosition::Move Move; - Move m_move; -public: - MoveSerializer(const Move& m, const UltimaPosition& /*ref*/) - : m_move(m) { } - - virtual QString SAN() const { return ""; } -}; - - -template <> -struct ComparePosition { - static bool apply(const UltimaPosition& /*chessPos*/, const UltimaPosition& /*pos*/) { - return true; - } -}; - -#endif // ULTIMA_H diff --git a/src/variants/variants-autogen b/src/variants/variants-autogen deleted file mode 100644 index 3989592..0000000 --- a/src/variants/variants-autogen +++ /dev/null @@ -1,12 +0,0 @@ -// AUTOGENERATED - DO NOT MODIFY -register_variant(); -register_variant(); -// register_variant(); -// register_variant(); -// register_variant(); -// register_variant(); -// register_variant(); -// register_variant(); -// register_variant(); -register_variant(); -// register_variant(); diff --git a/src/variants/xchess/animator.h b/src/variants/xchess/animator.h deleted file mode 100644 index d1e704f..0000000 --- a/src/variants/xchess/animator.h +++ /dev/null @@ -1,69 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#ifndef ANIMATOR_H -#define ANIMATOR_H - -#include "animation.h" -#include "fwd.h" - -class NamedSprite; -class Point; -class AnimationFactory; - -/** - * A generic animator. - * Can be used as a base class for other specialized animators. - */ -template -class BaseAnimator { -protected: - typedef typename UnwrappedGraphicalAPIPtr::type API; - typedef typename Variant::Position Position; - typedef typename Variant::Move Move; - typedef typename Variant::Piece Piece; - API m_cinterface; -public: - BaseAnimator(API cinterface) - : m_cinterface(cinterface) { } - virtual ~BaseAnimator() { } - - virtual AnimationGroupPtr warp(const Position& final); - virtual AnimationGroupPtr forward(const Position& final, const Move& move); - virtual AnimationGroupPtr back(const Position& final, const Move& move); -}; - - -template -class SimpleAnimator : BaseAnimator<_Variant> { -public: - typedef _Variant Variant; -private: - typedef BaseAnimator Base; -protected: - typedef typename Base::API API; - typedef typename Base::Position Position; - typedef typename Base::Move Move; - typedef typename Base::Piece Piece; - - using Base::m_cinterface; - - virtual SchemePtr movement(const NamedSprite& sprite, const Point& from, const Point& to); -public: - SimpleAnimator(API cinterface) - : Base(cinterface) { } - - virtual AnimationGroupPtr warp(const Position& final); - virtual AnimationGroupPtr forward(const Position& final, const Move& move); - virtual AnimationGroupPtr back(const Position& final, const Move& move); -}; - - -#endif // ANIMATOR_H diff --git a/src/variants/xchess/animator.impl.h b/src/variants/xchess/animator.impl.h deleted file mode 100644 index c7d962f..0000000 --- a/src/variants/xchess/animator.impl.h +++ /dev/null @@ -1,202 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#ifndef XCHESS__ANIMATOR_IMPL_H -#define XCHESS__ANIMATOR_IMPL_H - -#include "animator.h" -#include "animationfactory.h" -#include "common.h" -#include "namedsprite.h" -#include "variants/xchess/piecetype.h" - -template -AnimationGroupPtr BaseAnimator::warp(const Position& final) { - const Position* current = m_cinterface->position(); - AnimationFactory res(m_cinterface->inner()); - - for (Point i = current->first(); i <= current->last(); i = current->next(i)) { - Piece c = current->get(i); - Piece f = final.get(i); - - if( !c && f ) { - //current->set(i, f); - NamedSprite sprite = m_cinterface->setPiece(i, f, false); - res.addPreAnimation(Animate::appear(sprite), Animate::Instant); - } - else if (c && !f) { - NamedSprite old_sprite = m_cinterface->takeSprite(i); - res.addPreAnimation(Animate::disappear(old_sprite), Animate::Instant); - } - else if(c && f && !(c == f) ) { - NamedSprite old_sprite = m_cinterface->takeSprite(i); - NamedSprite sprite = m_cinterface->setPiece(i, f, false); - res.addPreAnimation(Animate::morph(old_sprite, sprite), Animate::Instant); - } - } - - return res; -} - -template -AnimationGroupPtr BaseAnimator::forward(const Position& final, const Move&) { - return warp(final); -} - - -template -AnimationGroupPtr BaseAnimator::back(const Position& final, const Move&) { - return warp(final); -} - -template -AnimationGroupPtr SimpleAnimator::warp(const Position& final) { - AnimationFactory res(m_cinterface->inner()); - - res.setGroup(Base::warp(final)); - - return res; -} - -template -SchemePtr SimpleAnimator::movement(const NamedSprite& sprite, const Point& from, const Point& to) { - bool knight = m_cinterface->position()->get(from).type() == KNIGHT; - int mtype = knight - ? Animate::move::LShaped | Animate::move::Rotating - : Animate::move::Straight; - return SchemePtr(new Animate::move(sprite, to, mtype)); -} - -template -AnimationGroupPtr SimpleAnimator::forward(const Position& final, const Move& move) { - AnimationFactory res(m_cinterface->inner()); - - NamedSprite piece = m_cinterface->takeSprite(move.from); - NamedSprite captured = m_cinterface->takeSprite(move.to); - m_cinterface->setSprite(move.to, piece); - - if (piece) - res.addPreAnimation(*movement(piece, move.from, move.to)); - else - ERROR("Bug!!!"); - - if (captured) - res.addPostAnimation(Animate::destroy(captured)); - - if (move.type() == Move::EnPassantCapture) { - Point phantom(move.to.x, move.from.y); - NamedSprite capturedPawn = m_cinterface->takeSprite(phantom); - - if (capturedPawn) { - QPoint real = m_cinterface->converter()->toReal(phantom); - res.addPostAnimation(Animate::disappear(capturedPawn)); - } - else - ERROR("Bug!!!"); - } - else if (move.type() == Move::Promotion) { - Piece promoted = final.get(move.to); - - if (promoted) { - QPoint real = m_cinterface->converter()->toReal(move.to); - NamedSprite old_sprite = m_cinterface->getSprite(move.to); - NamedSprite new_sprite = m_cinterface->setPiece(move.to, promoted, /*false,*/ false); - - res.addPostAnimation(Animate::morph(old_sprite, new_sprite)); - } - else - ERROR("Bug!!!"); - } - else if (move.type() == Move::KingSideCastling) { - Point rookSquare = move.to + Point(1,0); - Point rookDestination = move.from + Point(1,0); - - NamedSprite rook = m_cinterface->takeSprite(rookSquare); - m_cinterface->setSprite(rookDestination, rook); - res.addPreAnimation(Animate::move(rook, rookDestination)); - } - else if (move.type() == Move::QueenSideCastling) { - Point rookSquare = move.to + Point(-2,0); - Point rookDestination = move.from + Point(-1,0); - - NamedSprite rook = m_cinterface->takeSprite(rookSquare); - m_cinterface->setSprite(rookDestination, rook); - res.addPreAnimation(Animate::move(rook, rookDestination)); - } - - return res; -} - -template -AnimationGroupPtr SimpleAnimator::back(const Position& final, const Move& move) { - AnimationFactory res(m_cinterface->inner()); - - NamedSprite piece = m_cinterface->takeSprite(move.to); - NamedSprite captured; - if (Piece captured_piece = final.get(move.to)) { - captured = m_cinterface->setPiece(move.to, captured_piece, false); - res.addPreAnimation(Animate::appear(captured)); - } - - if (!piece) { - piece = m_cinterface->createPiece(move.to, final.get(move.from), false); - res.addPreAnimation(Animate::appear(piece)); - } - - m_cinterface->setSprite(move.from, piece); - - - if (move.type() == Move::EnPassantCapture) { - Point phantom(move.to.x, move.from.y); - - if (Piece pawn_piece = final.get(phantom)) { - NamedSprite captured_pawn = m_cinterface->setPiece(phantom, pawn_piece, false); - res.addPreAnimation(Animate::appear(captured_pawn)); - } - } - else if (move.type() == Move::Promotion) { - Piece pawn_piece = final.get(move.from); - if (pawn_piece) { - NamedSprite pawn = m_cinterface->createPiece(move.to, pawn_piece, false); - res.addPreAnimation(Animate::morph(piece, pawn)); - - // replace piece with pawn - m_cinterface->setSprite(move.from, pawn); - piece = pawn; - } - } - else if (move.type() == Move::KingSideCastling) { - Point rookSquare = move.to + Point(1,0); - Point rookDestination = move.from + Point(1,0); - - NamedSprite rook = m_cinterface->takeSprite(rookDestination); - m_cinterface->setSprite(rookSquare, rook); - - res.addPreAnimation(Animate::move(rook, rookSquare)); - } - else if (move.type() == Move::QueenSideCastling) { - Point rookSquare = move.to + Point(-2,0); - Point rookDestination = move.from + Point(-1,0); - - NamedSprite rook = m_cinterface->takeSprite(rookDestination); - m_cinterface->setSprite(rookSquare, rook); - - res.addPreAnimation(Animate::move(rook, rookSquare)); - } - - res.addPreAnimation(*movement(piece, move.to, move.from)); - - return res; - -} - -#endif // XCHESS__ANIMATOR_IMPL_H - - diff --git a/src/variants/xchess/dropanimator.h b/src/variants/xchess/dropanimator.h deleted file mode 100644 index beb848f..0000000 --- a/src/variants/xchess/dropanimator.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef XCHESS__DROPANIMATOR_H -#define XCHESS__DROPANIMATOR_H - -#include "animator.h" - -template -class DropAnimatorMixin : public Base { - typedef typename Base::Variant Variant; - using Base::m_cinterface; -protected: - typedef typename Base::API API; - typedef typename Base::Position Position; - typedef typename Base::Piece Piece; - typedef typename Base::Move Move; - - virtual void updatePool(const Position& final); -public: - DropAnimatorMixin(API cinterface); - - virtual AnimationGroupPtr warp(const Position& final); - virtual AnimationGroupPtr forward(const Position& final, const Move& move); - virtual AnimationGroupPtr back(const Position& final, const Move& move); -}; - -#endif // XCHESS__DROPANIMATOR_H - diff --git a/src/variants/xchess/dropanimator.impl.h b/src/variants/xchess/dropanimator.impl.h deleted file mode 100755 index 68938d2..0000000 --- a/src/variants/xchess/dropanimator.impl.h +++ /dev/null @@ -1,102 +0,0 @@ -#ifndef XCHESS__DROPANIMATOR_IMPL_H -#define XCHESS__DROPANIMATOR_IMPL_H - -#include "dropanimator.h" -#include "animator.impl.h" - -#include - -template -DropAnimatorMixin::DropAnimatorMixin(API cinterface) -: Base(cinterface) { } - - -template -void DropAnimatorMixin::updatePool(const Position& final) { - for(int color = 0; color < 2; color++) { - typename Piece::Color c = static_cast(color); - const typename Position::PlayerPool& before = m_cinterface->position()->rawPool(c); - const typename Position::PlayerPool& after = final.rawPool(c); - typename Position::PlayerPool::const_iterator before_it = before.begin(); - typename Position::PlayerPool::const_iterator after_it = after.begin(); - - int pos = 0; - - // oh, a nice bunch of write-only magic shit - while(before_it != before.end() || after_it != after.end()) { - bool skip_after = (after_it == after.end() || (before_it != before.end() - && before_it->first < after_it->first )); - bool skip_before = (before_it == before.end() || (after_it != after.end() - && after_it->first < before_it->first )); - int na = skip_after ? 0 : after_it->second; - int nb = skip_before ? 0 : before_it->second; - - if(nb < na) { - for(int i = nb; i < na; i++) - m_cinterface->insertPoolPiece(color, pos + (i - nb), Piece(c, after_it->first) ); - } - else if(na < nb) { - for(int i = na; i < nb; i++) - m_cinterface->removePoolSprite(color, pos); - } - - if(!skip_before) - ++before_it; - if(!skip_after) { - pos += after_it->second; - ++after_it; - } - } - } -} - -template -AnimationGroupPtr DropAnimatorMixin::warp(const Position& final) { - updatePool(final); - return Base::warp(final); -} - -template -AnimationGroupPtr DropAnimatorMixin::forward(const Position& final, const Move& move) { - AnimationFactory res(m_cinterface->inner()); - - if(move.drop()) { - std::pair dropped = m_cinterface->droppedPoolPiece(); - if(dropped.first != -1 && dropped.second != -1 - && m_cinterface->position()->pool(dropped.first).get(dropped.second) == move.drop()) { - NamedSprite drop = m_cinterface->takePoolSprite(dropped.first, dropped.second); - m_cinterface->setSprite(move.to, drop); - res.addPreAnimation(Animate::move(drop, move.to)); - return res; - } - else { - NamedSprite drop = m_cinterface->setPiece(move.to, move.drop(), false); - res.addPreAnimation(Animate::appear(drop)); - } - } - else { - res.setGroup(Base::forward(final, move)); - } - - updatePool(final); - return res; -} - -template -AnimationGroupPtr DropAnimatorMixin::back(const Position& final, const Move& move) { - AnimationFactory res(m_cinterface->inner()); - - if(move.drop()) { - NamedSprite drop = m_cinterface->takeSprite(move.to); - res.addPostAnimation(Animate::disappear(drop)); - - } - else { - res.setGroup(Base::back(final, move)); - } - - updatePool(final); - return res; -} - -#endif // XCHESS__DROPANIMATOR_IMPL_H diff --git a/src/variants/xchess/generator.h b/src/variants/xchess/generator.h deleted file mode 100644 index d8412ba..0000000 --- a/src/variants/xchess/generator.h +++ /dev/null @@ -1,175 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#ifndef GENERATOR_H -#define GENERATOR_H - -#include -#include "variants/xchess/piecetype.h" -#include "point.h" - -template -class LegalMove { - const Pos& m_pos; - typedef typename Pos::Move Move; -public: - bool operator()(Move& m) { return m_pos.testMove(m); } - LegalMove(const Pos& pos) : m_pos(pos) { } -}; - -template -class PseudolegalMove { - const Pos& m_pos; - typedef typename Pos::Move Move; -public: - bool operator()(Move& m) { return m_pos.pseudolegal(m); } - PseudolegalMove(const Pos& pos) : m_pos(pos) { } -}; - -template -class AbstractGenerator { -public: - virtual ~AbstractGenerator(){}; - virtual std::vector& generate() = 0; -}; - -template -class Generator : public AbstractGenerator { -public: - virtual ~Generator(){}; - - typedef typename Pos::Move Move; - typedef typename Pos::Piece Piece; - typedef typename Pos::Board Board; - - std::vector& generate(); - inline std::vector& moves() { return m_moves; } - explicit Generator(const Pos&); -protected: - const Pos& m_pos; - std::vector m_moves; - void generateFrom(const Point& p); - MoveTest m_test; - - bool addMove(const Point& p, const Point& q); - void generateSlide(const Point& p, const Point& dir); - void generateBishop(const Point& p); - void generateRook(const Point& p); -}; - -template -class MoveGenerator : public Generator { -public: - virtual ~MoveGenerator(){}; - explicit MoveGenerator(const Pos& pos) - : Generator(pos) { } -}; - -//BEGIN Implementation - -template -Generator::Generator(const Pos& pos) -: m_pos(pos) -, m_test(pos) { } - -template -std::vector& Generator::generate() { - for (Point from = m_pos.first(); from <= m_pos.last(); from = m_pos.next(from)) { - generateFrom(from); - } - return m_moves; -} - -template -void Generator::generateFrom(const Point& p) { - Piece piece = m_pos[p]; - if (piece && piece.color() == m_pos.turn()) { - switch (piece.type()) { - case PAWN: - { - Point dir = Piece::direction(piece.color()); - addMove(p, p + dir); - addMove(p, p + dir * 2); - addMove(p, p + dir + Point(1, 0)); - addMove(p, p + dir - Point(1, 0)); - } - break; - case KNIGHT: - addMove(p, p + Point(1,2)); - addMove(p, p + Point(1,-2)); - addMove(p, p + Point(-1,2)); - addMove(p, p + Point(-1,-2)); - addMove(p, p + Point(2,1)); - addMove(p, p + Point(2,-1)); - addMove(p, p + Point(-2,1)); - addMove(p, p + Point(-2,-1)); - break; - case BISHOP: - generateBishop(p); - break; - case ROOK: - generateRook(p); - break; - case QUEEN: - generateBishop(p); - generateRook(p); - break; - case KING: - addMove(p, p + Point(1,0)); - addMove(p, p + Point(1,1)); - addMove(p, p + Point(0,1)); - addMove(p, p + Point(-1,1)); - addMove(p, p + Point(-1,0)); - addMove(p, p + Point(-1,-1)); - addMove(p, p + Point(0,-1)); - addMove(p, p + Point(1,-1)); - break; - default: - break; - } - } -} - -template -void Generator::generateSlide(const Point& p, const Point& dir) { - Point q = p + dir; - while (m_pos.valid(q) && addMove(p,q)) - q += dir; -} - -template -bool Generator::addMove(const Point& p, const Point& q) { - Move move(p,q); - if (m_pos.valid(p) && m_pos.valid(q) && m_test(move)) { - m_moves.push_back(move); - return true; - } - else return false; -} - -template -void Generator::generateBishop(const Point& p) { - generateSlide(p, Point(1,1)); - generateSlide(p, Point(1,-1)); - generateSlide(p, Point(-1,1)); - generateSlide(p, Point(-1,-1)); -} - -template -void Generator::generateRook(const Point& p) { - generateSlide(p, Point(1,0)); - generateSlide(p, Point(0,1)); - generateSlide(p, Point(-1,0)); - generateSlide(p, Point(0,-1)); -} - -//END Implementation - -#endif diff --git a/src/variants/xchess/move.cpp b/src/variants/xchess/move.cpp deleted file mode 100644 index 9be91db..0000000 --- a/src/variants/xchess/move.cpp +++ /dev/null @@ -1,52 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#include "move.h" -#include "piece.h" -#include "usermove.h" - - -ChessMove::ChessMove(Point from, Point to, PieceType promotionType) -: m_type(Invalid) -, from(from), to(to) -, promotionType(promotionType) -, status(Untested) {} - -ChessMove::ChessMove() -: m_type(Invalid) -, promotionType(INVALID_TYPE) -, status(Untested) {} - -QString ChessMove::promotionSymbol() const { - return ChessPiece::typeSymbol(promotionType); -} - -QString ChessMove::toString(int xsize, int ysize) const { - QString res = from.toString(ysize) + to.toString(ysize); - if (m_type == Promotion) - res = res + "=" + promotionSymbol(); - - return res; -} - -bool ChessMove::operator==(const ChessMove& other) const { - return from == other.from - && to == other.to - && promotionType == other.promotionType; -} - -bool ChessMove::operator!=(const ChessMove& other) const { - return !operator==(other); -} - -ChessMove ChessMove::createDropMove(int pool, int piece_index, const Point&) { - return ChessMove(); -} - diff --git a/src/variants/xchess/move.h b/src/variants/xchess/move.h deleted file mode 100644 index 165d99f..0000000 --- a/src/variants/xchess/move.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#ifndef MOVE_H -#define MOVE_H - -#include "point.h" -#include "piecetype.h" -#include "moveserializer.h" - - -class ChessMove { -public: - enum Type { - Invalid = 0, - Normal, - EnPassantCapture, - EnPassantTrigger, - KingSideCastling, - QueenSideCastling, - Promotion - }; - Type m_type; -public: - enum Status { - Untested, - Legal, - Illegal - }; - Point from, to; - PieceType promotionType; - Status status; - Type type() const { return m_type; } - void setType(Type value) { m_type = value; } - - QString promotionSymbol() const; - - ChessMove(Point from, Point to, PieceType promotionType = INVALID_TYPE); - ChessMove(); - - static ChessMove invalid() { return ChessMove(Point::invalid(), Point::invalid()); } - inline bool valid() const { return from != Point::invalid() && to != Point::invalid(); } - QString toString(int xsize, int ysize) const; - bool operator==(const ChessMove& other) const; - bool operator!=(const ChessMove& other) const; - - static ChessMove createDropMove(int pool, int piece_index, const Point&); -}; - -#endif // MOVE_H diff --git a/src/variants/xchess/piece.cpp b/src/variants/xchess/piece.cpp deleted file mode 100644 index bae5ad8..0000000 --- a/src/variants/xchess/piece.cpp +++ /dev/null @@ -1,201 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#include "piece.h" - -ChessPiece::ChessPiece(ChessPiece::Color color, ChessPiece::Type type) -: m_color(color) -, m_type(type) { } - -ChessPiece::ChessPiece(const ChessPiece& other) -: m_color(other.color()) -, m_type(other.type()) { } - -ChessPiece& ChessPiece::operator=(const ChessPiece& other) { - m_color = other.m_color; - m_type = other.m_type; - return *this; -} - -ChessPiece* ChessPiece::clone() const { - return new ChessPiece(*this); -} - -bool ChessPiece::equals(const ChessPiece& other) const { - if (other) { - if (!valid()) - return false; - else - return m_color == other.color() && m_type == other.type(); - } - else - return !valid(); -} - -bool ChessPiece::operator==(const ChessPiece& other) const { - return equals(other); -} - -bool ChessPiece::sameColor(const ChessPiece& other) const { - return other && (m_color == other.color()); -} - -int ChessPiece::id() const { - int res = static_cast(type()); - if (color() == BLACK) res = -res - 1; - return res; -} - -ChessPiece::Color ChessPiece::colorFromId(int x) { - return x < 0 ? BLACK : WHITE; -} - -ChessPiece::Type ChessPiece::typeFromId(int x) { - return static_cast(x<0 ? (-1-x) : x); -} - -QString ChessPiece::typeName() const { - switch (m_type) { - case ROOK: - return "rook"; - case BISHOP: - return "bishop"; - case KNIGHT: - return "knight"; - case QUEEN: - return "queen"; - case KING: - return "king"; - case PAWN: - return "pawn"; - default: - return "unknown"; - } -} - -QString ChessPiece::name() const { - switch(m_color) { - case WHITE: - switch (m_type) { - case ROOK: - return "white_rook"; - case BISHOP: - return "white_bishop"; - case KNIGHT: - return "white_knight"; - case QUEEN: - return "white_queen"; - case KING: - return "white_king"; - case PAWN: - return "white_pawn"; - default: - return "unknown"; - } - case BLACK: - switch (m_type) { - case ROOK: - return "black_rook"; - case BISHOP: - return "black_bishop"; - case KNIGHT: - return "black_knight"; - case QUEEN: - return "black_queen"; - case KING: - return "black_king"; - case PAWN: - return "black_pawn"; - default: - return "unknown"; - } - default: - return "unknown"; - } -} - -ChessPiece::Type ChessPiece::getType(const QString& str) { - // assume the string is well formatted - if (str.isEmpty()) - return PAWN; - - char c = str[0].toAscii(); - switch (c) { - case 'Q': - case 'q': - return QUEEN; - case 'N': - case 'n': - return KNIGHT; - case 'B': - case 'b': - return BISHOP; - case 'K': - case 'k': - return KING; - case 'R': - case 'r': - return ROOK; - case 'P': - case 'p': - return PAWN; - default: - return INVALID_TYPE; - } -} - -QString ChessPiece::typeSymbol(ChessPiece::Type type) { - switch (type) { - case QUEEN: - return "Q"; - case KING: - return "K"; - case KNIGHT: - return "N"; - case BISHOP: - return "B"; - case ROOK: - return "R"; - case PAWN: - return "P"; - default: - return "?"; - } -} - -std::ostream& operator<<(std::ostream& os, const ChessPiece& p) { - return os << (p.color() == WHITE? "white" : p.color() == BLACK? "black" : "unknown") - << " " << p.typeName(); -} - -ChessPiece ChessPiece::fromDescription(const QString& description) { - if (description.size() == 1) { - QChar c = description[0]; - ChessPiece::Color color; - - if (c.category() == QChar::Letter_Uppercase) { - color = WHITE; - } - else if (c.category() == QChar::Letter_Lowercase) { - color = BLACK; - } - else { - return ChessPiece(); - } - - ChessPiece::Type type = ChessPiece::getType(c); - - if (type != INVALID_TYPE) { - return ChessPiece(color, type); - } - } - - return ChessPiece(); -} diff --git a/src/variants/xchess/piece.h b/src/variants/xchess/piece.h deleted file mode 100644 index 865e0b3..0000000 --- a/src/variants/xchess/piece.h +++ /dev/null @@ -1,195 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#ifndef PIECE_H -#define PIECE_H - -#include - -#include "point.h" -#include "move.h" -#include "piecetype.h" -#include "common.h" -#include "pathinfo.h" - - -class ChessPiece { -public: - typedef PieceType Type; - typedef Type PromotionType; - typedef PieceColor Color; -protected: - typedef ChessMove Move; -private: - Color m_color; - Type m_type; -public: - ChessPiece(Color = INVALID_COLOR, Type = INVALID_TYPE); - ChessPiece(const ChessPiece& p); - ChessPiece& operator=(const ChessPiece& p); - ChessPiece* clone() const; - - virtual ~ChessPiece() {} - inline static Color oppositeColor(Color color) { return color == WHITE ? BLACK : - color == BLACK ? WHITE : INVALID_COLOR; } - - inline Color color() const { return m_color; } - inline Type type() const { return m_type; } - inline bool valid() const { return m_color != INVALID_COLOR && - m_type != INVALID_TYPE; } - QString typeName() const; - QString name() const; - - template - Move::Type canMove(const Pos& position, Point from, Point to) const; - - bool equals(const ChessPiece& other) const; - bool sameColor(const ChessPiece& other) const; - int id() const; - static Color colorFromId(int); - static Type typeFromId(int); - - static Type getType(const QString& str); - static QString typeSymbol(Type type); - static ChessPiece fromDescription(const QString& description); - - inline Point kingStartingPosition() const { return Point(4, color() == WHITE? 7 : 0); } - inline int startingRank() const { return color() == WHITE? 6 : 1; } - inline Point direction() const { return direction(color()); } - static Point direction(Color color) { return Point(0, color == WHITE? -1 : 1); } - inline int promotionRank() const { return color() == WHITE? 0 : 7; } - bool operator==(const ChessPiece& p) const; - bool operator!=(const ChessPiece& p) const { return !operator==(p); } - bool operator!() const { return !valid(); } - bool operator<(const ChessPiece& p) const { - return m_color != p.m_color ? - m_color < p.m_color : - m_type < p.m_type; } - operator bool() const { return valid(); } -}; - -std::ostream& operator<<(std::ostream& os, const ChessPiece& p); - -template -ChessPiece::Move::Type ChessPiece::canMove(const Pos& position, - Point from, Point to) const { - switch (m_type) - { - - case ROOK: - { - if (from == to) - return Move::Invalid; - PathInfo path = position.path(from, to); - if (path.parallel() && path.clear() && !sameColor(position[to])) - return Move::Normal; - else - return Move::Invalid; - } - - case BISHOP: - { - if (from == to) - return Move::Invalid; - PathInfo path = position.path(from, to); - if (path.diagonal() && path.clear() && !sameColor(position[to])) - return Move::Normal; - else - return Move::Invalid; - } - - case KNIGHT: - if (sameColor(position[to])) - return Move::Invalid; - else - { - Point delta = from - to; - if (abs(delta.x) == 1 && abs(delta.y) == 2) - return Move::Normal; - if (abs(delta.y) == 1 && abs(delta.x) == 2) - return Move::Normal; - return Move::Invalid; - } - - case QUEEN: - { - if (from == to) - return Move::Invalid; - PathInfo path = position.path(from, to); - if (path.valid() && path.clear() && !sameColor(position[to])) - return Move::Normal; - else - return Move::Invalid; - } - - case KING: - { - if (from == to) - return Move::Invalid; - Point delta = to - from; - if (abs(delta.x) <= 1 && abs(delta.y) <= 1 && !sameColor(position[to])) - return Move::Normal; - else if (from == kingStartingPosition()) { - if (delta == Point(2,0)) { - if (!position[from + Point(1,0)] && - !position[to] && - position.castleKing(color())) - return Move::KingSideCastling; - } - else if (delta == Point(-2,0)) { - if (!position[from - Point(1,0)] && - !position[from - Point(2,0)] && - !position[to] && - position.castleQueen(color())) - return Move::QueenSideCastling; - } - } - return Move::Invalid; - } - - case PAWN: - { - ChessPiece destinationPiece = position[to]; - Point delta = to - from; - bool enPassant = position.enPassantSquare() == to; - - // moving - if (!destinationPiece && !enPassant) { - if (delta == direction()) - if (to.y == promotionRank()) - return Move::Promotion; - else - return Move::Normal; - if (from.y == startingRank() && delta == direction() * 2 - && !position[from + direction()]) - return Move::EnPassantTrigger; - else - return Move::Invalid; - } - - // capturing - else if (enPassant || !sameColor(destinationPiece)) - if (delta.y == direction().y && abs(delta.x) == 1) - if (enPassant) - return Move::EnPassantCapture; - else if (to.y == promotionRank()) - return Move::Promotion; - else - return Move::Normal; - - return Move::Invalid; - } - - default: - return Move::Invalid; - } -} - -#endif // PIECE_H diff --git a/src/variants/xchess/piecetype.h b/src/variants/xchess/piecetype.h deleted file mode 100644 index ad011af..0000000 --- a/src/variants/xchess/piecetype.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#ifndef PIECETYPE_H -#define PIECETYPE_H - -enum PieceColor { - INVALID_COLOR = -1, - WHITE = 0, - BLACK = 1 -}; - -enum PieceType { - INVALID_TYPE = -1, - PAWN, - KNIGHT, - BISHOP, - ROOK, - QUEEN, - KING -}; - -#endif // PIECETYPE_H diff --git a/src/variants/xchess/pool.h b/src/variants/xchess/pool.h deleted file mode 100644 index 0d831e5..0000000 --- a/src/variants/xchess/pool.h +++ /dev/null @@ -1,210 +0,0 @@ -#ifndef XCHESS__POOL_H -#define XCHESS__POOL_H - -#include - -template -struct PlayerPoolType { - typedef std::map type; -}; - -namespace MapUtilities { - -template -struct MapEqual { - static bool apply(const T& a, const T& b) { - return a == b; - } -}; - -template -struct MapEqual > { - typedef std::map Map; - static bool apply(const Map& a, const Map& b) { - typename Map::const_iterator i = a.begin(); - typename Map::const_iterator j = b.begin(); - - while (i != a.end() && j != b.end()) { - if (i->first < j->first) { - if (!MapEqual::apply(i->second, U())) - return false; - else - ++i; - } - else if (i->first > j->first) { - if (!MapEqual::apply(j->second, U())) - return false; - else - ++j; - } - else { - // same key, compare values - if (!MapEqual::apply(i->second, j->second)) - return false; - else { - ++i; - ++j; - } - } - } - - // check tail - while (i != a.end()) { - if (!MapEqual::apply(i->second, U())) - return false; - ++i; - } - while (j != b.end()) { - if (!MapEqual::apply(j->second, U())) - return false; - ++j; - } - - return true; - } -}; - -template -bool equal(const T& a, const T& b) { - return MapEqual::apply(a, b); -} - -} - -template -class PoolReferenceBase { -protected: - typedef typename Position::Piece Piece; - typedef typename Piece::Color Color; - typedef typename Piece::Type Type; -public: - class Pool { - typedef std::map Map; - Map m_map; - public: - Pool() { } - - PP& operator[](Color color) { return m_map[color]; } - const PP& operator[](Color color) const { return const_cast(m_map)[color]; } - bool operator==(const Pool& other) const { - return MapUtilities::equal(m_map, other.m_map); - } - - typedef typename Map::iterator iterator; - iterator begin() { return m_map.begin(); } - iterator end() { return m_map.end(); } - - typedef typename Map::const_iterator const_iterator; - const_iterator begin() const { return m_map.begin(); } - const_iterator end() const { return m_map.end(); } - }; -protected: - PP* m_p_pool; - const Color m_color; -public: - PoolReferenceBase(PP* p, Color color) - : m_p_pool(p) - , m_color(color) { } - - int size(); - Piece get(int idx); -}; - -template -class PoolReference : public PoolReferenceBase::type > { -public: - typedef typename PlayerPoolType::type PlayerPool; - typedef PoolReferenceBase Base; - typedef typename Base::Piece Piece; - typedef typename Base::Type Type; - typedef typename Base::Color Color; - - PoolReference(PlayerPool* p, Color color) - : PoolReferenceBase(p, color) { } - - int insert(int idx, const Piece& p); - Piece take(int idx); -}; - -template -class PoolConstReference : public PoolReferenceBase::type> { - typedef const typename PlayerPoolType::type PlayerPool; - typedef typename Position::Piece::Color Color; -public: - PoolConstReference(PlayerPool* p, Color color) - : PoolReferenceBase(p, color) { } -}; - -template -int PoolReferenceBase::size() { - if (!m_p_pool) - return 0; - - int retv = 0; - for (typename PP::const_iterator i = m_p_pool->begin(); i != m_p_pool->end(); ++i) - retv += i->second; - return retv; -} - -template -typename PoolReferenceBase::Piece PoolReferenceBase::get(int idx) { - if (idx < 0) - return Piece(); - - int fill = 0; - for (typename PP::const_iterator i = m_p_pool->begin(); i != m_p_pool->end(); ++i) { - if (idx < fill + i->second) - return Piece(m_color, i->first); - fill += i->second; - } - return Piece(); -} - -template -int PoolReference::insert(int idx, const Piece& p) { - if (this->m_color != p.color()) { - ERROR("Inserting a piece in the wrong pool?"); - return -1; - } - - int fill = 0; - for (typename PlayerPool::iterator i = this->m_p_pool->begin(); - (i != this->m_p_pool->end()) && i->first < p.type(); - ++i) { - fill += i->second; - } - - int nump = ++(*this->m_p_pool)[p.type()]; - - if (idx < fill) - return fill; - if (idx >= fill + nump) - return fill + nump - 1; - return idx; -} - - -template -typename PoolReference::Piece PoolReference::take(int idx) { - if (idx < 0) - return Piece(); - - int fill = 0; - for(typename PlayerPool::iterator i = this->m_p_pool->begin(); - i != this->m_p_pool->end(); - ++i) { - if(idx < fill + i->second) { - Type t = i->first; - if(!--i->second) - this->m_p_pool->erase(i); - return Piece(this->m_color, t); - } - fill += i->second; - } - return Piece(); -} - -#endif // XCHESS__POOL_H - diff --git a/src/variants/xchess/position.h b/src/variants/xchess/position.h deleted file mode 100644 index 9bc9f17..0000000 --- a/src/variants/xchess/position.h +++ /dev/null @@ -1,883 +0,0 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - 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. -*/ - -#ifndef POSITION_H -#define POSITION_H - -#include -#include -#include -#include - -#include -#include -#include - -#include "common.h" -#include "pathinfo.h" -#include "option.h" -#include "interactiontype.h" -#include "turnpolicy.h" -#include "variants/xchess/generator.h" -#include "variants/xchess/piecetype.h" - -class AlgebraicNotation; -class VerboseNotation; - -#define POLYMORPHIC_NEW(type, argsdecl, args) \ - virtual type* _new argsdecl const { return new type args; } - -template -class Position; - -template -class Position { -public: - typedef typename P::Type Type; - typedef typename P::Color Color; -protected: - Color m_turn; - - B m_board; - Point m_enPassantSquare; - - bool m_castleWhiteKing : 1; - bool m_castleWhiteQueen : 1; - bool m_castleBlackKing : 1; - bool m_castleBlackQueen : 1; - -public: - typedef M Move; - typedef P Piece; - typedef B Board; - - Position(int = 8, int = 8); - Position(const OptList& l, int = 8, int = 8); - Position(Color turn, - bool castleWhiteKing, bool castleWhiteQueen, - bool castleBlackKing, bool castleBlackQueen, - const Point& enPassant); - Position(const Position&); - template - Position(const Position&); - - Point size() const { return m_board.getSize(); } - - POLYMORPHIC_NEW(Position, (), ()) - POLYMORPHIC_NEW(Position, (const Position* other), (*other)) - virtual Position* clone() const { return new Position(*this); } - virtual ~Position(); - - virtual QString type() const { return "chess"; } - - bool operator==(const Position&) const; - bool operator!=(const Position&) const; - - virtual void setup(); - - void debugInfo() const; - - virtual const Point first() const { return m_board.first(); } - virtual const Point last() const { return m_board.last(); } - virtual Point next(const Point& p) const { return m_board.next(p); } - inline bool valid(const Point& p) const { return m_board.valid(p); } - - PathInfo path(const Point&, const Point&) const; - const Point& enPassantSquare() const { return m_enPassantSquare; } - - virtual P get(const Point& p) const { - return valid(p) ? m_board[p] : P(); - } - virtual P operator[](const Point& p) const { return get(p); } - inline void set(const Point& p, const P& piece) { - if(!valid(p)) - return; - m_board[p] = piece; - } - inline void removePiece(const Point& p) { set(p, 0); } - inline void basicMovePiece(const M&); - - inline InteractionType movable(const TurnTest& test, const Point& p) const { - if (!valid(p) || !m_board[p] || !test(m_board[p].color())) - return NoAction; - - return m_board[p].color() == m_turn ? Moving : Premoving; - } - inline InteractionType droppable(const TurnTest& test, int p) const { - if (!test(m_turn)) - return NoAction; - - Color c = static_cast(p); - return c == m_turn ? Moving : Premoving; - } - inline Color turn() const { return m_turn; } - inline Color previousTurn() const { return Piece::oppositeColor(m_turn); } - inline const B& board() const { return m_board; } - inline bool castleKing(Color color) const { return color == WHITE? m_castleWhiteKing : m_castleBlackKing; } - inline bool castleQueen(Color color) const { return color == WHITE? m_castleWhiteQueen : m_castleBlackQueen; } - - virtual bool attacks(Color, const Point& destination, Point& source) const; - virtual bool attacks(Color, const Point& destination) const; - virtual bool attacks(const Point& destination) const; - - Point findPiece(Color, typename P::Type) const; - virtual void switchTurn(); - virtual void setTurn(Color turn); - - typename P::Type promotionType() const { return QUEEN; } - - virtual void move(const M&); - virtual Position* legallyMove(M&) const; - void stepBack(const Position* oldPosition, const M& move); - virtual Color moveTurn(const M& move) const; - virtual bool checkPromotionType(const M&) const; - virtual bool pseudolegal(M&) const; - virtual bool testMove(M&) const; - virtual bool testPremove(M) const; - - virtual M getMove(const AlgebraicNotation& san, bool& ok) const; -// virtual M getMove(const QString& san, bool& ok) const; - static M getVerboseMove(Color, const VerboseNotation& m); - - //FIXME: Those are really very chess dependent, make them virtual at least - static Point kingStartingPosition(Color color) { return color == WHITE? Point(4,7) : Point(4,0); } - - inline M kingCastleMove() const { return m_turn == WHITE? - M(Point(4, 7), Point(6, 7)) : M(Point(4,0), Point(6,0)); } - inline M queenCastleMove() const { return m_turn == WHITE? - M(Point(4, 7), Point(2, 7)) : M(Point(4,0), Point(2,0)); } - - enum State { - InPlay, - WhiteWins, - BlackWins, - Draw - }; - State state() const; - virtual boost::shared_ptr > createLegalGenerator() const; - virtual bool stalled() const; -// virtual bool pseudoStalled() const; - bool check() const; - bool check(Color) const; - - virtual void basicDropPiece(P* piece, const Point& to); - virtual void executeCaptureOn(const Point& point); - - boost::shared_ptr

moveHint(const M&) const; - - void dump() const; - QString fen(int, int) const; - void fromFEN(const QString& fen, bool&); - QStringList borderCoords() const; -}; - -std::ostream& operator<<(std::ostream& os, const class GenericPosition&); - - -#include -#include "algebraicnotation.h" - -template -Position::Position(int width, int height) -: m_turn(WHITE) -, m_board(width, height) -, m_enPassantSquare(Point::invalid()) -, m_castleWhiteKing(true) -, m_castleWhiteQueen(true) -, m_castleBlackKing(true) -, m_castleBlackQueen(true) { } - -template -Position::Position(const OptList&, int width, int height) -: m_turn(WHITE) -, m_board(width, height) -, m_enPassantSquare(Point::invalid()) -, m_castleWhiteKing(true) -, m_castleWhiteQueen(true) -, m_castleBlackKing(true) -, m_castleBlackQueen(true) { } - -template -Position::Position(Color turn, bool castleWhiteKing, - bool castleWhiteQueen, - bool castleBlackKing, - bool castleBlackQueen, - const Point& enPassant) -: m_turn(turn) -, m_board(8, 8) -, m_enPassantSquare(enPassant) -, m_castleWhiteKing(castleWhiteKing) -, m_castleWhiteQueen(castleWhiteQueen) -, m_castleBlackKing(castleBlackKing) -, m_castleBlackQueen(castleBlackQueen) { } - -template -Position::Position(const Position& other) -: m_turn(other.m_turn) -, m_board(other.m_board) -, m_enPassantSquare(other.m_enPassantSquare) -, m_castleWhiteKing(other.m_castleWhiteKing) -, m_castleWhiteQueen(other.m_castleWhiteQueen) -, m_castleBlackKing(other.m_castleBlackKing) -, m_castleBlackQueen(other.m_castleBlackQueen) { } - -template -template -Position::Position(const Position& other) -: m_turn(other.turn()) -, m_board(other.board()) -, m_enPassantSquare(other.enPassantSquare()) -, m_castleWhiteKing(other.castleKing(WHITE)) -, m_castleWhiteQueen(other.castleQueen(WHITE)) -, m_castleBlackKing(other.castleKing(BLACK)) -, m_castleBlackQueen(other.castleQueen(BLACK)) { } - - -template -Position::~Position() { - -} - -template -QStringList Position::borderCoords() const -{ - QStringList retv; - Point p = m_board.getSize(); - for(int i=0; i -bool Position::operator==(const Position& other) const { -#if 0 - std::cout << "turn: " << m_turn << " " << other.m_turn << std::endl; - std::cout << "ep: " << m_enPassantSquare << " " << other.m_enPassantSquare << std::endl; - std::cout << "wk: " << m_castleWhiteKing << " " << other.m_castleWhiteKing << std::endl; - std::cout << "wq: " << m_castleWhiteQueen << " " << other.m_castleWhiteQueen << std::endl; - std::cout << "bk: " << m_castleBlackKing << " " << other.m_castleBlackKing << std::endl; - std::cout << "bq: " << m_castleBlackQueen << " " << other.m_castleBlackQueen << std::endl; - if (m_turn != other.m_turn) { - std::cout << "turns differ" << std::endl; - return false; - } - if (!(m_board == other.m_board)) { - std::cout << "boards differ" << std::endl; - return false; - } - if (m_castleWhiteKing != other.m_castleWhiteKing) { - std::cout << "wk differ" << std::endl; - return false; - } - if (m_castleWhiteQueen != other.m_castleWhiteQueen) { - std::cout << "wq differ" << std::endl; - return false; - } - if (m_castleBlackKing != other.m_castleBlackKing) { - std::cout << "bk differ" << std::endl; - return false; - } - if (m_castleBlackQueen != other.m_castleBlackQueen) { - std::cout << "bq differ" << std::endl; - return false; - } - std::cout << "equal" << std::endl; - return true; -#else - return m_turn == other.m_turn && - m_board == other.m_board && - m_enPassantSquare == other.m_enPassantSquare && - m_castleWhiteKing == other.m_castleWhiteKing && - m_castleWhiteQueen == other.m_castleWhiteQueen && - m_castleBlackKing == other.m_castleBlackKing && - m_castleBlackQueen == other.m_castleBlackQueen; -#endif -} - -template -bool Position::operator!=(const Position& other) const { - return !(*this == other); -} - -template -void Position::debugInfo() const { -#if 0 - std::cout << "board = " << &m_board << std::endl; -#endif -} - -#define SET_PIECE(i,j, color, type) m_board[Point(i,j)] = P(color, type) -template -void Position::setup() { - for (int i = 0; i < 8; i++) { - SET_PIECE(i, 1, BLACK, PAWN); - SET_PIECE(i, 6, WHITE, PAWN); - } - - SET_PIECE(0,0, BLACK, ROOK); - SET_PIECE(1,0, BLACK, KNIGHT); - SET_PIECE(2,0, BLACK, BISHOP); - SET_PIECE(3,0, BLACK, QUEEN); - SET_PIECE(4,0, BLACK, KING); - SET_PIECE(5,0, BLACK, BISHOP); - SET_PIECE(6,0, BLACK, KNIGHT); - SET_PIECE(7,0, BLACK, ROOK); - - SET_PIECE(0,7, WHITE, ROOK); - SET_PIECE(1,7, WHITE, KNIGHT); - SET_PIECE(2,7, WHITE, BISHOP); - SET_PIECE(3,7, WHITE, QUEEN); - SET_PIECE(4,7, WHITE, KING); - SET_PIECE(5,7, WHITE, BISHOP); - SET_PIECE(6,7, WHITE, KNIGHT); - SET_PIECE(7,7, WHITE, ROOK); - - m_turn = WHITE; - m_enPassantSquare = Point::invalid(); - m_castleWhiteKing = true; - m_castleWhiteQueen = true; - m_castleBlackKing = true; - m_castleBlackQueen = true; -} -#undef SET_PIECE - -template -PathInfo Position::path(const Point& from, const Point& to) const { - return m_board.path(from, to); -} -/* -template -void Position::movePiece(const M& move) { - executeCaptureOn(move.to); - basicMovePiece(move); - switchTurn(); -}*/ - -template -void Position::executeCaptureOn(const Point& point) { - m_board[point] = Piece(); -} - -template -void Position::basicMovePiece(const M& move) { - P p = m_board[move.from]; - if (p) { - m_board[move.to] = p; - m_board[move.from] = Piece(); - } -} - -template -void Position::basicDropPiece(P* piece, const Point& to) { - if (piece) { - m_board[to] = *piece; - switchTurn(); - } -} - -template -boost::shared_ptr

Position::moveHint(const M& move) const { - if (move.type() == M::Promotion) - return boost::shared_ptr

(new Piece(m_turn, move.promotionType)); - else - return boost::shared_ptr

(); -} - - -template -bool Position::checkPromotionType(const M& m) const { - return (m.promotionType == KNIGHT || - m.promotionType == BISHOP || - m.promotionType == ROOK || - m.promotionType == QUEEN); -} - -template -bool Position::pseudolegal(M& move) const { - if (!move.valid()) return false; - - if (!valid(move.from)) return false; - if (!valid(move.to)) return false; - P piece = m_board[move.from]; - if (!piece) return false; - Color thisTurn = piece.color(); - Color otherTurn = P::oppositeColor(thisTurn); - if (piece && (turn() == thisTurn)) { - move.setType(piece.canMove(*this, move.from, move.to)); - - if (move.type() == M::Invalid) return false; - - if (move.type() == M::Promotion && !checkPromotionType(move)) - return false; - - if (move.type() == M::KingSideCastling) { - if (attacks(otherTurn, move.from) || - attacks(otherTurn, move.from + Point(1,0))) return false; - } - else if (move.type() == M::QueenSideCastling) { - if (attacks(otherTurn, move.from) || - attacks(otherTurn, move.from + Point(-1,0))) return false; - } - - return true; - } - else return false; -} - -template -typename P::Color Position::moveTurn(const M& move) const { - P piece = m_board[move.from]; - if (piece) { - return piece.color(); - } - else { - return typename P::Color(); - } -} - -template -bool Position::testMove(M& move) const { - - if (move.status == M::Untested) { - if (pseudolegal(move)) { - Color turn = moveTurn(move); - - // check king safety - std::auto_ptr tempPosition(clone()); - tempPosition->move(move); - Point kingPos = tempPosition->findPiece(turn, KING); - - if (kingPos == Point::invalid()) - move.status = M::Illegal; - else if (tempPosition->attacks(P::oppositeColor(turn), kingPos)) - move.status = M::Illegal; - else - move.status = M::Legal; - } - else - move.status = M::Illegal; - } - - return move.status == M::Legal; -} - -template -M Position::getMove(const AlgebraicNotation& san, bool& ok) const { - M candidate = M::invalid(); - - if (san.invalid()) { - ok = false; - return candidate; - } - - if (san.castling != AlgebraicNotation::NoCastling) { - Point from = kingStartingPosition(turn()); - Point to = from + (san.castling == AlgebraicNotation::KingSide? Point(2,0) : Point(-2,0)); - P king = m_board[from]; - if (!(king && king.type() == KING)) { - ok = false; - return candidate; - } - else { - ok = true; - return M(from, to); - } - } - - if (san.from.valid()) { - candidate = M(san.from, san.to, static_cast(san.promotion)); - } - else { - for (Point i = first(); i <= last(); i = next(i)) { - P p = m_board[i]; - M mv(i, san.to, static_cast(san.promotion)); - if (i.resembles(san.from) && p && p.type() == san.type && p.color() == turn() - && testMove(mv)) { - - if (candidate.valid()) { - ok = false; - return candidate; // return the first matching move - } - else candidate = mv; - } - } - } - - ok = candidate.valid(); - return candidate; -} -/* -template -M Position::getMove(const QString& san, bool& ok) const { - return getMove(AlgebraicNotation(san), ok); -}*/ - -template -M Position::getVerboseMove(Color turn, const VerboseNotation& m) { - Point from, to; - if (m.castling == AlgebraicNotation::NoCastling) { - from = m.from; - to = m.to; - } - else { - from = kingStartingPosition(turn); - to = from + (m.castling == AlgebraicNotation::KingSide ? Point(2,0) : Point(-2, 0)); - } - - M res(from, to); - res.promotionType = static_cast(m.promotion); - - return res; -} - -template -Position* Position::legallyMove(M& mv) const { - if (testMove(mv)) { - Position* res = clone(); - res->move(mv); - return res; - } - else - return 0; -} - -template -void Position::fromFEN(const QString& fen, bool& ok) { - ok = false; - - QStringList data = fen.split(' ', QString::SkipEmptyParts); - const int boardIndex = 0; - const int turnIndex = 1; - const int castlingIndex = 2; - const int enPassantIndex = 3; - - // read turn - QChar turn = data[turnIndex][0].toLower(); - if (turn == 'w') - setTurn(WHITE); - else if (turn == 'b') - setTurn(BLACK); - - // read castling - for (int i = 0; i < data[castlingIndex].length(); i++) { - QChar symbol = data[castlingIndex][i]; - if (symbol == 'k') - m_castleBlackKing = true; - else if (symbol == 'q') - m_castleBlackQueen = true; - else if (symbol == 'K') - m_castleWhiteKing = true; - else if (symbol == 'Q') - m_castleWhiteQueen = true; - } - - // read en-passant - m_enPassantSquare = Point(data[enPassantIndex], size().y); - - // read board - Point cursor(0, 0); - for (int index = 0; index < data[boardIndex].length(); index++) { - QChar symbol = data[boardIndex][index]; - if (symbol == '/') { - cursor = Point(0, cursor.y + 1); - } - else if (symbol.isDigit()) { - int k = symbol.toAscii() - '0'; - cursor.x += k; - } - else { - if (!valid(cursor)) return; - typename P::Type type = P::getType(symbol); - if (type == INVALID_TYPE) return; - Color color = symbol.isUpper() ? WHITE : BLACK; - set(cursor, P(color, type)); - cursor.x++; - } - } - - ok = true; -} - -// adapted from Maurizio Monge's rattatechess -template -QString Position::fen(int halfmove, int fullmove) const { - QString str; - - // add board information - int cn = 0; - for (int i = 0; i < 8; i++) { - for (int j = 0; j < 8; j++) { - Point p = Point(j, i); - Piece pc = m_board[p]; - if (!pc) - cn++; - else { - if (cn > 0) { - str += QString::number(cn); - cn = 0; - } - QString symbol = P::typeSymbol(pc.type()); - if (pc.color() == BLACK) - symbol = symbol.toLower(); - str += symbol; - } - } - if (cn > 0) { - str += QString::number(cn); - cn = 0; - } - if (i != 7) - str += "/"; - } - - // add turn information - str += turn() == WHITE ? " w " : " b "; - - // add castling information - QString castleString; - if(m_castleWhiteKing) - castleString += "K"; - if(m_castleWhiteQueen) - castleString += "Q"; - if(m_castleBlackKing) - castleString += "k"; - if(m_castleBlackQueen) - castleString += "k"; - if (castleString == "") castleString = "-"; - str += castleString + " "; - - // add en passant information - if (m_enPassantSquare == Point::invalid()) - str += "-"; - else - str += m_enPassantSquare.toString(size().y); - str += " "; - - // add move count information - str += QString::number(halfmove) + " " + QString::number(fullmove); - - return str; -} - - -template -void Position::dump() const { - for (Point i = first(); i <= last(); i = next(i)) { - if (i.x == 0) - std::cout << "+---+---+---+---+---+---+---+---+" << std::endl; - - Piece piece = m_board[i]; - QString symbol; - if (piece) { - symbol = Piece::typeSymbol(piece.type()); - if (piece.color() == BLACK) symbol = symbol.toLower(); - } - else - symbol = ((i.x + i.y) % 2 == 0) ? " " : "."; - - std::cout << "| " << symbol << " "; - if (i.x == 7) - std::cout << "|" << std::endl; - } - std::cout << "+---+---+---+---+---+---+---+---+" << std::endl; -} - -template -void Position::move(const M& move) { - P piece = m_board[move.from]; - if (!piece) return; - - Color color = piece.color(); - typename P::Type type = piece.type(); - - executeCaptureOn(move.to); - basicMovePiece(move); - - if (move.type() == M::EnPassantTrigger) - m_enPassantSquare = (move.to + move.from) / 2; - else { - m_enPassantSquare = Point::invalid(); - - if (move.type() == M::EnPassantCapture) { - Point phantom(move.to.x, move.from.y); - m_board[phantom] = Piece(); - } - - else if (move.type() == M::Promotion) { - typename P::Type type = move.promotionType; - m_board[move.to] = P(piece.color(), type); - } - - else if (move.type() == M::KingSideCastling) { - Point rookSquare = move.to + Point(1,0); - Point rookDestination = move.from + Point(1,0); - basicMovePiece(M(rookSquare, rookDestination)); - } - - else if (move.type() == M::QueenSideCastling) { - Point rookSquare = move.to - Point(2,0); - Point rookDestination = move.from - Point(1,0); - basicMovePiece(M(rookSquare, rookDestination)); - } - } - - if (type == KING) { - if (color == WHITE) { - m_castleWhiteKing = false; - m_castleWhiteQueen = false; - } - else { - m_castleBlackKing = false; - m_castleBlackQueen = false; - } - } - - if (move.from == Point(0,0) || move.to == Point(0,0)) - m_castleBlackQueen = false; - else if (move.from == Point(7,0) || move.to == Point(7,0)) - m_castleBlackKing = false; - else if (move.from == Point(0,7) || move.to == Point(0,7)) - m_castleWhiteQueen = false; - else if (move.from == Point(7,7) || move.to == Point(7,7)) - m_castleWhiteKing = false; - - switchTurn(); -} - -template -void Position::switchTurn() { - m_turn = P::oppositeColor(turn()); -} - -template -void Position::setTurn(Color turn) { - m_turn = turn; -} - -template -bool Position::testPremove(M premove) const { - for (Point p = first(); p <= last(); p = next(p)) { - P piece = m_board[p]; - if (!piece || piece.color() != turn()) continue; - for (Point q = first(); q <= last(); q = next(q)) { - M move(p,q); - if (testMove(move)) { - Position tempPosition(*this); - tempPosition.move(move); - - if (tempPosition.testMove(premove)) return true; - } - } - } - - return false; -} - -template -bool Position::attacks(Color color, const Point& destination) const { - Point temp; - return attacks(color, destination, temp); -} - -template -bool Position::attacks(Color color, const Point& destination, Point& source) const { - for (Point p = first(); p <= last(); p = next(p)) { - P piece = m_board[p]; - if (piece && piece.color() == color && piece.canMove(*this, p, destination)) { - source = p; - return true; - } - } - return false; -} - -template -bool Position::attacks(const Point& destination) const { - return attacks(turn(), destination); -} - -template -Point Position::findPiece(Color color, typename P::Type type) const { - for (int i = 0; i < 8; i++) - for (int j = 0; j < 8; j++) { - Point p = Point(i,j); - P piece = m_board[p]; - if (piece && piece.color() == color && piece.type() == type) - return p; - } - return Point::invalid(); -} - -template -boost::shared_ptr > -Position::createLegalGenerator() const { - return boost::shared_ptr >( - new MoveGenerator >(*this)); -} - -// template -// boost::shared_ptr > createPseudoLegalGenerator() const { -// return boost::shared_ptr >( -// new MoveGenerator >(*this)); -// } - -template -bool Position::stalled() const { - boost::shared_ptr > generator = - createLegalGenerator(); - std::vector mv = generator->generate(); - return generator->generate().size() == 0; -} -/* -template -bool Position::pseudoStalled() const { - MoveGenerator > generator(*this); - return generator.generate().size() == 0; -}*/ - -template -bool Position::check(Color turn) const { - Point kingPosition = findPiece(turn, KING); - if (kingPosition == Point::invalid()) { - // a missing king is considered in check - return true; - } - else - return attacks(P::oppositeColor(turn), kingPosition); -} - -template -bool Position::check() const { - return check(turn()); -} - -template -typename Position::State Position::state() const { - if (!stalled()) return InPlay; - - if (check()) - return turn() == WHITE? BlackWins : WhiteWins; - else - return Draw; -} - - -//END Implementation - - - - - - - - -#endif // POSITION_H diff --git a/src/variants/xiangqi.cpp_ b/src/variants/xiangqi.cpp_ deleted file mode 100644 index 36fd20f..0000000 --- a/src/variants/xiangqi.cpp_ +++ /dev/null @@ -1,640 +0,0 @@ -#include "xiangqi.h" -#include "xchess/animator.impl.h" -#include "xchess/piece.h" -#include "xchess/move.h" -#include "highlevel.h" -#include "moveserializer.impl.h" -#include "crazyhouse_p.h" - -class XiangQiPiece { -public: - enum Type { - GENERAL, - HORSE, - CHARIOT, - ADVISOR, - CANNON, - SOLDIER, - ELEPHANT, - INVALID_TYPE - }; - enum Color { - RED, - BLACK, - INVALID_COLOR - }; - typedef bool PromotionType; -private: - Color m_color; - Type m_type; - bool m_promoted; -public: - XiangQiPiece() - : m_color(INVALID_COLOR) - , m_type(INVALID_TYPE) - , m_promoted(false) { } - XiangQiPiece(XiangQiPiece::Color color, XiangQiPiece::Type type, bool promoted = false); - XiangQiPiece(const XiangQiPiece& other); - - bool inPalace(const Point& p) const; - - void promote() { m_promoted = true; } - bool promoted() { return m_promoted; } - - bool operator<(const XiangQiPiece& p) const { - if (m_promoted == p.m_promoted) - if (m_color == p.m_color) - return m_type < p.m_type; - else - return m_color < p.m_color; - else - return m_promoted < p.m_promoted; - } - - QString name() const; - static QString typeName(XiangQiPiece::Type t); - bool valid() const { return m_color != INVALID_COLOR && m_type != INVALID_TYPE; } - operator bool() const { return valid(); } - bool operator!() const { return !valid(); } - - bool equals(const XiangQiPiece* p) const { - if (valid()) { - if (p) - return (*this) == (*p); - else - return false; - } - else - return !p; - } - - bool operator==(const XiangQiPiece& p) const { - return m_promoted == p.m_promoted && - m_color == p.m_color && - m_type == p.m_type; - } - - bool operator!=(const XiangQiPiece& p) const { - return !(operator==(p)); - } - - static Type getType(const QString& t); - static QString typeSymbol(XiangQiPiece::Type t); - - bool canMove(const class XiangQiPosition&, const Point&, const Point&) const; - Color color() const { return m_color; } - Type type() const { return m_type; } - - static Color oppositeColor(Color c) { return c == RED ? BLACK : BLACK ? RED : INVALID_COLOR; } - Point direction() const { return Point(0, m_color == RED ? -1 : 1); } -}; - -XiangQiPiece::XiangQiPiece(XiangQiPiece::Color color, XiangQiPiece::Type type, bool promoted) -: m_color(color) -, m_type(type) -, m_promoted(promoted) { } - -XiangQiPiece::XiangQiPiece(const XiangQiPiece& other) -: m_color(other.m_color) -, m_type(other.m_type) -, m_promoted(other.m_promoted) { } - -bool XiangQiPiece::inPalace(const Point& p) const { - if(m_color == RED) - return p.x > 2 && p.x < 6 && p.y > 6; - else - return p.x > 2 && p.x < 6 && p.y < 3; -} - -QString XiangQiPiece::name() const { - QString res = m_color == RED ? "red_" : "black_"; - if (m_promoted) - res += "p_"; - res += typeName(m_type); - return res; -} - -QString XiangQiPiece::typeName(XiangQiPiece::Type t) { - switch (t) { - case GENERAL: - return "general"; - case HORSE: - return "horse"; - case CHARIOT: - return "chariot"; - case ADVISOR: - return "advisor"; - case CANNON: - return "cannon"; - case SOLDIER: - return "soldier"; - case ELEPHANT: - return "elephant"; - default: - return "unknown"; - } -} - -XiangQiPiece::Type XiangQiPiece::getType(const QString&) { - return GENERAL; // FIXME -} - -QString XiangQiPiece::typeSymbol(XiangQiPiece::Type t) { - switch (t) { - case GENERAL: - return "G"; - case HORSE: - return "H"; - case CHARIOT: - return "R"; - case ADVISOR: - return "A"; - case CANNON: - return "C"; - case SOLDIER: - return "S"; - case ELEPHANT: - return "E"; - default: - return "?"; - } -} - -// ------------------------------ - -class XiangQiMove { - XiangQiPiece m_dropped; - bool m_promote; - template friend class MoveSerializer; -public: - Point from; - Point to; - - XiangQiMove(); - XiangQiMove(const Point& from, const Point& to, bool promote); - XiangQiMove(const XiangQiPiece& piece, const Point& to); - - static XiangQiMove createDropMove(const XiangQiPiece& piece, const Point& to); - QString toString(int) const; - - bool operator==(const XiangQiMove& other) const; - - const XiangQiPiece& dropped() const { return m_dropped; } - bool promote() const { return m_promote; } - bool valid() const { return to.valid(); } -}; - -XiangQiMove::XiangQiMove() -: m_promote(true) -, from(Point::invalid()) -, to(Point::invalid()) { } - -XiangQiMove::XiangQiMove(const Point& from, const Point& to, bool promote) -: m_promote(promote) -, from(from) -, to(to) { } - -XiangQiMove::XiangQiMove(const XiangQiPiece& piece, const Point& to) -: m_dropped(piece) -, m_promote(false) -, from(Point::invalid()) -, to(to) { } - -XiangQiMove XiangQiMove::createDropMove(const XiangQiPiece& piece, const Point& to) { - return XiangQiMove(piece, to); -} - -QString XiangQiMove::toString(int) const { - return ""; -} - -bool XiangQiMove::operator==(const XiangQiMove& other) const { - if (m_dropped) - return m_dropped == other.m_dropped - && to == other.to; - else - return m_promote == other.m_promote - && to == other.to - && from == other.from; -} - -// ------------------------------ - -class XiangQiPosition { -public: - typedef XiangQiPiece Piece; - typedef XiangQiMove Move; - typedef std::map Pool; -private: - Piece::Color m_turn; - Grid m_board; - Pool m_pool; -public: - template friend class MoveSerializer; - XiangQiPosition(); - XiangQiPosition(const XiangQiPosition&); - XiangQiPosition(Piece::Color turn, bool wk, bool wq, - bool bk, bool bq, const Point& ep); - XiangQiPosition(const QList >& opts); - virtual XiangQiPosition* clone() const { return new XiangQiPosition(*this); } - virtual ~XiangQiPosition() { } - - virtual void setup(); - - bool testMove(Move&) const; - bool pseudolegal(Move& m) const; - - virtual void addToPool(const Piece&, int) { } - virtual void removeFromPool(const Piece&, int) { } - Pool& pool() { return m_pool; } - const Pool& pool() const { return m_pool; } - - const XiangQiPiece* get(const Point& p) const; - XiangQiPiece* get(const Point& p); - void set(const Point& p, Piece* piece); - - XiangQiPiece operator[](const Point& p) const { return m_board[p]; } - - Piece::Color turn() const { return m_turn; } - void setTurn(Piece::Color turn) { m_turn = turn; } - Piece::Color previousTurn() const { return Piece::oppositeColor(m_turn); } - void switchTurn() { m_turn = Piece::oppositeColor(m_turn); } - - void move(const XiangQiMove& m); - - void fromFEN(const QString&, bool& ok) { ok = false; } - QString fen(int, int) const { return ""; } - bool operator==(const XiangQiPosition& p) const; - - static Move getVerboseMove(Piece::Color turn, const VerboseNotation& m); - Move getMove(const AlgebraicNotation&, bool& ok) const; - boost::shared_ptr moveHint(const XiangQiMove& m) const; - - Point size() const { return Point(9,10); } - void dump() const { } - - PathInfo path(const Point& from, const Point& to) const { return m_board.path(from, to); } - - QStringList borderCoords() const; -}; - -XiangQiPosition::XiangQiPosition() -: m_turn(XiangQiPiece::BLACK) -, m_board(9,10) { } - -XiangQiPosition::XiangQiPosition(const XiangQiPosition& other) -: m_turn(other.m_turn) -, m_board(other.m_board) -, m_pool(other.m_pool) { } - -XiangQiPosition::XiangQiPosition(Piece::Color turn, bool, bool, bool, bool, const Point&) -: m_turn(turn) -, m_board(9,10) { } - -XiangQiPosition::XiangQiPosition(const QList >&) -: m_turn(XiangQiPiece::BLACK) -, m_board(9,10) { } - -QStringList XiangQiPosition::borderCoords() const -{ - QStringList retv; - for(int i=1; i<=9; i++) - retv += QString::number(i); - for(int i=0; i<10; i++) - retv += QString(); - retv << QChar(0x4e5d) << QChar(0x516b) << QChar(0x4e03) << QChar(0x516d) - << QChar(0x4e94) << QChar(0x56db) << QChar(0x4e09) << QChar(0x4e8c) << QChar(0x4e00); - for(int i=0; i<10; i++) - retv += QString(); - return retv; -} - -bool XiangQiPiece::canMove(const XiangQiPosition& pos, - const Point& from, const Point& to) const { - if (!from.valid()) return false; - if (!to.valid()) return false; - if (from == to) return false; - if (pos[to].color() == m_color) return false; - Point delta = to - from; - - switch (m_type) { - case GENERAL: - return (abs(delta.x) + abs(delta.y) == 1) - && inPalace(to); - case ADVISOR: - return (abs(delta.x) == 1) && (abs(delta.y) == 1) - && inPalace(to); - case ELEPHANT: - return (abs(delta.x) == 2) && (abs(delta.y) == 2) - && !pos[Point((to.x+from.x)/2, (to.y+from.y)/2)] - && (m_color == RED ? to.y > 4 : to.y < 5); - case HORSE: - return (delta.x*delta.x + delta.y*delta.y) == 5 - && !pos[Point((to.x+from.x*2+1)/3, (to.y+from.y*2+1)/3)]; - case CHARIOT: - { - PathInfo path = pos.path(from, to); - return path.parallel() && path.clear(); - } - case CANNON: - { - PathInfo path = pos.path(from, to); - return path.parallel() && - (pos[to] ? path.numObstacles() == 1 : path.clear()); - } - case SOLDIER: - return (delta.x == 0 && delta.y == direction().y) - || (abs(delta.x) == 1 && delta.y == 0 && - (m_color == RED ? to.y < 5 : to.y > 4)); - default: - return false; - } -} - - -XiangQiPosition::Move XiangQiPosition::getVerboseMove(Piece::Color, const VerboseNotation&) { - return Move(); -} - -XiangQiPosition::Move XiangQiPosition::getMove(const AlgebraicNotation&, bool& ok) const { - ok = false; - return Move(); -} - -const XiangQiPiece* XiangQiPosition::get(const Point& p) const { - return m_board.valid(p) && m_board[p] ? &m_board[p] : 0; -} - -XiangQiPiece* XiangQiPosition::get(const Point& p) { - return m_board.valid(p) && m_board[p] ? &m_board[p] : 0; -} - -void XiangQiPosition::set(const Point& p, XiangQiPiece* piece) { - if (!m_board.valid(p)) return; - if (piece) - m_board[p] = *piece; - else - m_board[p] = Piece(); -} - -bool XiangQiPosition::operator==(const XiangQiPosition& p) const { - return m_turn == p.m_turn - && m_board == p.m_board; -} - -boost::shared_ptr XiangQiPosition::moveHint(const XiangQiMove& m) const { - if (m.dropped()) return boost::shared_ptr(new XiangQiPiece(m.dropped())); - else return boost::shared_ptr(); -} - -#define SET_PIECE(i,j, color, type) m_board[Point(i,j)] = Piece(XiangQiPiece::color, XiangQiPiece::type) -void XiangQiPosition::setup() { - SET_PIECE(0,0, BLACK, CHARIOT); - SET_PIECE(1,0, BLACK, HORSE); - SET_PIECE(2,0, BLACK, ELEPHANT); - SET_PIECE(3,0, BLACK, ADVISOR); - SET_PIECE(4,0, BLACK, GENERAL); - SET_PIECE(5,0, BLACK, ADVISOR); - SET_PIECE(6,0, BLACK, ELEPHANT); - SET_PIECE(7,0, BLACK, HORSE); - SET_PIECE(8,0, BLACK, CHARIOT); - SET_PIECE(1,2, BLACK, CANNON); - SET_PIECE(7,2, BLACK, CANNON); - for(int i=0;i<=8;i+=2) - SET_PIECE(i, 3, BLACK, SOLDIER); - - SET_PIECE(0,9, RED, CHARIOT); - SET_PIECE(1,9, RED, HORSE); - SET_PIECE(2,9, RED, ELEPHANT); - SET_PIECE(3,9, RED, ADVISOR); - SET_PIECE(4,9, RED, GENERAL); - SET_PIECE(5,9, RED, ADVISOR); - SET_PIECE(6,9, RED, ELEPHANT); - SET_PIECE(7,9, RED, HORSE); - SET_PIECE(8,9, RED, CHARIOT); - SET_PIECE(1,7, RED, CANNON); - SET_PIECE(7,7, RED, CANNON); - for(int i=0;i<=8;i+=2) - SET_PIECE(i, 6, RED, SOLDIER); - - m_turn = XiangQiPiece::RED; -} -#undef SET_PIECE - -bool XiangQiPosition::testMove(Move& m) const { - return pseudolegal(m); -} - -bool XiangQiPosition::pseudolegal(Move& m) const { - const Piece& p = m_board[m.from]; - return p.canMove(*this, m.from, m.to); -} - -void XiangQiPosition::move(const XiangQiMove& m) { - if (Piece captured = m_board[m.to]) { - addToPool(Piece(Piece::oppositeColor(captured.color()), - captured.type(), false), 1); - } - - m_board[m.to] = m_board[m.from]; - m_board[m.from] = Piece(); - - switchTurn(); -} - -template <> -class MoveSerializer { - const XiangQiMove& m_move; - const XiangQiPosition& m_ref; -public: - MoveSerializer(const XiangQiMove& m, const XiangQiPosition& r) - : m_move(m), m_ref(r) { } - QString SAN() const { - XiangQiPiece p = m_ref[m_move.from]; - int order = 0; - int num = 1; - for(int i=0;i<10;i++) { - Point f(m_move.from.x, i); - Point t(m_move.to-m_move.from+f); - if(f != m_move.from && m_ref[f] == p && m_ref.m_board.valid(t)) { - XiangQiMove m(f, m_move.to-m_move.from+f, false); - if(m_ref.testMove(m)) { - order = ((i < m_move.to.y) != (p.color() == XiangQiPiece::RED)) ? -1 : 1; - num++; - } - } - } - - QString retv; - if(num >= 4 && (order == 0 || order == num-1)) //4/5 pawns case - retv += (order==0) ? "-" : "+"; - else - retv += XiangQiPiece::typeSymbol(p.type()); - - if(num==1 || (num==3 && order==1) || (num==5 && order==2)) - retv += QString::number((p.color() == XiangQiPiece::RED) ? 9-m_move.from.x : m_move.from.x+1); - else - retv += (orderm_move.from.y) != (p.color() == XiangQiPiece::RED)) ? "+" : "-"; - retv += QString::number(abs(m_move.to.y-m_move.from.y)); - } - else { - retv += (m_move.to.y == m_move.from.y) ? "." : - ((m_move.to.y>m_move.from.y) != (p.color() == XiangQiPiece::RED)) ? "+" : "-"; - retv += QString::number((p.color() == XiangQiPiece::RED) ? 9-m_move.to.x : m_move.to.x+1); - } - return retv; - } - - DecoratedMove toDecoratedMove() const { - XiangQiPiece p = m_ref[m_move.from]; - int order = 0; - int num = 1; - for(int i=0;i<10;i++) { - Point f(m_move.from.x, i); - Point t(m_move.to-m_move.from+f); - if(f != m_move.from && m_ref[f] == p && m_ref.m_board.valid(t)) { - XiangQiMove m(f, m_move.to-m_move.from+f, false); - if(m_ref.testMove(m)) { - order = ((i < m_move.to.y) != (p.color() == XiangQiPiece::RED)) ? -1 : 1; - num++; - } - } - } - - DecoratedMove retv; - MovePart piece = QString(); - - std::cout << "num = " << num << " order = " << order << std::endl; - if(num >= 4 && (order == 0 || order == num-1)) //4/5 pawns case - piece = MovePart((order==0) ? "front" : "rear", MovePart::Figurine); - else - piece = MovePart(p.name(), MovePart::Figurine); - - if(num==1 || (num==3 && order==1) || (num==5 && order==2)) { - retv += piece; - retv += MovePart(((p.color() == XiangQiPiece::RED) ? "rn_" : "bn_") - + QString::number((p.color() == XiangQiPiece::RED) ? - 9-m_move.from.x : m_move.from.x+1), - MovePart::Figurine); - } - else { - retv += MovePart((orderm_move.from.y) != (p.color() == XiangQiPiece::RED)) - ? "advances" : "retreats", MovePart::Figurine); - retv += MovePart(((p.color() == XiangQiPiece::RED) ? "rn_" : "bn_") - + QString::number(abs(m_move.to.y-m_move.from.y)), MovePart::Figurine); - } - else { - retv += MovePart((m_move.to.y == m_move.from.y) ? "traverses" : - ((m_move.to.y>m_move.from.y) != (p.color() == XiangQiPiece::RED)) ? "advances" : - "retreats", MovePart::Figurine); - retv += MovePart(((p.color() == XiangQiPiece::RED) ? "rn_" : "bn_") - + QString::number((p.color() == XiangQiPiece::RED) ? 9-m_move.to.x : m_move.to.x+1), - MovePart::Figurine); - } - return retv; - } -}; - -#if 0 -class XiangQiAnimator : protected CrazyhouseAnimator { -protected: - typedef boost::shared_ptr AnimationPtr; - virtual boost::shared_ptr - createMovementAnimation(const Element& element, const QPoint& destination); -public: - XiangQiAnimator(PointConverter* converter, GraphicalPosition* position); - virtual ~XiangQiAnimator(){} - virtual AnimationPtr warp(AbstractPosition::Ptr); - virtual AnimationPtr forward(AbstractPosition::Ptr, const XiangQiMove& move); - virtual AnimationPtr back(AbstractPosition::Ptr, const XiangQiMove& move); -}; - -boost::shared_ptr -XiangQiAnimator::createMovementAnimation(const Element& element, const QPoint& destination) { - if (element.piece()->type() == static_cast(XiangQiPiece::HORSE)) - return boost::shared_ptr(new KnightMovementAnimation(element.sprite(), - destination, m_anim_rotate, 1.0)); - else - return boost::shared_ptr(new MovementAnimation(element.sprite(), - destination, 1.0)); -} - -XiangQiAnimator::XiangQiAnimator(PointConverter* converter, GraphicalPosition* position) -: CrazyhouseAnimator(converter, position) { } - -XiangQiAnimator::AnimationPtr XiangQiAnimator::warp(AbstractPosition::Ptr pos) { - return CrazyhouseAnimator::warp(pos); -} - -XiangQiAnimator::AnimationPtr XiangQiAnimator::forward(AbstractPosition::Ptr pos, const XiangQiMove& move) { - if (move.dropped()) - return CrazyhouseAnimator::forward(pos, CrazyhouseMove(CrazyhousePiece(WHITE, KING), move.to)); - else - return CrazyhouseAnimator::forward(pos, CrazyhouseMove(move.from, move.to)); -} - -XiangQiAnimator::AnimationPtr XiangQiAnimator::back(AbstractPosition::Ptr pos, const XiangQiMove& move) { - if (move.dropped()) - return CrazyhouseAnimator::back(pos, CrazyhouseMove(CrazyhousePiece(WHITE, KING), move.to)); - else - return CrazyhouseAnimator::back(pos, CrazyhouseMove(move.from, move.to)); -} -#endif - - -class XiangQiVariantInfo { -public: - typedef XiangQiPosition Position; - typedef Position::Move Move; - typedef Position::Piece Piece; - typedef SimpleAnimator Animator; - static const bool m_simple_moves = false; - static void forallPieces(PieceFunction& f); - static QStringList borderCoords(){ - return QStringList() << "i" << "h" << "g" << "f" << "e" << "d" << "c" << "b" << "a" - << "10" << "9" << "8" << "7" << "6" << "5" << "4" << "3" << "2" << "1"; - } - static int moveListLayout() { return 0; } - static OptList positionOptions() { return OptList(); } - static const char *m_name; - static const char *m_theme_proxy; -}; - -const char *XiangQiVariantInfo::m_name = "XiangQi"; -const char *XiangQiVariantInfo::m_theme_proxy = "XiangQi"; - - -void XiangQiVariantInfo::forallPieces(PieceFunction& f) { - ChessVariant::forallPieces(f); -} - -VariantInfo* XiangQiVariant::static_xiangqi_variant = 0; - -VariantInfo* XiangQiVariant::info() { - if (!static_xiangqi_variant) - static_xiangqi_variant = new WrappedVariantInfo; - return static_xiangqi_variant; -} - - -template <> -struct MoveFactory { - static XiangQiMove createNormalMove(const NormalUserMove& move) { - return XiangQiMove(move.from, move.to, move.promotionType >= 0); - } - static XiangQiMove createDropMove(const XiangQiPiece& dropped, const Point& to) { - return XiangQiMove(dropped, to); - } - - static NormalUserMove toNormal(const XiangQiMove& move) { - return NormalUserMove(move.from, move.to); - } -}; diff --git a/src/variants/xiangqi.h_ b/src/variants/xiangqi.h_ deleted file mode 100644 index 1164221..0000000 --- a/src/variants/xiangqi.h_ +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef VARIANTS__XIANGQI_H -#define VARIANTS__XIANGQI_H - -#include "chess.h" - -class XiangQiVariant { -private: - static VariantInfo* static_xiangqi_variant; -public: - static VariantInfo* info(); -}; - -#endif //VARIANTS__XIANGQI_H -- 2.11.4.GIT