小明ヾ(。`Д´。)ノ彡 2023-03-09 07:40 采纳率: 50%
浏览 16
已结题

有没有更短的判断井字棋胜利的代码(Python,如何解决?

如标题所示,我使用的是把所有可能性都列出来判断,但希望可以得到简化!非常感谢,下面是代码:

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)



**




```**


```

  • 写回答

3条回答 默认 最新

  • 文盲老顾 WEB应用领新星创作者 2023-03-09 08:51
    关注
    
    board = [[' ',' ',' '],[' ',' ',' '],[' ',' ',' ']]
    s = 'XO' # 棋子符号,因为只有两个,所以我们用步数取2的余来确定落子方
    step = 0 # 落子步数
    while True:
        for row in range(3):
            if row > 0:
                print('-+-+-')
            print('|'.join(board[2 - row])) # 为了符合数字小键盘样子,我们反着输出行
        c = s[step % 2] # 获得当前落子方的棋子符号
        print('当前落子为{}'.format(c))
        n = input('输入落子位置(1-9),0退出本局:')
        if n.isnumeric() and n in '0123456789':
            n = int(n)
            if n == 0:
                print('你中断了本局游戏')
                break
            n -= 1
            board[n // 3][n % 3] = s[step % 2]
            # 行或列判断胜利
            if ''.join(board[n // 3]) == c * 3 or ''.join([row[n % 3] for row in board]) == c * 3:
                print('游戏结束,{}胜利'.format(c))
                break
            # 斜线判断胜利
            if board[0][0] + board[1][1] + board[2][2] == c * 3 or board[0][2] + board[1][1] + board[2][0] == c * 3:
                print('游戏结束,{}胜利'.format(c))
                break
            step += 1
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 3月17日
  • 已采纳回答 3月9日
  • 创建了问题 3月9日

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程