0
私はPythonでプロジェクトをやっていますが、その中にはゲームのボード状態のランク付けが含まれています。このゲーム、Sneaky Statuesは、4つのピースを連続して獲得することで勝つ4つを接続するのと似ています。ボードの私の素朴な見積もりは、あなたが連続して何枚あるかです(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)
私の仕事は分かりますが、基本的には3回繰り返しています。私の質問は、私が自分自身を少なくするために、この機能を書くための最も無作法な方法は何ですか?