2017-07-30 79 views
0

我创建了一个井字棋方案2级的球员,但是当条件满足时,蟒蛇就不会打破:Python的井字棋 - if语句查询

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

中。在将if条件放入while和game_end循环之后,当我为行和列输入值时,游戏就会退出。 –

+1

@JavKoh那是因为一些条件是错误的。看看每种情况下的最后一部分 - 当它们实际上应该是其他东西时,其中一些是“移动[0]”。您应该重构此代码以减少重复性,因为重复自己总是容易出错。 –

+0

减少重复次数的一种简单方法(尽管你可以做得更多)是使用操作符链接。像'if moves [0] ==移动[1] ==的行'移动[2]!='':'几乎与你现有的代码完全等价(中间的值与每边的值和结果进行比较都和'和'结合在一起)。 – Blckknght