冰凌汇编

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 30|回复: 0
收起左侧

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

[复制链接]
Pandora 发表于 2022-8-4 22:41:53
井字棋功能扩展
书中的源码
[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赢
对Python编程快速上手里面的程序进行改写 - Pandora_冰凌汇编对Python编程快速上手里面的程序进行改写 - Pandora_冰凌汇编对Python编程快速上手里面的程序进行改写 - Pandora_冰凌汇编对Python编程快速上手里面的程序进行改写 - Pandora_冰凌汇编对Python编程快速上手里面的程序进行改写 - Pandora_冰凌汇编对Python编程快速上手里面的程序进行改写 - Pandora_冰凌汇编对Python编程快速上手里面的程序进行改写 - Pandora_冰凌汇编对Python编程快速上手里面的程序进行改写 - Pandora_冰凌汇编对Python编程快速上手里面的程序进行改写 - Pandora_冰凌汇编对Python编程快速上手里面的程序进行改写 - Pandora_冰凌汇编对Python编程快速上手里面的程序进行改写 - Pandora_冰凌汇编对Python编程快速上手里面的程序进行改写 - Pandora_冰凌汇编对Python编程快速上手里面的程序进行改写 - Pandora_冰凌汇编对Python编程快速上手里面的程序进行改写 - Pandora_冰凌汇编对Python编程快速上手里面的程序进行改写 - Pandora_冰凌汇编对Python编程快速上手里面的程序进行改写 - Pandora_冰凌汇编对Python编程快速上手里面的程序进行改写 - Pandora_冰凌汇编对Python编程快速上手里面的程序进行改写 - Pandora_冰凌汇编

源码分析
创建了一个字典 这个字典可以看成抽象的井字棋 字典里面包含了九个键值对 每个键值对表示井字棋的位置与在位置上的标志 所以第一步就完成了 引用书中的一句话
使用数据结构对真实世界建模
随后创建了一个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
这段代码可以缩短进行优化 但是 本人有点懒 哈哈

本帖最后由 Pandora 于 2022-8-4 22:43 编辑
冰凌汇编免责声明
以上内容均来自网友转发或原创,如存在侵权请发送到站方邮件9003554@qq.com处理。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|站点统计|Archiver|小黑屋|RSS|冰凌汇编 ( 滇ICP备2022002049号 滇公网安备 53032102000029号)|网站地图

GMT+8, 2022-10-6 22:08 , Processed in 0.133328 second(s), 8 queries , Redis On.

冰凌汇编 - 建立于2021年12月20日

Powered by Discuz! © 2001-2022 Comsenz Inc.

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