Python: 函数return 布尔和整数元组
Python: Function return bool and integer tuple
在 Python (V2.7) 中,我正在尝试针对计算机玩家制作 ConnectFour 游戏。我已经制定了一个简单的函数来连续找到 4 个(以确定游戏的结束)并且 return TRUE 如果是这样,现在我正在尝试使用相同的函数来找到连续的 3 个和 return 位置。
def finder(matrix, player,number):
for row in matrix:
count = 0
for item in row:
if item == player:
count +=1
if count == number:
return True
else:
count = 0
我可以在其中输入:finder(board, "X", 4) 以了解连续四个是 TRUE 还是仍默认为 FALSE(这确实有效)。现在我想尝试这样的事情:
def finder(matrix, player,number):
for row in matrix:
count = 0
for item in row:
if item == player:
count +=1
if count == number:
return True
location = row, item
return location
else:
count = 0
然而,这调用了一个错误,我没有初始化位置,所以我然后设置位置 = 0、0。现在它只是 returns TRUE 和元组 (0,0)。我怎样才能得到它给出连续 3 个元素的最后一个元素的位置?
编辑:我试过 return 三元组:TRUE、行、项目。但是,问题的第二部分是当函数为 TRUE 时如何获取行号和列号?下面的代码可以实现威胁的存在,但我无法找到一种方法来获取威胁的位置,因为存在威胁。
if finder(board, "X", 3) is True:
horizontal_threat = True
print row, item
你可以通过
for (i, row) in enumerate(matrix):
...
for (j, item) in row:
...
if (count==number):
return (i, j)
但是,当前代码中存在一些错误。
- 您有两个 return 语句。我不认为这是可能的 return (0, 0)
- 您计算矩阵中 X 的总数。你不检查他们是否在一条线上
您的第二个 return 语句未执行,原因由@Kevin 在上面的评论中指定。
你需要像
这样写你的声明
return True, location
无论你在哪里打电话 finder
你都必须像
found, values = finder(board, "X", 3)
# Complete, working, Python 3 solution
def finder(matrix, player, number):
location = None
for row in matrix:
count = 0
for item in row:
if item == player:
count +=1
if count == number:
location = row, item
break
else:
count = 0
return location
matrixMain = [ [1, 0, 0, 0], [2, 2, 2, 2], [1, 0, 0, 0], [1, 0, 0, 0] ]
for itemToFind in range( 1, 3 ):
locationFound = finder( matrixMain, itemToFind, 3 )
if locationFound is None:
print( "item {} not found".format( itemToFind ) )
else:
rowFound = locationFound[0]
itemFound = locationFound[1]
print( "found item {}".format( itemFound ) )
# Is forcing users to indent code by four spaces really the best Whosebug can do ?
在 Python (V2.7) 中,我正在尝试针对计算机玩家制作 ConnectFour 游戏。我已经制定了一个简单的函数来连续找到 4 个(以确定游戏的结束)并且 return TRUE 如果是这样,现在我正在尝试使用相同的函数来找到连续的 3 个和 return 位置。
def finder(matrix, player,number):
for row in matrix:
count = 0
for item in row:
if item == player:
count +=1
if count == number:
return True
else:
count = 0
我可以在其中输入:finder(board, "X", 4) 以了解连续四个是 TRUE 还是仍默认为 FALSE(这确实有效)。现在我想尝试这样的事情:
def finder(matrix, player,number):
for row in matrix:
count = 0
for item in row:
if item == player:
count +=1
if count == number:
return True
location = row, item
return location
else:
count = 0
然而,这调用了一个错误,我没有初始化位置,所以我然后设置位置 = 0、0。现在它只是 returns TRUE 和元组 (0,0)。我怎样才能得到它给出连续 3 个元素的最后一个元素的位置?
编辑:我试过 return 三元组:TRUE、行、项目。但是,问题的第二部分是当函数为 TRUE 时如何获取行号和列号?下面的代码可以实现威胁的存在,但我无法找到一种方法来获取威胁的位置,因为存在威胁。
if finder(board, "X", 3) is True:
horizontal_threat = True
print row, item
你可以通过
for (i, row) in enumerate(matrix):
...
for (j, item) in row:
...
if (count==number):
return (i, j)
但是,当前代码中存在一些错误。
- 您有两个 return 语句。我不认为这是可能的 return (0, 0)
- 您计算矩阵中 X 的总数。你不检查他们是否在一条线上
您的第二个 return 语句未执行,原因由@Kevin 在上面的评论中指定。
你需要像
这样写你的声明return True, location
无论你在哪里打电话 finder
你都必须像
found, values = finder(board, "X", 3)
# Complete, working, Python 3 solution
def finder(matrix, player, number):
location = None
for row in matrix:
count = 0
for item in row:
if item == player:
count +=1
if count == number:
location = row, item
break
else:
count = 0
return location
matrixMain = [ [1, 0, 0, 0], [2, 2, 2, 2], [1, 0, 0, 0], [1, 0, 0, 0] ]
for itemToFind in range( 1, 3 ):
locationFound = finder( matrixMain, itemToFind, 3 )
if locationFound is None:
print( "item {} not found".format( itemToFind ) )
else:
rowFound = locationFound[0]
itemFound = locationFound[1]
print( "found item {}".format( itemFound ) )
# Is forcing users to indent code by four spaces really the best Whosebug can do ?