0

理由はわかりませんが、次のPython 3コードtie(cell)では、内部ループで関数が希望どおりに実行されません(ボードがいっぱいになるとループの実行が停止します)。

誰かがその理由について何か知っていますか?ありがとう!

import random
cell = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
def owin(cell):
    if cell[0] == cell[1] == cell[2] == 'O' or cell[3] == cell[4] == cell[5] == 'O' or cell[6] == cell[7] == cell[8] == 'O' or cell[0] == cell[3] == cell[6] == 'O' or cell[1] == cell[4] == cell[7] == 'O' or cell[2] == cell[5] == cell[8]== 'O':
        return True
    else:
        return False
def xwin(cell):
    if cell[0] == cell[1] == cell[2] == 'X' or cell[3] == cell[4] == cell[5] == 'X' or cell[6] == cell[7] == cell[8] == 'X' or cell[0] == cell[3] == cell[6] == 'X' or cell[1] == cell[4] == cell[7] == 'X' or cell[2] == cell[5] == cell[8]== 'X':
        return True
    else:
        return False
def tie(cell):
    if cell[0] in ('O', 'X') and cell[1] in ('O', 'X') and cell[2] in ('O', 'X') and cell[3] in ('O', 'X') and cell[4] in ('O', 'X') and cell[5] in ('O', 'X') and cell[6] in ('O', 'X') and cell[7] in ('O', 'X') and cell[8] in ('O', 'X'):
        return True
    else:
        return False
board = '\n\t ' + cell[0] + ' | ' + cell[1] + ' | ' + cell[2] + '\n\t-----------\n\t ' + cell[3] + ' | ' + cell[4] + ' | ' + cell[5] + '\n\t-----------\n\t ' + cell[6] + ' | ' + cell[7] + ' | ' + cell[8]
print('\tTIC TAC TOE\n\t\tBy Lewis Cornwall')
instructions = input('Would you like to read the instructions? (y/n)')
if instructions == 'y':
    print('\nEach player takes turns to place a peice on the following grid:\n\n\t 1 | 2 | 3\n\t-----------\n\t 4 | 5 | 6\n\t-----------\n\t 7 | 8 | 9\n\nBy inputing a vaule when prompted. The first to 3 peices in a row wins.')
player1 = input('Enter player 1\'s name: ')
player2 = input('Enter player 2\'s name: ')
print(player1 + ', you are O and ' + player2 + ', you are X.')
nextPlayer = player1
while not owin(cell) and not xwin(cell) and not tie(cell):
    print('This is the board:\n' + board)
    if nextPlayer == player1:
        move = input('\n' + player1 + ', select a number (1 - 9) to place your peice: ')
        cell[int(move) - 1] = 'O'
        board = '\n\t ' + cell[0] + ' | ' + cell[1] + ' | ' + cell[2] + '\n\t-----------\n\t ' + cell[3] + ' | ' + cell[4] + ' | ' + cell[5] + '\n\t-----------\n\t ' + cell[6] + ' | ' + cell[7] + ' | ' + cell[8]
        nextPlayer = player2
    else:
        move = input('\n' + player2 + ', select a number (1 - 9) to place your peice: ')
        cell[int(move) - 1] = 'X'
        board = '\n\t ' + cell[0] + ' | ' + cell[1] + ' | ' + cell[2] + '\n\t-----------\n\t ' + cell[3] + ' | ' + cell[4] + ' | ' + cell[5] + '\n\t-----------\n\t ' + cell[6] + ' | ' + cell[7] + ' | ' + cell[8]
        nextPlayer = player1
if owin(cell):
    print('Well done, ' + player1 + ', you won!')
elif xwin(cell):
    print('Well done, ' + player2 + ', you won!')
else:
    print('Unfortunately, niether of you were able to win today.')
input('Press <enter> to quit.')
4

4 に答える 4

2

あなたが書いたコードでは、の唯一の外観tie(cell)はこれです:

while not owin(cell) and not xwin(cell) and not tie(cell):

これは論理式であり、ほとんどすべてのプログラミング言語(Pythonを含む)では、次のような論理式が最適化されていることに注意してください。

# here test_func() won't be invoked
if 1 or test_func():
    pass

# here test_func() won't be invoked
if 0 and test_func():
    pass

# here test_func() would be invoked
if 1 and test_func():
    pass

# here test_func() would be invoked
if 0 or test_func():
    pass

したがって、が呼び出されない理由tie(cell)は、owin(cell)またはxwin(cell)がtrueを返すためです。

于 2013-03-12T09:10:30.683 に答える
1

関数tie(cell)内の条件を置き換えます

if cell[0] in ('O', 'N') and cell[1] in ('O', 'N') and cell[2] in ('O', 'N') and cell[3] in ('O', 'N') and cell[4] in ('O', 'N') and cell[5] in ('O', 'N') and cell[6] in ('O', 'N') and cell[7] in ('O', 'N') and cell[8] in ('O', 'N'):

 if cell[0] in ('O', 'X') and cell[1] in ('O', 'X') and cell[2] in ('O', 'X') and cell[3] in ('O', 'X') and cell[4] in ('O', 'X') and cell[5] in ('O', 'X') and cell[6] in ('O', 'X') and cell[7] in ('O', 'X') and cell[8] in ('O', 'X'):
于 2013-03-12T09:32:05.090 に答える
0

@PengyuCHENが言ったことと同じように、
あなたのtie()関数は他の2つのチェックの後にのみ呼び出されます..それを再配置すると、問題が解決したことがわかります. したがって、これは機能するはずです:

while not tie(cell) and not (owin(cell) or xwin(cell)):

また、あなたはそれを求めていませんでしたが、私はあなたのコードをクリーンアップしようとしました...

import random
instructions = '''
Each player takes turns to place a piece on the following grid:

     1 | 2 | 3
    -----------
     4 | 5 | 6
    -----------
     7 | 8 | 9

By inputting a value when prompted. The first to 3 pieces in a row wins'''

form = '''
\t| %s | %s | %s |
\t-------------
\t| %s | %s | %s |
\t-------------
\t| %s | %s | %s |
'''

cell = ['1', '2', '3', '4', '5', '6', '7', '8', '9']

def win(cell, char):
    return cell[0] == cell[1] == cell[2] == char or\
       cell[3] == cell[4] == cell[5] == char or\
       cell[6] == cell[7] == cell[8] == char or\
       cell[0] == cell[3] == cell[6] == char or\
       cell[1] == cell[4] == cell[7] == char or\
       cell[2] == cell[5] == cell[8] == char

def tie(cell):
    return all(c in ('O','X') for c in cell)

def get_input(player):
    while True:
        m = input('\n%s, select a number (1 - 9) to place your peice: ' % player)
        try:
            return int(m)
        except BaseException:
            print 'Invalid Input'
            continue

board = form % tuple(cell)

print('\tTIC TAC TOE\n\t\tBy Lewis Cornwall')
instr = input('Would you like to read the instructions? (y/n) ')
if instr.startswith('y'):
    print(instructions)

player1 = input('Enter player 1\'s name: ').strip()
player2 = input('Enter player 2\'s name: ').strip()

print(player1 + ', you are O and ' + player2 + ', you are X.')
nextPlayer = player1

while not tie(cell) and not (win(cell, 'O') or win(cell,"X")):
    print('This is the board:\n' + board)
    if nextPlayer == player1:
        move = get_input(player1)
        cell[move - 1] = 'O'
        board = form % tuple(cell)
        nextPlayer = player2
    else:
        move = get_input(player2)
        cell[move - 1] = 'X'
        board = form % tuple(cell)
        nextPlayer = player1

if win(cell,"O"):
    print('Well done, ' + player1 + ', you won!')
elif win(cell,"X"):
    print('Well done, ' + player2 + ', you won!')
else:
    print('Unfortunately, neither of you were able to win today.')
input('Press <enter> to quit.')
于 2013-03-12T10:41:56.700 に答える
0

条件は次のようになります。

while (not owin(cell) or not xwin(cell)) and not tie(cell):
于 2013-03-12T09:11:44.267 に答える