检查矩阵中的列或对角线是否 = x(没有 Numpy)
Check if column or diagonal in matrix = x (Without Numpy)
我可以使用这段代码来检查矩阵中的一行是否 = x:
q = [[1,2,1],[1,2,1],[2,1,2]]
answer = [sum(row) for row in q]
for i in range(0, len(q)):
if answer[i] == 6:
print "Player 2 won!"
if answer[i] == 3:
print "Player 1 won!"
if answer[i] != 6 and 3:
print "It's a tie!"
如何在不使用 Numpy 的情况下检查我的矩阵是否具有 = x 的对角线或列(是否有如上所示的数学方法?)
示例:(X = 无关紧要的东西)
q = [[1,X,X],[1,X,X],[1,X,X]]
应该打印 True
q = [[1,X,X],[X,1,X],[X,X,1]]
应该打印 True
(对角线)
q = [[X,X,1],[X,1,X],[1,X,X]]
应该打印 True
(Diagonal{Other One})
q = [[1,X,X],[X,1,X],[X,1,X]]
应该打印 False
q = [[X,1,X],[X,1,X],[X,1,X]]
应打印 True
(水平)
How the matrix should have its "winning conditions"
好吧,您可以将获胜条件的枚举转换为元组,成对的元组……在 3x3 棋盘世界中没有太多工作。
像下面这样的事情(拿你的样品板)并导致平局应该让你开始进一步学习 Python:
#! /usr/bin/env python
"""Check in snaive 3x3 game board world for diagonal,
column, or row all ocupied by one player."""
from __future__ import print_function
players = (1, 2) # Code for the players
board = [[1, 2, 1], # Board interpreted as 3 lists rows
[1, 2, 1],
[2, 1, 2]]
winning_configs = ( # outer-inner-index pairs that win:
((0, 0), (1, 1), (2, 2)), # TL to BR diagonal
((0, 2), (1, 1), (2, 0)), # TR to BL diagonal
((0, 0), (1, 0), (2, 0)), # L column
((0, 1), (1, 1), (2, 1)), # M column
((0, 2), (1, 2), (2, 2)), # R column
((0, 0), (0, 1), (0, 2)), # L row
((1, 0), (1, 1), (1, 2)), # M row
((2, 0), (2, 1), (2, 2)), # R row
)
def and_the_winner_is(players, board, winning_configs):
"""First one matching rules is returned as winner,
otherwise None to indicate a tie."""
for player in players:
for cfg in winning_configs:
if all([board[i][j] == player for i, j in cfg]):
return player
else:
return None
def main():
"""Determine the result from board."""
winner = and_the_winner_is(players, board, winning_configs)
if winner in players:
print('Winner is Player({})'.format(winner))
else:
print('A tie')
if __name__ == '__main__':
main()
这个怎么样?适用于 q
的任意形状
def check_col_diag (q, x):
"""
Returns:
0 if there was a column,
1 if there was a row,
2 if there was a diagonal on 1st direction
3 if there was a diagonal on 2nd direction
"""
# Get a mask to store the positions
# on each row of q where q == x
mask = q
for row_ix in range(len(q[0])):
for elem_ix in range(len(q[1])):
if q[row_ix][elem_ix] == x:
mask[row_ix][elem_ix] = 1
else:
mask[row_ix][elem_ix] = 0
# Check rows
c = [1]*len(q[0])
for row in mask:
# element-wise list multiplication
c = [a*b for a,b in zip(c,row)]
# Return 0 if there was a column
if any(c):
return 0
# Check columns
c = [1]*len(q[1])
# Iterate through rows of transposed list
_q = list(map(list, zip(*mask)))
for row in _q:
c = [a*b for a,b in zip(c,row)]
# Return 1 if there was a row
if any(c):
return 1
# Check diagonal 1
c = 1
for row_ix in range(len(q[0])):
c *= mask[row_ix][row_ix]
# Return 2 if there was a 1st diagonal
if c == 1:
return 2
# Check diagonal 2
c = 1
for row_ix in range(len(_q[0])):
c *= mask[row_ix][row_ix]
# Return 3 if there was a 2nd diagonal
if c == 1:
return 3
q = [[1,2,1],[1,2,1],[2,1,2]]
v = check_col_diag (q, 1)
我可以使用这段代码来检查矩阵中的一行是否 = x:
q = [[1,2,1],[1,2,1],[2,1,2]]
answer = [sum(row) for row in q]
for i in range(0, len(q)):
if answer[i] == 6:
print "Player 2 won!"
if answer[i] == 3:
print "Player 1 won!"
if answer[i] != 6 and 3:
print "It's a tie!"
如何在不使用 Numpy 的情况下检查我的矩阵是否具有 = x 的对角线或列(是否有如上所示的数学方法?)
示例:(X = 无关紧要的东西)
q = [[1,X,X],[1,X,X],[1,X,X]]
应该打印 True
q = [[1,X,X],[X,1,X],[X,X,1]]
应该打印 True
(对角线)
q = [[X,X,1],[X,1,X],[1,X,X]]
应该打印 True
(Diagonal{Other One})
q = [[1,X,X],[X,1,X],[X,1,X]]
应该打印 False
q = [[X,1,X],[X,1,X],[X,1,X]]
应打印 True
(水平)
How the matrix should have its "winning conditions"
好吧,您可以将获胜条件的枚举转换为元组,成对的元组……在 3x3 棋盘世界中没有太多工作。
像下面这样的事情(拿你的样品板)并导致平局应该让你开始进一步学习 Python:
#! /usr/bin/env python
"""Check in snaive 3x3 game board world for diagonal,
column, or row all ocupied by one player."""
from __future__ import print_function
players = (1, 2) # Code for the players
board = [[1, 2, 1], # Board interpreted as 3 lists rows
[1, 2, 1],
[2, 1, 2]]
winning_configs = ( # outer-inner-index pairs that win:
((0, 0), (1, 1), (2, 2)), # TL to BR diagonal
((0, 2), (1, 1), (2, 0)), # TR to BL diagonal
((0, 0), (1, 0), (2, 0)), # L column
((0, 1), (1, 1), (2, 1)), # M column
((0, 2), (1, 2), (2, 2)), # R column
((0, 0), (0, 1), (0, 2)), # L row
((1, 0), (1, 1), (1, 2)), # M row
((2, 0), (2, 1), (2, 2)), # R row
)
def and_the_winner_is(players, board, winning_configs):
"""First one matching rules is returned as winner,
otherwise None to indicate a tie."""
for player in players:
for cfg in winning_configs:
if all([board[i][j] == player for i, j in cfg]):
return player
else:
return None
def main():
"""Determine the result from board."""
winner = and_the_winner_is(players, board, winning_configs)
if winner in players:
print('Winner is Player({})'.format(winner))
else:
print('A tie')
if __name__ == '__main__':
main()
这个怎么样?适用于 q
def check_col_diag (q, x):
"""
Returns:
0 if there was a column,
1 if there was a row,
2 if there was a diagonal on 1st direction
3 if there was a diagonal on 2nd direction
"""
# Get a mask to store the positions
# on each row of q where q == x
mask = q
for row_ix in range(len(q[0])):
for elem_ix in range(len(q[1])):
if q[row_ix][elem_ix] == x:
mask[row_ix][elem_ix] = 1
else:
mask[row_ix][elem_ix] = 0
# Check rows
c = [1]*len(q[0])
for row in mask:
# element-wise list multiplication
c = [a*b for a,b in zip(c,row)]
# Return 0 if there was a column
if any(c):
return 0
# Check columns
c = [1]*len(q[1])
# Iterate through rows of transposed list
_q = list(map(list, zip(*mask)))
for row in _q:
c = [a*b for a,b in zip(c,row)]
# Return 1 if there was a row
if any(c):
return 1
# Check diagonal 1
c = 1
for row_ix in range(len(q[0])):
c *= mask[row_ix][row_ix]
# Return 2 if there was a 1st diagonal
if c == 1:
return 2
# Check diagonal 2
c = 1
for row_ix in range(len(_q[0])):
c *= mask[row_ix][row_ix]
# Return 3 if there was a 2nd diagonal
if c == 1:
return 3
q = [[1,2,1],[1,2,1],[2,1,2]]
v = check_col_diag (q, 1)