def _calculateBrazucaTreeNodesRecurrent(board, cell, depth, max_depth): player = depth % 2 cellSimulationBoard = core.add_move_to_board(board, cell) if core.last_player_has_won(cellSimulationBoard): if player == 0: return MAX_SCORE else: return MIN_SCORE elif core.board_is_full(cellSimulationBoard) or depth >= max_depth: return 0 else: if player == 0: minScore = MAX_SCORE for freeCell in core.get_free_cells(cellSimulationBoard): score = calculateBrazucaTreeNodesRecurrent( cellSimulationBoard, freeCell, depth + 1, max_depth) if score <= MIN_SCORE: return score minScore = min(minScore, score) return minScore else: highestScore = MIN_SCORE for freeCell in core.get_free_cells(cellSimulationBoard): score = calculateBrazucaTreeNodesRecurrent( cellSimulationBoard, freeCell, depth + 1, max_depth) if score >= MAX_SCORE: return score highestScore = max(highestScore, score) return highestScore
def calculateBrazucaTreeNodesRecurrent(board, currentNode): player = currentNode.treeLevel % 2 cellSimulationBoard = core.add_move_to_board(board,currentNode.cell) if core.last_player_has_won(cellSimulationBoard): if player == 0: currentNode.set_content(player=player, board=cellSimulationBoard,result=1) else: currentNode.set_content(player=player, board=cellSimulationBoard, result=-1) elif core.board_is_full(cellSimulationBoard): currentNode.set_content(player=player, board=cellSimulationBoard,result=0) else: if player == 0: minScore = 1 for freeCell in core.get_free_cells(cellSimulationBoard): newCurrentNode = BrazucaTreeNode(parent = currentNode, cell = freeCell, treeLevel = currentNode.treeLevel + 1) newCurrentNode.board = cellSimulationBoard currentNode.append_child(newCurrentNode) calculateBrazucaTreeNodesRecurrent(cellSimulationBoard,newCurrentNode) minScore = min(minScore, newCurrentNode.result) currentNode.result = minScore else: highestScore = -1 for freeCell in core.get_free_cells(cellSimulationBoard): newCurrentNode = BrazucaTreeNode(parent=currentNode, cell=freeCell, treeLevel=currentNode.treeLevel + 1) newCurrentNode.board = cellSimulationBoard currentNode.append_child(newCurrentNode) calculateBrazucaTreeNodesRecurrent(cellSimulationBoard, newCurrentNode) highestScore = max(highestScore, newCurrentNode.result) currentNode.result = highestScore
def _calculateBrazucaTreeNodesRecurrent(board, cell, depth): player = depth % 2 cellSimulationBoard = core.add_move_to_board(board, cell) if core.last_player_has_won(cellSimulationBoard): if player == 0: return 1 else: return -1 elif core.board_is_full(cellSimulationBoard): return 0 else: if player == 0: minScore = 1 for freeCell in core.get_free_cells(cellSimulationBoard): score = calculateBrazucaTreeNodesRecurrent( cellSimulationBoard, freeCell, depth + 1) minScore = min(minScore, score) return minScore else: highestScore = -1 for freeCell in core.get_free_cells(cellSimulationBoard): score = calculateBrazucaTreeNodesRecurrent( cellSimulationBoard, freeCell, depth + 1) highestScore = max(highestScore, score) return highestScore
def _getNextBrazucaMove(board): free_cells = core.get_free_cells(board) max_depth = get_max_depth(board, free_cells) simulationResult = {} for cell in core.get_free_cells(board): score = calculateBrazucaTreeNodesRecurrent(board, cell, 0, max_depth) if score >= MAX_SCORE: return cell simulationResult[cell] = score maxScoreCell = max(simulationResult.iterkeys(), key=lambda x: simulationResult[x]) return maxScoreCell
def getNextBrazucaMove(board): simulationResult = {} for cell in core.get_free_cells(board): score = calculateBrazucaTreeNodesRecurrent(board, cell, 0) simulationResult[cell] = score maxScoreCell = max(simulationResult.iterkeys(), key=lambda x: simulationResult[x]) return maxScoreCell
def _getNextBrazucaMove(board): free_cells = core.get_free_cells(board) max_depth = get_max_depth(board, free_cells) simulationResult = {} for i in random.sample(range(len(free_cells)), len(free_cells)): cell = free_cells[i] score = calculateBrazucaTreeNodesRecurrent(board, cell, 0, max_depth) # if score >= MAX_SCORE: # return cell simulationResult[cell] = score maxScoreCell = max(simulationResult.iterkeys(), key=lambda x: simulationResult[x]) return maxScoreCell
def getNextBrazucaMove(board): simulationResult = {} for cell in core.get_free_cells(board): #print "Checking Cell " + str(cell) #core.print_board(board) node = BrazucaTreeNode(parent=None,cell= cell,treeLevel=0) node.board = board calculateBrazucaTreeNodesRecurrent(board, node) #print("FinalScore=" + str(node.result)) simulationResult[cell] = node.result maxScoreCell = max(simulationResult.iterkeys(), key=lambda x: simulationResult[x]) # print "move: " + str(maxScoreCell) + " & score: " + str(simulationResult[maxScoreCell]) + " & simulationResult: " + str(simulationResult) return maxScoreCell return simple.strategy(board)
def _calculateScoreRecurrent(board, currentCell, scoreSign, round): cellSimulationBoard = core.add_move_to_board(board, currentCell) #core.print_board(cellSimulationBoard) if core.last_player_has_won(cellSimulationBoard): if scoreSign >= 0: return [create_win(round)] return [create_loss(round)] elif core.board_is_full(cellSimulationBoard): return [create_draw(round)] else: totalScore = {} for freeCell in core.get_free_cells(cellSimulationBoard): scores = calculateScoreRecurrent(cellSimulationBoard, freeCell, scoreSign * -1, round + 1) for score in scores: scoreForRound = totalScore.setdefault(score.round, create_zero(score.round)) totalScore[score.round] = score.add(scoreForRound) return totalScore.values()
def getNextBrazucaMove(board): simulationResult = {} for cell in core.get_free_cells(board): #print "Checking Cell " + str(cell) #core.print_board(board) scores = calculateScoreRecurrent(board, cell, 1, 1) if True in [x.only_loose() for x in scores if x.round <= 2]: # If the move leads to a situation where I only loose on the next round of the other player, then discard it! print "Discarding " + str(scores) continue finalScore = sum([x.come_to_a_number() for x in scores]) print(str(scores) + "FinalScore=" + str(finalScore)) simulationResult[cell] = finalScore if (len(simulationResult) == 0): print "No Strategy has been found! Going to SimpleStrategy!" return simple.strategy(board) maxScoreCell = max(simulationResult.iterkeys(), key=lambda x: simulationResult[x]) #print "move: " + str(maxScoreCell) + " & score: " + str(simulationResult[maxScoreCell]) + " & simulationResult: " + str(simulationResult) return maxScoreCell
def strategy(board): return core.get_free_cells(board)[0]
def strategy(board): ret = random.choice(core.get_free_cells(board)) # print "Bokito Choice: " + str(ret) return ret