本文整理汇总了Python中block.Block.add_stone方法的典型用法代码示例。如果您正苦于以下问题:Python Block.add_stone方法的具体用法?Python Block.add_stone怎么用?Python Block.add_stone使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类block.Block
的用法示例。
在下文中一共展示了Block.add_stone方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from block import Block [as 别名]
# 或者: from block.Block import add_stone [as 别名]
def __init__(self, size):
"""Initialize board:
argument: size
"""
self.size = size
self.side = BLACK
self.goban = {} #actual board
self.init_hash()
#Create and initialize board as empty size*size
for pos in self.iterate_goban():
#can't use set_goban method here, because goban doesn't yet really exists
self.goban[pos] = EMPTY
self.current_hash = self.current_hash ^ self.board_hash_values[EMPTY, pos]
self.blocks = {} #blocks dictionary
#Create and initialize one whole board empty block
new_block = Block(EMPTY)
for pos in self.iterate_goban():
new_block.add_stone(pos)
self.block_dict = {}
self.add_block(new_block)
self.chains = {}
self.stone_count = {}
for color in EMPTY+BLACK+WHITE:
self.stone_count[color] = 0
self.ko_flag = PASS_MOVE
BoardAnalysis.__init__(self)
示例2: split_marked_group
# 需要导入模块: from block import Block [as 别名]
# 或者: from block.Block import add_stone [as 别名]
def split_marked_group(self, block, mark):
"""move all stones with given mark to new block
Return splitted group.
"""
new_block = Block(block.color)
for stone, value in block.stones.items():
if value==mark:
block.remove_stone(stone)
new_block.add_stone(stone)
return new_block
示例3: simple_same_block
# 需要导入模块: from block import Block [as 别名]
# 或者: from block.Block import add_stone [as 别名]
def simple_same_block(self, pos_list):
"""Check if all positions in pos_list are in same block.
This searches only at immediate neighbour.
Return True if they are or False if not or can't decide with this simple search.
"""
if len(pos_list) <= 1:
return True
color = self.goban[pos_list[0]]
temp_block = Block(color)
#Add all stones in pos_list and their neighbour to block if they have same color.
for pos in pos_list:
temp_block.add_stone(pos)
for pos2 in self.iterate_neighbour(pos):
if self.goban[pos2]==color:
temp_block.add_stone(pos2)
new_mark = 2 #When stones are added they get by default value True (==1)
self.flood_mark(temp_block, pos_list[0], new_mark)
for pos in pos_list:
if temp_block.stones[pos]!=new_mark:
return False
return True
示例4: add_stone
# 需要导入模块: from block import Block [as 别名]
# 或者: from block.Block import add_stone [as 别名]
def add_stone(self, color, pos):
"""add stone or empty at given position
color: color of stone or empty
pos: position of stone
This will add stone to existing block or create new block.
In case need to join blocks, smaller blocks are added to bigger blocks one stone at time.
Everytime stone is added, neighbour attribute is also updated.
If there is possibility of split, then we flood fill each neighbour width first step at time until floods meet or smaller fill stops.
Flood is continued as long as there is more than one active flood area.
If flood fill shows split, then smaller block is removed from bigger block.
"""
#global board_backup
#board_backup = copy.deepcopy(self)
old_block = self.blocks[pos]
old_color = old_block.color
self.set_goban(color, pos)
base_add_block = None #biggest block
blocks_add_list = [] #other blocks being combined
add_block_neighbour = {} #potentially new neighbours
pos_split_list = [] #positions that might now belong to separate blocks
#analyze changes
for pos2 in self.iterate_neighbour(pos):
stone = self.goban[pos2]
if stone==color:
other_block = self.blocks[pos2]
if other_block==base_add_block or other_block in blocks_add_list:
continue
if base_add_block:
if base_add_block.size()<other_block.size():
blocks_add_list.append(base_add_block)
base_add_block = other_block
else:
blocks_add_list.append(other_block)
else:
base_add_block = other_block
else:
add_block_neighbour[pos2] = True
if stone==old_color:
pos_split_list.append(pos2)
#combine blocks
if not base_add_block:
new_block = Block(color)
new_block.add_stone(pos)
self.add_block(new_block)
else:
base_add_block.add_stone(pos)
self.blocks[pos] = base_add_block
for other_block in blocks_add_list:
self.combine_blocks(base_add_block, other_block)
del base_add_block.neighbour[pos]
new_block = base_add_block
new_block.neighbour.update(add_block_neighbour)
#split blocks
if len(pos_split_list)==0:
self.delete_block(old_block)
else:
#ff=flood_fill
flood_fill_list = []
for pos2 in pos_split_list:
ff = FloodFill(pos2, self)
flood_fill_list.append(ff)
blocks_created = []
i = 0
#iterate until meets another or exhausted
while len(flood_fill_list)>1:
if i>=len(flood_fill_list):
i = 0
try:
pos2 = flood_fill_list[i].next()
#meets another, remove extra
for j in range(len(flood_fill_list)):
if j==i:
continue
ff2 = flood_fill_list[j]
if pos2 in ff2.seen:
del flood_fill_list[i]
break
else:
i = i + 1
except StopIteration:
#exhausted, create new block
block = flood_fill_list[i].create_block()
blocks_created.append(block)
del flood_fill_list[i]
#remove new blocks from old blocks
for block in blocks_created:
for stone in block.stones:
old_block.remove_stone(stone)
for pos2 in block.neighbour:
self.check_neighbour(old_block, pos2)
self.add_block(block)
old_block.remove_stone(pos)
old_block.neighbour[pos] = True
#are pos neighbour positions also neighbour to reduced old_block?
for pos2 in self.iterate_neighbour(pos):
self.check_neighbour(old_block, pos2)
示例5: analyze_color_unconditional_status
# 需要导入模块: from block import Block [as 别名]
# 或者: from block.Block import add_stone [as 别名]
#.........这里部分代码省略.........
block2.status = UNCONDITIONAL_LIVE
#Unconditional dead part:
#Mark all groups with only 1 potential empty point and completely surrounded by live groups as dead.
#All empty points adjacent to live group are not counted.
for eye_group in not_ok_eye_list:
eye_group.dead_analysis_done = False
for eye_group in not_ok_eye_list:
if eye_group.dead_analysis_done: continue
eye_group.dead_analysis_done = True
true_eye_list = []
false_eye_list = []
eye_block = Block(eye_colors)
#If this is true then creating 2 eyes is impossible or we need to analyse false eye status.
#If this is false, then we are unsure and won't mark it as dead.
two_eyes_impossible = True
has_unconditional_neighbour_block = False
maybe_dead_group = Eye()
blocks_analysed = []
blocks_to_analyse = eye_group.parts[:]
while blocks_to_analyse and two_eyes_impossible:
block = blocks_to_analyse.pop()
if block.eye:
block.eye.dead_analysis_done = True
blocks_analysed.append(block)
if block.status==UNCONDITIONAL_LIVE:
if block.color==color:
has_unconditional_neighbour_block = True
else:
two_eyes_impossible = False
continue
maybe_dead_group.parts.append(block)
for pos in block.stones:
eye_block.add_stone(pos)
if block.color==EMPTY:
eye_type = self.analyse_eye_point(pos, color)
elif block.color==color:
eye_type = self.analyse_opponent_stone_as_eye_point(pos)
else:
continue
if eye_type==None:
continue
if eye_type==True:
if len(true_eye_list) == 2:
two_eyes_impossible = False
break
elif len(true_eye_list) == 1:
if self.are_adjacent_points(pos, true_eye_list[0]):
#Second eye point is adjacent to first one.
true_eye_list.append(pos)
else: #Second eye point is not adjacent to first one.
two_eyes_impossible = False
break
else: #len(empty_list) == 0
true_eye_list.append(pos)
else: #eye_type==False
false_eye_list.append(pos)
if two_eyes_impossible:
#bleed to neighbour blocks that are at other side of blocking color block:
#consider whole area surrounded by unconditional blocks as one group
for pos in block.neighbour:
block = self.blocks[pos]
if block not in blocks_analysed and block not in blocks_to_analyse:
blocks_to_analyse.append(block)
#must be have some neighbour groups:
示例6: old_add_stone
# 需要导入模块: from block import Block [as 别名]
# 或者: from block.Block import add_stone [as 别名]
def old_add_stone(self, color, pos):
"""add stone or empty at given position
color: color of stone or empty
pos: position of stone
This will create new block for stone
and add stones from same colored neighbour blocks if any.
Also makes every position in combined block to point to block.
Remove pos from existing block and potentially split it.
Finally calculate new neighbours for all changed blocks:
This is needed only when block is split into 2 or more blocks.
Other cases are handed in need to do basis.
"""
old_block = self.blocks[pos]
old_color = old_block.color
old_block.remove_stone(pos)
if old_block.size()==0:
self.block_list.remove(old_block)
self.set_goban(color, pos)
new_block = Block(color)
new_block.add_stone(pos)
self.add_block(new_block)
changed_blocks = [] #only those blocks that need complete neighbour calculation
#old_block: Is anything left?
# Is it split into pieces?
#new_block: Is there existing same colored neighbour blocks?
#both and all existing neighbours: calculate neighbor list (from scratch?)
#........OO.........
#......OO.O.........
#......O.!.O...OOOO.
#.......O.OO...O..O.
#.XXX...XX....XX.O..
#.X.!XX.X.!XX.X.!...
#.XXX...XX.....X.O..
#..........X!X......
#...........O.......
#combine and split blocks as needed
split_list = []
for pos2 in self.iterate_neighbour(pos):
other_block = self.blocks[pos2]
if self.goban[pos2]==color:
new_block = self.combine_blocks(new_block, other_block)
else:
new_block.neighbour[pos2] = True
if self.goban[pos2]==old_color:
split_list.append(pos2)
#If these splits are actually trivially same: do update fast
if self.simple_same_block(split_list):
old_block.neighbour[pos] = True
#are pos neighbour positions also neighbour to reduced old_block?
for pos2 in self.iterate_neighbour(pos):
if pos2 not in old_block.stones: #this if is slight optimization: can't be neighbour if belongs to block
for pos3 in self.iterate_neighbour(pos2):
if pos3 in old_block.stones:
break #yes, pos2 is neighbour
else: #no, it's not neighbour
#was it neighbour to old_block? remove it if it is
if pos2 in old_block.neighbour:
del old_block.neighbour[pos2]
else:
changed_blocks.append(old_block) #now we need this
old_block.mark_stones(0)
last_old_mark = 0
for pos2 in split_list:
other_block = self.blocks[pos2]
if other_block.stones[pos2]==0:
last_old_mark = last_old_mark + 1
self.flood_mark(other_block, pos2, last_old_mark)
if last_old_mark>1:
splitted_block = self.split_marked_group(other_block, last_old_mark)
self.add_block(splitted_block)
changed_blocks.append(splitted_block)
if pos in new_block.neighbour:
del new_block.neighbour[pos]
for block in changed_blocks:
self.calculate_neighbour(block)