# coding: utf-8
# -----------------棋盘的定义------------------------------------------
theBoard = {'top_L': ' ', 'top_M': ' ', 'top_R': ' ',
'mid_L': ' ', 'mid_M': ' ', 'mid_R': ' ',
'low_L': ' ', 'low_M': ' ', 'low_R': ' '}
# ----------------棋盘刻画函数----------------------------------------
def printBoard(board):
"""将棋盘刻画出来"""
print(board['top_L'] + '|' + board['top_M'] + '|' + board['top_R'])
print('-+-+-')
print(board['mid_L'] + '|' + board['mid_M'] + '|' + board['mid_R'])
print('-+-+-')
print(board['low_L'] + '|' + board['low_M'] + '|' + board['low_R'])
return board
# -------------------玩家输入下棋-----------------------------------------
player_1 = 'O'
player_2 = 'X'
player = player_1
for time in range(9):
board = printBoard(theBoard)
print(board)
# ---------------------判断胜利--------------------------------------------------------
if theBoard['top_L'] == theBoard['top_M'] == theBoard['top_R'] == player_1:
print('play_1 wins')
break
elif theBoard['top_L'] == theBoard['top_M'] == theBoard['top_R'] == player_1:
print('play_2 wins')
break
elif theBoard['mid_L'] == theBoard['mid_M'] == theBoard['mid_R'] == player_1:
print('play_2 wins')
break
elif theBoard['low_L'] == theBoard['low_M'] == theBoard['low_R'] == player_1:
print('play_2 wins')
break
elif theBoard['top_L'] == theBoard['mid_M'] == theBoard['low_R'] == player_1:
print('play_2 wins')
break
elif theBoard['top_R'] == theBoard['mid_M'] == theBoard['low_L'] == player_1:
print('play_2 wins')
break
elif theBoard['top_L'] == theBoard['top_M'] == theBoard['top_R'] == player_2:
print('play_2 wins')
break
elif theBoard['mid_L'] == theBoard['mid_M'] == theBoard['mid_R'] == player_2:
print('play_2 wins')
break
elif theBoard['low_L'] == theBoard['low_M'] == theBoard['low_R'] == player_2:
print('play_2 wins')
break
elif theBoard['top_L'] == theBoard['mid_M'] == theBoard['low_R'] == player_2:
print('play_2 wins')
break
elif theBoard['top_R'] == theBoard['mid_M'] == theBoard['low_L'] == player_2:
print('play_2 wins')
break
# -------------------------------------------------------------------------------------
print("Choose which position do you want to set? (top_L, top_M, top_R; mid_L, mid_M, mid_R; low_L, low_M, low_R)")
move = input()
theBoard[move] = player
if player == player_1:
player = player_2
elif player == player_2:
player = player_1
井字棋盘
最新推荐文章于 2021-12-10 11:08:17 发布