0
我正在做一个python项目,其中的一部分涉及在游戏中排列棋盘状态。这个游戏,Sneaky雕像,类似于连接四个,你可以连续获得四个棋子。我对董事会的天真估计是你连续有多少件(1,2,3或4)。棋盘是一个三角形,所以你可以将棋子连成一排,或者沿任一方向对角。这是我现在使用的函数来查找片段。如何在Python中有效地重用代码
def score(player):
player_x = sorted(player, key=lambda statue: statue.x) #player's pieces sorted by x coordinate
player_y = sorted(player, key=lambda statue: statue.y)
max_score = [0]
count = 1
#pieces are in a horizontal line if they share a y coord and have sequential x coords
for cur_s, next_s in zip(player_x, player_x[1:]):
if cur_s.x + 1 == next_s.x and cur_s.y == next_s.y:
count += 1
else:
max_score.append(count)
count = 1
max_score.append(count)
count = 1
#pieces are diagonal if they share an x and have sequental y's
for cur_s, next_s in zip(player_y, player_y[1:]):
if cur_s.y + 1 == next_s.y and cur_s.x == next_s.x:
count += 1
else:
max_score.append(count)
count = 1
max_score.append(count)
count = 1
#they are diagonal if both x's and y's are sequential
for cur_s, next_s in zip(player_y, player_y[1:]):
if cur_s.y + 1 == next_s.y and cur_s.x + 1 == next_s.x:
count += 1
else:
max_score.append(count)
count = 1
max_score.append(count)
return max(max_score)
据我可以告诉它的工作,但我基本上是在重复自己的三倍。我的问题是,我写这个函数的最pythonic方式是什么,这样我就可以更少地重复自己了?