对Python编程快速上手里面的程序进行改写
本帖最后由 paypojie 于 2022-8-4 22:43 编辑井字棋功能扩展
书中的源码
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 = turn
if turn == 'X':
turn = 'O'
else:
turn = 'X'
printBoard(theBoard)
本人改写后的代码
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-Ltop-Mtop-R
mid-Lmid-Mmid-R
low-Llow-Mlow-R'''
print()
print(d)
print()
printBoard(theBoard)
print('Turn for ' + turn + ' 你想要在井字棋的哪个位置着手?')
move = input()
theBoard = 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赢
https://static.52pojie.cn/static/image/hrline/line8.pnghttps://static.52pojie.cn/static/image/hrline/line8.pnghttps://static.52pojie.cn/static/image/hrline/line8.pnghttps://static.52pojie.cn/static/image/hrline/line8.pnghttps://static.52pojie.cn/static/image/hrline/line8.pnghttps://static.52pojie.cn/static/image/hrline/line8.pnghttps://static.52pojie.cn/static/image/hrline/line8.pnghttps://static.52pojie.cn/static/image/hrline/line8.pnghttps://static.52pojie.cn/static/image/hrline/line8.pnghttps://static.52pojie.cn/static/image/hrline/line8.pnghttps://static.52pojie.cn/static/image/hrline/line8.pnghttps://static.52pojie.cn/static/image/hrline/line8.pnghttps://static.52pojie.cn/static/image/hrline/line8.pnghttps://static.52pojie.cn/static/image/hrline/line8.pnghttps://static.52pojie.cn/static/image/hrline/line8.pnghttps://static.52pojie.cn/static/image/hrline/line8.pnghttps://static.52pojie.cn/static/image/hrline/line8.pnghttps://static.52pojie.cn/static/image/hrline/line8.png
源码分析
创建了一个字典 这个字典可以看成抽象的井字棋 字典里面包含了九个键值对 每个键值对表示井字棋的位置与在位置上的标志 所以第一步就完成了 引用书中的一句话
使用数据结构对真实世界建模
随后创建了一个printBoard函数 调用函数 函数里面会在屏幕打印一个看起来像真实世界的井字棋的字符画
定义了一个变量turn 这个变量表示要添加到井字棋的占位符(我是这么理解的)
随后 进入for循环 循环九次
进行条件判断 如果是第一次进入 就输出提示 然后调用函数 输出一段文本
turn是会发生变化的 第一次为X 第二次就为O 第三次又变化为X 后面的if分支就足以看的出来
if turn == 'X':
turn = 'O'
else:
turn = 'X'
要求输入 并将输入值保存到变量move 然后 字典对应的键保存turn的值
move = input()
theBoard = turn
这两行代码也可以这么理解 如下
move是对应的是要进行选择的真实世界井字棋的位置 theBoard对应井字棋九个填充位置的标志 你选择哪一个move 那么 你选择的那一个位置就填充一个X或者O这样的标志
第一个 if else 的作用上面讲过了 第二个if elif 的作用简单来说就是判断哪一方赢 X赢还是O赢 如何判断的呢 在真实世界中 把三个相同的标志看成点 如果三个点能连成一条直线 那么就赢了
如何用程序判断 在这里 我是这样想的 把所有预估能赢的路线给写出来 一共八种路线 要么先手赢 要么后手赢 先手用X表示 后手则是O
这段代码可以缩短进行优化 但是 本人有点懒 哈哈
最近学python,学习学习 female20 发表于 2022-8-4 22:37
最近学python,学习学习
加油 推荐文档加视频结合在一起学习:lol 加油伙计 大学的时候没好好学,现在又想认真学了 加油学习啊 准备学Python 正在学python,学习一下 一起加油兄弟!! 本帖最后由 当钟声敲响回忆 于 2022-8-5 05:41 编辑
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('\n123\n456\n789\n')
while q<9:
printBoard(theBoard)
print('Turn for ' + turn + ' 你想要在井字棋的哪个位置着手?')
move = input()
if theBoard == ' ':
theBoard = turn
else:
print("不可覆盖落子")
continue
q+=1
if turn == 'X':
turn = 'O'
else:
turn = 'X'
for j in range(3):
if theBoard] == theBoard] == theBoard]!=" " or theBoard] == theBoard] == theBoard]!=" ":
return theBoard]+"赢"
if theBoard] == theBoard] == theBoard]!=" ":
return theBoard]+"赢"
elif theBoard] == theBoard] == theBoard]!=" ":
return theBoard]+"赢"
return "平局"
print(startg())
帮楼主小改了一下))