def ExpandNode(root, depth, player): # print depth,root.store[0],"depth of root" if depth == 1: moveSet = generateCandidateMoves(root.store[0], player) for i in range(0, len(moveSet)): pseudoBoard = root.store[0][0:64] Move(pseudoBoard, moveSet[i][0], moveSet[i][1]) tempRoot = tree.tree(pseudoBoard) root.AddSuccessor(tempRoot) root.store[0] = [] # print root.store[1] for i in range(0, len(root.store[1])): # print root.store[0],"Printing Root layer" if player == 10: root.store[0] = root.store[0] + processNode( root.store[1][i], depth - 1, True) else: root.store[0] = root.store[0] + processNode( root.store[1][i], depth - 1, False) # print root.store[0], depth,"list of moves + depth" return root.store[0] moveSet = generateCandidateMoves(root.store[0], player) for i in range(0, len(moveSet)): pseudoBoard = root.store[0][0:64] Move(pseudoBoard, moveSet[i][0], moveSet[i][1]) tempRoot = tree.tree(pseudoBoard) root.AddSuccessor(tempRoot) if player / 10 == 1: ExpandNode(root.store[1][i], depth - 1, 20) else: ExpandNode(root.store[1][i], depth - 1, 10) ''' for i in range(0,len(root.store[1])): if player/10 ==1: extend(root.store[1][i],depth-1,20) else: extend(root.store[1][i],depth-1,10) ''' for i in range(0, len(root.store[1])): root.store[1][i] = root.store[1][i].store[0] # print root.store[1],"All the positions" return root.store[1]
def genMoveTree(board, player, depth, isMax, alpha, beta, scoreTree): if (depth == 0): state = evalBoard(board, player) return state if (player == 10): opponent= 20 elif(player == 20): opponent = 10 else: return False if (isMax): bestMove = -9999 L = GetPlayerPositions(board, player) for i in L: legal = GetPieceLegalMoves(board, i) for j in legal: #we only want to keep track of this record if the depth is not too high, to save memory, which is why #tree is only added to if depth >=2 tmp= tree(-1) boardCopy = list(board) boardCopy[j] = boardCopy[i] boardCopy[i] = 0 bestMove = max([bestMove, genMoveTree(boardCopy, player, depth - 1, False, alpha, beta, tmp)]) if (depth >=2): tmp.store[0] = bestMove scoreTree.AddSuccessor(copy.copy(tmp)) alpha = max([alpha, bestMove]) if (beta <= alpha): return bestMove return bestMove else: bestMove = 9999 L = GetPlayerPositions(board, opponent) for i in L: legal = GetPieceLegalMoves(board, i) for j in legal: tmp = tree(-1) boardCopy = list(board) boardCopy[j] = boardCopy[i] boardCopy[i] = 0 bestMove = min([bestMove, genMoveTree(boardCopy, player, depth-1, True, alpha, beta, tmp)]) if (depth >=2): tmp.store[0] = bestMove scoreTree.AddSuccessor(copy.copy(tmp)) beta = min([beta, bestMove]) if (beta <= alpha): return bestMove return bestMove
def stratLookAhead(board, depth, player): outMoves = [] values = [] Root = tree.tree(list(board)) moveSet = generateCandidateMoves(board, player) Root = extend(Root, depth, player) #if flag is true, player is white if player / 10 == 1: flag = True else: flag = False if len(Root.store[1]) != 0: for i in range(0, len(Root.store[1])): temp = processNode(Root.store[1][i], depth - 1, flag) outMoves = outMoves + [[moveSet[i], temp.store[0]]] values = values + [temp.store[0]] Root.store[1][i] = temp if flag == True: Root.store[0] = max(values) else: Root.store[0] = min(values) # print Root.Get_LevelOrder() # raw_input() return [outMoves, Root.Get_LevelOrder()]
def ConvertToTree(self): if self.store[2] != None: return [False, None] outTree = t.tree(self) outTree = self.ConvertRoot(outTree) outTree.CleanToInt() return [True, outTree]
def ConvertRoot(self, outTree): extender = t.tree(self.store[1]) if self.store[1] != None: outTree.AddSuccessor(extender) outTree.store[1][0].store[0].ConvertRoot(outTree.store[1][0]) i = 1 while extender.store[0] != None: extender = t.tree(extender.store[0].store[2]) if extender.store[0] != None: outTree.AddSuccessor(extender) outTree.store[1][i].store[0].ConvertRoot(outTree.store[1][i]) i = i + 1 return outTree
def stratSafeLookAhead(board, depth, player): outMoves = [] Root = tree.tree(list(board)) moveSet = safeMoves(board, player) Root = safeExtend(Root, depth, player) #if flag is true, player is white if player / 10 == 1: flag = True else: flag = False if len(Root.store[1]) != 0: for i in range(0, len(Root.store[1])): # print "depth going in",depth temp = processNode(Root.store[1][i], depth - 1) # print "First successor" # raw_input() if len(temp) != 0: if flag == True: # print temp,"The nodal values" Root.store[1][i] = min(temp) # print min(temp), "min of moves(white) want the worst white case" # raw_input() else: Root.store[1][i] = max(temp) # print max(temp),"max of moves (black) want the worst black case" # raw_input() else: Root.store[1][i] = None # print Root.store[1],"The successors of Root" # raw_input() WorstCases = Root.store[1] if len(temp) != 0: for w in range(0, len(WorstCases)): outMoves = outMoves + [[moveSet[w], WorstCases[w]]] ''' #for white want the best worst case if flag == True: maxIndex = WorstCases.index(max(WorstCases)) move = moveSet[maxIndex] print maxIndex #for black want best worst case(black is negative) else: minIndex = WorstCases.index(min(WorstCases)) move = moveSet[minIndex] print minIndex ''' return outMoves
def stratLeanLookAhead(board,depth,player): Root = tree.tree(list(board)) moveSet = generateCandidateMoves(board,player) print moveSet, "mmoves set" Pathways = ExpandNode(Root,depth,player) #if flag is true, player is white # print Pathways,"lean pathways" # print "length of pathways",len(Pathways),len(moveSet) # raw_input() if player/10 == 1: flag=True else: flag = False #player is white if flag ==True: for i in range(0,len(moveSet)): Pathways[i]= min(Pathways[i]) # print min(temp), "min of moves(white) want the worst white case" # raw_input() else:#player is black for i in range(0,len(moveSet)): Pathways[i]= max(Pathways[i]) # # print max(temp),"max of moves (black) want the worst black case" # raw_input() '''if len(Root.store[1])!=0: for i in range(0,len(Root.store[1])): # print "depth going in",depth temp = processNode(Root.store[1][i],depth-1) # print "First successor" # raw_input() if len(temp) != 0: if flag ==True: Root.store[1][i]= min(temp) # print min(temp), "min of moves(white) want the worst white case" # raw_input() else: Root.store[1][i]= max(temp) # print max(temp),"max of moves (black) want the worst black case" # raw_input() else: Root.store[1][i] = None
def stratLookAhead(board,depth,player): Root = tree.tree(list(board)) moveSet = generateCandidateMoves(board,player) Root = extend(Root,depth,player) if len(Root.store[1])!=0: for i in range(1,len(Root.store[1])): temp = processNode(Root.store[1][i],depth) if len(temp) != 0: Root.store[1][i] =sum(temp)/len(temp) Sums = Root.store[1] minIndex = Sums.index(min(Sums)) maxIndex = Sums.index(max(Sums)) if player/10 == 1: move = moveSet[maxIndex] else: move = moveSet[minIndex] return move
def extend(root,depth,player): if depth == 0: return root pseudoBoard = root.store[0][0:64] moveSet = generateCandidateMoves(root.store[0],player) for i in moveSet: pseudoBoard = root.store[0][0:64] Move(pseudoBoard,i[0],i[1]) tempRoot =tree.tree(pseudoBoard) root.AddSuccessor(tempRoot) for i in range(1,len(root.store[1])): if player/10 ==1: extend(root.store[1][i],depth-1,20) else: extend(root.store[1][i],depth-1,10) return root
def safeExtend(root, depth, player): # print depth,"depth of root" if depth == 0: return root pseudoBoard = list(root.store[0]) moveSet = safeMoves(root.store[0], player) for i in moveSet: pseudoBoard = root.store[0][0:64] Move(pseudoBoard, i[0], i[1]) tempRoot = tree.tree(pseudoBoard) root.AddSuccessor(tempRoot) for i in range(0, len(root.store[1])): if player / 10 == 1: safeExtend(root.store[1][i], depth - 1, 20) else: safeExtend(root.store[1][i], depth - 1, 10) return root
from chesslib import * from chessPlayer_tree import tree a = initBoard(None) root = tree(a) # print root.store looks = extend(root,2,10) basket = processNode(looks,2) weight = sum(basket)/len(basket) print weight
def chessPlayer(board, player): if (len(board)!=64 or (player <> 10 and player <> 20)): return [False, [], [], []] for i in range(0, len(board)): if (not(isBlack(board, i) or isWhite(board, i) or (board[i] ==0))): return [False, [], [], []] candidates = [] opponent = getOpponent(player) root = tree(-1) #an arbitrary value. Made to be the head of the eval tree so all board moves can be represented as one tree kingPos = GetPos(board, player + 5) check = IsPositionUnderThreat(board, kingPos, player) if (check): #try to get out of check to not lose L = GetPlayerPositions(board, player) for i in L: legal = GetPieceLegalMoves(board, i) for j in legal: boardCopy = list(board) boardCopy[j] = boardCopy[i] boardCopy[i] = 0 kingPos = GetPos(boardCopy, player + 5) threat = IsPositionUnderThreat(boardCopy, kingPos, player) if (not threat): score = evalBoard(boardCopy, player) if(IsPositionUnderThreat(boardCopy, j, player)): score = score - (pieceEval(boardCopy[j], player)) #this is so we don't sacrifice a more expensive piece candidates = candidates + [[[i, j], score]] else: L = GetPlayerPositions(board, player) for i in L: legal = GetPieceLegalMoves(board, i) for j in legal: tmp = tree(-1) boardCopy = list(board) boardCopy[j] = boardCopy[i] boardCopy[i] = 0 #we set maximizing player to false here because we are maximizing the score in this section currently. Need to moveScore = genMoveTree(boardCopy, player, 2, False, -10000, 10000, tmp) tmp.store[0] = moveScore root.AddSuccessor(copy.copy(tmp)) #This tree is to keep a record of all the scores from the board states evaluated by genMove Tree candidates = candidates + [[[i, j], moveScore]] if (candidates == []): return [False, [], [], []] #Move cannot put you in check: for i in range(0, len(candidates)): fromPos = candidates[i][0][0] toPos = candidates[i][0][1] boardCopy = list(board) boardCopy[toPos] = boardCopy[fromPos] boardCopy[fromPos] = 0 kingPos = GetPos(boardCopy, player + 5) if (IsPositionUnderThreat(boardCopy, kingPos, player)): candidates[i][1] = -100000 cop = list(candidates) candidates = [] for i in cop: if i[1] != -100000: candidates = candidates + [i] bestMove = candidates[0] rest = candidates[1:] for k in rest: if k[1] > bestMove[1]: bestMove = k #in the case where multiple pieces have the same score, we don't want to choose randomly. Choose the spots closer to the centre, and prioritieze control there #This also tests for if we are threatening opponents and whether we are going to a threatening spot repeats = [] for i in candidates: if i[1] == bestMove[1]: repeats = repeats + [i] for j in repeats: boardCopy = list(board) boardCopy[j[0][1]] = boardCopy[j[0][0]] boardCopy[j[0][0]] = 0 if (IsPositionUnderThreat(boardCopy, j[0][1], player)): j[1] = j[1] - pieceEval(board[j[0][0]], player) #minus points if the place is under threat if(belongsTo(board, j[0][1]) == opponent): j[1] = j[1] - pieceEval(board[j[0][0]], player) #add points (sinse opponent piece is negative) if we capture a piece here if (j[0][1] == 27 or j[0][1] == 28 or j[0][1] == 35 or j[0][1] == 36): j[1] = j[1] + 1 elif(j[0][1] == 26 or j[0][1] == 29 or j[0][1] == 34 or j[0][1] == 37): j[1]= j[1] + 0.5 bestMove = repeats[0] rest = repeats[1:] for k in rest: if k[1] > bestMove[1]: bestMove = k final = [] for i in repeats: if i[1] == bestMove[1]: final = final + [i] rand = randint(0, len(final) - 1) #this is to avoid repetitive results, as taking just the first move with the best score results in no board presence, # but rather, only moves the pieces with a lower number bestMove = final[rand] evalTree = root.Get_LevelOrder() if (evalTree == [-1]): #this means nothing was added to the evalTree evalTree = None return [True, bestMove[0], candidates, evalTree]
def stratLeanLookAhead(board, depth, player): Root = tree.tree(list(board)) moveSet = generateCandidateMoves(board, player) # print moveSet, "mmoves set" Pathways = ExpandNode(Root, depth, player) #if flag is true, player is white # print Pathways,"lean pathways" # print "length of pathways",len(Pathways),len(moveSet) # raw_input() if player / 10 == 1: flag = True else: flag = False #player is white if flag == True: for i in range(0, len(moveSet)): Pathways[i] = min(Pathways[i]) # print min(temp), "min of moves(white) want the worst white case" # raw_input() else: #player is black for i in range(0, len(moveSet)): Pathways[i] = max(Pathways[i]) # # print max(temp),"max of moves (black) want the worst black case" # raw_input() '''if len(Root.store[1])!=0: for i in range(0,len(Root.store[1])): # print "depth going in",depth temp = processNode(Root.store[1][i],depth-1) # print "First successor" # raw_input() if len(temp) != 0: if flag ==True: Root.store[1][i]= min(temp) # print min(temp), "min of moves(white) want the worst white case" # raw_input() else: Root.store[1][i]= max(temp) # print max(temp),"max of moves (black) want the worst black case" # raw_input() else: Root.store[1][i] = None print Root.store[1],"The successors of Root" # raw_input() ''' WorstCases = Pathways # print"Pathways",Pathways if len(Pathways) != 0: #for white want the best worst case if flag == True: maxIndex = WorstCases.index(max(WorstCases)) move = moveSet[maxIndex] #for black want best worst case(black is negative) else: minIndex = WorstCases.index(min(WorstCases)) move = moveSet[minIndex] return move