要打印井字游戏板吗?

时间:2019-11-11 16:43:25

标签: python python-3.x list tic-tac-toe

我正在用python制作井字游戏,并且所有实际的游戏功能均正常运行,但是无法正确打印游戏板。

game = [[' ',' ',' '],[' ',' ',' '],[' ',' ',' ']]
for row in game:
  print('   |'*(len(game)-1)+'   ')
  for space in row:
    print(' '+space+' |',end='')
  print('\n'+'   |'*(len(game)-1)+'   ')
  if game.index(row) < len(game)-1:
    print('----'+('----'*(len(game)-2))+'---')

由于某种原因,除非每一行都没有移动,否则行索引不会增加。当游戏开始为空时,输出为:

   |   |   
   |   |   |
   |   |   
-----------
   |   |   
   |   |   |
   |   |   
-----------
   |   |   
   |   |   |
   |   |   
----------- 

底部不应该有一条线,但是每行都有移动时,它会消失。我还试图摆脱右侧每个空格中间的“ |”。任何提示或建议,将不胜感激!

3 个答案:

答案 0 :(得分:2)

我认为您应该按照我之前的建议进行字符串格式化。

尽管我的解决方案与您的代码有很大不同,但这是一个函数,该函数可打印(创建字符串,然后打印该字符串)通用板,其中图块用|隔开,每行用{{1隔开}}。 -中的每个元素都将放置在myboard空格的中间。

tile_width

这输出。

def print_board(myboard, tile_width=3):
    msg = ""
    for i, row in enumerate(myboard):
        # Print row
        element = row[0]  # first element does not need leading |
        element_str = '{:^{width}}'.format(element, width=tile_width)
        msg = msg + element_str

        for element in row[1:]:
            element_str = '|{:^{width}}'.format(element, width=tile_width)  # elements with leading |
            msg = msg + element_str

        msg = msg + '\n'

        # Print row divider if its not the last row
        if i is not len(myboard) - 1:
            element_str = '{:-^{width}}'.format("", width=((tile_width + 1) * len(row) - 1))  # '*' as fill char
            msg = msg + element_str
            msg = msg + '\n'
    print(msg)


cur = [['x', ' ', 'x'], [' ', 'o', ' '], ['o', '', ' ']]
print_board(cur)

答案 1 :(得分:1)

问题似乎是您使用的是index方法,而不是枚举之类的方法。 index返回给定元素在列表中的位置,而枚举可用于执行您想要做的更多事情(请参见Accessing the index in 'for' loops?)。使用index(row)时,您要求的是给定行所在的最低索引值。在空白板上,该值始终返回为0。

另一种解决方法是为行设置一个计数器(如下例所示)。

对于字符行上的多余行,您可以通过不同地打印行内容来解决此问题,如下例所示。

cur = [[' ',' ',' '],[' ',' ',' '],[' ',' ',' ']]

# initialize a row counter
rownumb = 0

for row in cur:
    print(cur.index(row))

    # increment the row counter
    rownumb += 1
    print('   |'*(len(cur)-1)+'   ')

    # a different approach to printing the contents of each row
    print(' ' + row[0] + ' | ' + row[1] + ' | ' + row[2])

    print('   |'*(len(cur)-1)+'   ')

    # check the counter and don't print the line after the final row
    if rownumb < 3:
        print('----'+('----'*(len(cur)-2))+'---')

答案 2 :(得分:0)

尽管我的解决方案与您的不同。但我有一个建议。为什么不简单地使用字符串格式打印板,而不是使用如下嵌套循环:

values=[' ' for x in range(9)]
print("\n")
print("\t     |     |")
print("\t  {}  |  {}  |  {}".format(values[0], values[1], values[2]))
print('\t_____|_____|_____')

print("\t     |     |")
print("\t  {}  |  {}  |  {}".format(values[3], values[4], values[5]))
print('\t_____|_____|_____')

print("\t     |     |")

print("\t  {}  |  {}  |  {}".format(values[6], values[7], values[8]))
print("\t     |     |")
print("\n")

输出如下:

     |     |   
_____|_____|_____
     |     |
     |     |   
_____|_____|_____
     |     |
     |     |   
     |     |
相关问题