本文整理汇总了Python中matrix.Matrix.diagonal方法的典型用法代码示例。如果您正苦于以下问题:Python Matrix.diagonal方法的具体用法?Python Matrix.diagonal怎么用?Python Matrix.diagonal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类matrix.Matrix
的用法示例。
在下文中一共展示了Matrix.diagonal方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: input
# 需要导入模块: from matrix import Matrix [as 别名]
# 或者: from matrix.Matrix import diagonal [as 别名]
while True:
try:
play = input(turn + ': ')
# Player entered a number, now search for this number in our board.
row, col = board.index(play)
assert board[row, col] not in ('x', 'o')
board[row, col] = turn
break
except Exception as e:
print(e)
# Make a list of all lines that pass through the cell played.
directions = [board.row(row),
board.col(col),
board.diagonal(row, col, +1),
board.diagonal(row, col, -1)]
# Convert lines to a single space separated string.
str_directions = ' '.join(''.join(line) for line in directions)
# Verify if there are three of the player symbols in a row.
if turn * 3 in str_directions:
# Maps every cell removing the ones that are still numbers.
clean_board = board.map(lambda i: i if i in 'xo' else '')
print('\n{} wins!\n{}'.format(turn, clean_board))
break()
if all(i in ('x', 'o') for i in board):
print('\nDraw!\n{}'.format(board))
break
示例2: Gomoku
# 需要导入模块: from matrix import Matrix [as 别名]
# 或者: from matrix.Matrix import diagonal [as 别名]
class Gomoku(object):
COMPUTER = 'C'
def __init__(self, size, empty_spot):
self.size = size
self.empty_spot = empty_spot
self.m = Matrix(self.size, self.size, self.empty_spot)
self.musashi = Musashi(self.m, self.COMPUTER)
# Check for a spot to be placed a move
def check_spot(self, row, col):
return self.m[row,col] == self.empty_spot
# Do the move
def do_move(self, row, col, player):
self.m[row,col] = player
return self.copy_matrix()
# Copy the matrix
def copy_matrix(self):
return self.m[(0,0):]
# Show the matrix
def show_matrix(self):
return str(self.m)
# TODO: Computer turn
def computer_turn(self):
comp_move = self.musashi.domove(self.m)
return self.do_move(comp_move[0], comp_move[1], self.COMPUTER)
# Check for a final state of the game
def is_over(self):
r = False
for direction in directions:
r = (r or self.check_over(direction))
return r
# Check if its over on one direction
def check_over(self, direction):
# mes(direction)
if direction == 'H':
return self.iterate_over('row')
elif direction == 'V':
return self.iterate_over('col')
elif direction == 'D':
return self.iterate_diagonal()
else:
return False
# Iterate over a line to search a sequence of 5
def iterate_over(self, where):
sequence = 0
sym_list = []
for line in range(0,self.size):
if where == 'row':
sym_list = self.m.row(line)
elif where == 'col':
sym_list = self.m.col(line)
else:
return False
back_pos = 0
for symbol in sym_list:
if symbol != self.empty_spot:
if symbol == sym_list[back_pos]:
sequence += 1
else:
sequence = 0
else:
sequence = 0
if sequence == SEQUENCE:
return True
back_pos += 1
sequence = 0
return False
# Iterate over the diagonals to search a sequence of 5
def iterate_diagonal(self):
sequence = 0
sym_list = []
for diagonal in range(-((self.size*2)-1),self.size*2):
back_pos = 0
sym_list = self.m.diagonal(diagonal)
for symbol in sym_list:
if symbol != self.empty_spot:
if symbol == sym_list[back_pos]:
sequence += 1
else:
sequence = 0
else:
sequence = 0
if sequence == SEQUENCE:
return True
back_pos += 1
sequence = 0
return False
示例3: Matrix
# 需要导入模块: from matrix import Matrix [as 别名]
# 或者: from matrix.Matrix import diagonal [as 别名]
# caian 25/08/2015
# This is simple test file to diagonal function from matrix
from matrix import Matrix
m = Matrix(15,15)
for i in range(len(m)):
m[i] = i
print m
for d in range(-30, 30):
print m.diagonal(d)