diff --git a/main.py b/main.py index 6fea9bd..5aee522 100644 --- a/main.py +++ b/main.py @@ -258,14 +258,25 @@ def rot_hash_board(board): class Node(object): - def __init__(self, board): + def __init__(self, board, move, parent_node): + self.parent = parent_node self.board = board - self.moves = gen_moves(board) + self.move = move + + if self.parent: + self.moves = self.parent.moves + [move] + else: + if move: + self.moves = [move] + else: + self.moves = [] self.visited = False - self.children = [] + self.complete = False + + self.children = gen_moves(self.board) -def main(): +def iterative_main(): board = deepcopy(STARTING_BOARD) moves_this_game = [] while True: @@ -282,9 +293,30 @@ def main(): this_move = moves_this_game[-1] board = undo_move(this_move, board) - this_move = moves[0] - moves_this_game.append(this_move) - board = play_move(this_move, board) + else: + this_move = moves[0] + moves_this_game.append(this_move) + board = play_move(this_move, board) + + +def main(): + root = Node(board=STARTING_BOARD, move=None, parent_node=None) + node = root + while True: + if len(node.moves) == 0: + pawns_left = count_pawns(node.board) + if pawns_left == 1: + print("WIN") + break + else: + # backtrack + node.visited = True + board = undo_move(node.move, node.board) + else: + # there is at least one move + move = moves[0] + new_board = play_move(move, node.board) + node = Node(new_board, move, node) if __name__ == "__main__":