题目描述
设计一个在n×n网格上两个玩家之间玩的Tic-tac-toe游戏。 您可以假定以下规则: 1.一个移动被保证是有效的,并且被放置在一个空块上。 2.一旦达到获胜条件,就不再允许移动。 3.成功地在水平、垂直或对角线行中放置n个分数的玩家赢得比赛。 `
class TicTacToe:
def __init__(self, n):
self.n = n
self.player_map = collections.defaultdict(int)
def move(self, col, row, player):
self.player_map[(player, row, 'row')] += 1
self.player_map[(player, col, 'col')] += 1
if col == row:
self.player_map[(player, 'dia')] += 1
if col + row == self.n - 1:
self.player_map[(player, 'adia')] += 1
if self.n in self.player_map.values():
return player
`