如何创建python游戏板

时间:2018-12-08 23:52:20

标签: python dataframe format nested-lists

我需要帮助来创建类似的东西。 (我只是一个初学者,我需要一些启发来启发如何用这些数字创建此板,该板应为参数n,通过该参数,我可以选择该板的大小[9x9或13x13]) !

 0 1 2 3 4 5 6 7 8
0       * * *
1       * D *
2       * D *
3 * * * * D * * * *
4 * D D D X D D D *
5 * * * * D * * * *
6       * D *
7       * D *   
8       * * *

我的董事会列表如下:

board = [[' ',' ',' ','*','*','*',' ',' ',' '],
              [' ',' ',' ','*','D','*',' ',' ',' '],
              [' ',' ',' ','*','D','*',' ',' ',' '],
              ['*','*','*','*','D','*','*','*','*'],
              ['*','D','D','D','X','D','D','D','*'],
              ['*','*','*','*','D','*','*','*','*'],
              [' ',' ',' ','*','D','*',' ',' ',' '],
              [' ',' ',' ','*','D','*',' ',' ',' '],
              [' ',' ',' ','*','*','*',' ',' ',' ']]

1 个答案:

答案 0 :(得分:0)

这两行将起作用:

print(' '+' '.join(map(str,range(9))))
print('\n'.join([' '.join([str(i)]+v) for i,v in enumerate(board)]))

输出:

 0 1 2 3 4 5 6 7 8
0       * * *      
1       * D *      
2       * D *      
3 * * * * D * * * *
4 * D D D X D D D *
5 * * * * D * * * *
6       * D *      
7       * D *      
8       * * *   

str.join束可以做到!!