Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 要打印Tic Tac Toe表格,哪种代码更好?_Python 3.x - Fatal编程技术网

Python 3.x 要打印Tic Tac Toe表格,哪种代码更好?

Python 3.x 要打印Tic Tac Toe表格,哪种代码更好?,python-3.x,Python 3.x,这是Tictatcoe游戏的一部分。它们都打印一张Tic-Tac-Toe表,输出相同,但哪一个在算法和代码质量上更好?有什么可以修改以使其更好吗 #self.board is a list contains value of this table. # e.g. an empty board should look like this: # 0 1 2 # 0 | | # ----------- # 1 | | # ----------- # 2

这是Tictatcoe游戏的一部分。它们都打印一张Tic-Tac-Toe表,输出相同,但哪一个在算法和代码质量上更好?有什么可以修改以使其更好吗

#self.board is a list contains value of this table.
# e.g. an empty board should look like this:
#    0   1   2
# 0    |   |
#   -----------
# 1    |   |
#   -----------
# 2    |   |


# code 1
    printFormat = "\n   0   1   2 \n0  {} | {} | {} \n   -----------\n1  {} | {} | {} \n   -----------\n2  {} | {} | {} "
    print(printFormat.format(*self.board[0],*self.board[1],*self.board[2]))

# code 2
    printList = []
    txtFormat ="{:} {:^3}"+"|{:^3}"*(self.size-1)
    titleFormat = "{:^3} " * self.size
    numbers = range(self.size)
    line1 = "\n   "+titleFormat.format(*numbers)
    printList.append(line1)

    for i in range(self.size):
        contentLine = txtFormat.format(numbers[i],*self.board[i])
        printList.append(contentLine)
        cutOffLine = "   "+"-"*(4*self.size-1)
        printList.append(cutOffLine)

    printList.pop()
    for item in printList:
        print(item)

此问题与堆栈溢出的主题无关。如果您有工作代码并要求查看代码,则会有一个StackExchange站点用于此问题:。在我看来,它们都同样不可读。至少#2易于扩展,以使用实际交叉和O。