def main(): # Initialization # 1. Read map csv file maze = mz.Maze("data/maze_complex.csv") # 2. Valid UID file point = score.Scoreboard("data/UID.csv") # 3. Team name team_name = "Team_XXX" # 4. Judge server IP ip = "114.34.123.174:5000" # 5. Car direction "South" ip = "114.34.123.174:5000" # 6. Other variable # Mode 0: for treasure-hunting with rule 1 if (sys.argv[1] == '1'): print("Mode 1: Game Start!") # TODO: Finish Game rule 1 # When you start the game, call the following judge code. ######### Do not change ######### ########## Juge code ############ url_start = 'http://' + ip + '/{}/start/'.format(team_name) r = requests.get(url_start) ################################# # When you get an UID, please call the following judge code. ######### Do not change ######### ########## Juge code ############ current_score = point.getCurrentScore() url = url = 'http://' + ip + '/{}/{}/'.format(team_name, current_score) r = requests.get(url_start) ################################# # Mode 1: for treasure-hunting with rule 2 elif (sys.argv[1] == '2'): print("Mode 2: Game Start!") # TODO: Finish Game rule 2 # When you start the game, call the following judge code. ######### Do not change ######### ########## Juge code ############ url_start = 'http://' + ip + '/{}/start/'.format(team_name) r = requests.get(url_start) ################################# # When you get an UID, please call the following judge code. ######### Do not change ######### ########## Juge code ############ current_score = point.getCurrentScore() url = url = 'http://' + ip + '/{}/{}/'.format(team_name, current_score) r = requests.get(url_start) ################################# # Mode 2: Self-testing mode. elif (sys.argv[1] == '3'): print("Mode 3: Self-testing mode.")
def main(): maze = mz.Maze("data/small_maze.csv") point = score.Scoreboard("data/UID.csv", "team_NTUEE") interf = interface.interface() # TODO : Initialize necessary variables if (sys.argv[1] == '0'): print("Mode 0: for treasure-hunting") # TODO : for treasure-hunting, which encourages you to hunt as many scores as possible elif (sys.argv[1] == '1'): print("Mode 1: Self-testing mode.")
def main(): maze = mz.Maze("data/maze_test.csv") point = score.Scoreboard("data/UID.csv") interf = interface.interface() # the part of calling interface.py was commented out. # TODO : Initialize necessary variables if (sys.argv[1] == '0'): print("Mode 0: for treasure-hunting with rule 1") # TODO : for treasure-hunting with rule 1, which encourages you to hunt as many scores as possible elif (sys.argv[1] == '1'): print("Mode 1: for treasure-hunting with rule 2") # TODO : for treasure-hunting with rule 2, which requires you to hunt as many specified treasures as possible elif (sys.argv[1] == '2'): print("Mode 2: Self-testing mode.")
def main(): point = score.Scoreboard("data/UID.csv", "team_NTUEE") interf = interface.interface() # TODO : Initializ1e necessary variables print("Mode 0: for checkpoint") direction = 2 #input("Enter the initial direction:(1,2,3,4)") in_node = 1 interf.send_action(mz.Action(1)) while(in_node < 13): command = interf.get_command() if command == "n": if in_node == 1 or in_node == 2 or in_node == 3: interf.send_action(mz.Action(1)) in_node += 1 elif in_node == 4: interf.send_action(mz.Action(3)) in_node += 1 elif in_node == 5: interf.send_action(mz.Action(2)) in_node += 1 elif in_node == 6: interf.send_action(mz.Action(1)) in_node += 1 elif in_node == 7: interf.send_action(mz.Action(3)) in_node += 1 elif in_node == 8: interf.send_action(mz.Action(4)) in_node += 1 elif in_node == 9: interf.send_action(mz.Action(3)) in_node += 1 elif in_node == 10: interf.send_action(mz.Action(2)) in_node += 1 elif in_node == 11: interf.send_action(mz.Action(1)) in_node += 1 elif in_node == 12: interf.send_action(mz.Action(5)) in_node += 1 print(in_node) # TODO : for treasure-hunting, which encourages you to hunt as many scores as possible interf.end_process()
def main(): maze = mz.Maze("maze_405_0508.csv") next_nd = maze.getStartPoint(1) car_dir = Direction.SOUTH point = score.Scoreboard("score1.csv") #interface = student.interface() the part of calling student.py was commented out. if (sys.argv[1] == '0'): maze.strategy_process(point, sys.argv[1]) #TODO: Impliment your algorithm here and return the UID for evaluation function # ================================================ # Basically, you will get a list of nodes and corresponding UID strings after the end of algorithm. # The function add_UID() would convert the UID string score and add it to the total score. # In the sample code, we call this function after getting the returned list. # You may place it to other places, just make sure that all the UID strings you get would be converted. # ================================================ elif (sys.argv[1] == '1'): ndlist = [] #TODO: Implement your algorithm here and return the UID for evaluation function for i in range(5): nd = int(input("destination: ")) ndlist.append(nd) maze.bestway = [ndlist] maze.strategy_process(point, sys.argv[1]) if (len(ndlist) == 0): print("end process") print('') else: print("unfinished nodes:" + ','.join([str(x) for x in ndlist])) """ node = 0 while(not node): node = interface.wait_for_node() interface.end_process() """ print("complete") print("") a = point.getCurrentScore() print("The total score: ", a)
def main(): maze = mz.Maze("data/mid_map.csv") point = score.Scoreboard("data/UID.csv", "team_puipui") #start_point="1.0" #正式競賽由這個取代下兩行 車車放在1的位置面向北邊 #car_d="3" start_point = input("input the start point: ") car_d = input("input the car direction (N1 E2 S3 W4): ") car_direction = Direction(int(car_d)) sequence = maze.BFS(start_point) #list of type:str interf = interface.interface() for i in range(0, len(sequence) - 1): a = maze.getAction(car_direction, sequence[i], sequence[i + 1]) interf.send_action(a) print(a) car_direction = maze.update_cardirc(sequence[i], sequence[i + 1]) interf.send_action("e") while True: point.add_UID(interf.read()) # TODO : Initialize necessary variables '''
def main(): maze = mz.Maze("data/medium_maze.csv") point = score.Scoreboard("data/UID.csv", "team_4") interf = interface.interface() # it will send "s" to arduino # TODO : Initialize necessary variables if (sys.argv[1] == '0'): print("Mode 0: for treasure-hunting") # TODO : for treasure-hunting, which encourages you to hunt as many scores as possible shortest_path = maze.ShortestPath(int(float(str(maze.getStartPoint())))) print("THE PATH:", shortest_path, "\n") end_node = maze.FindEndNode() end_node.pop(0) i = 0 while i < len(shortest_path) - 1: if interf.ser.SerialReadString() == "1": nd_from = maze.nd_dict[shortest_path[i]] nd_to = maze.nd_dict[shortest_path[i + 1]] print(f"arrive at Node{shortest_path[i]}") interf.ser.SerialWrite(f"{maze.getAction(nd_from, nd_to)}") if shortest_path[i] in end_node : UID = interf.ser.SerialReadByte() while UID == "0" or UID == "31": UID = interf.ser.SerialReadByte() UID = str(hex(int(UID))) print("讀到的:", UID) point.add_UID(UID) i += 1 if interf.ser.SerialReadString() == "1": interf.ser.SerialWrite("2") UID = interf.ser.SerialReadByte() while UID == "0" or UID == "31": UID = interf.ser.SerialReadByte() UID = str(hex(int(UID))) point.add_UID(UID) interf.end_process()
def main(): maze = mz.Maze("data/small_maze.csv") next_nd = maze.getStartPoint() node_dict = maze.getNodeDict() car_dir = Direction.SOUTH point = score.Scoreboard("data/UID.csv") interf = interface.interface() # the part of calling interface.py was commented out. # Mode 0: for treasure-hunting with rule 1, which encourages you to hunt as many scores as possible. if (sys.argv[1] == '0'): print("Mode 0: for treasure-hunting with rule 1, which encourages you to hunt as many scores as possible") while (1): # ================================================ # Basically, you will get a list of nodes and corresponding UID strings after the end of algorithm. # The function add_UID() would convert the UID string score and add it to the total score. # In the sample code, we call this function after getting the returned list. # You may place it to other places, just make sure that all the UID strings you get would be converted. # ================================================ ndList = [] deadend_node_num = 0 for node in maze.nodes: if len(node.getSuccessors()) == 1: deadend_node_num += 1 start_nd = next_nd for i in range(1, deadend_node_num + 1): BFS_list = maze.strategy(start_nd) start_nd = BFS_list[-1] if i == deadend_node_num: ndList = ndList + BFS_list else: ndList = ndList + BFS_list[:-1] print("The route to deadend {}: {}".format(i, [j.getIndex() for j in BFS_list])) # Check the result for the whole BFS! print("The whole BFS route:", [node.getIndex() for node in ndList]) # Modify the ndList to match the final contest's rule ndList = ndList[1:] count = 0 for i in range(1, len(ndList)): current_node = ndList[i - 1] next_node = ndList[i] print("The coming node: Node", current_node.getIndex()) print("The next going node: Node", next_node.getIndex()) # current car position + current node + next node => action + new car direction print("Current car direction:", car_dir) action, car_dir = maze.getAction(car_dir, current_node, next_node) print("Updated car direction:", car_dir) # Wait until the BT says the car reaches a node while (1): python_get_information = interf.ser.SerialReadString() print(python_get_information is 'N') if python_get_information is 'N': count = count + 1 print("The car see a node!\n") break # if the node is a leaf node and the node is not the starting node => read RFID if len(current_node.getSuccessors()) == 1 and current_node.getIndex() != 1: print("Reading RFID...") interf.send_readRFID() while (1): # Will enter infinite loop if RFID doesn't read properly. # state_cmd = input("Please enter a mode command: ") # interf.ser.SerialWrite(state_cmd) cnt = 0 cnt_limit = 300 (read_UID, waiting) = interf.ser.SerialReadByte() print("Unused UID: {}, waiting: {}".format(read_UID, waiting)) (read_UID, waiting) = interf.ser.SerialReadByte() while read_UID == "Not receive" and waiting == 0 and cnt < cnt_limit: (read_UID, waiting) = interf.ser.SerialReadByte() cnt += 1 print("Read count: {}".format(cnt)) print("UID: {}, waiting: {}".format(read_UID, waiting)) if cnt < cnt_limit: while waiting < 4: (read_UID_tmp, waiting_tmp) = interf.ser.SerialReadByte() if waiting_tmp != 0: waiting = waiting + waiting_tmp read_UID = read_UID + read_UID_tmp print("UID: {}, waiting: {}".format(read_UID, waiting)) print("***** waiting: ", waiting) if read_UID != "Not receive" and waiting == 4: print("***** RFID ID: ", read_UID) break else: break print("-----End reading RFID.-----") # Tell BT to send the action back to Arduino print("Get action:", action) interf.send_action(action) # TODO: get UID under the node. print("Get action: ", mz.Action.HALT) interf.send_action(mz.Action.HALT) break # Mode 1: for treasure-hunting with rule 2, which requires you to hunt as many specified treasures as possible. elif (sys.argv[1] == '1'): print("Mode 1: for treasure-hunting with rule 2, which requires you to hunt as many specified treasures as possible.") while (1): nd = int(input("destination: ")) if (nd == 0): print("end process") print('') break try: nd = node_dict[nd] except: print("Your input is not a valid node!") raise IndexError("No node!") ndList = maze.strategy_2(next_nd, nd) count = 0 for i in range(1, len(ndList)): current_node = ndList[i - 1] next_node = ndList[i] # current car position + current node + next node => action + new car direction print("Current car direction:", car_dir) action, car_dir = maze.getAction(car_dir, current_node, next_node) print("Updated car direction:", car_dir) # Wait until the BT says the car reaches a node while (1): python_get_information = interf.ser.SerialReadString() print(python_get_information) if python_get_information is 'N': count = count + 1 print(python_get_information) print("The car see a node!\n") break # Tell BT to send the action back to Arduino print("Get action:", action) interf.send_action(action) print("Get action: ", mz.Action.HALT) interf.send_action(mz.Action.HALT) break # Mode 2: Self-testing mode. elif (sys.argv[1] == '2'): print("Mode 2: Self-testing mode.") # TODO: You can write your code to test specific function. while (1): state_cmd = input("Please enter a mode command: ") interf.ser.SerialWrite(state_cmd)
def main(): # Initialization maze = mz.Maze("data/maze_small.csv") next_nd = maze.getStartPoint() node_dict = maze.getNodeDict() car_dir = Direction.SOUTH point = score.Scoreboard("data/UID.csv") interf = interface.interface( ) # the part of calling interface.py was commented out. team_name = "Team_1" # Mode 0: for treasure-hunting with rule 1 if (sys.argv[1] == '0'): # When you start the game, call the following judge code. ######### Do not change ######### ### Add Here !!! ########## Juge code ############ point.start_Judging() ################################# print("Mode 0: Game Start!") while (1): ndList = [] deadend_node_num = 0 for node in maze.nodes: if len(node.getSuccessors()) == 1: deadend_node_num += 1 start_nd = next_nd for i in range(1, deadend_node_num + 1): BFS_list = maze.strategy(start_nd) start_nd = BFS_list[-1] if i == deadend_node_num: ndList = ndList + BFS_list else: ndList = ndList + BFS_list[:-1] print("The route to deadend {}: {}".format( i, [j.getIndex() for j in BFS_list])) print("The whole BFS route:", [node.getIndex() for node in ndList]) count = 0 for i in range(1, len(ndList)): current_node = ndList[i - 1] next_node = ndList[i] print("The coming node: Node", current_node.getIndex()) print("The next going node: Node", next_node.getIndex()) # current car position + current node + next node => action + new car direction print("Current car direction:", car_dir) action, car_dir = maze.getAction(car_dir, current_node, next_node) print("Updated car direction:", car_dir) # Node Detecting while (1): python_get_information = interf.ser.SerialReadString() print(python_get_information is 'N') if python_get_information is 'N': count = count + 1 print("The car see a node!\n") break # Tell BT to send the action back to Arduino print("Get action:", action) interf.send_action(action) # RFID receiving (read_UID, waiting) = interf.ser.SerialReadByte() while read_UID == "Not receive" and waiting == 0: (read_UID, waiting) = interf.ser.SerialReadByte() while waiting < 4: (read_UID_tmp, waiting_tmp) = interf.ser.SerialReadByte() if waiting_tmp != 0: waiting = waiting + waiting_tmp read_UID = read_UID + read_UID_tmp print("read_UID: ", read_UID, " waiting: ", waiting) print("***** waiting: ", waiting) if read_UID != "Not receive" and waiting == 4: print("RFID ID: ", read_UID) point.add_UID(read_UID) current_score = point.getCurrentScore() # When you get an UID, please call the following judge code. ######### Do not change ######### ########## Juge code ############ # current_score = point.getCurrentScore() ################################# print("Get action: ", mz.Action.HALT) interf.send_action(mz.Action.HALT) break # Mode 1: for treasure-hunting with rule 2 elif (sys.argv[1] == '1'): print("Mode 1: Game Start!") while (1): nd = int(input("destination: ")) if (nd == 0): print("end process") print('') break try: nd = node_dict[nd] except: print("Your input is not a valid node!") raise IndexError("No node!") ndList = maze.strategy_2(next_nd, nd) # Mode 2: Self-testing mode. elif (sys.argv[1] == '2'): print("Mode 2: Self-testing mode.") # TODO: You can write your code to test specific function. while (1): state_cmd = input("Please enter a mode command: ") interf.ser.SerialWrite(state_cmd)
def main(): """ Main Program """ pygame.init() # -------------- Screen Settings ---------------- # Set the height and width of the screen size = [constants.SCREEN_WIDTH, constants.SCREEN_HEIGHT] screen = pygame.display.set_mode(size) # Top of window caption pygame.display.set_caption("Muffin Knight Clone") # ----------------- Scores + Players ------------------- # Create the player player_one = Player(constants.PLAYER_ONE_AT) player_two = Player(constants.PLAYER_TWO_AT) # Scores player_one_score = 0 player_two_score = 0 p1_old_score = -1 p2_old_score = -1 # ---------------- Level Assignment --------------- # Create all the levels level_list = [] level_list.append(levels.Level_01(player_one, player_two)) # Set the current level current_level_no = 0 current_level = level_list[current_level_no] # ----------------- Create Sprite Groups ------------------- # Create list of players sprites active_sprite_list = pygame.sprite.Group() # Create list of scores sprites scores_list = pygame.sprite.Group() # Group that only contains scoreboard scoreboard_list = pygame.sprite.GroupSingle() # --------------- Assign Player Attributes -------------- # Set the players levels player_one.level = current_level player_two.level = current_level # Set the player locations player_one.rect.x = 100 player_two.rect.x = 1100 player_one.rect.y = 865 - player_one.rect.height player_two.rect.y = 865 - player_two.rect.height # Add players to their sprite list active_sprite_list.add(player_one) active_sprite_list.add(player_two) # ----------------- Scoreboard ----------------- # Create the scoreboard frame and add to it's sprite list scoreboard = score.Scoreboard() scoreboard_list.add(scoreboard) # ------------------- Timing -------------------- # Used to manage how fast the screen updates clock = pygame.time.Clock() # Player 1 time between bullets p1_bullet_time = time.time() # Payer 2 time between bullets p2_bullet_time = time.time() # ---------------- Sounds -------------------- # Load backround music music = pygame.mixer.music.load('music.mid') # Backround music loop pygame.mixer.music.play(-1) # -------- MAIN PROGRAM LOOP ----------- done = False #Loop until the user hits backspace while not done: # ---------- Player Controls ------------ for event in pygame.event.get(): # User did something if event.type == pygame.QUIT: # If user clicked close done = True # Flag that we are done so we exit this loop if event.type == pygame.KEYDOWN: # Player One controls if event.key == pygame.K_a: player_one.go_left() if event.key == pygame.K_d: player_one.go_right() if event.key == pygame.K_w: player_one.jump() if event.key == pygame.K_SPACE: p1_temp_btime = time.time() if (p1_temp_btime - p1_bullet_time) >= .5: bullet = weapons.Bullet(player_one) current_level.bullet_list.add(bullet) p1_bullet_time = p1_temp_btime # Player Two controls if event.key == pygame.K_LEFT: player_two.go_left() if event.key == pygame.K_RIGHT: player_two.go_right() if event.key == pygame.K_UP: player_two.jump() if event.key == pygame.K_KP0: p2_temp_btime = time.time() if (p2_temp_btime - p2_bullet_time) >= .5: bullet = weapons.Bullet(player_two) current_level.bullet_list.add(bullet) p2_bullet_time = p2_temp_btime # Quit game if event.key == pygame.K_ESCAPE: done = True if event.type == pygame.KEYUP: # Player One controls if event.key == pygame.K_a and player_one.change_x < 0: player_one.stop() if event.key == pygame.K_d and player_one.change_x > 0: player_one.stop() # Player Two controls if event.key == pygame.K_LEFT and player_two.change_x < 0: player_two.stop() if event.key == pygame.K_RIGHT and player_two.change_x > 0: player_two.stop() # --------------- Updates ----------------- # Update the player current_level.update() active_sprite_list.update() # Update items in the level (for moving platforms) #current_level.update() # ------------------- Check For Stun ------------------ for player in active_sprite_list: player_stun_list = pygame.sprite.spritecollide( player, current_level.bullet_list, False) # If player is hit by a bullet if player_stun_list: if player.pname != player_stun_list[0].player_name: if player.stunned == False: player.stun_wait = time.time() # Check if the player has cooled down since last stun if player.stun_wait - player.stun_start >= 5: player.stunned = True player.stunned_direction = player.direction player.stun_start = time.time() # ------------ Check If Player Is Hit By Enemy --------- hit_players = pygame.sprite.groupcollide(active_sprite_list, current_level.enemy_list, False, False) for hit_player in hit_players: # Send player to respawn point hit_player.rect.x = 585 hit_player.rect.y = 100 # Remove a life and point from hit player hit_player.lives -= 1 if hit_player.pname == 'p1': player_one_score -= 1 else: player_two_score -= 1 # Need to add a delay and explosion class # ----------- Check If Players Are Off Map ---------- for guy in active_sprite_list: if guy.rect.y > constants.SCREEN_HEIGHT: # Move to the top middle platform guy.rect.x = 585 guy.rect.y = 100 # Minus a life guy.lives -= 1 # Lose five points for falling death if guy.pname == 'p1': player_one_score -= 5 else: player_two_score -= 5 # -------------- Bullet Impacts ------------ # Lists of bullet impacts for bullet in current_level.bullet_list: bullet_hit_list = pygame.sprite.spritecollide( bullet, current_level.enemy_list, False) for enemy in bullet_hit_list: enemy.health -= 1 if enemy.health <= 0: # Create explosion instance here enemy.kill() if bullet.player_name == 'p1': player_one_score += 1 else: player_two_score += 1 bullet.kill() # Create an explosion in dead enemy's place #for enemy in enemy_explode: # explosion = explosions.WalkerExplosion() # explosion_list.add(explosion) bullet_wall_hit = pygame.sprite.groupcollide( level_list[0].bullet_list, current_level.platform_list, True, False) # ------------ Scoreboard Display ------------ # Set score to zero if it's negative if player_one_score < 0: player_one_score = 0 elif player_two_score < 0: player_two_score = 0 # Used to decide if score had changed if player_one_score != p1_old_score or player_two_score != p2_old_score: p1_old_score = player_one_score p2_old_score = player_two_score #Removes old scores from list scores_list.empty() # Returns images of player one's score new_score = scoreboard.display_score(player_one_score, 1) # Add player one's scores to list scores_list.add(new_score[0]) scores_list.add(new_score[1]) # Return images of player two's score new_score = scoreboard.display_score(player_two_score, 0) # Adds player two's score to list scores_list.add(new_score[0]) scores_list.add(new_score[1]) # --------------- Draw To Display --------------- # ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT current_level.draw(screen) scoreboard_list.draw(screen) scores_list.draw(screen) active_sprite_list.draw(screen) # ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT # Limit to 60 frames per second clock.tick(60) # Go ahead and update the screen with what we've drawn. pygame.display.flip() if player_one_score >= 20: print 'PLAYER ONE WINS!!' done = True elif player_two_score >= 20: print 'PLAYER TWO WINS!!' done = True # Be IDLE friendly. If you forget this line, the program will 'hang' # on exit. pygame.quit()
def game_1(car_dir=node.Direction.South, cur_nd=1, move=True): point = score.Scoreboard('data/UID_405_20180502.csv') action_list, order, total_path = mz.strategy_1(car_dir, cur_nd) #action_list = [3, 5, 2, 1, 3, 6] #order, total_path = mz.BFS() i = 0 j = 0 rfid_list = [] Interface.sendstartsignal(move) #SerialWrite('s') while True: try: if Interface.ser.ser.in_waiting != 0: readstring = Interface.ser.SerialReadString() #print out trash print("arduino(readstring): ", readstring) #cur_nd = next_nd #next_nd = total_path[i+1] act = action_list[i] if 'q' in readstring: #at node print('python node') if act == 5: #at deadend Interface.send_action(act) time.sleep(1) i += 1 Interface.send_action(action_list[i]) time.sleep(1) read = Interface.ser.SerialReadString() else: Interface.send_action(act) print('current node:', total_path[j]) print(act) i += 1 j += 1 elif 'y' in readstring: print("arduino print(read): ", read) #read = Interface.ser.SerialReadString() time.sleep(0.5) rfid_hex = Interface.wait_for_node() ### rfid_list.append(rfid_hex) print('rfid_hex', rfid_hex) print(rfid_list) ### #rfid_hex = Interface.wait_for_node() point.add_UID(rfid_hex) mz.deadend = mz.deadend[1:] if act == 6: #should stop Interface.end_process() break except KeyboardInterrupt: instruction = str(input('Continue(c) or End(e):')) if instruction == 'c': print(mz.deadend) direc = int( input( 'Please input current direction:(1->N, 2->S, 3->W, 4->E)' )) cur_nd = int(input('Please input current node:')) game_1(direc, cur_nd, False) elif instruction == 'e': print(rfid_list) print('---complete---') print('') print('The total score: ', point.getCurrentScore()) Interface.end_process() break print(rfid_list) print('---complete---') print('') print('The total score: ', point.getCurrentScore())
def main(): maze = mz.Maze("data/small_maze.csv") point = score.Scoreboard("data/UID.csv", "team_NTUEE") interf = interface.interface() # TODO : Initializ1e necessary variables if (sys.argv[1] == '0'): print("Mode 0: for treasure-hunting") direction = "2" # input("Enter the initial direction:(1,2,3,4)") route = maze.strategy("6") print(route) start = 0 action = maze.getAction(direction, route[start], route[start + 1]) interf.send_action(action) direction = str( int(maze.nd_dict[route[start]].getDirection(route[start + 1]))) start += 1 while (start < (len(route) - 1)): command = interf.get_command() print(command) if command == "n": action = maze.getAction(direction, route[start], route[start + 1]) interf.send_action(action) if action == mz.Action(2): uidcode = interf.get_UID() print(uidcode) point.add_UID(str(uidcode)) print(point.getCurrentScore()) direction = str( int(maze.nd_dict[route[start]].getDirection(route[start + 1]))) start += 1 # if uidcode != 0: # point.add_UID(str(uidcode)) # print(point.getCurrentScore()) # print(uidcode) # point.add_UID(str(uidcode)) # print(point.getCurrentScore()) command = interf.get_command() interf.send_action(mz.Action(5)) print(route) # TODO : for treasure-hunting, which encourages you to hunt as many scores as possible elif (sys.argv[1] == '1'): # TODO: You can write your code to test specific function. print("Mode 1: for checkpoint") direction = "2" # input("Enter the initial direction:(1,2,3,4)") in_node = 1 interf.send_action(mz.Action(1)) while (in_node < 13): command = interf.get_command() if command == "n": if in_node == 1 or in_node == 2 or in_node == 3: interf.send_action(mz.Action(1)) in_node += 1 elif in_node == 4: interf.send_action(mz.Action(3)) in_node += 1 elif in_node == 5: interf.send_action(mz.Action(2)) in_node += 1 uidcode = interf.get_UID() print(uidcode) point.add_UID(str(uidcode)) print(point.getCurrentScore()) elif in_node == 6: interf.send_action(mz.Action(1)) in_node += 1 elif in_node == 7: interf.send_action(mz.Action(3)) in_node += 1 elif in_node == 8: interf.send_action(mz.Action(4)) in_node += 1 elif in_node == 9: interf.send_action(mz.Action(3)) in_node += 1 elif in_node == 10: interf.send_action(mz.Action(2)) in_node += 1 uidcode = interf.get_UID() print(uidcode) point.add_UID(str(uidcode)) print(point.getCurrentScore()) elif in_node == 11: interf.send_action(mz.Action(1)) in_node += 1 elif in_node == 12: interf.send_action(mz.Action(5)) in_node += 1 print(in_node) command = interf.get_command() interf.send_action(mz.Action(5)) elif (sys.argv[1] == '2'): # TODO: You can write your code to test specific function. print("Mode 2: for checkpoint with backward") direction = "2" # input("Enter the initial direction:(1,2,3,4)") in_node = 1 interf.send_action(mz.Action(1)) while (in_node < 13): command = interf.get_command() if command == "n": if in_node == 1 or in_node == 2 or in_node == 3: interf.send_action(mz.Action(1)) in_node += 1 elif in_node == 4: interf.send_action(mz.Action(6)) in_node += 2 uidcode = interf.get_UID(2) print(uidcode) point.add_UID(str(uidcode)) print(point.getCurrentScore()) elif in_node == 6: interf.send_action(mz.Action(1)) in_node += 1 elif in_node == 7: interf.send_action(mz.Action(3)) in_node += 1 elif in_node == 8: interf.send_action(mz.Action(4)) in_node += 1 elif in_node == 9: interf.send_action(mz.Action(3)) in_node += 1 elif in_node == 10: interf.send_action(mz.Action(2)) in_node += 1 uidcode = interf.get_UID() print(uidcode) point.add_UID(str(uidcode)) print(point.getCurrentScore()) elif in_node == 11: interf.send_action(mz.Action(1)) in_node += 1 elif in_node == 12: interf.send_action(mz.Action(5)) in_node += 1 print(in_node) command = interf.get_command() interf.send_action(mz.Action(5)) interf.end_process()
def start_socket(): global point point = score.Scoreboard("data/UID.csv", "Team 3 ")
def main(): maze = mz.Maze("data/maze_test.csv") next_nd = maze.getStartPoint() node_dict = maze.getNodeDict() car_dir = Direction.SOUTH point = score.Scoreboard("data/UID.csv") interface = student.interface( ) # the part of calling student.py was commented out. # Do not have destination if (sys.argv[1] == '0'): print("Mode 0") while (1): #TODO: Impliment your algorithm here and return the UID for evaluation function # ================================================ # Basically, you will get a list of nodes and corresponding UID strings after the end of algorithm. # The function add_UID() would convert the UID string score and add it to the total score. # In the sample code, we call this function after getting the returned list. # You may place it to other places, just make sure that all the UID strings you get would be converted. # ================================================ # for i in ndList: # print(i.getIndex()) # ndList2 = maze.strategy(ndList[-1]) # for i in ndList2: # print(i.getIndex()) # ndList3 = maze.strategy(ndList2[-1]) # for i in ndList3: # print(i.getIndex()) ndList = [] deadend_node_num = 0 for node in maze.nodes: if len(node.getSuccessors()) == 1: deadend_node_num += 1 start_nd = next_nd BFS_list = [] for i in range(1, deadend_node_num + 1): BFS_list_run = maze.strategy(start_nd) BFS_list = BFS_list_run start_nd = BFS_list[-1] if i == deadend_node_num: ndList = ndList + BFS_list else: ndList = ndList + BFS_list[:-1] for j in BFS_list: print(j.getIndex()) ## Check the result for the whole BFS!!! for node in ndList: print(node.getIndex()) state_cmd = input("Please enter a mode command: ") interface.ser.SerialWrite(state_cmd) state_cmd = input("Please enter a mode command: ") interface.ser.SerialWrite(state_cmd) ## Testing encounter the node !! count = 0 for i in range(1, len(ndList)): current_node = ndList[i - 1] next_node = ndList[i] action = maze.getAction(car_dir, current_node, next_node) # current car position + to node => get action # print("Get Action: ", action, "\n") print("Current Car direction: ", car_dir) if action == mz.Action.ADVANCE: car_dir = car_dir elif action == mz.Action.U_TURN: if car_dir == Direction.NORTH: car_dir = Direction.SOUTH elif car_dir == Direction.SOUTH: car_dir = Direction.NORTH elif car_dir == Direction.WEST: car_dir = Direction.EAST elif car_dir == Direction.EAST: car_dir = Direction.WEST elif action == mz.Action.TURN_RIGHT: if car_dir == Direction.NORTH: car_dir = Direction.EAST elif car_dir == Direction.EAST: car_dir = Direction.SOUTH elif car_dir == Direction.SOUTH: car_dir = Direction.WEST elif car_dir == Direction.WEST: car_dir = Direction.NORTH elif action == mz.Action.TURN_LEFT: if car_dir == Direction.NORTH: car_dir = Direction.WEST elif car_dir == Direction.WEST: car_dir = Direction.SOUTH elif car_dir == Direction.SOUTH: car_dir = Direction.EAST elif car_dir == Direction.EAST: car_dir = Direction.NORTH # When car arrive to a node !!! while (1): python_get_information = interface.ser.SerialReadString() if python_get_information is 'N': count = count + 1 print("Get to a node!!\n") break # python_get_information = interface.ser.SerialReadString() # while python_get_information is not 'N': # python_get_information = interface.ser.SerialReadString() # print("Get to a node!!") # while(1): # python_get_information = interface.ser.SerialReadString() # if python_get_information is 'N': # print(python_get_information) # print("Get to a node!!\n") # break # Send the state to Arduino print("Get action: ", action) interface.send_action(action) # node = 0 # get_UID = "just a test" # point.add_UID(get_UID) print("Get action: ", mz.Action.HALT) interface.send_action(mz.Action.HALT) break # Have destination elif (sys.argv[1] == '1'): print("Mode 1") while (1): #TODO: Implement your algorithm here and return the UID for evaluation function nd = int(input("destination: ")) if (nd == 0): print("end process") print('') break try: nd = node_dict[nd] except: print("Your input is not a valid node !!") raise IndexError("No node!") # print(nd) # print(next_nd) ndList = maze.strategy_2(next_nd, nd) ###Testing for getting into a node !! state_cmd = input("Please enter a mode command: ") interface.ser.SerialWrite(state_cmd) state_cmd = input("Please enter a mode command: ") interface.ser.SerialWrite(state_cmd) ## Testing encounter the node !! count = 0 for i in range(1, len(ndList)): current_node = ndList[i - 1] next_node = ndList[i] action = maze.getAction(car_dir, current_node, next_node) # current car position + to node => get action # print("Get Action: ", action, "\n") print("Current Car direction: ", car_dir) if action == mz.Action.ADVANCE: car_dir = car_dir elif action == mz.Action.U_TURN: if car_dir == Direction.NORTH: car_dir = Direction.SOUTH elif car_dir == Direction.SOUTH: car_dir = Direction.NORTH elif car_dir == Direction.WEST: car_dir = Direction.EAST elif car_dir == Direction.EAST: car_dir = Direction.WEST elif action == mz.Action.TURN_RIGHT: if car_dir == Direction.NORTH: car_dir = Direction.EAST elif car_dir == Direction.EAST: car_dir = Direction.SOUTH elif car_dir == Direction.SOUTH: car_dir = Direction.WEST elif car_dir == Direction.WEST: car_dir = Direction.NORTH elif action == mz.Action.TURN_LEFT: if car_dir == Direction.NORTH: car_dir = Direction.WEST elif car_dir == Direction.WEST: car_dir = Direction.SOUTH elif car_dir == Direction.SOUTH: car_dir = Direction.EAST elif car_dir == Direction.EAST: car_dir = Direction.NORTH # When car arrive to a node !!! while (1): python_get_information = interface.ser.SerialReadString() print(python_get_information) if python_get_information is 'N': count = count + 1 print(python_get_information) print("Get to a node!!\n") break # python_get_information = interface.ser.SerialReadString() # while python_get_information is not 'N': # python_get_information = interface.ser.SerialReadString() # print("Get to a node!!") # while(1): # python_get_information = interface.ser.SerialReadString() # if python_get_information is 'N': # print(python_get_information) # print("Get to a node!!\n") # break # Send the state to Arduino print("Get action: ", action) interface.send_action(action) # node = 0 # get_UID = "just a test" # point.add_UID(get_UID) print("Get action: ", mz.Action.HALT) interface.send_action(mz.Action.HALT) break # Our testing mode elif (sys.argv[1] == '2'): print("Mode 2") while (1): state_cmd = input("Please enter a mode command: ") # if state_cmd == 's': # print(state_cmd) # if state_cmd == '': # print(state_cmd) # if state_cmd == 's': # print(state_cmd) # if state_cmd == 's': # print(state_cmd) # if state_cmd == 's': # print(state_cmd) # if state_cmd == 's': # print(state_cmd) interface.ser.SerialWrite(state_cmd) # interface.ser.SerialReadString() # if state_cmd == "break": # break """ node = 0 while(not node): node = interface.wait_for_node() interface.end_process() """ print("complete") print("") a = point.getCurrentScore() print("The total score: ", a)
def main(): maze = mz.Maze("data/small_maze.csv") interf = interface.interface() if (sys.argv[1] == '1'): sequence = input("Enter your sequence.(by index, split by spacebars): ") point = score.Scoreboard("data/UID.csv", "Mingto's English Corner", sys.argv[1]) # TODO : Initialize necessary variables if (sys.argv[1] == '0'): print("Mode 0: for treasure-hunting with rule 1") # TODO : for treasure-hunting with rule 1, which encourages you to hunt as many scores as possible maze.getStartPoint() message_L = [] direct_L = [] path = maze.strategy_from_far(maze.now) while len(path) != 0: direct_L.append(maze.getAction(maze.now_d, maze.now, path.pop(0))) while len(maze.deadend) != 0: path = maze.strategy(maze.now) while len(path) != 0: direct_L.append(maze.getAction(maze.now_d, maze.now, path.pop(0))) while len(direct_L) != 0: message_L.append(interf.save_action(direct_L.pop(0))) message_L.pop(0) #send all paths message = ''.join(message_L) interf.send_action(message) #score while True: UID = interf.get_UID() if UID != 0: point.add_UID(UID) now_score = point.getCurrentScore() print("score:", now_score) elif (sys.argv[1] == '1'): print("Mode 1: for treasure-hunting with rule 2") # TODO : for treasure-hunting with rule 2, which requires you to hunt as many specified treasures as possible maze.getStartPoint() message_L = [] direct_L = [] while len(sequence) != 0: path = maze.strategy_2(maze.now, sequence.pop(0)) while len(path) != 0: direct_L.append(maze.getAction(maze.now_d, maze.now, path.pop(0))) while len(direct_L) != 0: message_L.append(interf.save_action(direct_L.pop(0))) message_L.pop(0) #send all paths message = ''.join(message_L) interf.send_action(message) #score while True: UID = interf.get_UID() if UID != 0: point.add_UID(UID) now_score = point.getCurrentScore() print("score:", now_score) #TEST!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! elif (sys.argv[1] == '2'): while True: print(point.getCurrentScore()) #
def main(): nd_list=[] dir_List = [] maze = mz.Maze("test_dis.csv") next_nd = maze.getStartPoint() car_dir = Direction.SOUTH point = score.Scoreboard("UID_405.csv") #interface = student.interface() the part of calling student.py was commented out. ############################connect######################################################### ############################Game1############################################### if(sys.argv[1] == '0'): nd_list = maze.BFS(1.0) print(nd_list) breakcheck_ = 0 check_read = 1 while(1): for i in range(len(maze.dd_List)): if(maze.dd_List[i]!=0): nd_list_tmp = maze.BFS(nd_list[(len(nd_list)-1)]) for j in range(len(nd_list_tmp)): nd_list.append(nd_list_tmp[j]) break if(i==len(maze.dd_List)-1): breakcheck_=1 if(breakcheck_): break dir_List.append('U') for i in range(len(nd_list)-1): if(i==0): dir_last = 2 dir_next = maze.nodes[(int)(nd_list[0])-1].getDirection(maze.nodes[(int)(nd_list[1])-1]) else: dir_sum = maze.getdir(nd_list[i-1],nd_list[i],nd_list[i+1]) dir_last = (int)(((int)(dir_sum)) / 10) dir_next = ((int)(dir_sum)) % 10 direction = maze.dircheck(dir_last,dir_next) #if((direction == 'R') or (direction == 'L')): #dir_List.append('S') if(direction!='S'): dir_List.append(direction) dir_List.append('D') print(dir_List) bt = BT.bluetooth() bt.do_connect('COM3') _quit = False count = 0 bt.SerialWrite('U') #for i in range (100): #bt.SerialWrite('U') #sleep(0.05) #print('U') bt.SerialReadString() while _quit is False: #readstring = bt.SerialReadString() readstring = bt.SerialReadString() print(readstring) if ('N\n' in readstring): print("i read it") count = count+1 if(count<len(dir_List)): cmd = dir_List[count] bt.SerialWrite(cmd) print(cmd) if cmd != 'D': sleep(1.2) else: sleep(0.2) else: bt.SerialWrite('S') bt.SerialReadString() elif(readstring == ''): continue elif('R\n' in readstring): sleep(0.2) word = bt.SerialReadString() UID = '' for i in range(8): UID+=word[i] #UID = str(UID) print(UID) point.add_UID(UID) if count == len(dir_List): _quit = True bt.SerialWrite('S') print(bt.ser.isOpen()) bt.disconnect() #TODO: Impliment your algorithm here and return the UID for evaluation function # ================================================ # Basically, you will get a list of nodes and corresponding UID strings after the end of algorithm. # The function add_UID() would convert the UID string score and add it to the total score. # In the sample code, we call this function after getting the returned list. # You may place it to other places, just make sure that all the UID strings you get would be converted. # ================================================ #for i in range(1, len(ndList)): # node = 0 # get_UID = "just a test" # point.add_UID(get_UID) ############Game2################################################### elif(sys.argv[1] == '1'): next_nd = 1 now_nd = 1 count = 0 while (1): #TODO: Implement your algorithm here and return the UID for evaluation function now_nd = next_nd next_nd = int(input("destination: ")) count = count+1 nd_list_tmp = maze.BFS_2(now_nd,next_nd) for i in range(len(nd_list_tmp)): nd_list.append(nd_list_tmp[i]) if(count == 5): break dir_List.append('U') for i in range(len(nd_list)-1): if(i==0): dir_last = 2 dir_next = maze.nodes[(int)(nd_list[0])-1].getDirection(maze.nodes[(int)(nd_list[1])-1]) else: dir_sum = maze.getdir(nd_list[i-1],nd_list[i],nd_list[i+1]) dir_last = (int)(((int)(dir_sum)) / 10) dir_next = ((int)(dir_sum)) % 10 direction = maze.dircheck(dir_last,dir_next) #if((direction == 'R') or (direction == 'L')): #dir_List.append('S') dir_List.append(direction) dir_List.append('S') print(dir_List) bt = BT.bluetooth() bt.do_connect('COM1') _quit = False count = 0 while _quit is False: readstring = bt.SerialReadString() if readstring == 'Node encountered.': print("i read it") count = count+1 cmd = dir_List[count] bt.SerialWrite(cmd) if count == len(dir_List): _quit = True else: print("ya") point.add_UID(readstring) print(bt.ser.isOpen()) bt.disconnect() #for i in range(1, len(ndList)): # get_UID = "just a test" # point.add_UID(get_UID) """ node = 0 while(not node): node = interface.wait_for_node() interface.end_process() """ print("complete") print("") a = point.getCurrentScore() print("The total score: ", a)
def main(): maze = mz.Maze("maze_2.csv") now_nd = maze.getStartPoint() car_dir = Direction.SOUTH point = score.Scoreboard("UID_score_maze2.csv") interface = student.interface() #the part of calling student.py was commented out. if(sys.argv[1] == '0'): while (1): #TODO: Impliment your algorithm here and return the UID for evaluation function ndList = maze.strategy(now_nd,1,1,0.8) #the whole list of nodes should go get_UID=interface.wait_for_node() while get_UID == '0': get_UID=interface.wait_for_node() print(type(get_UID)) print('UID: ',get_UID) #UID from BT point.add_UID(get_UID) print('1 motion done') for next_nd in ndList: #nd: the node should go to // type : node act=int(maze.getAction(car_dir,now_nd,next_nd)) print('action: ',act) interface.send_action(act) #send action car_dir=now_nd.getDirection(next_nd) now_nd=next_nd get_UID=interface.wait_for_node() while get_UID == '0': get_UID=interface.wait_for_node() print(type(get_UID)) print('UID: ',get_UID) #UID from BT point.add_UID(get_UID) print('1 motion done') break # ================================================ # Basically, you will get a list of nodes and corresponding UID strings after the end of algorithm. # The function add_UID() would convert the UID string score and add it to the total score. # In the sample code, we call this function after getting the returned list. # You may place it to other places, just make sure that all the UID strings you get would be converted. # ================================================ elif(sys.argv[1] == '1'): while (1): #TODO: Implement your algorithm here and return the UID for evaluation function input_nd = int(input("destination: ")) if(input_nd == 0): print("end process") print('') break end_nd=maze.nd_dict[input_nd] ndList = maze.stategy_2(now_nd,end_nd) for next_nd in ndList: #nd: the node should go to // type : node interface.send_action(maze.getAction(car_dir,now_nd,next_nd))#send action car_dir=now_nd.getDirection(next_nd) now_nd=next_nd get_UID=interface.wait_for_node() while get_UID == '0': get_UID=interface.wait_for_node() print(type(get_UID)) print('UID: ',get_UID) #UID from BT point.add_UID(get_UID) print('1 motion done') """ node = 0 while(not node): node = interface.wait_for_node() interface.end_process() """ print("complete") print("") a = point.getCurrentScore() print("The total score: ", a)