python - 连续Python中相同的13个字符的列表

标签 python list recursion input character

到目前为止,对于我的代码,我有:

while True:
    """
    determines if there is a list of x's or o's in a horizontal row
    """
    game = list(input())
    if len(game) == 0:
        print("We should now check for vertical or diagonal winners!")
    elif game[0] == game[1]:
        if game[1] == game[2]:
            if game[2] == game[3]:
                if game[3] == game[4]:
                    if game[4] == game[5]:
                        if game[5] == game[6]:
                            if game[6] == game[7]:
                                if game[7] == game[8]:
                                    if game[8] == game[9]:
                                        if game[9] == game[10]:
                                            if game[10] == game[11]:
                                                if game[11] == game[12]:
                                                    if game[12] == "o":
                                                        print("Player o won")
                                                    else:
                                                        print("Player x won")
                                                else:
                                                    del game[0:12]
                                            else:
                                                del game[0:12]
                                        else:
                                            del game[0:12]
                                    else:
                                        del game[0:12]
                                else:
                                    del game[0:12]
                            else:
                                del game[0:12]
                        else:
                            del game[0:12]
                    else:
                        del game[0:12]
                else:
                    del game[0:12]
            else:
                del game[0:12]
        else:
            del game[0:12]
    else:
        del game[0:12]

我觉得必须有一个更短的方式来写这个。我也只是想到了一个方法来判断是否有水平的赢家。我不确定如何着手解决垂直或对角赢家。我还在第二行测试了 x winning 的代码,它没有打印 x won,所以我想知道我的错误在哪里?

非常感谢任何帮助!谢谢。

最佳答案

伙计,你急需学习正则表达式!

这看起来很像家庭作业问题,但既然其他人都在回答......我会给你最快的答案:-)。对于这个问题,Regex 可能比您用纯 Python 编写的任何东西都快,因为它使用编译的 C 代码。

您可以使用正则表达式直接从输入字符串中相当轻松地测试水平或垂直匹配。

import re

# find 13 x's or o's in a row that begin some multiple of 13 characters from the beginning of the input string
horizMatch_regex = re.compile("^(.{13})*(xxxxxxxxxxxxx|ooooooooooooo)")
# find 13 x's or o's that appear with exactly 12 characters in between, which corresponds to columns.  Requires lookahead (?=) 
verticalMatch_regex = re.compile("(x(.{12})(?=x)){12}|(o(.{12})(?=o)){12}")
# slightly trickier - you need 4 separate match groups to test for each possible diagonal.  There are a variety of ways to do that, but here's one
diagonalMatch_regex = re.compile("(^(x.{13}){12}x)|(^(o.{13}){12}o)|((x.{11}){13}.$)|((o.{11}){13}.$)")

if horizMatch_regex.search(input_str):
    print("We have a horizontal tic tac toe!")

if verticalMatch_regex.search(input_str):
    print("We have a vertical tic tac toe!")

if diagonalMatch_regex.search(input_str):
    print("We have a diagonal tic tac toe!")

# string with horizontal, vertical, and diagonal tic tac toe's
input_str = "xooooooooooooxxxxxxxxxxxxxxoxoooooooooxxxxxxxxxxxxxoxoooxoooooooxxxxxxxxxxxxxoxoooooxoooooxxxxxxxxxxxxxoxoooooooxoooxxxxxxxxxxxxxoxoooooooooxoxxxxxxxxxxxxxoxooooooooooox"

We have a horizontal tic tac toe!
We have a vertical tic tac toe!
We have a diagonal tic tac toe!

关于python - 连续Python中相同的13个字符的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30315333/

相关文章:

python - DynamoDB 并行扫描不拆分结果

python - 在列表中替换时遇到问题 - Python

jquery - 根据内容更改 <p> 类,通过单击不同的表格单元格添加

java - 无法理解为什么每次递归调用都要加1

c - C中的递归深度是否有任何硬连线限制

python - 日志记录中标准输出捕获的最大递归深度

python - 可调用实例参数的表示法是什么?

python - 如何批量安装多个 Python 包?

python - 有没有办法在 python 中删除 zip 存档注释?

.net - 如何修改 .NET 中的 List<T> 结构?