Что не так с моей функцией computer_move?
TIE = "Tie" O = 'O' X = 'X' EMPTY = " " winner = 0 def instructions(): print (''' 0 : 1 : 2 ---------- 3 : 4 : 5 ---------- 6 : 7 : 8 ''') def who_goes_first(): answer = None while answer not in ('y','n'): answer = input ('You want to go first? "y" or "n".') if answer == 'y': human = X computer = O elif answer == 'n': human = O computer = X return human, computer, answer def empty_board(): board = [] for square in range (9): board.append(EMPTY) return board def display_board(board): print (board[0],':' ,board[1],':', board[2]) print ('----------') print (board[3], ':', board[4], ':', board[5]) print ('----------') print (board[6], ':', board[7], ':', board[8]) def legal_move(board): legal = [] unlegal = [] for square in range(9): if board[square] == EMPTY: legal.append(square) elif board[square] != EMPTY: unlegal.append(square) return legal, unlegal def human_move(human, board, legal): move = None print ('Moves you are allowed:', legal) while move not in legal: move = int(input ('Enter your move:')) return move def computer_move (human, computer, board, unlegal): way_to_win = [(0,1,2), (3,4,5), (6,7,8), (0,3,6), (1,4,7), (2,5,8), (0,4,8), (2,4,6)] priority = [4, 0, 8, 6, 2, 1, 3, 7, 5] for number in unlegal: if number in priority: priority.remove(number) for row in way_to_win: if board[row[0]] == board[row[1]] == computer and board[row[2]] == EMPTY: move = row[2] elif board[row[1]] == board[row[2]] == computer and board[row[0]] == EMPTY: move = row[0] elif board[row[0]] == board[row[2]] ==computer and board[row[1]] == EMPTY: move = row[1] if board[row[0]] == board[row[1]] == human and board[row[2]] == EMPTY: move = row[2] elif board[row[1]] == board[row[2]] == human and board[row[0]] == EMPTY: move = row[0] elif board[row[0]] == board[row[2]] == human and board[row[1]] == EMPTY: move = row[1] else: move = priority[0] break return move def switch_turn(board, answer, computer, human): human_count = 0 computer_count = 0 for square in board: if square == computer: computer_count +=1 elif square == human: human_count += 1 if answer == 'y' and human_count == computer_count: human_turn = 1 elif answer == 'y' and human_count > computer_count: human_turn = 0 elif answer == 'n' and human_count == computer_count: human_turn = 0 elif answer == 'n' and computer_count > human_count: human_turn = 1 return human_turn def return_board(human, computer, board, move): if human_turn == 1: board[move] = human elif human_turn == 0: board[move] = computer return board def outcome(board, human, computer): global winner way_to_win = [(0,1,2), (3,4,5), (6,7,8), (0,3,6), (1,4,7), (2,5,8), (0,4,8), (2,4,6)] for row in way_to_win: if board[row[0]] == board[row[1]] == board[row[2]] == human: winner = human elif board[row[0]] == board[row[1]] == board[row[2]] == computer: winner = computer elif EMPTY not in board: winner = TIE return winner ### MAIN PROGRAM instructions() human, computer, answer = who_goes_first() board = empty_board() display_board(board) while winner == 0: human_turn = switch_turn(board, answer, computer, human) legal,unlegal = legal_move(board) if human_turn == 1: move = human_move(human, board, legal) print ('\n***********\n') elif human_turn ==0: move = computer_move (human, computer, board, unlegal) print ('\n***********\n') board = return_board(human, computer, board,move) display_board(board) winner = outcome(board, human, computer) if winner == TIE: print ('It\'s a draw') if winner in (human, computer): print ('{0} won.'.format(winner))
Что я уже пробовал:
Я пытаюсь сделать программу "крестики-нолики". Я пытаюсь заставить компьютер блокировать, если игрок собирается выиграть, или продолжать, если он собирается выиграть,но его ход неустойчив. Что не так с моей логикой?