gpt4 book ai didi

python - 主板打印不正确

转载 作者:行者123 更新时间:2023-11-30 22:57:09 25 4
gpt4 key购买 nike

我应该连接四个控制台,由于某种原因,我的主板显示 0 而不是“.”。我不确定我做错了什么。有 3 个模块,但我只会展示这个模块,因为我认为这就是根本问题所在。

import connectfour

def _print_board_num():
for i in range(len(connectfour.new_game().board)):
print(str(i+1), end = ' ')
print()

def print_board(game: 'connectfour.GameState')->[(str)]:
_print_board_num()
for row in range(connectfour.BOARD_ROWS):
for column in range(connectfour.BOARD_COLUMNS):
if game.board[column][row] == ' ':
print('.', end = ' ')
else:
print(game.board[column][row], end = ' ')

print()

def move()->str:
input('Drop or Pop? ')

def column1()->int:
int(input('What number of column? '))

我的主板打印如下:

1 2 3 4 5 6 7 
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0

但它应该打印如下:

1 2 3 4 5 6 7 
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .

这是connectfour模块的游戏板功能

def _new_game_board() -> [[int]]:
'''
Creates a new game board. Initially, a game board has the size
BOARD_COLUMNS x BOARD_ROWS and is comprised only of integers with the
value NONE
'''
board = []

for col in range(BOARD_COLUMNS):
board.append([])
for row in range(BOARD_ROWS):
board[-1].append(NONE)

return board



def _copy_game_board(board: [[int]]) -> [[int]]:
'''Copies the given game board'''
board_copy = []

for col in range(BOARD_COLUMNS):
board_copy.append([])
for row in range(BOARD_ROWS):
board_copy[-1].append(board[col][row])

我认为它是这两者之一。主要是放置功能,因为我注释掉了播放器错误,但它显示了放置功能。

def player(game: 'connectfour.GameState') -> None:
while True:
player = input('Would you like to drop(d) or pop(p)?')
if player == 'd':
drop(game)
return
elif player == 'p':
pop(game)
return
else:
connectfour.InvalidMoveError(Exception)
#print('Invalid Move')

def drop(game: 'connectfour.GameState') -> bool:
try:
col = connectfouroverlap.column1()
board = connectfour.drop(game,col-1)
connectfouroverlap.print_board(board)
if gameover(board) != connectfour.NONE:
return
else:
player(board)
except:
connectfour.InvalidMoveError(Exception)
print('Invalid Move')

这就是我运行它时的输出。

1 2 3 4 5 6 7 
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
Would you like to drop(d) or pop(p)?d
What number of column? 2
1 2 3 4 5 6 7
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. 1 . . . . .
Invalid Move

最佳答案

检查您的代码后,我假设 game.board 是一个仅包含 0 (整数?)值的 double 组。

如果这是真的,请尝试替换:

        if game.board[column][row] == ' ':
print('.', end = ' ')
else:
print(game.board[column][row], end = ' ')

与:

        if game.board[column][row] == 0:  # <--- change the condition here
print('.', end = ' ')
else:
print(game.board[column][row], end = ' ')

编辑: print_board 函数定义中的 ->[str] 注释表明该函数旨在返回字符串数组。但事实并非如此。

您正在创建一个全新的板来打印列号。这是一种不好的做法,请使用现有的做法。

您还可以直接在数组上使用 for in 循环,而不是使用索引。这将需要更少的代码,并且会更清晰、更容易编写:

for row in game.board:  # <--  since game.board is your [[int]]
for number in row:
do_stuff_on(number)

关于python - 主板打印不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36817207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com