Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的Tic-tac-toe循环未运行_Python_Python 3.x_Tic Tac Toe - Fatal编程技术网

Python中的Tic-tac-toe循环未运行

Python中的Tic-tac-toe循环未运行,python,python-3.x,tic-tac-toe,Python,Python 3.x,Tic Tac Toe,我不知道为什么,但是下面的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

我不知道为什么,但是下面的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.')
随机导入
单元格=['1','2','3','4','5','6','7','8','9']
def owin(单元):
如果单元[0]==单元[1]==单元[2]==单元[0]或单元[3]==单元[4]==单元[5]==单元[7]==单元[8]=='O'或单元[0]==单元[3]==单元[6]='O'或单元[1]==单元[4]==单元[7]='O'或单元[2]==单元[5]==单元[8]='O':
返回真值
其他:
返回错误
def xwin(单元):
如果单元[0]==单元[1]==单元[2]==单元[X]或单元[3]==单元[4]==单元[5]='X'或单元[6]==单元[7]==单元[8]='X'或单元[0]==单元[3]==单元[6]='X'或单元[1]==单元[4]==单元[7]='X'或单元[2]==单元[5]==单元[8]='X':
返回真值
其他:
返回错误
def tie(电池):
如果('O','X')中的单元[0]和('O','X')中的单元[1]和('O','X')中的单元[2]和('O','X')中的单元[3]和('O','X')中的单元[4]和('O','X')中的单元[5]和('O','X')中的单元[6]和('O','X')中的单元[7]和('O','X')中的单元[8]:
返回真值
其他:
返回错误
board='\n\t'+单元[0]+''.''.''.+单元[1]+'.''.''.+单元[2]+'\n\t------\n\t'.+单元[3]+'.'.''.+单元[4]+'.'.+单元[5]+'\n\t------\n\t'.+单元[6]+'.''.'.+单元[7]+'.+单元[8]
打印('\tTIC TAC TOE\n\t\t由Lewis Cornwall'提供)
指令=输入('您想阅读指令吗?(y/n)')
如果指令==“y”:
打印(“\n每个玩家轮流在以下网格上放置一个peice:\n\t 1 | 2 | 3\n\t------\n\t 4 | 5 | 6\n\t------\n\t 7 | 8 | 9\n\n在提示时输入一个值。一行中的第一到三个peice获胜。”)
player1=输入('输入播放器1的名称:')
player2=输入('输入播放器2的名称:')
打印(player1+,你是O,“+player2+”,你是X.)
nextPlayer=player1
而不是owin(单元)和xwin(单元)以及tie(单元):
打印('这是电路板:\n'+电路板)
如果nextPlayer==player1:
move=input('\n'+player1+',选择一个数字(1-9)以放置您的peice:')
单元[int(move)-1]=“O”
board='\n\t'+单元[0]+''.''.''.+单元[1]+'.''.''.+单元[2]+'\n\t------\n\t'.+单元[3]+'.'.''.+单元[4]+'.'.+单元[5]+'\n\t------\n\t'.+单元[6]+'.''.'.+单元[7]+'.+单元[8]
nextPlayer=player2
其他:
move=input(“\n”+player2+”,选择一个数字(1-9)来放置您的peice:“)
单元[int(move)-1]=“X”
board='\n\t'+单元[0]+''.''.''.+单元[1]+'.''.''.+单元[2]+'\n\t------\n\t'.+单元[3]+'.'.''.+单元[4]+'.'.+单元[5]+'\n\t------\n\t'.+单元[6]+'.''.'.+单元[7]+'.+单元[8]
nextPlayer=player1
如果owin(单元格):
打印(“干得好,+player1+”,您赢了!”)
elif xwin(单元):
打印(“干得好,+player2+”,您赢了!”)
其他:
print('不幸的是,你们中的尼瑟今天赢了')
输入('按退出')

在您编写的代码中,
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
while (not owin(cell) or not xwin(cell)) and not tie(cell):

因此,未调用
tie(cell)
的原因是
owin(cell)
xwin(cell)
返回true。

在您编写的代码中,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
while (not owin(cell) or not xwin(cell)) and not tie(cell):

因此,不调用
tie(cell)
的原因是
owin(cell)
xwin(cell)
返回true。

条件应如下:

# 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
while (not owin(cell) or not xwin(cell)) and not tie(cell):

条件应该是这样的:

# 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
while (not owin(cell) or not xwin(cell)) and not tie(cell):

更换功能接头(单元)内的状况

如果('O','N')中的单元[0]和('O','N')中的单元[1]和('O','N')中的单元[2]和('O','N')中的单元[3]和('O','N')中的单元[4]和('O','N')中的单元[5]和('O','N')中的单元[6]和('O','N')中的单元[7]和('O','N')中的单元[8]:


更换功能接头(单元)内的状况

如果('O','N')中的单元[0]和('O','N')中的单元[1]和('O','N')中的单元[2]和('O','N')中的单元[3]和('O','N')中的单元[4]和('O','N')中的单元[5]和('O','N')中的单元[6]和('O','N')中的单元[7]和('O','N')中的单元[8]:


正如@PengyuCHEN所说,
您的
tie()
函数仅在其他两次检查后调用。。所以如果你重新安排,你会发现你的问题解决了。因此,这应该是可行的:

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.')
随机导入
指令=“”
每个玩家轮流在以下网格上放置一个棋子:
1 | 2 | 3
-----------
4 | 5 | 6
-----------
7 | 8 | 9
在提示时输入一个值。连续第1到第3个棋子获胜“
形式='''
\t |%s |%s |%s|
\t-------------
\t |%s |%s |%s|
\t-------------
\t |%s |%s |%s|
'''
单元格=['1','2','3','4','5','6','7','8','9']
def win(单元格、字符):
返回单元格[0]==单元格[1]==单元格[2]==字符或\
单元格[3]==单元格[4]==单元格[5]==字符或\
单元格[6]==单元格[7]==单元格[8]==字符或\
单元格[0]==单元格[3]==单元格[6]==字符或\
单元格[1]==单元格[4]==单元格[7]==字符或\
单元格[2]==单元格[5]==单元格[8]==字符
def tie(电池):
返回全部(单元格中c的c输入('O','X'))
def get_输入(播放器):
尽管如此:
m=输入('\n%s,选择一个数字(1-9)来放置您的peice:“%player”)
尝试:
返回整数(m)
除基本例外情况外:
打印“无效输入”
持续
board=表格%元组(单元格)
打印('\tTIC TA