Tentative MiniShogi implementation over move generator.master/minishogi
authorYann Dirson <ydirson@altern.org>
Sun, 20 Apr 2008 19:52:42 +0000 (20 21:52 +0200)
committerYann Dirson <ydirson@altern.org>
Sun, 20 Apr 2008 19:52:42 +0000 (20 21:52 +0200)
13 files changed:
debian/.gitignore
debian/control
debian/tagua-variant-minishogi.files [new file with mode: 0644]
src/variantfactories/builtinvariants.cpp
src/variants/CMakeLists.txt
src/variants/minishogi/CMakeLists.txt [new file with mode: 0644]
src/variants/minishogi/behaviour.cpp [new file with mode: 0644]
src/variants/minishogi/behaviour.h [new file with mode: 0644]
src/variants/minishogi/global.h [new file with mode: 0644]
src/variants/minishogi/minishogi.cpp [new file with mode: 0644]
src/variants/minishogi/state.cpp [new file with mode: 0644]
src/variants/minishogi/state.h [new file with mode: 0644]
src/variants/minishogi/tagua-minishogi.desktop [new file with mode: 0644]

index 0b6b6da..f56fe00 100644 (file)
@@ -7,4 +7,5 @@ tagua-variant-chess
 tagua-variant-chess5x5
 tagua-variant-crazyhouse
 tagua-variant-shogi
+tagua-variant-minishogi
 tagua-dbg
index 4721787..b6b978d 100644 (file)
@@ -67,3 +67,12 @@ Provides: tagua-variant
 Description: Shogi support for the tagua board-game frontend
  This package allows to play Shogi, a japanese chess-like game, from
  tagua.
+
+Package: tagua-variant-minishogi
+Architecture: any
+Depends: tagua, tagua-variant-shogi
+Suggests: gnushogi
+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.
diff --git a/debian/tagua-variant-minishogi.files b/debian/tagua-variant-minishogi.files
new file mode 100644 (file)
index 0000000..2a0fde2
--- /dev/null
@@ -0,0 +1,2 @@
+usr/lib/kde4/taguaminishogi.so
+usr/share/kde4/services/tagua-minishogi.desktop
index ed75bb7..7a0ec62 100644 (file)
@@ -7,6 +7,7 @@ extern "C" Repository* taguachess_initrepo(IVariantLoader*);
 extern "C" Repository* taguachess5x5_initrepo(IVariantLoader*);
 extern "C" Repository* taguacrazyhouse_initrepo(IVariantLoader*);
 extern "C" Repository* taguashogi_initrepo(IVariantLoader*);
+extern "C" Repository* taguaminishogi_initrepo(IVariantLoader*);
 
 // FIXME: a KDE plugin willing to reuse a builtin one crashes
 
@@ -22,6 +23,7 @@ static const struct builtin {
   { "Chess 5x5", taguachess5x5_initrepo, "Chess" },
   { "Crazyhouse", taguacrazyhouse_initrepo, "Chess" },
   { "Shogi", taguashogi_initrepo, "Shogi" },
+  { "MiniShogi", taguaminishogi_initrepo, "Shogi" },
 };
 
 static const struct builtin* const get_builtin(QString name) {
index 8e7de5d..ec5288f 100644 (file)
@@ -3,6 +3,7 @@ add_subdirectory(chess5x5)
 add_subdirectory(crazyhouse)
 
 add_subdirectory(shogi)
+add_subdirectory(minishogi)
 
 if(MONOLITH)
 kde4_add_library(taguavariants SHARED dummy.cpp)
@@ -11,6 +12,7 @@ target_link_libraries(taguavariants
   taguachess5x5
   taguacrazyhouse
   taguashogi
+  taguaminishogi
 )
 install(TARGETS taguavariants DESTINATION ${LIB_INSTALL_DIR})
 endif(MONOLITH)
diff --git a/src/variants/minishogi/CMakeLists.txt b/src/variants/minishogi/CMakeLists.txt
new file mode 100644 (file)
index 0000000..ab6f07e
--- /dev/null
@@ -0,0 +1,21 @@
+set(taguaminishogi_SRCS
+  minishogi.cpp
+  behaviour.cpp
+  state.cpp
+)
+
+include_directories(${CMAKE_SOURCE_DIR}/src/)
+
+if(MONOLITH)
+kde4_add_library(taguaminishogi SHARED ${taguaminishogi_SRCS})
+else(MONOLITH)
+kde4_add_plugin(taguaminishogi ${taguaminishogi_SRCS})
+endif(MONOLITH)
+
+target_link_libraries(taguaminishogi
+  taguacore
+  ${KDE4_KDECORE_LIBS}
+)
+
+install(TARGETS taguaminishogi DESTINATION ${PLUGIN_INSTALL_DIR})
+install(FILES tagua-minishogi.desktop DESTINATION ${SERVICES_INSTALL_DIR})
diff --git a/src/variants/minishogi/behaviour.cpp b/src/variants/minishogi/behaviour.cpp
new file mode 100644 (file)
index 0000000..1a3d88f
--- /dev/null
@@ -0,0 +1,8 @@
+#include "behaviour.h"
+
+namespace MiniShogi {
+
+Behaviour::Behaviour(IBehaviour* behaviour)
+  : Delegators::Behaviour(behaviour) { }
+
+} // namespace MiniShogi
diff --git a/src/variants/minishogi/behaviour.h b/src/variants/minishogi/behaviour.h
new file mode 100644 (file)
index 0000000..cdcaf8b
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+  Copyright (c) 2008 Yann Dirson <ydirson@altern.org>
+
+  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 MINISHOGI__BEHAVIOUR_H
+#define MINISHOGI__BEHAVIOUR_H
+
+#include <core/delegators/behaviour.h>
+#include <core/promotionmanager.h>
+
+class Move;
+
+namespace MiniShogi {
+
+class TAGUA_EXPORT Behaviour : public Delegators::Behaviour {
+Q_OBJECT
+  Behaviour* m_delegator;
+public:
+  Behaviour(IBehaviour* behaviour);
+
+public Q_SLOTS: // delegated
+  void setPromotionManager(PromotionManager* pm) {
+    Q_ASSERT(QMetaObject::invokeMethod(m_dgate_behaviour, "setPromotionManager",
+                                      Q_ARG(PromotionManager*, pm)));
+  }
+
+  const PromotionManager* promotionManager() const {
+    const PromotionManager* result;
+    Q_ASSERT(QMetaObject::invokeMethod(m_dgate_behaviour, "promotionManager",
+                                      Q_RETURN_ARG(const PromotionManager*, result)));
+    return result;
+  }
+
+  bool mayPromote(const IState* state, const Move& move) const {
+    bool result;
+    Q_ASSERT(QMetaObject::invokeMethod(m_dgate_behaviour, "mayPromote",
+                                      Q_RETURN_ARG(bool, result),
+                                      Q_ARG(const IState*, state),
+                                      Q_ARG(const Move, move)));
+    return result;
+  }
+
+public Q_SLOTS: // overridden
+  unsigned promotionZoneWidth() const { return 1; }
+};
+
+} // namespace MiniShogi
+
+#endif // MINISHOGI__BEHAVIOUR_H
diff --git a/src/variants/minishogi/global.h b/src/variants/minishogi/global.h
new file mode 100644 (file)
index 0000000..dfed3e6
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+  Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
+
+  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 <core/type.h>
+#include <core/color.h>
+
+namespace MiniShogi {
+
+extern const IType* pawn;
+extern const IType* king;
+extern const IType* gold;
+extern const IType* silver;
+extern const IType* bishop;
+extern const IType* rook;
+extern const IColor* players[];
+
+}
+
diff --git a/src/variants/minishogi/minishogi.cpp b/src/variants/minishogi/minishogi.cpp
new file mode 100644 (file)
index 0000000..4b5628a
--- /dev/null
@@ -0,0 +1,74 @@
+#include "behaviour.h"
+#include "state.h"
+
+#include <core/color.h>
+#include <core/point.h>
+#include <core/repository.h>
+#include <core/variantloader.h>
+#include <core/state.h>
+#include <core/type.h>
+
+#include <KDebug>
+
+using namespace MiniShogi;
+
+#define LOAD_TYPE(NAME) \
+  NAME = requestInterface<IType>(shogi->getComponent("type/" #NAME))
+
+namespace MiniShogi {
+  const IType* pawn;
+  const IType* king;
+  const IType* gold;
+  const IType* silver;
+  const IType* bishop;
+  const IType* rook;
+  const IColor* players[2];
+}
+
+extern "C" KDE_EXPORT Repository*
+taguaminishogi_initrepo(IVariantLoader* loader) {
+  Repository* repo = new Repository;
+  Repository* shogi = loader->getRepository("shogi");
+  if (!shogi)
+    // bail out if there is no shogi variant
+    return 0;
+
+  repo->setProxy(shogi);
+
+  // get shogi state factory
+  Component* shogi_state_comp = shogi->getComponent("state");
+  IState* shogi_state = requestInterface<IState>(shogi_state_comp);
+
+  // base behaviour on shogi
+  Component* shogi_behaviour_comp = shogi->getComponent("behaviour");
+  Q_ASSERT(shogi_behaviour_comp);
+  IBehaviour* behaviour_clone = NULL;
+  Q_ASSERT(QMetaObject::invokeMethod(shogi_behaviour_comp, "clone",
+                                    Q_RETURN_ARG(IBehaviour*, behaviour_clone),
+                                    Q_ARG(PromotionManager*, NULL)));
+  Q_ASSERT(behaviour_clone);
+  Behaviour* behaviour = new Behaviour(behaviour_clone);
+
+  // create minishogi state factory
+  IState* state_clone = 0;
+  Q_ASSERT(QMetaObject::invokeMethod(shogi_state_comp, "clone",
+                                    Q_RETURN_ARG(IState*, state_clone),
+                                    Q_ARG(const IBehaviour*, behaviour),
+                                    Q_ARG(Point, Point(5, 5))));
+  Q_ASSERT(state_clone);
+  State* state = new State(state_clone);
+
+  // set state factory
+  repo->addComponent("state", state);
+
+  LOAD_TYPE(pawn);
+  LOAD_TYPE(king);
+  LOAD_TYPE(gold);
+  LOAD_TYPE(silver);
+  LOAD_TYPE(bishop);
+  LOAD_TYPE(rook);
+  players[0] = Black::self();
+  players[1] = White::self();
+
+  return repo;
+}
diff --git a/src/variants/minishogi/state.cpp b/src/variants/minishogi/state.cpp
new file mode 100644 (file)
index 0000000..185f716
--- /dev/null
@@ -0,0 +1,35 @@
+#include "global.h"
+#include "state.h"
+
+#include <core/board.h>
+#include <core/color.h>
+
+namespace MiniShogi {
+
+State::State(IState* state)
+: Delegators::DefaultState(state) { }
+
+IState* State::clone() const {
+  return new State(m_dgate_defaultstate->clone());
+}
+
+#define COL(i,c) players[c]==White::self() ? i : board()->size().x - i - 1
+void State::setup() {
+  for (int c = 0; c < 2; c++) {
+    const IColor* color = players[c];
+    int r0 = rank(0, color);
+    int r1 = rank(1, color);
+
+    board()->set(Point(COL(0,c), r1), Piece(color, pawn));
+    board()->set(Point(COL(0,c), r0), Piece(color, king));
+    board()->set(Point(COL(1,c), r0), Piece(color, gold));
+    board()->set(Point(COL(2,c), r0), Piece(color, silver));
+    board()->set(Point(COL(3,c), r0), Piece(color, bishop));
+    board()->set(Point(COL(4,c), r0), Piece(color, rook));
+  }
+
+  setTurn(White::self());
+}
+#undef COL
+
+} // namespace MiniShogi
diff --git a/src/variants/minishogi/state.h b/src/variants/minishogi/state.h
new file mode 100644 (file)
index 0000000..a581880
--- /dev/null
@@ -0,0 +1,24 @@
+#ifndef MINISHOGI__STATE_H
+#define MINISHOGI__STATE_H
+
+#include <core/delegators/defaultstate.h>
+#include <core/component.h>
+#include <core/piece.h>
+
+/**
+  * @brief Namespace holding Minishogi components.
+  */
+namespace MiniShogi {
+
+class State : public Delegators::DefaultState {
+Q_OBJECT
+public:
+  State(IState* state);
+  virtual IState* clone() const;
+  virtual void setup();
+};
+
+} // namespace MiniShogi
+
+#endif // MINISHOGI__STATE_H
+
diff --git a/src/variants/minishogi/tagua-minishogi.desktop b/src/variants/minishogi/tagua-minishogi.desktop
new file mode 100644 (file)
index 0000000..44cdf55
--- /dev/null
@@ -0,0 +1,20 @@
+[Desktop Entry]
+Encoding=UTF-8
+Name=MiniShogi
+Comment=Shogi variant on a 5x5 board
+Icon=tagua
+Type=Service
+ServiceTypes=Tagua/Variant
+
+X-KDE-Library=taguaminishogi
+X-KDE-PluginInfo-Author=Yann Dirson
+X-KDE-PluginInfo-Email=ydirson@altern.org
+X-KDE-PluginInfo-Name=minishogi
+X-KDE-PluginInfo-Version=0.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=Shogi
+X-Tagua-Hidden=false