All right, next step...more BoardNode properties, as mentioned in class (and you can drop the .parents, since we don't need it).
Here's the new BoardNode's __init__():
class BoardNode:
def __init__(self,layout):
self.layout
= layout
self.endState = None # if this is a terminal board, endState == 'x' or 'o' for
wins, of 'd' for draw, else None if this board is not final
self.children = [] # all layouts that can be reached with a single move
self.best_move # cell position
(0-8) of the best move from this layout, or -1 if this is a final layout
self.moves_to_end # how many
moves until the end of the game, if played perfectly. 0 if this is a final
layout
self.final_state #
expected final state ('x' if 'x' wins, 'o' if 'o' wins, else 'd' for a draw)
You may add any other properties that you feel you need.
Call your program file: ttt3.py
Your program will be called with a single command-line argument, which will be a 9-character board layout: for instance: $ ./ttt3.py x_ox_o___
Your program should print the following answers (if the layout above is given):
move=6
best move is lower-left
x wins in 1 move
The first output line is formatted for computer input and the other two are for human eyes.
Best move choice:
If this layout is a final board, there's no choice
else, if any of this board's children's final_state is a win for you, choose moving to the child with the smallest moves_to_end. If more than one, choose randomly.
else, if any child's final_state is a draw, choose one of them randomly (they will all have the same moves_to_end)
else, choose the child with the largest moves_to_end. If more than one, choose randomly.
Timing: You are allowed to cheat if given an empty board and you have to make the first move (any first move will lead to a draw in 9 moves, if played perfectly by both sides). After that, however, your program should deliver an answer in under 2 seconds.
Testing: Now you can really play with tictactoe against your creation. It should never lose. Make it so.