吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1357|回复: 30
收起左侧

[讨论] 对Python编程快速上手里面的程序进行改写

[复制链接]
paypojie 发表于 2022-8-4 22:24
本帖最后由 paypojie 于 2022-8-4 22:43 编辑

                                                                                                  井字棋功能扩展
书中的源码
[Python] 纯文本查看 复制代码
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'])
turn = 'X'
for i in range(9):
    printBoard(theBoard)
    print('Turn for ' + turn + '. Move on which space?')
    move = input()
    theBoard[move] = turn
    if turn == 'X':
        turn = 'O'
    else:
        turn = 'X'
printBoard(theBoard)

本人改写后的代码
[Python] 纯文本查看 复制代码
theBoard = {'top-L':' ','top-M':' ','top-R':' ','mid-L':' ','mid-M':' ','mid-R':' ','low-L':' ','low-M':' ','low-R':' '}
def printBoard(Board):
    s = '-----'
    print(Board['top-L'] + '|' + Board['top-M'] + '|' + Board['top-R'])
    print(s)
    print(Board['mid-L'] + '|' + Board['mid-M'] + '|' + Board['mid-R'])
    print(s)
    print(Board['low-L'] + '|' + Board['low-M'] + '|' + Board['low-R'])
turn = 'X'
for i in range(9):
    if i == 0:
        print('欢迎进入井字棋游戏')
        d = '''top-L  top-M  top-R
mid-L  mid-M  mid-R
low-L  low-M  low-R'''
        print()
        print(d)
        print()
    printBoard(theBoard)
    print('Turn for ' + turn + ' 你想要在井字棋的哪个位置着手?')
    move = input()
    theBoard[move] = turn
    if turn == 'X':
        turn = 'O'
    else:
        turn = 'X'

    if theBoard['top-L'] == 'X' and theBoard['top-M'] == 'X' and theBoard['top-R'] == 'X':
        print()
        print('X赢')
        print()
    elif theBoard['mid-L'] == 'X' and theBoard['mid-M'] == 'X' and theBoard['mid-R'] == 'X':
        print()
        print('X赢')
        print()
    elif theBoard['low-L'] == 'X' and theBoard['low-M'] == 'X' and theBoard['low-R'] == 'X':
        print()
        print('X赢')
        print()
    elif theBoard['top-L'] == 'X' and theBoard['mid-L'] == 'X' and theBoard['low-L'] == 'X':
        print()
        print('X赢')
        print()
    elif theBoard['top-M'] == 'X' and theBoard['mid-M'] == 'X' and theBoard['low-M'] == 'X':
        print()
        print('X赢')
        print()
    elif theBoard['top-R'] == 'X' and theBoard['mid-R'] == 'X' and theBoard['low-R'] == 'X':
        print()
        print('X赢')
        print()
    elif theBoard['top-L'] == 'X' and theBoard['mid-M'] == 'X' and theBoard['low-R'] == 'X':
        print()
        print('X赢')
        print()
    elif theBoard['top-R'] == 'X' and theBoard['mid-M'] == 'X' and theBoard['low-L'] == 'X':
        print()
        print('X赢')
        print()
    elif theBoard['top-L'] == 'O' and theBoard['top-M'] == 'O' and theBoard['top-R'] == 'O':
        print()
        print('O赢')
        print()
    elif theBoard['mid-L'] == 'O' and theBoard['mid-M'] == 'O' and theBoard['mid-R'] == 'O':
        print()
        print('O赢')
        print()
    elif theBoard['low-L'] == 'O' and theBoard['low-M'] == 'O' and theBoard['low-R'] == 'O':
        print()
        print('O赢')
        print()
    elif theBoard['top-L'] == 'O' and theBoard['mid-L'] == 'O' and theBoard['low-L'] == 'O':
        print()
        print('O赢')
        print()
    elif theBoard['top-M'] == 'O' and theBoard['mid-M'] == 'O' and theBoard['low-M'] == 'O':
        print()
        print('O赢')
        print()
    elif theBoard['top-R'] == 'O' and theBoard['mid-R'] == 'O' and theBoard['low-R'] == 'O':
        print()
        print('O赢')
        print()
    elif theBoard['top-L'] == 'O' and theBoard['mid-M'] == 'O' and theBoard['low-R'] == 'O':
        print()
        print('O赢')
        print()
    elif theBoard['top-R'] == 'O' and theBoard['mid-M'] == 'X' and theBoard['low-L'] == 'X':
        print()
        print('O赢')
        print()

增加的功能 判断X赢还是O赢


源码分析
创建了一个字典 这个字典可以看成抽象的井字棋 字典里面包含了九个键值对 每个键值对表示井字棋的位置与在位置上的标志 所以第一步就完成了 引用书中的一句话
使用数据结构对真实世界建模

随后创建了一个printBoard函数 调用函数 函数里面会在屏幕打印一个看起来像真实世界的井字棋的字符画
定义了一个变量turn 这个变量表示要添加到井字棋的占位符(我是这么理解的)

随后 进入for循环 循环九次
进行条件判断 如果是第一次进入 就输出提示 然后调用函数 输出一段文本
turn是会发生变化的 第一次为X 第二次就为O 第三次又变化为X 后面的if分支就足以看的出来
[Python] 纯文本查看 复制代码
if turn == 'X':
        turn = 'O'
    else:
        turn = 'X'

要求输入 并将输入值保存到变量move 然后 字典对应的键保存turn的值  
[Python] 纯文本查看 复制代码
move = input()
theBoard[move] = turn

这两行代码也可以这么理解 如下
move是对应的是要进行选择的真实世界井字棋的位置 theBoard[move]对应井字棋九个填充位置的标志 你选择哪一个move 那么 你选择的那一个位置就填充一个X或者O这样的标志
第一个 if else 的作用上面讲过了 第二个if elif 的作用简单来说就是判断哪一方赢 X赢还是O赢 如何判断的呢 在真实世界中 把三个相同的标志看成点 如果三个点能连成一条直线 那么就赢了
如何用程序判断 在这里 我是这样想的 把所有预估能赢的路线给写出来 一共八种路线 要么先手赢 要么后手赢 先手用X表示 后手则是O
这段代码可以缩短进行优化 但是 本人有点懒 哈哈

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
plutoking + 1 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

female20 发表于 2022-8-4 22:37
最近学python,学习学习
 楼主| paypojie 发表于 2022-8-4 22:42
female20 发表于 2022-8-4 22:37
最近学python,学习学习

加油 推荐文档加视频结合在一起学习  
外酥内嫩 发表于 2022-8-4 23:26
Sameal 发表于 2022-8-5 00:19
大学的时候没好好学,现在又想认真学了
包子学渗透 发表于 2022-8-5 01:00
加油学习啊
Maxwell67 发表于 2022-8-5 01:44
准备学Python
nanqian 发表于 2022-8-5 01:50
正在学python,学习一下
ProtonNN 发表于 2022-8-5 02:20
一起加油兄弟!!
当钟声敲响回忆 发表于 2022-8-5 03:56
本帖最后由 当钟声敲响回忆 于 2022-8-5 05:41 编辑

[Python] 纯文本查看 复制代码
theBoard = {'1':' ','2':' ','3':' ','4':' ','5':' ','6':' ','7':' ','8':' ','9':' '}
def printBoard(Board):
    s = '-----'
    print(Board['1'] + '|' + Board['2'] + '|' + Board['3'])
    print(s)
    print(Board['4'] + '|' + Board['5'] + '|' + Board['6'])
    print(s)
    print(Board['7'] + '|' + Board['8'] + '|' + Board['9'])
def startg():
    a = list(theBoard.keys())
    turn = 'X'
    q= 0
    print('欢迎进入井字棋游戏')
    print('\n1  2  3\n4  5  6\n7  8  9\n')
    while q<9:
        printBoard(theBoard)
        print('Turn for ' + turn + ' 你想要在井字棋的哪个位置着手?')
        move = input()
        if theBoard[move] == ' ':
            theBoard[move] = turn
        else:
            print("不可覆盖落子")
            continue
        q+=1
        if turn == 'X':
            turn = 'O'
        else:
            turn = 'X'
        for j in range(3):
            if theBoard[a[j]] == theBoard[a[j+3]] == theBoard[a[j+6]]!=" " or theBoard[a[j*3]] == theBoard[a[j*3+1]] == theBoard[a[j*3+2]]!=" ":
                return theBoard[a[j]]+"赢"
        if theBoard[a[0]] == theBoard[a[4]] == theBoard[a[8]]!=" ":
            return theBoard[a[0]]+"赢"
        elif theBoard[a[2]] == theBoard[a[4]] == theBoard[a[6]]!=" ":
            return theBoard[a[2]]+"赢"
    return "平局"
print(startg())


帮楼主小改了一下))
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-10 09:10

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表