[英]Print 7x6 two-dimensional list
I need to print a two-dimensional list for a connect four game with 7 columns and 6 rows. 我需要为7列6行的四连体游戏打印二维列表。 Here is my code but it only prints a 6x6 table. 这是我的代码,但它仅打印6x6表格。 The global constants from the connectfour module are: BOARD_ROWS = 6 and BOARD_COLUMNS = 7 connectfour模块的全局常量为:BOARD_ROWS = 6和BOARD_COLUMNS = 7
def print_board(game_state: list)-> None:
for i in range(connectfour.BOARD_ROWS):
for j in range(connectfour.BOARD_COLUMNS):
if j != connectfour.BOARD_COLUMNS -1:
if game_state[j][i] == connectfour.NONE:
print('.', end=' ')
elif game_state[j][i] == connectfour.RED:
print('R', end=' ')
elif game_state[j][i] == connectfour.YELLOW:
print('Y', end=' ')
else:
print('\n',end='')
This is the output I get: 这是我得到的输出:
. . . . . .
. . . . . .
Y . . . . .
R . . . . .
R . . . . .
R R R Y Y Y
As you can see there are only 6 columns. 如您所见,只有6列。 However I know the 7th one is there because in this particular output the "Y" player won after dropping a piece in the 7th column. 但是我知道第7列在那里,因为在此特定输出中,“ Y”玩家在第7列中丢了一块后获胜。 I just don't understand why it doesn't print the 7th column. 我只是不明白为什么它不打印第7列。 Thanks in advance for the help. 先谢谢您的帮助。
if j != connectfour.BOARD_COLUMNS -1:
means you are specifically excluding the last column. if j != connectfour.BOARD_COLUMNS -1:
表示您专门排除最后一列。 Remove that if
construction and undent the following if
s and you should be good. if
将其删除, if
删除, if
s if
取消以下注释, if
您应该很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.