热线电话:13121318867

登录
2019-04-09 阅读量: 505
Python使用随机数实现自动TicTacToe游戏(2)

numpyrandomPython库用于构建这个游戏。代码不是要求用户在电路板上放置标记,而是随机选择电路板上的一个位置并放置标记。除非玩家获胜,否则它将在每次转弯后显示棋盘。如果游戏得到平局,则返回-1。

# Checks whether the player has three

# of their marks in a vertical row

def col_win(board, player):

for x in range(len(board)):

win = True

for y in range(len(board)):

if board[y][x] != player:

win = False

continue

if win == True:

return(win)

return(win)

# Checks whether the player has three

# of their marks in a diagonal row

def diag_win(board, player):

win = True

for x in range(len(board)):

if board[x, x] != player:

win = False

return(win)

# Evaluates whether there is

# a winner or a tie 

def evaluate(board):

winner = 0

for player in [1, 2]:

if (row_win(board, player) or

col_win(board,player) or

diag_win(board,player)):

winner = player

0.0000
2
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子