《付録》HoneyComb.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 System.Windows import *
from System.Windows.Media import *
from System.Windows.Shapes import *

from hexagon import HexStone
## --------------------               
class Ostone(object):
    def __init__(self, client, x, y, state, Stroke=None, Fill=None):
        self.name = "_%d_%d"%(x, y)
        self.shape = self._shape(x, y)       ;
        if Stroke: self.shape.Stroke = Stroke;
        if Fill  : self.shape.Fill   = Fill  ;

    def __repr__(self):
        return self.name
        
    def _shape(self, x, y):
        gap = 1
        x = HexStone._dx + x * (HexStone._width +gap)
        y = HexStone._dy + y * (HexStone._height+gap)

        s = Polygon(
            Name=self.name,
            HorizontalAlignment=HorizontalAlignment.Center,
            VerticalAlignment=VerticalAlignment.Center,
            Points=self.pointCollection([Point(x+dx*2, y+dy*2)
                for dx, dy in HexStone(x, y, False).vertices]),
            )
        s.MouseUp += self.mouseUp
        return s

    def mouseUp(self, sender, e):
        print ">>>",sender.Name,sender.Fill

    def pointCollection(self, vertices):
        s = PointCollection()
        for e in vertices:
            s.Add(e)
        return s

class CombStone(Ostone):
    def __init__(self, client, x, y):
        super(self.__class__, self).__init__(client, x, y, state=False,
            Stroke=Brushes.Black, Fill=Brushes.Green)

class BlackStone(Ostone):
    def __init__(self, client, x, y):
        super(self.__class__, self).__init__(client, x, y, state=False,
            Stroke=Brushes.Black, Fill=Brushes.Black)

class WhiteStone(Ostone):
    def __init__(self, client, x, y):
        super(self.__class__, self).__init__(client, x, y, state=False,
            Stroke=Brushes.Black, Fill=Brushes.White)
    
class NullStone(Ostone):
    def __init__(self, client, x, y):
        super(self.__class__, self).__init__(client, x, y, state=False,
            Stroke=Brushes.Gray, Fill=None)
    
## --------------------               

テストケースを記述する(1)

Jython で作成した)既存のモジュール hexagon.py を再利用しながら、新たなモジュールの動作を検証するために、テストケースを作成します。

class ExWindow(Window):
    def init(self):
        target = "canvas",
        self._Controls(target)
        self._comb()

    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)

空地となる領域に「緑」の CombStone を敷き詰めます。

    def addBlack(self):
        for x, y in (6,6), (9,8), (6,10):
            cell = BlackStone(self, x, y)
            self.addShape(cell.shape)

先手の領域に「黒」の BlackStone を敷き詰めます。

    def addWhite(self):
        for x, y in (10,6), (7,8), (10,10):
            cell = WhiteStone(self, x, y)
            self.addShape(cell.shape)

後手の領域に「白」の WhiteStone を敷き詰めます。