【发布时间】:2021-12-27 13:59:42
【问题描述】:
我目前正在从事井字游戏项目。我希望电路板具有自定义尺寸。但我不知道如何检查所有固定长度为 k 的行、列和对角线(k 是可定制的)。这里有一个例子来说明我的意思。
"""
For example if the board is a 4x4 matrix and I want to check the rows, cols, and diagonals of consecutive cells with fixed length of 3
"""
board = [[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10,11,12]
[13,14,15,16]]
# Print all rows:
# Output: [1,2,3], [2,3,4], [5,6,7], [6,7,8], [9,10,11], [10,11,12], [13,14,15], [14,15,16]
# Print all cols:
# Output: [1,5,9], [5,9,13], [2,6,10], [6,10,14], [3,7,11], [7,11,15], [4,8,12], [8,12,16]
# Print all diagonals (beside 2 main diagonals there're more smaller diagonals):
# Output: [1,6,11], [6,11,16], [4,7,10], [7,10,13], [3,6,9], [2,7,12], [5,10,15], [8,11,14]
【问题讨论】:
标签: python-3.x matrix tic-tac-toe