def add_node_front(node, val): if node is not None: new_node = nodes.Node(val) new_node.next = node else: new_node = nodes.Node(val) new_node.next = None
def __init__(self): ######################## # SCREEN INITIALIZED # ###################### self.WIDTH = 1300 self.HEIGHT = 600 self.backgroundColor = (255, 255, 255) self.gridColor = (100, 100, 100) self.squareSize = 15 self.clock = pygame.time.Clock() self.screen = pygame.display.set_mode((self.WIDTH, self.HEIGHT), 0, 32) self.surface = pygame.Surface(self.screen.get_size()) self.surface = self.surface.convert() self.startNode = nodes.Node(self.squareSize) self.startNodeBool = False self.endNodeBool = False self.endNode = nodes.Node(self.squareSize) self.obstackles = list() self.obstacklesBool = False self.matrix = list() self.space = False self.result = list() # X, Y, Type (0 normal, 1 start, 2 end, 3 obstacle), visited # for i in range(0, self.WIDTH, self.squareSize): temp = list() for j in range(0, self.HEIGHT, self.squareSize): temp.append(((i, j), 0, 0)) self.matrix.append(temp)
def add_node(node, val): if node is not None: temp_node = node while temp_node.next is not None: temp_node = temp_node.next new_node = nodes.Node(val) temp_node.next = new_node new_node.prev = temp_node else: print('Adding a node to the empty list.') temp_node = nodes.Node(val) temp_node.next = None
def execute(self, command): if (command == RIGHTCLICK): if (self.startNodeBool): self.startNode.delete(self.surface) self.matrix[int(self.startNode.x / self.squareSize)][int( self.startNode.y / self.squareSize)] = ((self.startNode.x, self.startNode.y), 0, 0) self.startNode = nodes.Node(self.squareSize, 1) self.startNode.draw( self.surface, pygame.mouse.get_pos()[0] - pygame.mouse.get_pos()[0] % self.squareSize, pygame.mouse.get_pos()[1] - pygame.mouse.get_pos()[1] % self.squareSize) self.matrix[int(self.startNode.x / self.squareSize)][int( self.startNode.y / self.squareSize)] = ((self.startNode.x, self.startNode.y), 1, 0) self.startNodeBool = True if (command == LEFTCLICK): if (self.endNodeBool): self.endNode.delete(self.surface) self.matrix[int(self.endNode.x / self.squareSize)][int( self.endNode.y / self.squareSize)] = ((self.endNode.x, self.endNode.y), 0, 0) self.endNode = nodes.Node(self.squareSize, 2) self.endNode.draw( self.surface, pygame.mouse.get_pos()[0] - pygame.mouse.get_pos()[0] % self.squareSize, pygame.mouse.get_pos()[1] - pygame.mouse.get_pos()[1] % self.squareSize) self.endNodeBool = True self.matrix[int(self.endNode.x / self.squareSize)][int( self.endNode.y / self.squareSize)] = ((self.endNode.x, self.endNode.y), 2, 0) if (command == MIDDLECLICK): x = pygame.mouse.get_pos( )[0] - pygame.mouse.get_pos()[0] % self.squareSize y = pygame.mouse.get_pos( )[1] - pygame.mouse.get_pos()[1] % self.squareSize self.obstackles.append( obstacle.Obstackle(self.squareSize, self.surface, x, y)) x = int(x / self.squareSize) y = int(y / self.squareSize) self.matrix[x][y] = ((x * self.squareSize, y * self.squareSize), 3, 0)
def insertAtBeginning(self, newData): NewNode = nodes.Node(newData) # Update the new nodes NewNode.nextval = self.headval self.headval = NewNode
def learnNetwork(self, data) -> nodes.Node: """ Create the network of nodes using the provided dataset """ termination_condition = 50 # Simple termination condition for when leaves are created if len(data) < termination_condition: #TODO: Import Chow-Liu Tree and return it here return 'Chow-Liu Tree' variable, index, inf_gain, split_value = self.selectVariable(data) _, counts = np.unique(index, return_counts=True) probability = counts / len(data) # Create Node node = nodes.Node() node.left_prob, node.right_prob = probability node.variable = variable node.split_value = split_value node.data_points = len(data) node.left_child = self.learnNetwork(data[index == 1]) node.right_child = self.learnNetwork(data[index == 2]) node.information_gain = inf_gain return node
def open_node(curr_node, type_next_node, text, found_span): prev_node = curr_node prev_node.set_text(text[:found_span[0]]) curr_node = nodes.Node(type_next_node, [text[found_span[1]:]], prev_node, []) prev_node.add_next_node(curr_node) return prev_node, curr_node
def insertInbetween(self, middle_node, newData): # Check if the node exists if middle_node is None: print("The node you requested is misisng") return # Now Place after the mentioned node NewNode = nodes.Node(newData) NewNode.nextval = middle_node.nextval middle_node.nextval = NewNode
def main(): node1 = nodes.Node(10) print('\nCurrent Nodes - - - - - - - -') show_values(node1) add_node(node1, 20) add_node(node1, 30) print('\nCurrent Nodes - - - - - - - -') show_values(node1) # add a front node add_node_front(node1, 5) print('\nCurrent Nodes - - - - - - - -') show_values(node1)
def insertAtEnd(self, newData): NewNode = nodes.Node(newData) # Check to see if this is the first addition to the list if self.headval is None: self.headval = NewNode return # Set laste value to the headval and transverse until None # Then add the NewNode as the last value laste = self.headval while (laste.nextval): laste = laste.nextval laste.nextval = NewNode
def newNode(state, move, parent, cost): return nodes.Node(state, move, parent, cost)
#result += [n.generated_text(self.LANG) for n in nouns] for n in nouns: if self._can_modify_with_noun(node, n): result.append(n.generated_text(self.LANG)) assert (self._modifiers_are_done(modifiers)) result += template return result def generator_factory(lang): '''Generates a SINGLETON generator that lives in this module''' assert (lang in utility.LANGUAGES) if lang == 'en': return EnGenerator() elif lang == 'zh': return ZhGenerator() else: raise Exception('Unimplemented generator for language:' + lang) # module-level singletons analyzer = Analyzer() generators = {lang: generator_factory(lang) for lang in utility.LANGUAGES} if __name__ == '__main__': print(EnGenerator().LANG) nodes.Node()
def game(p1_name, p2_name, *args): #Initialize everything clock = pygame.time.Clock() countdown = 45 grid, card_list, card_pics, popup = [], [], [], [] #All lists card_type = [ "elf", "santa", "reindeer", "snow", "fire", "lightning", "wood", "brick", "castle", "mine", "factory", "northpole" ] pick_card = False #Defaults for cards card_num, action, rand_x, rand_y = (int, ) * 4 player_turn = 1 card_info_list = unit_stats.unit_stats() # Get card information done, start, end_click, bag_visible, button, correct, gather_mana, bag_clicked = ( False, ) * 8 #Flags mana_increase = 110 p1 = gamer.Player(2500, 100, 100, 105, p1_name) # Create players p2 = gamer.Player(2500, 100, 100, 105, p2_name) text_btn = textbox.init_textbox(918, 10, 100, 60) # Create textbox screen = pygame.display.set_mode([1300, 675]) # Create the playing board for y in range(8): grid.append([]) for x in range(16): grid[y].append(nodes.Node(x, y)) player_turn, p1, p2, mana_increase, card_list, card_pics = turn_update.init_update( player_turn, p1, p2, mana_increase, grid, card_type, card_pics, card_list, args) #setup initalized variables # Initialize Timer start_time = time.time() # Main loop while not done: #Draw all mainscreen objects display_timer = screen_graphics.render_main(screen, player_turn, end_click, card_list, p1, p2, countdown, start_time, grid, popup) # Process events for event in pygame.event.get(): if event.type == pygame.QUIT: done = True elif event.type == pygame.MOUSEBUTTONDOWN: pick_card, card_num, x, y, bag_visible, gather_mana, popup = events.downclick( pick_card, player_turn, card_num, card_list, p1, p2, card_pics, grid, screen, rand_x, rand_y, bag_visible, gather_mana, popup) elif event.type == pygame.MOUSEBUTTONUP: countdown, display_timer, correct = events.upclick( countdown, text_btn, display_timer, gather_mana, mana_increase, correct) elif event.type == pygame.KEYDOWN and text_btn.selected: input_entered = text_btn.char_add(event) # Card follows mouse on selection of card track_card.tracking(screen, pick_card, card_num, card_type, player_turn, card_list) # Mana if display_timer == 36: rand_x, rand_y, = gather.get_random() bag_visible = True if 15 < display_timer <= 35: gather.gather_mana(rand_x, rand_y, bag_visible, grid, screen) gather.gathering(gather_mana, correct, mana_increase, screen, player_turn, text_btn) correct, gather_mana = gather.gathered(correct, gather_mana, text_btn, p1, p2, player_turn, mana_increase) if display_timer == 25: bag_visible, gather_mana = gather.reset_gather(text_btn) # Update at the end of a turn if display_timer <= 0: player_turn, card_list, mana_increase, popup, pick_card, done = turn_update.stats_update( player_turn, p1, p2, card_list, card_pics, grid, mana_increase, popup, pick_card) # Update the screen pygame.display.flip()
file_name_html = file_name.rstrip(".md") file_name_html = file_name_html + ".html" exists_output_file(file_name_html) dest_file = open(file_name_html, "w+") return dest_file if __name__ == '__main__': if (sys.argv[0] == sys.argv[-1]): print("Please add a file to compile") exit(1) file_name, source_file, flags = checks.check_all(sys.argv) dest_file = make_output_file_html(file_name) standard_head_text = output_file_html_standard_head_text(file_name) dest_file.write(standard_head_text) track_node = keep_track_of_nodes.Keep_track_of_nodes() start_node = nodes.Node("root", [source_file.read()]) track_node.set_start_node(start_node) node_tree = make_nodes.analyse_source(source_file, track_node) html = make_html.make_html(track_node) # html = 'html' dest_file.write(html) standard_bottom_text = output_file_html_standard_bottom_text() dest_file.write(standard_bottom_text) start_node.visualize_node_stream()
if HeadVal == None: return prev.nextval = HeadVal.nextval HeadVal = None # Create the list list1 = SinglyLinkedList() # Insert the head value list1.headval = nodes.Node("Mon") # Create two extra nodes e2 = nodes.Node("Tues") e3 = nodes.Node("Wed") # Create the link list1.headval.nextval = e2 e2.nextval = e3 # Print the list ( with stars for dividers ) print("*" * 20) print('First list print') list1.listprint()
def MUTATION(self, l_vals): total_nodes = 0 N_nodes = 0 temp_rand1 = 0 temp_rand2 = 0 last_fitness = 0 temp_node = nodes.Node() temp_node_back = nodes.Node() for index in range(self.pobSiz): temp_random = random.random() if (temp_random <= self.mutRt and self.INDIVIDUALS[index].fitness > 0): total_nodes = nodes.nodeCount(self.INDIVIDUALS[index].chrom, self.isNd, self.isBin) N_nodes = nodes.nodeCount(self.INDIVIDUALS[index].chrom, self.isNd, self.isBin) if (self.mut_s == BIT_MUT): temp_rand1 = random.randint(0, total_nodes - 1) nodes.getIN(temp_rand1, self.INDIVIDUALS[index].chrom, temp_node, self.isBin) temp_node_back.data = temp_node.data last_fitness = self.INDIVIDUALS[index].fitness if (self.isNd(temp_node.data)): temp_node.data = self.gtOp() else: temp_node.data = self.gtLf() nodes.setIN(temp_rand1, self.INDIVIDUALS[index].chrom, temp_node, self.isBin) self.INDIVIDUALS[index].evalFitness(self, l_vals) if (self.INDIVIDUALS[index].fitness > last_fitness): temp_node.data = temp_node_back.data nodes.setIN(temp_rand1, self.INDIVIDUALS[index].chrom, temp_node, self.isBin) self.INDIVIDUALS[index].evalFitness(self, l_vals) elif (self.mut_s == OPR_MUT): temp_rand1 = random.randint(0, N_nodes - 1) nodes.getIST(temp_rand1, self.INDIVIDUALS[index].chrom, temp_node, self.isNd, self.isBin) temp_node_back.data = temp_node.data last_fitness = self.INDIVIDUALS[index].fitness temp_node.data = self.gtOp() nodes.setIST(temp_rand1, self.INDIVIDUALS[index].chrom, temp_node, self.isNd, self.isBin) self.INDIVIDUALS[index].evalFitness(self, l_vals) if (self.INDIVIDUALS[index].fitness > last_fitness): temp_node.data = temp_node_back.data nodes.setIST(temp_rand1, self.INDIVIDUALS[index].chrom, temp_node, self.isNd, self.isBin) self.INDIVIDUALS[index].evalFitness(self, l_vals) elif (self.mut_s == SUB_MUT): temp_rand1 = random.randint(0, N_nodes - 1) if (random.randint(0, 500) == 0): temp_rand2 = 1 else: temp_rand2 = 0 nodes.getIST(temp_rand1, self.INDIVIDUALS[index].chrom, temp_node, self.isNd, self.isBin) temp_node_back = temp_node last_fitness = self.INDIVIDUALS[index].fitness temp_node = nodes.newNode( self.fill_s, int( math.log2( nodes.totalNodeCount(temp_node, self.isBin) + temp_rand2)), self.gtOp(), self.isBin, self.gtLf, self.gtOp) nodes.setIST(temp_rand1, self.INDIVIDUALS[index].chrom, temp_node, self.isNd, self.isBin) self.INDIVIDUALS[index].evalFitness(self, l_vals) if (self.INDIVIDUALS[index].fitness > last_fitness): temp_node = temp_node_back nodes.setIST(temp_rand1, self.INDIVIDUALS[index].chrom, temp_node, self.isNd, self.isBin) self.INDIVIDUALS[index].evalFitness(self, l_vals)
def make_empty_node(curr_node, type_next_node, text, found_span): next_node = nodes.Node(type_next_node, [''], curr_node, []) next_node.set_text(text[found_span[0]:found_span[1]]) curr_node.add_next_node(next_node) curr_node.set_text(text[:found_span[0]]) curr_node.add_text(text[found_span[1]:])