2017-07-30 2 views
0

は、私が2人のための三目並べのプログラムを作成したが、条件が満たされた場合、Pythonは壊れていないならば:Pythonの三目並べ - ステートメントのクエリ

def display_board(moves): 
    print (' ' + moves[0] + ' | ' + moves[1] + ' | ' + moves[2]) 
    print ('---- ---- ----') 
    print (' ' + moves[3] + ' | ' + moves[4] + ' | ' + moves[5]) 
    print ('---- ---- ----') 
    print (' ' + moves[6] + ' | ' + moves[7] + ' | ' + moves[8]) 

moves = ('   ') 

turn = 1 

player = 'X' 

def play_move(moves, row, col, turn): 
    if turn % 2 == 1: 
     player = 'X' 
    else: 
     player = 'O' 
    pos = (3 * (row - 1) + col) - 1 
    moves = moves[:pos] + player + moves[pos + 1:] 
    return moves 

game_end = False 

if moves[0] == moves[1] and moves[1] == moves[2] and moves[0] != ' ': 
    game_end = True 
if moves[3] == moves[4] and moves[4] == moves[5] and moves[3] != ' ': 
    game_end = True 
if moves[6] == moves[7] and moves[7] == moves[8] and moves[6] != ' ': 
    game_end = True  
if moves[0] == moves[3] and moves[3] == moves[6] and moves[0] != ' ': 
    game_end = True 
if moves[1] == moves[4] and moves[4] == moves[7] and moves[0] != ' ': 
    game_end = True 
if moves[2] == moves[5] and moves[5] == moves[8] and moves[0] != ' ': 
    game_end = True 
if moves[0] == moves[4] and moves[4] == moves[8] and moves[0] != ' ': 
    game_end = True  
if moves[2] == moves[4] and moves[4] == moves[6] and moves[2] != ' ': 
    game_end = True 

while not game_end: 
    display_board(moves) 
    row = int(input('Please enter the row number: ')) 
    col = int(input('Please enter the column number: ')) 
    moves = play_move(moves, row, col, turn) 
    turn += 1 
    if game_end == True: 
     break 

Link to code in a live environment

+0

を、実際の例をリンクし、削除キッチン。 –

+0

すべての 'if'文を' while'ループの中に入れます。 – LuCima

答えて

0

です古典的な無限ループ。あなたは、ゲームが一度だけ終了したかどうかをチェックしています:ゲームを開始する前に!

game_end = False 

if moves[0] == moves[1] and moves[1] == moves[2] and moves[0] != ' ': 
    game_end = True 
if moves[3] == moves[4] and moves[4] == moves[5] and moves[3] != ' ': 
    game_end = True 
if moves[6] == moves[7] and moves[7] == moves[8] and moves[6] != ' ': 
    game_end = True  
if moves[0] == moves[3] and moves[3] == moves[6] and moves[0] != ' ': 
    game_end = True 
if moves[1] == moves[4] and moves[4] == moves[7] and moves[0] != ' ': 
    game_end = True 
if moves[2] == moves[5] and moves[5] == moves[8] and moves[0] != ' ': 
    game_end = True 
if moves[0] == moves[4] and moves[4] == moves[8] and moves[0] != ' ': 
    game_end = True  
if moves[2] == moves[4] and moves[4] == moves[6] and moves[2] != ' ': 
    game_end = True 


while not game_end: 
    display_board(moves) 
    row = int(input('Please enter the row number: ')) 
    col = int(input('Please enter the column number: ')) 
    moves = play_move(moves, row, col, turn) 
    turn += 1 
    if game_end == True: 
     break 

while not game_endでループを開始した後、あなたは変数game_end内部ループ変更されていないので、それは、ステップすることはありません:ポストにコードを編集

game_end = False 
while not game_end: 
    display_board(moves) 
    row = int(input('Please enter the row number: ')) 
    col = int(input('Please enter the column number: ')) 
    moves = play_move(moves, row, col, turn) 
    turn += 1 

    game_end = False 
    if moves[0] == moves[1] and moves[1] == moves[2] and moves[0] != ' ': 
     game_end = True 
    if moves[3] == moves[4] and moves[4] == moves[5] and moves[3] != ' ': 
     game_end = True 
    if moves[6] == moves[7] and moves[7] == moves[8] and moves[6] != ' ': 
     game_end = True  
    if moves[0] == moves[3] and moves[3] == moves[6] and moves[0] != ' ': 
     game_end = True 
    if moves[1] == moves[4] and moves[4] == moves[7] and moves[1] != ' ': 
     game_end = True 
    if moves[2] == moves[5] and moves[5] == moves[8] and moves[2] != ' ': 
     game_end = True 
    if moves[0] == moves[4] and moves[4] == moves[8] and moves[0] != ' ': 
     game_end = True  
    if moves[2] == moves[4] and moves[4] == moves[6] and moves[2] != ' ': 
     game_end = True 
+0

がハングします。 while条件をgame_endではなくwhileループの中に入れた後、行と列の値を入れると、ゲームは終了します。 –

+1

@ JavKohそれは条件のいくつかが間違っているからです。それぞれの条件の最後の部分を見てください。実際に何か他のものになるべきときには 'moves [0]'があります。反復回数が少なくなるように、このコードをリファクタリングする必要があります。繰り返しを繰り返すと、エラーが発生しやすくなります。 –

+0

繰り返しの量を減らす簡単な方法の1つは、演算子の連鎖を使用することです。 'move [0] == moves [1] == moves [2]!= '':'のような行は、あなたの既存のコードとほとんど同じです(中間の値は各側の値と比較され、結果すべて 'と'と組み合わせてあります)。 – Blckknght

関連する問題