solution 1 found
This commit is contained in:
parent
b61912aaed
commit
457e73c67f
47
main.py
47
main.py
@ -264,16 +264,33 @@ class Node(object):
|
|||||||
self.move = move
|
self.move = move
|
||||||
|
|
||||||
if self.parent:
|
if self.parent:
|
||||||
self.moves = self.parent.moves + [move]
|
self.prev_moves = self.parent.prev_moves + [move]
|
||||||
else:
|
else:
|
||||||
if move:
|
if move:
|
||||||
self.moves = [move]
|
self.prev_moves = [move]
|
||||||
else:
|
else:
|
||||||
self.moves = []
|
self.prev_moves = []
|
||||||
self.visited = False
|
self.visited = False
|
||||||
self.complete = False
|
self.complete = False
|
||||||
|
|
||||||
self.children = gen_moves(self.board)
|
self.moves = gen_moves(self.board)
|
||||||
|
self.children = {move: None for move in self.moves}
|
||||||
|
|
||||||
|
@property
|
||||||
|
def non_explored_moves(self):
|
||||||
|
rv = []
|
||||||
|
for move in self.children.keys():
|
||||||
|
node = self.get_node_from_move(move)
|
||||||
|
if not node.visited:
|
||||||
|
rv.append(move)
|
||||||
|
return rv
|
||||||
|
|
||||||
|
def get_node_from_move(self, move):
|
||||||
|
if self.children.get(move) is None:
|
||||||
|
self.children[move] = Node(play_move(move, deepcopy(self.board)), move, self)
|
||||||
|
return self.children[move]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def iterative_main():
|
def iterative_main():
|
||||||
@ -303,7 +320,7 @@ def main():
|
|||||||
root = Node(board=STARTING_BOARD, move=None, parent_node=None)
|
root = Node(board=STARTING_BOARD, move=None, parent_node=None)
|
||||||
node = root
|
node = root
|
||||||
while True:
|
while True:
|
||||||
if len(node.moves) == 0:
|
if len(node.non_explored_moves) == 0:
|
||||||
pawns_left = count_pawns(node.board)
|
pawns_left = count_pawns(node.board)
|
||||||
if pawns_left == 1:
|
if pawns_left == 1:
|
||||||
print("WIN")
|
print("WIN")
|
||||||
@ -311,13 +328,23 @@ def main():
|
|||||||
else:
|
else:
|
||||||
# backtrack
|
# backtrack
|
||||||
node.visited = True
|
node.visited = True
|
||||||
board = undo_move(node.move, node.board)
|
node = node.parent
|
||||||
else:
|
else:
|
||||||
# there is at least one move
|
# there is at least one move
|
||||||
move = moves[0]
|
move = node.non_explored_moves[0]
|
||||||
new_board = play_move(move, node.board)
|
node = node.get_node_from_move(move)
|
||||||
node = Node(new_board, move, node)
|
|
||||||
|
|
||||||
|
def play_moves():
|
||||||
|
board = STARTING_BOARD
|
||||||
|
moves = input("> ")
|
||||||
|
for move in moves.split(','):
|
||||||
|
move = move.strip()[1:-1]
|
||||||
|
print(move)
|
||||||
|
board = play_move(move, board)
|
||||||
|
print_board(board)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
# main()
|
||||||
|
play_moves()
|
Loading…
Reference in New Issue
Block a user