判子草案

shhgs <"".join(chr(ord(c) -2) for c in 'ujjiu0ghjknvBiockn0eqo')> 贡献 请大家共同参详

   1 #coding=cp936
   2 
   3 WHITE = "WHITE"
   4 BLACK = "BLACK"
   5 BORDER = "BORDER"
   6 AVAILABLE = "AVAILBALE"
   7 
   8 class Board(object) :
   9     def __init__(self) :
  10         self.stones = []
  11         self.initialize()
  12     def initialize(self) :
  13         for i in range(0, 21) :
  14             self.stones.append([None,] * 21)
  15         for x in (0, 20) :
  16             for y in range(0, 21) :
  17                 self.stones[x][y] = Stone(self, BORDER, x, y)
  18         for y in (0, 20) :
  19             for x in range(0, 21) :
  20                 self.stones[x][y] = Stone(self, BORDER, x, y)
  21         for x in range(1, 20) :
  22             for y in range(1, 20) :
  23                 self.stones[x][y] = Stone(self, AVAILABLE, x, y)
  24     def __str__(self) :
  25         result = ""
  26         for y in range(0, 21) :
  27             line = ""
  28             for x in range(0, 21) :
  29                 line += str(self.stones[x][y])
  30             result = line + '\n' + result
  31         return result
  32     def WipeBlock(self, block) :
  33         for stone in block :
  34             x = stone.X
  35             y = stone.Y
  36             self.stones[x][y] = Stone(self, AVAILABLE, x, y)
  37     def __iter__(self) :
  38         stones = []
  39         for x in range(1, 20) :
  40             for y in range(1, 20) :
  41                 stones.append(self.stones[x][y])
  42         return iter(stones)
  43     def GetBlocks(self) :
  44         result = []
  45         for stone in self :
  46             if stone.Color not in (BLACK, WHITE) :
  47                 continue
  48             neighbor_blocks = []
  49             for block in result :
  50                 if block.AddStone(stone) :
  51                     neighbor_blocks.append(block)
  52             if len(neighbor_blocks) == 0 :
  53                 new_block = Block.CreateBlock(stone)
  54                 result.append(new_block)
  55             elif len(neighbor_blocks) >= 2 :
  56                 result.append(Block.merge(neighbor_blocks))
  57                 for block in neighbor_blocks :
  58                     result.remove(block)
  59         return result
  60     def AddStone(self, stone) :
  61         x = stone.X
  62         y = stone.Y
  63         self.stones[x][y] = stone
  64         if stone.Color == WHITE :
  65             my = WHITE
  66             other = BLACK
  67         else :
  68             my = BLACK
  69             other = WHITE
  70         blocks = self.GetBlocks()
  71         for block in blocks :
  72             if block.Color == other and block.Breath == 0 :
  73                 self.WipeBlock(block)
  74         blocks = self.GetBlocks()
  75         for block in blocks :
  76             if block.Color == my and block.Breath == 0 :
  77                 self.WipeBlock(block)
  78     
  79 class Block(object) :
  80     def __init__(self, color) :
  81         assert color in (BLACK, WHITE)
  82         self.stones = set()
  83         self.color  = color
  84     @property
  85     def Color(self) :
  86         return self.color
  87     def AddStone(self, stone) :
  88         if stone.color != self.color :
  89             return False
  90         if len(self.stones) == 0 :
  91             self.stones.add(stone)
  92             return True
  93         for direction in ('Up', 'Down', 'Left', 'Right') :
  94             neighbor = getattr(stone, direction)
  95             if neighbor in self.stones :
  96                 self.stones.add(stone)
  97                 return True
  98         else :
  99             return False
 100     def __iter__(self) :
 101         return iter(self.stones)
 102     @classmethod
 103     def merge(self, blocks) :
 104         newBlock = Block(blocks[0].Color)
 105         for block in blocks :
 106             for stone in block :
 107                 newBlock.stones.add(stone)
 108         return newBlock
 109     @classmethod
 110     def CreateBlock(self, stone) :
 111         if stone.Color not in (BLACK, WHITE) :
 112             return
 113         block = Block(stone.color)
 114         block.stones.add(stone)
 115         return block
 116     @property
 117     def Breath(self) :
 118         breaths = set()
 119         for stone in self.stones :
 120             for direction in ('Up', 'Down', 'Left', 'Right') :
 121                 neighbor = getattr(stone, direction)
 122                 if neighbor.IsAvailable :
 123                     breaths.add(neighbor)
 124         return len(breaths)
 125     def __str__(self) :
 126         result = "------------------------\n"
 127         for s in self.stones :
 128             result += repr(s) + "\n"
 129         result += "------------------------"
 130         return result
 131 
 132 class Stone(object) :
 133     def __init__(self, board, color , x, y) :
 134         assert color in (BLACK, WHITE, BORDER, AVAILABLE)
 135         self.board = board
 136         self.color = color
 137         self.x = x
 138         self.y = y
 139     def __repr__(self) :
 140         return "<%s %d, %d>" % (self.Color, self.X, self.Y)
 141     @property 
 142     def X(self) :
 143         return self.x
 144     @property 
 145     def Y(self) :
 146         return self.y
 147     @property
 148     def Color(self) :
 149         return self.color
 150     @property 
 151     def IsBorder(self) :
 152         if self.X in (0, 20) or self.Y in (0, 20) :
 153             return True
 154         else :
 155             return False
 156     @property 
 157     def IsBlack(self) :
 158         return self.color == BLACK
 159     @property 
 160     def IsWhite(self) :
 161         return self.color == WHITE
 162     @property 
 163     def IsAvailable(self) :
 164         return self.color == AVAILABLE
 165     def __str__(self) :
 166         if self.IsBorder :
 167             if self.X in (0, 20) and self.Y in (0, 20) :
 168                 return "+"
 169             elif self.Y in (0, 20) :
 170                 return "--"
 171             else :
 172                 return '|'
 173         elif self.IsBlack :
 174             return ' X'
 175         elif self.IsWhite :
 176             return ' O'
 177         else :
 178             return ' .'
 179     def __eq__(self, other) :
 180         try :
 181             assert type(self) == type(other)
 182             assert self.color == other.color
 183             assert self.board == other.board
 184             assert self.X == other.X
 185             assert self.Y == other.Y
 186             return True
 187         except :
 188             return False
 189     def __ne__(self, other) :
 190         return not self == other
 191     @property
 192     def Up(self) :
 193         x = self.x 
 194         y = self.y + 1
 195         return self.board.stones[x][y]
 196     @property
 197     def Down(self) :
 198         x = self.x
 199         y = self.y - 1
 200         return self.board.stones[x][y]
 201     @property
 202     def Left(self) :
 203         x = self.x - 1
 204         y = self.y
 205         return self.board.stones[x][y]
 206     @property
 207     def Right(self) :
 208         x = self.x + 1
 209         y = self.y
 210         return self.board.stones[x][y]
 211 
 212 if __name__ == "__main__" :
 213     board = Board()
 214     TURNS = (BLACK, WHITE)
 215     round = 0
 216     while 1 :
 217         turn = TURNS[round % 2]
 218         round += 1
 219         x = int(raw_input("x: "))
 220         if x == 0 :
 221             break
 222         y = int(raw_input("y: "))
 223         stone = Stone(board, turn, x, y)
 224         board.AddStone(stone)
 225         print board