gpt4 book ai didi

Python:列表不对角搜索

转载 作者:太空宇宙 更新时间:2023-11-03 11:49:48 24 4
gpt4 key购买 nike

我正在实现黑白棋/黑白棋游戏的 Python 版本。但是,我的算法在西南方向搜索时似乎遇到了问题。

这里有一些重要的函数来理解我当前的代码是如何工作的:

def _new_game_board(self)->[[str]]:
board = []
for row in range(self.rows):
board.append([])
for col in range(self.columns):
board[-1].append(0)
return board
def _is_valid_position(self, turn:list)->bool:
'''return true if the turn is a valid row and column'''
row = int(turn[0]) - 1
column = int(turn[1]) - 1
if row >= 0:
if row < self.rows:
if column >= 0:
if column < self.columns:
return True
else:
return False


def _is_on_board(self, row:int, col:int)->bool:
'''returns true is coordinate is on the board'''
if row >=0:
if row < self.rows:
if col >=0:
if col < self.columns:
return True




def _searchNorthEast(self)->None:
'''Search the board NorthEast'''
print("NorthEast")
row = self.move_row
column = self.move_column
should_be_flipped = list()
row += 1
column -= 1
if self._is_on_board(row, column):
print("column searching NorthEast on board")
if self.board[row][column] == self._opponent:
should_be_flipped.append([row, column])
while True:
row += 1
column -= 1
if self._is_on_board(row, column):
if self.board[row][column] == self._opponent:
should_be_flipped.append([row, column])
continue
elif self.board[row][column] == self.turn:
self._to_be_flipped.extend(should_be_flipped)
break
else:
break
else:
self._to_be_flipped.extend(should_be_flipped)
else:
pass

def _searchSouthWest(self)->None:
'''Search the board SouthWest'''
print("in SouthWest")
row = self.move_row
column = self.move_column
should_be_flipped = list()
row -= 1
column += 1
if self._is_on_board(row, column):
print("column searching SouthWest on board")
if self.board[row][column] == self._opponent:
should_be_flipped.append([row, column])
while True:
row -= 1
column += 1
if self._is_on_board(row, column):
if self.board[row][column] == self._opponent:
should_be_flipped.append([row, column])
continue
elif self.board[row][column] == self.turn:
self._to_be_flipped.extend(should_be_flipped)
break
else:
break
else:
self._to_be_flipped.extend(should_be_flipped)
else:
pass

def _move_is_valid(self, turn:list)->bool:
'''Verify move is valid'''
self._to_be_flipped = list()
self._opponent = self._get_opposite(self.turn)
if self._is_valid_position(turn):
self.move_row = int(turn[0]) - 1
self.move_column = int(turn[1]) - 1
self._searchRight()
self._searchLeft()
self._searchUp()
self._searchDown()
self._searchNorthWest()
self._searchNorthEast
self._searchSouthEast()
self._searchSouthWest()
if len(self._to_be_flipped) > 0:
return True
else:
return False

现在假设当前的面板如下所示:

. . . .
W W W .
. B B .
. B . .

Turn: B

玩家移动到第 1 行第 4 列,它说无效,因为它没有检测到第 2 行第 3 列中的白色棋子被翻转。我的所有其他功能都以相同的方式编写。除了在这种情况下,我可以让它在所有其他方向上工作。

知道为什么它没有检测到这个对角线方向的棋子吗?

最佳答案

不要重复自己。 _search* 方法非常冗余,这使得很难看出

中的标志
row -= 1
column += 1

是正确的。由于您只给出了两个方向(NE、SW)并且没有关于电路板方向的文档,我无法判断这些标志是否与电路板布局一致,甚至与它们本身一致。

_search* 方法也太长,应该分成多个函数,但这是次要问题。

关于Python:列表不对角搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30488611/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com