UltraDebug

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: A C D R G Y M Z X S P
公益项目,接受捐赠
查看: 2258|回复: 0
收起左侧

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

[复制链接]
Pandora

主题

0

回帖

UD

新手上路

UID
73
积分
11
注册时间
2022-7-31
最后登录
1970-1-1
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)
SyntaxHighlighter Copyright 2004-2013 Alex Gorbatchev.
本人改写后的代码
[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()
SyntaxHighlighter Copyright 2004-2013 Alex Gorbatchev.
增加的功能 判断X赢还是O赢
对Python编程快速上手里面的程序进行改写 - Pandora_UltraDebug对Python编程快速上手里面的程序进行改写 - Pandora_UltraDebug对Python编程快速上手里面的程序进行改写 - Pandora_UltraDebug对Python编程快速上手里面的程序进行改写 - Pandora_UltraDebug对Python编程快速上手里面的程序进行改写 - Pandora_UltraDebug对Python编程快速上手里面的程序进行改写 - Pandora_UltraDebug对Python编程快速上手里面的程序进行改写 - Pandora_UltraDebug对Python编程快速上手里面的程序进行改写 - Pandora_UltraDebug对Python编程快速上手里面的程序进行改写 - Pandora_UltraDebug对Python编程快速上手里面的程序进行改写 - Pandora_UltraDebug对Python编程快速上手里面的程序进行改写 - Pandora_UltraDebug对Python编程快速上手里面的程序进行改写 - Pandora_UltraDebug对Python编程快速上手里面的程序进行改写 - Pandora_UltraDebug对Python编程快速上手里面的程序进行改写 - Pandora_UltraDebug对Python编程快速上手里面的程序进行改写 - Pandora_UltraDebug对Python编程快速上手里面的程序进行改写 - Pandora_UltraDebug对Python编程快速上手里面的程序进行改写 - Pandora_UltraDebug对Python编程快速上手里面的程序进行改写 - Pandora_UltraDebug

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

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

随后 进入for循环 循环九次
进行条件判断 如果是第一次进入 就输出提示 然后调用函数 输出一段文本
turn是会发生变化的 第一次为X 第二次就为O 第三次又变化为X 后面的if分支就足以看的出来
[Python] 纯文本查看 复制代码
if turn == 'X':
        turn = 'O'
    else:
        turn = 'X'
SyntaxHighlighter Copyright 2004-2013 Alex Gorbatchev.
要求输入 并将输入值保存到变量move 然后 字典对应的键保存turn的值  
[Python] 纯文本查看 复制代码
move = input()
    theBoard[move] = turn
SyntaxHighlighter Copyright 2004-2013 Alex Gorbatchev.
这两行代码也可以这么理解 如下
move是对应的是要进行选择的真实世界井字棋的位置 theBoard[move]对应井字棋九个填充位置的标志 你选择哪一个move 那么 你选择的那一个位置就填充一个X或者O这样的标志
第一个 if else 的作用上面讲过了 第二个if elif 的作用简单来说就是判断哪一方赢 X赢还是O赢 如何判断的呢 在真实世界中 把三个相同的标志看成点 如果三个点能连成一条直线 那么就赢了
如何用程序判断 在这里 我是这样想的 把所有预估能赢的路线给写出来 一共八种路线 要么先手赢 要么后手赢 先手用X表示 后手则是O
这段代码可以缩短进行优化 但是 本人有点懒 哈哈
UltraDebug免责声明
✅以上内容均来自网友转发或原创,如存在侵权请发送到站方邮件9003554@qq.com处理。
✅The above content is forwarded or original by netizens. If there is infringement, please send the email to the destination 9003554@qq.com handle.

相关帖子

回复 打印

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|站点地图|UltraDebug ( 滇ICP备2022002049号-2 滇公网安备 53032102000034号)

GMT+8, 2025-6-20 14:56 , Processed in 0.029801 second(s), 10 queries , Redis On.

Powered by Discuz X3.4

© 2001-2023 Discuz! Team.

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