我被困在關於從棋盤上獲得核心棋子的問題上。 例如,如果棋盤是3x3,那麼如果拼圖數量爲「5」,則只返回「中心」。如果董事會是一個2×2,那麼我會返回每個瓦片的「中心」。由於我很難編碼這個問題,所以我遇到了問題。到目前爲止,我有:從板上找到中心棋子
def getMiddle(width, height, tile):
width = width
height = height
tile = tile
while width*height == 1 or 2 or 4:
return "center"
while width*height == 9:
if tile == 5:
return "center"
else:
return "not center"
這一直是我的一般做法,但我一直在告訴我,這是硬編碼。 我後來發現我應該在我的代碼的頂部有這些功能,並在我的代碼中使用它們。防爆。如果行== 0:等等等等
row = (tile - 1) // width
col = (tile - 1) % width
我真的失去了對如何開始這個編碼使用上述函數來創建一個通用的解決方案,使我沒有硬編碼。將不勝感激!