2 回答
TA贡献1712条经验 获得超3个赞
您只需要在更新之前检查您正在更新的位置是否为 0
def player1_turn():
player1_option = int(input("Player 1 | Please enter a number between 1-25: "))
if player1_option <= 0:
print("You can only enter a number between 1 and 25")
player1_turn()
elif player1_option > 25:
print("You can only enter a number between 1 and 25")
player1_turn()
player1 = (player1_option - 1) #Counter-acts the elements from starting at 0
#You must check if the position is a 0
if (grid[int(player1) // 5][int(player1) % 5] == 0):
grid[int(player1) // 5][int(player1) % 5] = 1 #places a 1 in inputted position
else:
#Do something else here instead of updating it
for row in grid: #for each row in the grid
print(*row, sep=" ")
print()
至于填满的板子,你可以使用常用的方式(双循环)或更直观的方式使用列表理解来检查它
def board_filled():
for i in grid:
for j in i:
if j == 0:
return False
return True
def board_filled():
return (sum([0 in i for i in grid]) == 0)
TA贡献1891条经验 获得超3个赞
如果您为此使用二维数组,您可以检查一个地方是否充满,
filled_array = np.array([[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]])
selected_pos = 23
# Get row
row = np.floor(selected_pos)
# Get pos in row
pos = selected_pos % 5 - 1
# Check if pos is not 0
if(filled_array[row][pos] != 0):
raise ["Place is filled"]
编辑:使用它来检查是否没有剩余的 0
if(np.count_nonzero(filled_array) == 25):
raise ["No 0's left"]
忘了说:
import numpy as np
让它工作
添加回答
举报