在我不断寻求完成我的tic tac toe游戏时,我尝试编写一个功能来检查获胜者。游戏看起来像这样: 董事会由一系列清单组成:
lst = ['1','2','3','4','5','6','7','8','9']
def board():
print (lst[0:3])
print (lst[3:6])
print (lst[6:])
['1', '2', '3']
['4', '5', '6']
['7', '8', '9']
then this not so elegant function to check for a winner:
def conclusion():
if all('o' == item for item in (lst[0],lst[1],lst[2])):
print('Player 2 wins!')
elif all('o' == item for item in (lst[3],lst[4],lst[5])):
print('Player 2 wins!')
elif all('o' == item for item in (lst[6],lst[7],lst[8])):
print('Player 2 wins')
elif all('o' == item for item in (lst[0],lst[3],lst[6])):
print('Player 2 wins')
elif all('o' == item for item in (lst[1],lst[4],lst[7])):
print('Player 2 wins')
elif all('o' == item for item in (lst[3],lst[6],lst[9])):
print('Player 2 wins')
elif all('o' == item for item in (lst[0],lst[4],lst[8])):
print('Player 2 wins')
elif all('o' == item for item in (lst[2],lst[4],lst[6])):
print('Player 2 wins')
elif all('x' == item for item in (lst[0],lst[1],lst[2])):
print('Player 1 wins!')
elif all('x' == item for item in (lst[3],lst[4],lst[5])):
print('Player 1 wins!')
elif all('x' == item for item in (lst[6],lst[7],lst[8])):
print('Player 1 wins')
elif all('x' == item for item in (lst[0],lst[3],lst[6])):
print('Player 1 wins')
elif all('x' == item for item in (lst[1],lst[4],lst[7])):
print('Player 1 wins')
elif all('x' == item for item in (lst[3],lst[6],lst[9])):
print('Player 1 wins')
elif all('x' == item for item in (lst[0],lst[4],lst[8])):
print('Player 1 wins')
elif all('x' == item for item in (lst[2],lst[4],lst[6])):
print('Player 1 wins')
else:
pass
然后是游戏玩法的这些功能:
def move2():
conclusion()
move2=(input('Player 2: Type a number!'))
for x in lst:
if move2 == x:
lst[int(move2)-1] = 'o'
board()
move()
elif move2.isdigit() and move2 not in lst:
print('Not that number dipshit!')
break
board()
move2()
elif not move2.isdigit():
print('Not that number dipshit!')
break
board()
move2()
def move():
conclucion()
move1=(input('Player 1: Type a number!'))
for x in lst:
if move1 == x:
lst[int(move1)-1] = 'x'
board()
move2()
elif move1.isdigit() and move1 not in lst:
print('Not that number dipshit!')
board()
move()
break
elif not move1.isdigit():
print('Not that number dipshit!')
board()
move()
break
问题是当我尝试运行它时,我不断收到此错误:
<ipython-input-27-8688e8182de8> in move()
1 def move():
----> 2 conclucion()
3 move1=(input('Player 1: Type a number!'))
4 for x in lst:
5 if move1 == x:
NameError: name 'conclucion' is not defined
有什么想法吗?精简这一点的建议也非常受欢迎。
答案 0 :(得分:3)
你拼写了#34;结论&#34;错误,因为&#34; conclucion&#34;。