首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在python上检查tic tac toe游戏

在python上检查tic tac toe游戏
EN

Stack Overflow用户
提问于 2013-10-15 02:41:41
回答 1查看 553关注 0票数 0

我在这个项目中工作,我检查了一个完成的井字游戏。用户在每行(共3行)中输入x、o和句点(最多3个)的组合。我必须检查x或o是否转得太多。这就是我所拥有的:

代码语言:javascript
运行
复制
if ttt.count(x) > 5:
                    print("x took too many turns")

这只适用于x,当我运行程序,测试这个语句是否适用于x时,什么也没有出现。我做错了什么?

下面是一个示例运行:

代码语言:javascript
运行
复制
0 - check a finished tic-tac-toe
1 - check a finished sudoku
2 - check a game of connect four
3 - quit
choose an option: 0
option 0
For each row, start with x. Enter a combination of x'sand o's up to three characters. For a blank space,enter a period '.'.
top row:xox
middle row:xox
bottom row:xox  
['xox', 'xox', 'xox']
0 - check a finished tic-tac-toe
1 - check a finished sudoku
2 - check a game of connect four
3 - quit
choose an option: 

这是代码的更大部分:

代码语言:javascript
运行
复制
for x in i:
            if x not in valid_symbols:
                    print("invalid board - invalid symbol " + x )
                    done = True
                    break
            if ttt.count(x) > 5:
                print("x took too many turns")

如果我插入了一个无效的符号,那么它将打印该语句。

EN

回答 1

Stack Overflow用户

发布于 2013-10-15 02:54:37

您似乎误解了ttt的内容。打开解释器并尝试以下操作:

代码语言:javascript
运行
复制
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> tic_tac_toe = []
>>> tic_tac_toe.append(input('Top row: '))
Top row: xxx
>>>
>>> tic_tac_toe
['xxx']
>>> tic_tac_toe.count('x')
0
>>> tic_tac_toe.count('xxx')
1

当你.append('xox')的时候会发生什么?这个列表是什么样子的?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19366969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档