《付録》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)
    
## --------------------