Get the children sorting figured out
This commit is contained in:
parent
a29e9bd8cf
commit
878d8f479c
1 changed files with 4 additions and 8 deletions
12
tictactoe.py
12
tictactoe.py
|
@ -33,7 +33,7 @@ class Board:
|
||||||
@property
|
@property
|
||||||
def full(self):
|
def full(self):
|
||||||
return self.num_x + self.num_o == BOARD_SIZE
|
return self.num_x + self.num_o == BOARD_SIZE
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_o_turn(self):
|
def is_o_turn(self):
|
||||||
x_ahead_one = (self.num_x == self.num_o + 1)
|
x_ahead_one = (self.num_x == self.num_o + 1)
|
||||||
|
@ -82,7 +82,7 @@ class Board:
|
||||||
result = __check(diag)
|
result = __check(diag)
|
||||||
if result:
|
if result:
|
||||||
return result
|
return result
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -96,9 +96,6 @@ class Board:
|
||||||
Minimax algorithm, implemented recursively, to evaluate board state and
|
Minimax algorithm, implemented recursively, to evaluate board state and
|
||||||
make a move.
|
make a move.
|
||||||
'''
|
'''
|
||||||
if self.score:
|
|
||||||
return self.score
|
|
||||||
|
|
||||||
score = None
|
score = None
|
||||||
winner = self.winner
|
winner = self.winner
|
||||||
if winner == Board.O:
|
if winner == Board.O:
|
||||||
|
@ -110,7 +107,7 @@ class Board:
|
||||||
else:
|
else:
|
||||||
minmax = max if self.is_o_turn else min
|
minmax = max if self.is_o_turn else min
|
||||||
score = minmax([c.evaluate() for c in self.children])
|
score = minmax([c.evaluate() for c in self.children])
|
||||||
self._children.sort(key=lambda b: b.score, reverse=self.is_o_turn)
|
self._children.sort(key=lambda b: b.score, reverse=True)
|
||||||
|
|
||||||
self.score = score
|
self.score = score
|
||||||
return self.score
|
return self.score
|
||||||
|
@ -125,8 +122,7 @@ class Board:
|
||||||
yield Board(b)
|
yield Board(b)
|
||||||
|
|
||||||
def move(self):
|
def move(self):
|
||||||
for c in self.children:
|
self.evaluate()
|
||||||
c.evaluate()
|
|
||||||
try:
|
try:
|
||||||
return self.children[0]
|
return self.children[0]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue