井字游戏的输出不正确

问题描述 投票:0回答:1

我在更正此代码的输出时遇到麻烦:


    print('--------')
    print('|'+board[7]+ '|' +board[8] +'|' +board[9]+'|')
    print('--------')
    print('|' +board[4]+ '|'+board[5]+'|' +board[6]+'|')
    print('--------')
    print('|' +board[1]+ '|'+board[2]+'|' +board[3]+'|')
    print('--------')

real_board=['#','#','#','#','#','#','#','#','#','#']
def xchecker_tool(board, mark):
    vertical=print(f'congratulations player {mark}! you have won by lining up vertically on the board!')  
    horizontal=print(f'congratulations player {mark}! you have won by lining up horizontally on the board!')
    diagonal= print(f'congratulations player {mark}! you have won by lining up diagonally!')

    if board[1]==mark and board[2]==mark and board[3]==mark: 
        horizontal
    elif board[4]==mark and board[5]==mark and board[6]==mark:
         horizontal
    elif board[7]==mark and board[8]==mark and board[9]==mark:
        horizontal

    if board[1]==mark and board[4]==mark and board[7]==mark:
        vertical
    elif board[2]==mark and board[5]==mark and board[8]==mark:
            vertical
    elif board[3]==mark and board[6]==mark and board[9]==mark:
        vertical

    if board[1]==mark and board[5]==mark and board[9]==mark:
            diagonal
    elif board[3]==mark and board[5]==mark and board[7]==mark:
         diagonal
def game_rounds(round):
    player_moves=int(input('choose a number from 1 to 9: '))
    while True:
        if (player_moves)<1 or (player_moves)>9:
            print('unacceptable range')
        elif (player_moves)==1:
            real_board[1]='X'
        elif (player_moves)==2:
            real_board[2]='X'
        elif (player_moves)==3:
            real_board[3]='X'
        elif (player_moves)==4:
            real_board[4]='X'
        elif (player_moves)==5: 
            real_board[5]='X'
        elif (player_moves)==6:
            real_board[6]='X'           
        elif (player_moves)==7:
            real_board[7]='X'
        elif (player_moves)==8:
            real_board[8]='X'
        else:
            real_board[9]='X'
        break
    display_board(real_board)

def y_rounds(round):
    y_moves=int(input('choose a number from 1 to 9: '))

    while True:
        if (y_moves)<1 or (y_moves)>9:
            print('unacceptable range')
        elif (y_moves)==1:
            real_board[1]='O'
        elif (y_moves)==2:
            real_board[2]='O'
        elif (y_moves)==3:
            real_board[3]='O'
        elif (y_moves)==4:
            real_board[4]='O'
        elif (y_moves)==5: 
            real_board[5]='O'
        elif (y_moves)==6:
            real_board[6]='O'           
        elif (y_moves)==7:
            real_board[7]='O'
        elif (y_moves)==8:
            real_board[8]='O'
        else:
            real_board[9]='O'
        break
    display_board(real_board)
def run_game(full):
    game_on=True
    game_off=False
    t=0    
    while game_on:
            game_rounds(1)
            t+=1
            if t==5:
                break
            y_rounds(1)
            xchecker_tool(real_board, 'X')
            xchecker_tool(real_board, 'O')


run_game(1)

我正在获得此输出。据我了解,xchecker_tool函数的定义不正确,这就是为什么即使没有排列“ x”或“ o”时也每两圈打印一次“祝贺”消息的原因。我也希望帮助清理脚本,因为它运行很长,但是由于经常出现错误,因此无法在当前级别执行此操作。

choose a number from 1 to 9: 3
--------
|#|#|#|
--------
|#|#|#|
--------
|#|#|X|
--------
choose a number from 1 to 9: 5
--------
|#|#|#|
--------
|#|O|#|
--------
|#|#|X|
--------
congratulations player X! you have won by lining up vertically on the board!
congratulations player X! you have won by lining up horizontally on the board!
congratulations player X! you have won by lining up diagonally!
congratulations player O! you have won by lining up vertically on the board!
congratulations player O! you have won by lining up horizontally on the board!
congratulations player O! you have won by lining up diagonally!
python python-3.x tic-tac-toe
1个回答
0
投票

这里:

vertical=print(f'congratulations player {mark}! you have won by lining up vertically on the board!')  
horizontal=print(f'congratulations player {mark}! you have won by lining up horizontally on the board!')
diagonal= print(f'congratulations player {mark}! you have won by lining up diagonally!')

print将被执行,其返回值将绑定到变量。您定义了三个变量-因此print将执行3次,将文本输出到终端。

这样的代码:

if board[1]==mark and board[2]==mark and board[3]==mark: 
        horizontal

仅“提及”变量horizontal,它实际上没有执行任何操作。您可能还写过:

if board[1]==mark and board[2]==mark and board[3]==mark: 
    None  # won't do anything
    ...   # won't do anything either
    print
    board[1]

这些都不执行任何东西。

您应该在if语句中打印:

if board[1]==mark and board[2]==mark and board[3]==mark: 
    print(f'congratulations player {mark}! you have won by lining up horizontally on the board!')
# and so on for the other `if`s
© www.soinside.com 2019 - 2024. All rights reserved.