状況設定
ここでは、Swing/Java で構築された既存のシステムを、WPF/C# で再構築するとともに、新機能を追加するような状況を想定しています。この課題では、既存のリソースとして、Swing/Jython で作成された、オリジナル版のゲームがあるものとします。要求仕様の変更は、6角形を基調とするデザインに一新して、これを WPF アプリケーションとして作成することです。
《Note》.NET アプリケーションに、IronPython で作成したモジュール群を埋め込むのは簡単です。具体的な手順は、IronPython をダウンロードすると得られる、IronPython Tutorial 6: Embedding IronPython を参照してください。□
開放閉鎖原則〔OCL: Open-Closed Principle〕に沿って、既存のモジュールを修正せずに、新たなモジュールを追加するだけで、要求仕様の変更に対処できるのを理想とします。
オリジナル版は正方形ですが、仕様変更に伴って、今回は6角形を基調とするデザインに一新します。そのため、Swing/WPF の違いも含めて、その部分を新規作成する必要があります。
すでに、特定のビュー(Swing)に依存しないように、モデル(Jython)は独立したモジュールとして提供されているので、これは再利用できそうです。問題があるとすれば、Jython/IronPython の互換性ですが、プログラミング言語(Java/C#)の違いは問題にはなりません。そこが、glue 言語としての Python の効能のひとつです。
《付録》exHoneyComb.py
# -*- coding: utf-8 -*- #=============================================================================== # Copyright (C) 2000-2008, 小泉ひよ子とタマゴ倶楽部 # # Change History: WPF examples # 2008/01/25, IronPython 1.1.1 (download) # 2008/08/22, IronPython 1.1.2 (download) # 2008/03/16, ver.2.0, WPF # 2008/00/00, ver.2.1, IronPython 1.1.2 # Change History: Games # 1988/05, Smalltalk # 2004/09, Java # 2005/02, C# # 2005/03, Jython #=============================================================================== from _ant import * from System.Windows import * from HoneyComb import * ## -------------------- class ExWindow(Window): def __init__(self, Content=None, **args): self.InitializeComponent(Content) self.init() def InitializeComponent(self, Content): self.Content = LoadXaml(Content) def init(self): target = "canvas", self._Controls(target) self.comb = self._comb() def _Controls(self, target): controls = xaml_controls(self) for e in target: setattr(self, e, controls[e]) def _comb(self): self.addComb() self.addBlack() self.addWhite() def addComb(self): for n, x, y in [ (6, 3, 0), (7, 2, 2), (8, 1, 4), (9, 0, 6), (8, 1, 8), (9, 0, 10), (8, 1, 12), (7, 2, 14), (6, 3, 16), ]: for e in range(n): cell = CombStone(self, e*2+x, y) self.addShape(cell.shape) def addBlack(self): for x, y in (6,6), (9,8), (6,10): cell = BlackStone(self, x, y) self.addShape(cell.shape) def addWhite(self): for x, y in (10,6), (7,8), (10,10): cell = WhiteStone(self, x, y) self.addShape(cell.shape) def addShape(self, shape): return self.canvas.Children.Add(shape) ## -------------------- if __name__ == "__main__": xaml = __file__.replace(".py",".xaml") win = ExWindow( Title=__file__, Width=298, Height=286, Content=xaml, ) Application().Run(win) ## -------------------- ## c:\easyWPF\chap07>..\..\..\IronPython-1.1.1\ipy.exe exHoneyComb.py ## >>> _7_8 #FFFFFFFF ## >>> _9_8 #FF000000 ## >>> _8_6 #FF008000