Reputation: 135
I am trying to make tic tac toe using minimax algorithm. I am referring to the following code. But I am not understanding the use of [0]. Please explain the following line:
value = self.move(x,y).minimax(not player)[0]
taking the reference of the code given below. And also tell the use of returning "None" along with +1 or -1 or 0. Please do not negative vote my question or I will be blocked.
def minimax(self, player):
if self.won():
if player:
return (-1,None)
else:
return (+1,None)
elif self.tied():
return (0,None)
elif player:
best = -2,0
for x,y in self.fields:
if self.fields[x,y]==self.empty:
value = self.move(x,y).minimax(not player)[0]
if value>best[0]:
best = (value,(x,y))
return best
else:
best = (+2,None)
for x,y in self.fields:
if self.fields[x,y]==self.empty:
value = self.move(x,y).minimax(not player)[0]
if value<best[0]:
best = (value,(x,y))
return best
Upvotes: 1
Views: 242
Reputation: 49920
minimax
returns a tuple; the statement in question is calling minimax
, taking the first element of the returned tuple (the [0]
bit) and assigning that to best
.
Upvotes: 2
Reputation: 27333
But I am not understanding the use of [0]
The function returns a tuple of (value,(x,y)), so accessing the 0th element of that will give you value
.
The function returns None
in the value when the game is over, since no new move is made. The value
will be who won (with 0 meaning they tied).
Upvotes: 1