0
チックタックトーゲームを終了するための私の進行中の探求で、私は勝者をチェックする関数を書こうとしました。 ボードはリストのリストで構成されています:ゲームはこのようになります名前が定義されていないエラー
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
どのようなアイデアですか?これを合理化するための提案もまた大歓迎です。
def move(): 締め切り() 'それは打ち間違いですか?最後の 'c'を 's'に変更すればうまくいくはずです。 –
あなたの関数は '結論'ではなく '結論'と呼ばれています – schwobaseggl
おかげで恥ずかしかったです。しかし今、それは 'リストのインデックスが範囲外です'と言っている@Noah Christopher – Boris