This repository has been archived by the owner on Sep 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.py
147 lines (99 loc) · 3.79 KB
/
tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import gameboard
import game
import utils
import random
# Class Node of a Tree
class Node:
def __init__(self, id, board, piece, depth):
self.id = id
self.board = board
self.piece = piece
self.visited = False
self.depth = depth
self.edges = []
def set_depth(self, depth):
self.depth = depth
def get_depth(self):
return self.depth
def allEdges(self):
random.shuffle(self.edges)
return self.edges
def printEdges(self):
for edge in self.edges:
print((self.piece, edge.origin))
print("Depth:")
print(self.depth)
gameboard.display(edge.node.board)
print('-----------')
def isEmpty(self):
return len(self.edges) == 0
def printBoard(self):
return gameboard.display(self.board)
# Class Edge betweeen Nodes of a Tree
class Edge:
def __init__(self, origin, node):
self.origin = origin
self.node = node
# Class of a game Tree
class Tree:
def __init__(self):
self.nodes = []
def addNode(self, node):
self.nodes.append(node)
def addEdge(self, s, d, piece):
if s in self.nodes and d not in self.nodes:
e = Edge(piece, d)
d.set_depth(s.depth + 1)
s.edges.append(e)
self.nodes.append(d)
return 0
else:
return -1
def printAllEdges(self):
for n in self.nodes:
n.printEdges()
def addTree(self, s1, s2):
self.addEdge(s1, s2)
# This method will generate the possible boards for all possible moves of the
# pieces of the input player and stores them in a dictionary.
def nextPlayerBoardsGen(board, player):
m = {}
pieces = gameboard.getPieceCoords(board, player)
for piece in pieces:
new_boards = []
possible_moves = gameboard.calculateValidMoves(piece, board)
possible_moves = utils.removeDuplicates(possible_moves)
move = ()
for move in possible_moves:
new_board = game.make_move(piece, move, board)
new_boards.append(new_board)
m[(piece, move)] = list(new_boards)
return m
# This method will create the Game Tree, starting by the board stored in
# the initial node Init and generate the possible moves with the help of
# nextPlayerBoardsGen(board, player). For each move create there, it creates
# another tree recursively and alternating the player, in order to make the
# Min and Max layers
def createGameTree(board, player, depth, tree, init):
if (init is None):
init = Node(1, board, None, 1)
tree.addNode(init)
index = 2
while (depth > 0):
maps = nextPlayerBoardsGen(board, player)
new_boards = maps.values()
for list_per_piece in new_boards:
(piece, move) = list(filter(lambda x: maps[x] == list_per_piece, maps))[0]
random.shuffle(list_per_piece)
for b in list_per_piece:
node = Node(index,b, move, init.get_depth() + 1)
tree.addEdge(init, node, piece)
if player == 1:
tree = createGameTree(b, 2, depth - 1, tree, node)
else:
tree = createGameTree(b, 1, depth - 1, tree, node)
depth -= 1
index += 1
return tree