Python 如何创建一个公式来检查谁在没有大量if语句的情况下赢得了一场井字游戏?
我有下面的代码,觉得它可以更有效。这意味着这是3x3板,可以手动完成,但如果是30x30板或更大的板呢Python 如何创建一个公式来检查谁在没有大量if语句的情况下赢得了一场井字游戏?,python,Python,我有下面的代码,觉得它可以更有效。这意味着这是3x3板,可以手动完成,但如果是30x30板或更大的板呢 x = [[1, 2, 0],[2, 1, 0],[2, 1, 0]] for y in range (3): if ((x[0][0] == x[1][0] == x[2][0] == y) or (x[0][1] == x[1][1] == x[2][1] == y) or (x[0][2] == x[1][2] == x[2][2] == y) or
x = [[1, 2, 0],[2, 1, 0],[2, 1, 0]]
for y in range (3):
if ((x[0][0] == x[1][0] == x[2][0] == y) or
(x[0][1] == x[1][1] == x[2][1] == y) or
(x[0][2] == x[1][2] == x[2][2] == y) or
(x[0][0] == x[0][1] == x[0][2] == y) or
(x[1][0] == x[1][1] == x[1][2] == y) or
(x[2][0] == x[2][1] == x[2][2] == y) or
(x[0][0] == x[1][1] == x[2][2] == y) or
(x[0][2] == x[1][1] == x[2][0] == y)):
if y==1:
print('Player 1 won!!!')
if y==2:
print('Player 2 won!!!')
if y==0:
print('Nobody won')
有没有办法使条件部分更好?如果您不介意导入numpy,这将适用于任何正方形网格和任意数量的玩家:
import numpy as np
def check(arr):
def check_one_dim(arr):
for player in np.arange(Nplayer)+1:
# instead of Nplayer you could write arr.max()
arrtest = arr == player
s = arrtest.astype(int).prod(axis=0)
t = arrtest.diagonal().astype(int).prod()
if (s == 1).any() or t == 1:
return player
else:
return 0
return max(check_one_dim(arr), check_one_dim(arr[::-1].T))
Nplayer = 2
x = np.array([[1, 2, 0],[2, 1, 0],[2, 1, 0]])
winner = check(x)
if winner > 0:
print("Player {}").format(winner)
else:
print("No winner")
我不确定它是否更快,但“如果”更少:)您可以使用这样的函数生成器:
def cell_owner(player):
"""returns a function which can check if a cell belongs to provided player"""
def wrapped(cell):
return player == cell
return wrapped
因此,您可以调用cell\u owner(1)
来获取一个函数,该函数接受一个值并检查它是否为1。
这似乎没有用,但使用此功能,您可以使用并检查一行中的整个单元格组:
# will return True if each cell in <cells_group> belong to <player>
all(map(cell_owner(<player>), <cells_group>)
注意:此代码适用于任何大小的正方形网格。我很难理解您的代码。我还没碰发电机呢。但它似乎奏效了。非常感谢
np.array([[0,2,1],[0,2,1],[0,2,0]])
,当玩家2赢得游戏时,此代码会产生无赢家
。这是因为在“s=…”和“t=…”行中有一个输入错误(“arr”而不是“arrtest”)。现在它工作得很好。
import random
def gen_random_grid(size):
"""just for test purpose: generate a randomly filled grid"""
return [[random.randint(0, 2) for col in range(size)] for row in range(size)]
def print_grid(grid):
"""just for test purpose: prints the grid"""
size = len(grid)
row_sep = "+{}+".format("+".join(["---"] * size))
row_fmt = "|{}|".format("|".join([" {} "] * size))
print(row_sep)
for row in grid:
print(row_fmt.format(*row))
print(row_sep)
def cell_owner(player):
"""returns a function which can check is a cell belongs to provided player"""
def wrapped(cell):
return player == cell
return wrapped
def get_winner(grid):
"""determines if there is a winner"""
size = len(grid)
# prepare list of potentially winning cells groups
cells_groups_to_check = grid[:] # rows
cells_groups_to_check += [[grid[row][col] for row in range(size)]
for col in range(size)] #cols
cells_groups_to_check.append(
[grid[index][index] for index in range(size)]) # diag 1
cells_groups_to_check.append(
[grid[index][size - 1 - index] for index in range(size)]) # diag 2
winner = 0
for y in range(1, 3): # 0 is not a player, no need to test it
# for each player...
for group in cells_groups_to_check:
# ... check if a cells group is complete
if (all(map(cell_owner(y), group))):
winner = y
break
if winner: break
return winner
# generate some random grids of different sizes
TEST_GRIDS = [gen_random_grid(3) for _ in range(3)]
TEST_GRIDS += [gen_random_grid(2) for _ in range(3)]
TEST_GRIDS += [gen_random_grid(4) for _ in range(3)]
# demonstration
for grid in TEST_GRIDS:
print_grid(grid)
print("Winner is {}".format(get_winner(grid)))