我创建了一个井字棋方案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
编辑的代码进入后,联活生生的例子和删除闲聊。 –
将'if'语句置于'while'循环 – LuCima