python打印板游戏-为什么即使板未满,代码也会结束?

python打印板游戏-为什么即使板未满,代码也会结束?,python,printing,Python,Printing,下面的代码是python棋盘游戏的一部分——为什么即使棋盘未满,代码也会结束 下降块功能代码应一直运行,直到电路板已满,但末端的电路板未满。 下面是输出的最后一部分。董事会还没有满。跳板代码应该一直运行到板满为止,对吗?但事实并非如此 | | | | | | o | | | | | x | x | | o | | | | | x | x | | o | | | | | o | o | x | o | x | |

下面的代码是python棋盘游戏的一部分——为什么即使棋盘未满,代码也会结束

下降块功能代码应一直运行,直到电路板已满,但末端的电路板未满。 下面是输出的最后一部分。董事会还没有满。跳板代码应该一直运行到板满为止,对吗?但事实并非如此

|    |    |   |   |   | o |   |
|    |    | x | x |   | o |   |
|    |    | x | x |   | o |   |
|    |    | o | o | x | o | x |
|    |    | o | x | x | x | o |
| o  |    | x | o | o | x | x |

Board FULL!

drop piece函数只测试使用该函数输入的列的所有行,而不是所有列的所有行。因此,当指定的列已满时,drop-piece函数将返回False。为了测试整个电路板是否已满,您必须使用以下其他功能进行测试:

def板未满()
对于板中的行:
对于行中的列:
如果行[列]==件无:
返回真值
返回错误

drop piece功能只测试使用该功能输入的列的所有行,而不是所有列的所有行。因此,当指定的列已满时,drop-piece函数将返回False。为了测试整个电路板是否已满,您必须使用以下其他功能进行测试:

def板未满()
对于板中的行:
对于行中的列:
如果行[列]==件无:
返回真值
返回错误

当一列填满时,代码返回'None',这相当于python中的False。我修改了您的代码以跟踪该列,并且仅在所有列都已满时返回false

import random


ROWS       = 6
COLUMNS    = 7

PIECE_NONE = ' '
PIECE_ONE  = 'x'
PIECE_TWO  = 'o'
FULL_COLUMS = ["has_space"] * COLUMNS # List to keep track of full colums


# Board Functions
def create_board(rows=ROWS, columns=COLUMNS):
    ''' Creates empty Connect 4 board '''
    board = []

    for row in range(rows):
        board_row = []
        for column in range(columns):
            board_row.append(PIECE_NONE)
        board.append(board_row)

    return board






def print_board( board ):
    ''' Prints Connect 4 board '''
    for row in board:    # like for i in list,  the board has 6 items or rows,
        print ('| ' + ' | '.join(row) + ' |')    # front and end has a '|',  in the middle, for each row which has 7 items, join the 7 items by each '|', now it prints 7 slots.


print("\n---------print:   7 items, join the 7 items by each '|', now it prints 7 slots--------------\n" )
#print_board( create_board( ) )







def drop_piece(board, column, piece):
    ''' Attempts to drop specified piece into the board at the
    specified column

    If this succeeds, return True, otherwise return False.
    '''

    ''' ************** Debug Notes  ************************
        Your code in this loop will and return None once the given 'column' is full. This is your bug :)
        To correct it, you would need to check if all columns are full, and only then do you return full.
        
        ***There are several ways of doing this. I will use the simplest one. So, I will keep track of every full colums.
        Therefore, every time the for loop is exectuted to completion, I will take the value of 'column' and mark that 
        the column corespoding to that number is full. I will keep doing this until all colums are full.
        To return false, all columns will need to be full. 
    '''
    for row in reversed(board):  # reverse the board
        if row[column] == PIECE_NONE:   # if each row from bottom to top, row[column] blank, drop ball, if not, what about the next one, i.e. second last row,
            row[column] = piece
            return True    # this code should run until the board is FULL, but the board at last is NOT FULL.
    #if we get here, one of the colums if full, so lets mark that
    FULL_COLUMS[column] = "Full"
    return "has_space" in FULL_COLUMS # this will return false only if all colums are full





print("\n---------print while drop_piece( )------------\n" )

import random

Board = create_board()
Turn=0
Players=(PIECE_ONE, PIECE_TWO)

while drop_piece(Board, random.randint(0, COLUMNS - 1), Players[Turn % 2]):
# while drop_piece( ) is True:
    print_board(Board)
    print()
    Turn += 1


print ('Board FULL!')
这是修改后得到的输出

| o | o | o | x | x | x | o |
| x | o | o | o | o | x | x |
| x | o | x | o | x | o | o |
| o | x | o | o | o | x | x |
| x | o | o | x | x | x | o |
| o | x | o | o | x | x | x |

Board FULL!

当一列填满时,代码返回“None”,这相当于python中的False。我修改了您的代码以跟踪该列,并且仅在所有列都已满时返回false

import random


ROWS       = 6
COLUMNS    = 7

PIECE_NONE = ' '
PIECE_ONE  = 'x'
PIECE_TWO  = 'o'
FULL_COLUMS = ["has_space"] * COLUMNS # List to keep track of full colums


# Board Functions
def create_board(rows=ROWS, columns=COLUMNS):
    ''' Creates empty Connect 4 board '''
    board = []

    for row in range(rows):
        board_row = []
        for column in range(columns):
            board_row.append(PIECE_NONE)
        board.append(board_row)

    return board






def print_board( board ):
    ''' Prints Connect 4 board '''
    for row in board:    # like for i in list,  the board has 6 items or rows,
        print ('| ' + ' | '.join(row) + ' |')    # front and end has a '|',  in the middle, for each row which has 7 items, join the 7 items by each '|', now it prints 7 slots.


print("\n---------print:   7 items, join the 7 items by each '|', now it prints 7 slots--------------\n" )
#print_board( create_board( ) )







def drop_piece(board, column, piece):
    ''' Attempts to drop specified piece into the board at the
    specified column

    If this succeeds, return True, otherwise return False.
    '''

    ''' ************** Debug Notes  ************************
        Your code in this loop will and return None once the given 'column' is full. This is your bug :)
        To correct it, you would need to check if all columns are full, and only then do you return full.
        
        ***There are several ways of doing this. I will use the simplest one. So, I will keep track of every full colums.
        Therefore, every time the for loop is exectuted to completion, I will take the value of 'column' and mark that 
        the column corespoding to that number is full. I will keep doing this until all colums are full.
        To return false, all columns will need to be full. 
    '''
    for row in reversed(board):  # reverse the board
        if row[column] == PIECE_NONE:   # if each row from bottom to top, row[column] blank, drop ball, if not, what about the next one, i.e. second last row,
            row[column] = piece
            return True    # this code should run until the board is FULL, but the board at last is NOT FULL.
    #if we get here, one of the colums if full, so lets mark that
    FULL_COLUMS[column] = "Full"
    return "has_space" in FULL_COLUMS # this will return false only if all colums are full





print("\n---------print while drop_piece( )------------\n" )

import random

Board = create_board()
Turn=0
Players=(PIECE_ONE, PIECE_TWO)

while drop_piece(Board, random.randint(0, COLUMNS - 1), Players[Turn % 2]):
# while drop_piece( ) is True:
    print_board(Board)
    print()
    Turn += 1


print ('Board FULL!')
这是修改后得到的输出

| o | o | o | x | x | x | o |
| x | o | o | o | o | x | x |
| x | o | x | o | x | o | o |
| o | x | o | o | o | x | x |
| x | o | o | x | x | x | o |
| o | x | o | o | x | x | x |

Board FULL!

我在while循环的顶部添加了board_is_not_full(board)条件,但是下面有这个错误。当棋盘未满时(棋盘):当棋盘未满时(棋盘,random.randint(0,列-1),玩家[回合%2]):回溯(最后一次调用):文件“e14_7_10_(a)_Connect4_game_drop_piece3.py”,第117行,当棋盘未满时(棋盘):文件“e14_7_10_(a)_Connect4_game_drop_piece3.py”,第55行,棋盘未满[column]==PIECE\u NONE:TypeError:列表索引必须是整数或片,不能从行[column]==PIECE\u NON更改为列==PIECE\u NON,现在可以工作了。太好了。def board\u未满():对于棋盘中的行:对于行中的列:if column==PIECE\u NONE:return True return false我在while循环的顶部添加了棋盘未满(board)条件,但下面有此错误。虽然棋盘未满(board):而drop\u PIECE(board,random.randint(0,COLUMNS-1),玩家[回合%2]):Traceback(最近一次调用last):文件“e14_7_10_(a)_Connect4_game_drop_piece3.py”,第117行,在board_未满时(board):文件“e14_7_10_(a)_Connect4_game_drop_piece3.py”,第55行,如果行[列]==PIECE_无:类型错误:列表索引必须是整数或切片,而不是从行[列]更改的==PIECE\u NON to column==PIECE\u NON,现在它可以工作了。很好。def board\u未满():对于board中的行:对于row中的列:如果column==PIECE\u NONE:返回True返回False