# How to write minimax algorithm accurately?

I've made MiniMax function but it seems it doesn't work, It should return the optimal play in form [1,2] for example,

``````def minimax(board,player_to_play):
"""
Returns the optimal action for the current player on the board.
"""

available = actions(board) ##empty slots
to_move = None
if player_to_play == "X": #maxmizing
current_best = -100
for play in available:
temp_board = copy.deepcopy(board)
temp_board[play[0]][play[1]] = "X"
o_move = minimax(temp_board,"O")
score = utility(o_move)
if score > current_best :
to_move = play
else: #minmizing
current_best = 100
for play in available:
temp_board = copy.deepcopy(board)
temp_board[play[0]][play[1]] = "O"
x_move = minimax(temp_board,"X")
score = utility(x_move)
if score < current_best:
to_move = play
``````

where `utility()` return score of board

``````def utility(board):
"""
Returns 1 if X has won the game, -1 if O has won, 0 otherwise.
"""
if terminal(board):
if winner(board) == "X":
return 1
elif winner(board) == "O":
return -1
else:
return 0
else:
return 0
``````

Is that logically right or what I'm miss understanding?