Python tic-tac-toe - if语句查询

时间:2017-07-30 08:18:50

标签: python

我为2名玩家创建了一个井字游戏程序但是当满足条件时,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

1 个答案:

答案 0 :(得分: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