Recently, I used Python to do a chess program and the Air Max 2011 Womens Purple Black Grey code released on Nike Free Run 3 Women Github up. The less than 1,000 Air Jordan Outlet 597806-400 Nike LeBron X EXT QS Denim-Pink Outlet lines of code, used to achieve about 20% AI. In this article I will explain how this AI work, what to do every part of why it can work up that. You can read this article, or to download the code, look at the code being read. Although there are other files to see what AI dependent classes Nike Air Max may also be helpful, but all in AI part AI.py 579756 403 Purple White Nike Black Mamba 24 Kobe Sale file. AI AI portion of the total before making a decision referred to three different steps through. First, he found that all the rules allow moves (usually 20-30 at the start, then will be reduced to a few). Second, it generates a tree moves to subsequently determine the best decisions. Although the size of the tree with depth exponentially, Air Max 2011 Womens Grey Green but the depth of the tree can be arbitrary. Suppose every decision has an average of 20 optional moves, that depth of 1 corresponds to 20 moves, corresponding to a depth of 2 400 moves a depth of 3 corresponds to 8000 move. Finally, it traverses the tree, take x-step after the results of the best moves, x is the depth we have chosen tree. Later articles For simplicity, I'll assume that the tree is deeply 2. Generation moves tree moves the heart of the tree is the AI. Classes that make this tree is MoveNode.py file MoveNode. His initialization method is as follows: def __init __ (self, move, children, parent): self.move = move self.children = children self.parent = parent pointAdvantage = None depth = 1 This class has five properties. The first is the move, ie moves it contains, it is a Move type, in this is not very important, just need to know it is to tell a driver where to go moves, what the child can eat, and so on. Then the children, it is also a MoveNode class. The third property is the parent, so it can know through a layer of what MoveNode. pointAdvantage attribute is used to determine the AI moves are good or bad use. depth attribute specifies the node in the first layers, that is to say how many nodes above the node. Code generation moves tree as follows: def generateMoveTree (self): moveTree = [] for move in self.board.getAllMovesLegal (self.side): moveTree.append (MoveNode (move, [], None)) for node in moveTree : self.board.makeMove (node.move) self.populateNodeChildren (node) self.board.undoLastMove () return moveTree variable moveTree began as an empty list, then it is loaded instance MoveNode class. After the first cycle, it has not just a parent node, MoveNode New Nike Free Run 3 Shoes Silver 3 array of child nodes, that is, some of the root node. The second loop through moveTree, adding with populateNodeChildren function to each child node: def populateNodeChildren (self, node): node.pointAdvantage = self.board.getPointAdvantageOfSide (self.side) node.depth = node.getDepth () if node.depth == self.depth: return side = self.board.currentSide legalMoves = self.board.getAllMovesLegal (side) if not legalMoves: if self.board.isCheckmate (): node.move.checkmate = True return elif self 586590-300 Nike Kobe 8 System Elite GC Poison Green Superhero Outlet .board.isStalemate (): node.move.stalemate = True node.pointAdvantage = 0 return for move in legalMoves: node.children.append (MoveNode (move, [], node)) self.board.makeMove (move) self .populateNodeChildren (node.children [-1]) self.board.undoLastMove () This function is recursive, and it is a bit difficult to image expression. He started to pass it a MoveNode objects. This MoveNode objects have a depth, because it has no parent node. We still assume that the AI is set at a depth of 2. Therefore, the function of the first to pass the node will skip the first if statement. Then, decide all the rules allow the move. But this is beyond the scope of this article to discuss, if you want to see if the code on Github. The next if statement to check whether there is compliance with the rules of the move. If one did not, either will die, or draw a. If you are going to die, because there is no other moves you can go to node.move.checkmate property to True and return. A draw is similar, but because neither of no advantage, we node.pointAdvantage set to zero. If it is not checkmate or a draw, then legalMoves variable moves all child nodes are added to the current node as MoveNode, then the function is called to add their own MoveNode node to the sub. When the junction depth equal self.depth (this example 2) do nothing, the child of the current node is left empty array. Suppose traverse the tree / MoveNode we have a tree, we need to traverse him, find the best move. This logic, some subtle, it takes a little time to think to understand it (before understand that this is a good algorithm, I should be more to go with Google). So I will try to explain it fully. Let's say this is our moves tree: If this AI stupid, only depth 1, he will choose to take the 'like' eat 'car', causing it to get 5 points and the total benefits Air Jordan Heel to +7. Then the next step, 'soldiers' will eat it 'post', now the advantage goes from +7 -2, because it does not expect the next advance. Assuming its depth is 2. Will see it with the 'after' eating 'horse' leading to scores -4 move 'after' causing scores +1, 'like' eat 'car' leads to score -2. Therefore, he chose Nike Free Run 3 Women to move back. This is a common technique when designing AI, you can find more information on this (minimax algorithm). So we choose the best move when it turn AI, and AI opponents will assume the choice of AI is most unfavorable moves. The following shows how this is implemented: def getOptimalPointAdvantageForNode (self, node): if node.children: for child in node.children: child.pointAdvantage = self.getOptimalPointAdvantageForNode (child) #If the depth is divisible by 2, it's a move for the AI's side, so return max if node.children [0] .depth% 2 == 1: return (max (node.children) .pointAdvantage) else: return (min (node.children) .pointAdvantage) else: return node.pointAdvantage this is Air Max 2011 Womens Red a recursive function, it is a difficult to see what it's doing. There are two cases: the current node has no child nodes or child nodes. Suppose moves tree is just like the previous figure (there will be more nodes on each branch in practice). In the first case, the current node has children. Take the first step, for example, Q eat N. The depth of its child nodes is 2, so in addition to 2 to take more than 2 instead of 1. This means that the child node contains the opponent's move, it returns the smallest number of steps (assuming AI opponents will be out for the most unfavorable moves). Whose child nodes will not have their own node, because we assume a depth of 2. Therefore, they but their real value (-4 and +5). The smallest of them is Nike Free Run 3 -4, so the first step, Q eat N, is given as a value -4. Repeat this step two steps are also other, to move 'back' to scores of +1, 'like' eat 'car' score to -2. Select the best move hardest part has been completed, and now the AI thing to do is to choose from the highest score moves in. def bestMovesWithMoveTree (self, moveTree): bestMoveNodes = [] for moveNode in moveTree: moveNode.pointAdvantage = self.getOptimalPointAdvantageForNode (moveNode) if not bestMoveNodes: bestMoveNodes.append (moveNode) elif moveNode \u0026 amp; gt; bestMoveNodes [0]: bestMoveNodes = [] bestMoveNodes.append (moveNode) elif moveNode == bestMoveNodes [0]: bestMoveNodes.append (moveNode) return [node.move for node in bestMoveNodes] At this time there are three cases. BestMoveNodes If the variable is empty, then the value is the number moveNode, are added to this list. If the value is higher than the first element moveNode bestMoveNodes, and emptied the list and then add the moveNode. MoveNode If the value is the same, then add to the list. The final step is to select a random (AI can predict is very bad) bestMoves = self.bestMovesWithMoveTree (moveTree) from the best move in randomBestMove = random.choice (bestMoves) That's all content. AI generates a tree, filled with the child node to an arbitrary depth, traverse the tree to find Nike Lebron 10(X) the scores for each moves, then randomly choose the best. There are various places can be optimized, pruning, razors, still searching, etc., but I hope this article explains how well-based 2015 Nike Free 5.0 violence Chess AI algorithms work.use Python to write a chess AI program