如标题所示,我使用的是把所有可能性都列出来判断,但希望可以得到简化!非常感谢,下面是代码:
the_board = {'top-L': '', 'top-M': '', 'top-R': '',
'mid-L': '', 'mid-M': '', 'mid-R': '',
'low-L': '', 'low-M': '', 'low-R': ''}
# print(the_board)
def print_board(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'])
# print_board(the_board)
turn = 'X'
for i in range(9):
print_board(the_board)
move = input('把' + turn + '画在哪:')
if move not in the_board.keys():
print('请输入正确的位置')
continue
the_board[move] = turn
if turn == 'X':
turn = 'O'
else:
turn = 'X'
if the_board['top-L'] == the_board['top-M'] == the_board['top-R'] == 'X' or the_board['mid-L'] == the_board['mid-M'] == the_board['mid-R'] == 'X' or the_board['low-L'] == the_board['low-M'] == the_board['low-R'] == 'X' or the_board['top-L'] == the_board['mid-L'] == the_board['low-L'] == 'X' or the_board['top-M'] == the_board['mid-M'] == the_board['low-M'] == 'X' or the_board['top-R'] == the_board['mid-R'] == the_board['low-R'] == 'X' or the_board['top-L'] == the_board['mid-M'] == the_board['low-R'] == 'X' or the_board['top-R'] == the_board['mid-M'] == the_board['low-L'] == 'X' :
print('X,获胜')
break
elif the_board['top-L'] == the_board['top-M'] == the_board['top-R'] == 'O' or the_board['mid-L'] == the_board['mid-M'] == the_board['mid-R'] == 'O' or the_board['low-L'] == the_board['low-M'] == the_board['low-R'] == 'O' or the_board['top-L'] == the_board['mid-L'] == the_board['low-L'] == 'O' or the_board['top-M'] == the_board['mid-M'] == the_board['low-M'] == 'O' or the_board['top-R'] == the_board['mid-R'] == the_board['low-R'] == 'O' or the_board['top-L'] == the_board['mid-M'] == the_board['low-R'] == 'O' or the_board['top-R'] == the_board['mid-M'] == the_board['low-L'] == 'O':
print('O,获胜')
break
print_board(the_board)
**
```**
```