- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试用 Python 制作井字棋游戏,但我无法让它检测到胜利。这是本书的一部分:使用 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
#X wins
if theBoard['top-L' and 'top-M' and 'top-R'] == 'X':
print('X Won!')
break
if theBoard['mid-L' and 'mid-M' and 'mid-R'] == 'X':
print('X Won!')
break
if theBoard['low-L' and 'low-M' and 'low-R'] == 'X':
print('X Won!')
break
if theBoard['top-L' and 'mid-L' and 'low-L'] == 'X':
print('X Won!')
break
if theBoard['top-M' and 'mid-M' and 'low-M'] == 'X':
print('X Won!')
break
if theBoard['top-R' and 'mid-R' and 'low-R'] == 'X':
print('X Won!')
break
if theBoard['top-L' and 'mid-M' and 'low-R'] == 'X':
print('X Won!')
break
if theBoard['top-R' and 'mid-M' and 'low-L'] == 'X':
print('X Won!')
break
#O wins
if theBoard['top-L' and 'top-M' and 'top-R'] == 'O':
print('O Won!')
break
if theBoard['mid-L' and 'mid-M' and 'mid-R'] == 'O':
print('O Won!')
break
if theBoard['low-L' and 'low-M' and 'low-R'] == 'O':
print('O Won!')
break
if theBoard['top-L' and 'mid-L' and 'low-L'] == 'O':
print('O Won!')
break
if theBoard['top-M' and 'mid-M' and 'low-M'] == 'O':
print('O Won!')
break
if theBoard['top-R' and 'mid-R' and 'low-R'] == 'O':
print('O Won!')
break
if theBoard['top-L' and 'mid-M' and 'low-R'] == 'O':
print('O Won!')
break
if theBoard['top-R' and 'mid-M' and 'low-L'] == 'O':
print('O Won!')
break
if turn == 'X':
turn = 'O'
else:
turn = 'X'
printBoard(theBoard)
发生的事情是:当我键入例如:mid-R 时,它会立即说 X 赢了。前四个“Xwins”工作得很好,但之后一切都出错了,正如我刚才解释的那样。
最佳答案
你的代码的问题是 if theBoard['mid-L' and 'mid-M' and 'mid-R'] == 'X':
没有按照你的想法去做它在做。它不检查所有三个位置是否都是“X”。它只是始终返回最正确的值。参见 Boolean Operation来自文档:
The expression
x and y
first evaluatesx
; ifx
is false, its value is returned; otherwise,y
is evaluated and the resulting value is returned.The expression
x or y
first evaluatesx
; ifx
is true, its value is returned; otherwise,y
is evaluated and the resulting value is returned.
由于非空字符串的 bool 值始终为 True
,因此 'mid-L' and 'mid-M' and 'mid-R'
将始终返回'mid-R'
,它为您提供 theBoard['mid-R'] == 'X'
的条件并将产生 True
,给你 X 获胜的条件。
至于补救措施,我相信@Endyd 已经为您解决了。
最理想的情况是不要对所有获胜条件进行硬编码,但这需要重构您的代码……也许当您有更好的理解时,我建议您回来尝试更动态的解决方案。至于现在,祝你学习愉快!
关于python - 为什么我的井字游戏有时过快宣布获胜?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54093749/
能否解释一下proclaim、declaim和declare这三个符号的区别? 最佳答案 它们是 symbols , 不是 keywords . proclaim命名为function使全局化 dec
我正在尝试使现有应用程序尽可能易于访问以进行语音传输。 目前,我有一个 uiviewcontroller,它基本上是一个分页照片 View ,在 uiscrollView (tourScrollVie
选择 UITableViewCell 时,画外音会宣布“已选择”,我不希望画外音说“已选择”。我怎样才能做到这一点? 我尝试过但没有成功的事情: 更改了单元格 accessibilityHint 和
我正在测试一个带有 TalkBack 的 Android 应用程序,“LIST”这个词被宣布为“Fifty first”。显然,TalkBack 假设大写字母“LI”是罗马数字 51。 我无法访问源代
假设 你在 github 上 fork 了一个项目 多人(少于 5 人)正在使用这个 fork 目标是对我们的更改提出 pull 请求 在对我们的分支进行几次提交之后,我们现在想要将我们的分支更新为源
我是一名优秀的程序员,十分优秀!