From fc4e0b5a1bd28c39d0fdedfe36e69a1375b7316c Mon Sep 17 00:00:00 2001 From: Yann Dirson Date: Wed, 9 Apr 2008 00:20:36 +0200 Subject: [PATCH] Tentative Randomless-Entropy variant. --- data/themes/squares/Default/theme_shogi.desktop | 2 +- data/themes/squares/Default/theme_solid.desktop | 2 +- debian/control | 9 +++ debian/tagua-variant-entropy.files | 2 + src/variants/CMakeLists.txt | 2 + src/variants/randomless-entropy/CMakeLists.txt | 23 +++++++ src/variants/randomless-entropy/colors.cpp | 20 ++++++ src/variants/randomless-entropy/colors.h | 22 ++++++ src/variants/randomless-entropy/entropy.cpp | 68 +++++++++++++++++++ .../{shogi => randomless-entropy}/state.cpp | 74 ++++---------------- src/variants/randomless-entropy/state.h | 78 ++++++++++++++++++++++ src/variants/randomless-entropy/stone.cpp | 48 +++++++++++++ src/variants/randomless-entropy/stone.h | 34 ++++++++++ .../tagua-randomlessentropy.desktop | 20 ++++++ src/variants/shogi/state.cpp | 3 +- 15 files changed, 344 insertions(+), 63 deletions(-) create mode 100644 debian/tagua-variant-entropy.files create mode 100644 src/variants/randomless-entropy/CMakeLists.txt create mode 100644 src/variants/randomless-entropy/colors.cpp create mode 100644 src/variants/randomless-entropy/colors.h create mode 100644 src/variants/randomless-entropy/entropy.cpp copy src/variants/{shogi => randomless-entropy}/state.cpp (53%) create mode 100644 src/variants/randomless-entropy/state.h create mode 100644 src/variants/randomless-entropy/stone.cpp create mode 100644 src/variants/randomless-entropy/stone.h create mode 100644 src/variants/randomless-entropy/tagua-randomlessentropy.desktop diff --git a/data/themes/squares/Default/theme_shogi.desktop b/data/themes/squares/Default/theme_shogi.desktop index 85a8f42..799ad83 100644 --- a/data/themes/squares/Default/theme_shogi.desktop +++ b/data/themes/squares/Default/theme_shogi.desktop @@ -4,4 +4,4 @@ Name=Shogi Type=Application Comment=Shogi-like squares X-Tagua-ThemeType=Squares -X-Tagua-Variants=Shogi[default],LargeShogi[default],ToriShogi[default],Chess +X-Tagua-Variants=Shogi[default],LargeShogi[default],ToriShogi[default],Chess,Entropy diff --git a/data/themes/squares/Default/theme_solid.desktop b/data/themes/squares/Default/theme_solid.desktop index 3bafcfd..3f311f9 100644 --- a/data/themes/squares/Default/theme_solid.desktop +++ b/data/themes/squares/Default/theme_solid.desktop @@ -4,4 +4,4 @@ Name=Custom Color Type=Application Comment=Squares with a solid custom color X-Tagua-ThemeType=Squares -X-Tagua-Variants=Chess, Shogi, XiangQi +X-Tagua-Variants=Chess, Shogi, XiangQi, Entropy diff --git a/debian/control b/debian/control index b6b978d..ed01cf4 100644 --- a/debian/control +++ b/debian/control @@ -76,3 +76,12 @@ Provides: tagua-variant Description: MiniShogi support for the tagua board-game frontend This package allows to play MiniShogi, a variant on a 5x5 board of the japanese Shogi chess-like game, from tagua. + +Package: tagua-variant-entropy +Architecture: any +Depends: tagua, tagua-variant-chess +Provides: tagua-variant +Description: Randomless-Entropy support for the tagua board-game frontend + This package allows to play Randomless-Entropy, a variant of Entropy + where Chaos does not randomly draws the next stone to place, but can + choose from all stones in the pool, from tagua. diff --git a/debian/tagua-variant-entropy.files b/debian/tagua-variant-entropy.files new file mode 100644 index 0000000..723dad9 --- /dev/null +++ b/debian/tagua-variant-entropy.files @@ -0,0 +1,2 @@ +usr/lib/kde4/tagua*entropy.so +usr/share/kde4/services/tagua-*entropy.desktop diff --git a/src/variants/CMakeLists.txt b/src/variants/CMakeLists.txt index df850f3..3c721df 100644 --- a/src/variants/CMakeLists.txt +++ b/src/variants/CMakeLists.txt @@ -4,3 +4,5 @@ add_subdirectory(crazyhouse) add_subdirectory(shogi) add_subdirectory(minishogi) + +add_subdirectory(randomless-entropy) diff --git a/src/variants/randomless-entropy/CMakeLists.txt b/src/variants/randomless-entropy/CMakeLists.txt new file mode 100644 index 0000000..f14f6fb --- /dev/null +++ b/src/variants/randomless-entropy/CMakeLists.txt @@ -0,0 +1,23 @@ +set(taguarandomlessentropy_SRCS + entropy.cpp + + stone.cpp + +# behaviour.cpp + colors.cpp +# moveserializer.cpp + state.cpp +# validator.cpp +) + +include_directories(${CMAKE_SOURCE_DIR}/src/) + +kde4_add_plugin(taguarandomlessentropy ${taguarandomlessentropy_SRCS}) + +target_link_libraries(taguarandomlessentropy + taguacore + ${KDE4_KDECORE_LIBS} +) + +install(TARGETS taguarandomlessentropy DESTINATION ${PLUGIN_INSTALL_DIR}) +install(FILES tagua-randomlessentropy.desktop DESTINATION ${SERVICES_INSTALL_DIR}) diff --git a/src/variants/randomless-entropy/colors.cpp b/src/variants/randomless-entropy/colors.cpp new file mode 100644 index 0000000..c82d1c5 --- /dev/null +++ b/src/variants/randomless-entropy/colors.cpp @@ -0,0 +1,20 @@ +/* + Copyright (c) 2007 Paolo Capriotti + (c) 2007 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 "colors.h" + +namespace RandomlessEntropy { + +// Chaos = Black +// Order = White + +IColor* COLORS[] = { Black::self(), White::self() }; + +} diff --git a/src/variants/randomless-entropy/colors.h b/src/variants/randomless-entropy/colors.h new file mode 100644 index 0000000..2561f16 --- /dev/null +++ b/src/variants/randomless-entropy/colors.h @@ -0,0 +1,22 @@ +/* + Copyright (c) 2007 Paolo Capriotti + (c) 2007 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 RANDOMLESSENTROPY__COLORS_H +#define RANDOMLESSENTROPY__COLORS_H + +#include + +namespace RandomlessEntropy { + +extern IColor* COLORS[2]; + +} + +#endif // RANDOMLESSENTROPY__COLORS_H diff --git a/src/variants/randomless-entropy/entropy.cpp b/src/variants/randomless-entropy/entropy.cpp new file mode 100644 index 0000000..8748b5a --- /dev/null +++ b/src/variants/randomless-entropy/entropy.cpp @@ -0,0 +1,68 @@ +/* + Copyright (c) 2007 Paolo Capriotti + (c) 2007 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 + +//#include "behaviour.h" +#include "colors.h" +//#include "moveserializer.h" +#include "state.h" +#include "stone.h" +//#include "validator.h" + +#include + +using namespace RandomlessEntropy; + +namespace RandomlessEntropy { + const unsigned gamesize = 3; +} + +extern "C" KDE_EXPORT Repository* taguarandomlessentropy_initrepo(IVariantLoader* loader) { + Repository* repo = new Repository; + Repository* chess = loader->getRepository("chess"); + if (!chess) { + kError() << "Could not load chess variant"; + return 0; + } + + repo->addComponent("player/0", dynamic_cast(COLORS[0])); + repo->addComponent("player/1", dynamic_cast(COLORS[1])); + + // FIXME + repo->addComponent("type/stone0", Stone::color(0)); + repo->addComponent("type/stone1", Stone::color(1)); + repo->addComponent("type/stone2", Stone::color(2)); + + IState* chess_state = requestInterface(chess->getComponent("state")); + const IBehaviour* behaviour = chess_state->behaviour(); + repo->addComponent("state", new State(behaviour, Point(gamesize, gamesize))); + + repo->addComponent("validator", chess->getComponent("validator")); // FIXME + repo->addComponent("animator_factory", chess->getComponent("animator_factory")); + repo->addComponent("namer", chess->getComponent("namer")); + repo->addComponent("policy", new DefaultPolicy); // FIXME ? + +#if 1 // FIXME + // set move serializers + Repository::ComponentMap serializers = chess->listComponents("move_serializer"); + for (Repository::ComponentMap::const_iterator it = serializers.begin(), + end = serializers.end(); it != end; ++it) { + repo->addComponent("move_serializer/" + it.key(), it.value()); + } +#else + repo->addComponent("move_serializer/simple", + new MoveSerializer("simple", validator)); +#endif + + return repo; +} diff --git a/src/variants/shogi/state.cpp b/src/variants/randomless-entropy/state.cpp similarity index 53% copy from src/variants/shogi/state.cpp copy to src/variants/randomless-entropy/state.cpp index 48ba5c3..93dc97c 100644 --- a/src/variants/shogi/state.cpp +++ b/src/variants/randomless-entropy/state.cpp @@ -16,9 +16,9 @@ #include #include "colors.h" -#include "types.h" +#include "stone.h" -namespace Shogi { +namespace RandomlessEntropy { State::State(const IBehaviour* behaviour, const Point& size) @@ -26,8 +26,8 @@ State::State(const IBehaviour* behaviour, , m_behaviour(behaviour) , m_delegator(this) { DefaultPoolCollection* c = new DefaultPoolCollection; - for (int i = 0; i < 2; i++) - c->addPool(COLORS[i], new DefaultPool(COLORS[i])); + // only Chaos/Black has a pool + c->addPool(Black::self(), new DefaultPool(Black::self())); m_pools = c; } @@ -35,7 +35,6 @@ State::State(const State& other) : IState() , DefaultState() , m_board(other.m_board) -, m_flags(other.m_flags) , m_turn(other.m_turn) , m_behaviour(other.m_behaviour) , m_delegator(this) @@ -49,34 +48,15 @@ IState* State::clone() const { return s; } -#define COL(i, c) c == Black::self() ? (i) : (m_board.size().x - i - 1) void State::setup() { - for (int c = 0; c < 2; c++) { - IColor* color = COLORS[c]; - int r0 = rank(0, color); - int r1 = rank(1, color); - int r2 = rank(2, color); - - for (int i = 0; i < m_board.size().x; i++) { - m_board.set(Point(i, r2), Piece(color, Pawn::self())); - } - m_board.set(Point(0, r0), Piece(color, Lance::self())); - m_board.set(Point(1, r0), Piece(color, Knight::self())); - m_board.set(Point(2, r0), Piece(color, Silver::self())); - m_board.set(Point(3, r0), Piece(color, Gold::self())); - m_board.set(Point(4, r0), Piece(color, King::self())); - m_board.set(Point(5, r0), Piece(color, Gold::self())); - m_board.set(Point(6, r0), Piece(color, Silver::self())); - m_board.set(Point(7, r0), Piece(color, Knight::self())); - m_board.set(Point(8, r0), Piece(color, Lance::self())); - - m_board.set(Point(COL(1, color), r1), Piece(color, Bishop::self())); - m_board.set(Point(COL(7, color), r1), Piece(color, Rook::self())); - } - - m_turn = White::self(); + // Chaos starts, board is empty + m_turn = Black::self(); + // FIXME: all pieces in pool + IPool* pool = pools()->pool(Black::self()); + pool->insert(-1, Piece(Black::self(), Stone::color(0))); + pool->insert(-1, Piece(Black::self(), Stone::color(1))); + pool->insert(-1, Piece(Black::self(), Stone::color(2))); } -#undef COL const Board* State::board() const { return &m_board; @@ -96,14 +76,12 @@ void State::setTurn(const IColor* turn) { bool State::equals(IState* other) const { return m_board.equals(other->board()) && - m_turn == other->turn() && - m_flags == *other->flags(); + m_turn == other->turn(); } void State::assign(const IState* other) { m_board = *other->board(); m_turn = other->turn(); - m_flags = *other->flags(); const IPoolCollection* pools = other->pools(); if (pools) { @@ -114,41 +92,19 @@ void State::assign(const IState* other) { void State::move(const Move& m) { if (m.drop() != Piece()) { - //const Piece captured = board()->get(m.dst()); board()->set(m.dst(), m.drop()); pools()->pool(m.drop().color())->take(m.drop()); -#if 0 - // handle capturing by drop: some variants could use it - if (captured != Piece()) { - if (captured.get("promoted").toBool()) { - captured.setType(pawn); - } - pools()->pool(behaviour()->opponent(captured.color())) - ->insert(-1, captured); - } -#endif } else { const Piece piece = m_board.get(m.src()); if (piece == Piece()) return; - Point captureSquare = behaviour()->captureSquare(this, m); - behaviour()->captureOn(m_delegator, captureSquare); behaviour()->move(m_delegator, m); - - const IType* promotion_type = m.promotion(); - - if (promotion_type != 0) { - m_board.set(m.dst(), Piece(piece.color(), promotion_type)); - } } behaviour()->advanceTurn(m_delegator); } -const TaguaObject* State::flags() const { return &m_flags; } -TaguaObject* State::flags() { return &m_flags; } - int State::rank(int n, const IColor* turn) const { if (turn == White::self()) return m_board.size().y - n - 1; @@ -161,13 +117,11 @@ const IPoolCollection* State::pools() const { return m_pools; } const IBehaviour* State::behaviour() const { return m_behaviour; } -void State::setDelegator(IState* delegator) { - m_delegator = delegator; -} +void State::setDelegator(IState* delegator) { m_delegator = delegator; } Component* State::clone(const IBehaviour* behaviour, const Point& size) const { return new State(behaviour, size); } -} // namespace Shogi +} // namespace RandomlessEntropy diff --git a/src/variants/randomless-entropy/state.h b/src/variants/randomless-entropy/state.h new file mode 100644 index 0000000..be43c61 --- /dev/null +++ b/src/variants/randomless-entropy/state.h @@ -0,0 +1,78 @@ +/* + Copyright (c) 2007 Paolo Capriotti + (c) 2007 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 RANDOMLESSENTROPY__STATE_H +#define RANDOMLESSENTROPY__STATE_H + +#include +#include +#include +#include + +/** + * @brief Namespace holding Randomless-Entropy components. + * + * @details Randomless-Entropy is a variant of Entropy where Chaos + * does not randomly draws the next stone to place, but can choose + * from all stones in the pool. + */ +namespace RandomlessEntropy { + +class State : public DefaultState { +Q_OBJECT + Board m_board; + const IColor* m_turn; + const IBehaviour* m_behaviour; + IState* m_delegator; + IPoolCollection* m_pools; +protected: + State(const State&); +public: + State(const IBehaviour* behaviour, + const Point& size); + virtual ~State(); +public: + virtual IState* clone() const; + + virtual void setup(); + + virtual const Board* board() const; + virtual Board* board(); + + virtual const IColor* turn() const; + + virtual void setTurn(const IColor* turn); + + virtual bool equals(IState* other) const; + + virtual void assign(const IState* other); + + virtual void move(const Move& move); + + virtual TaguaObject* flags() { return NULL; }; + virtual const TaguaObject* flags() const { return NULL; }; + + virtual int rank(int n, const IColor* turn) const; + + virtual const IPoolCollection* pools() const; + virtual IPoolCollection* pools(); + + virtual const IBehaviour* behaviour() const; + + virtual void setDelegator(IState* delegator); +public Q_SLOTS: + virtual Component* clone(const IBehaviour* behaviour, + const Point& size) const; +}; + +} // namespace RandomlessEntropy + +#endif // RANDOMLESSENTROPY__STATE_H + diff --git a/src/variants/randomless-entropy/stone.cpp b/src/variants/randomless-entropy/stone.cpp new file mode 100644 index 0000000..4fd38c1 --- /dev/null +++ b/src/variants/randomless-entropy/stone.cpp @@ -0,0 +1,48 @@ +/* + Copyright (c) 2007 Paolo Capriotti + (c) 2007 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 "stone.h" + +namespace RandomlessEntropy { + +Stone::Stone() { + const MoveDefinition movesDefinition[] = { + {-1, 0, 0 }, + { 1, 0, 0 }, + { 0, 1, 0 }, + { 0, -1, 0 }, + }; + + for (unsigned i = 0; i < sizeof(movesDefinition)/sizeof(movesDefinition[0]); i++) + m_moveDefinitions.push_back(movesDefinition[i]); +} + +QString Stone::name() const { return "stone" + m_color; } + +const std::vector * const Stone::movesDefinitions() const { + return &m_moveDefinitions; +} + +int Stone::index() const { return m_color; } + +// FIXME: parametrize number of colors +// FIXME: find a way to set m_color in Stone constructor instead +Stone* Stone::color(unsigned color) { + static Stone s_instance[3]; + static bool colors_initialized = false; + if (!colors_initialized) { + for (int i=0; i<3;i++) + s_instance[i].m_color = i; + } + + return &s_instance[color]; +} + +} diff --git a/src/variants/randomless-entropy/stone.h b/src/variants/randomless-entropy/stone.h new file mode 100644 index 0000000..5eec319 --- /dev/null +++ b/src/variants/randomless-entropy/stone.h @@ -0,0 +1,34 @@ +/* + Copyright (c) 2007 Paolo Capriotti + (c) 2007 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 RANDOMLESSENTROPY__STONE_H +#define RANDOMLESSENTROPY__STONE_H + +#include +#include + +namespace RandomlessEntropy { + +class TAGUA_EXPORT Stone : public Component, public DefaultType { +Q_OBJECT + Stone(); +public: + virtual QString name() const; + virtual const std::vector * const movesDefinitions() const; + virtual int index() const; + static Stone* color(unsigned color); +private: + std::vector m_moveDefinitions; + unsigned m_color; +}; + +} + +#endif // RANDOMLESSENTROPY__STONE_H diff --git a/src/variants/randomless-entropy/tagua-randomlessentropy.desktop b/src/variants/randomless-entropy/tagua-randomlessentropy.desktop new file mode 100644 index 0000000..04d749d --- /dev/null +++ b/src/variants/randomless-entropy/tagua-randomlessentropy.desktop @@ -0,0 +1,20 @@ +[Desktop Entry] +Encoding=UTF-8 +Name=Randomless Entropy +Comment=Randomless variant of Entropy +Icon=tagua +Type=Service +ServiceTypes=Tagua/Variant + +X-KDE-Library=taguarandomlessentropy +X-KDE-PluginInfo-Author=Yann Dirson +X-KDE-PluginInfo-Email=ydirson@altern.org +X-KDE-PluginInfo-Name=randomlessentropy +X-KDE-PluginInfo-Version=0.5.1 +X-KDE-PluginInfo-Website=http://www.tagua-project.org +X-KDE-PluginInfo-Depends= +X-KDE-PluginInfo-License=GPL +X-KDE-PluginInfo-EnabledByDefault=true + +X-Tagua-Proxy=Entropy +X-Tagua-Hidden=false diff --git a/src/variants/shogi/state.cpp b/src/variants/shogi/state.cpp index 48ba5c3..a399f7a 100644 --- a/src/variants/shogi/state.cpp +++ b/src/variants/shogi/state.cpp @@ -93,7 +93,8 @@ const IColor* State::turn() const { void State::setTurn(const IColor* turn) { m_turn = turn; } - + +// FIXME: should take pools into account bool State::equals(IState* other) const { return m_board.equals(other->board()) && m_turn == other->turn() && -- 2.11.4.GIT