首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >电路板无法正常打印

电路板无法正常打印
EN

Stack Overflow用户
提问于 2016-04-24 06:30:59
回答 1查看 99关注 0票数 1

我应该做一个连接4的控制台,但由于某种原因,我的主板显示0而不是"."s。我不确定我做错了什么。有3个模块,但我将只显示这个模块,因为我假设这是潜在问题所在。

代码语言:javascript
复制
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? '))

我的黑板打印如下:

代码语言:javascript
复制
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 

但它应该是这样打印的:

代码语言:javascript
复制
1 2 3 4 5 6 7 
. . . . . . . 
. . . . . . . 
. . . . . . . 
. . . . . . . 
. . . . . . . 
. . . . . . . 

这是connectfour模块的游戏板函数

代码语言:javascript
复制
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])

我猜是这两个中的任何一个。主要是drop函数,因为我注释掉了player错误,但它显示在drop函数中。

代码语言:javascript
复制
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')

这是我运行它时的输出。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2016-04-24 06:37:58

检查代码后,我假设game.board是一个只包含整数( 0?)的双精度数组。值。

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

代码语言:javascript
复制
        if game.board[column][row] == ' ':
            print('.', end = ' ')
        else:
            print(game.board[column][row], end = ' ')

通过以下方式:

代码语言:javascript
复制
        if game.board[column][row] == 0:  # <--- change the condition here
            print('.', end = ' ')
        else:
            print(game.board[column][row], end = ' ')

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

你在创建一块全新的板来打印列号。这是一种糟糕的做法,请使用现有的做法。

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

代码语言:javascript
复制
for row in game.board:  # <--  since game.board is your [[int]]
    for number in row:
        do_stuff_on(number)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36817207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档