-
Notifications
You must be signed in to change notification settings - Fork 0
/
chessPlayer_tree.py
74 lines (67 loc) · 1.71 KB
/
chessPlayer_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
from chessPlayer_chess import *
class queue(object):
def __init__(self):
self.store=[]
def push(self,val):
self.store=self.store+[val]
def pop(self):
temp=self.store[0]
self.store=self.store[1:]
return temp
def printQ(self):
print self.store
return 0
def empty(self):
if self.store:
return True
else:
return False
class treenode:
def __init__(self, eval_score, move_list, board):
self.eval = eval_score
self.Successor = []
self.board = board
#Move needed to get to this state: [init_pos, final_pos pairs]
self.move = move_list
self.LegalMoves = []
def addMove(self, init_fin_pos_pair):
self.move = self.move + init_fin_pos_pair
return True
def getLegalMoves(self,player):
out = []
my_pos = GetPlayerPositions(self.board,player)
for i in my_pos:
i_moves = GetPieceLegalMoves(self.board,i)
for j in i_moves:
out = out + [[i,j]]
self.LegalMoves = out
return out
def AddSuccessor(self,treenode):
self.Successor = self.Successor + [treenode]
return True
def Print_DepthFirst(self, depth):
#depth starts with 0: use print function by always passing in a 0 to it
print depth * ' ' + str(self.eval)
for i in range (0,len(self.Successor)):
self.Successor[i].Print_DepthFirst(depth+1)
return True
def Get_LevelOrder(self):
out = []
Q = queue()
Q.push(self)
while len(Q.store)!=0:
node = Q.pop()
out.append(node.eval)
for i in node.Successor:
Q.push(i)
return out
# x=treenode(30)
# y=treenode(50)
# y1=treenode(14)
# z=treenode(60)
# y.AddSuccessor(y1)
# x.AddSuccessor(y)
# x.AddSuccessor(z)
# print x.Successor
# x.Print_DepthFirst(0)
# print x.Get_LevelOrder()