Пример #1
0
 def placeBlock(self,coordinates):
     # Checks whether the block can be placed
     check = False
     bottom_coordinates = coordinates.copy()
     bottom_coordinates[2] -= 1
     if self.isBlock(coordinates):
         message = 'There is already a block there'
         Debug.msg(message)
         Debug.error(message)
         raise RobotError(ErrorCode.E0015, message)
         return False
     elif coordinates[2] == 1:
         Debug.msg("The block is placed on the floor")
         self.block_manipulating.placeBlock(coordinates)
         self.blocks.append(self.block_manipulating)
         self.block_manipulating = []
         return True
     elif self.isBlock(bottom_coordinates):
         Debug.msg("The block is placed on another block")
         self.block_manipulating.placeBlock(coordinates)
         self.blocks.append(self.block_manipulating)
         self.block_manipulating = []
         return True 
     else:
         # Implement method for bridge block
         test_block = self.block_manipulating # Used to test whether the block can be placed on two points
         test_block.placeBlock(bottom_coordinates)
         possible_coordinates = test_block.get_positions
         #print('Possible coordinates {}'.format(possible_coordinates))
         blocks_below = 0
         for cord in possible_coordinates:
             #print('Coordinates to check : {}'.format(cord))
             if self.isBlock(cord):
                 blocks_below +=1
                 Debug.msg('There is a block below but not in the center')
         if blocks_below >= 2:
             Debug.msg("The block is placed on multiple other blocks")
             self.block_manipulating.placeBlock(coordinates)
             self.blocks.append(self.block_manipulating)
             self.block_manipulating = []
             return True  
         else:
             message = "Der Block kann nicht in der Luft losgelassen werden. Platziere den Block auf dem Boden oder auf einem anderen Block"
             Debug.error(message)
             raise RobotError(ErrorCode.E0015, message)
     return check 
Пример #2
0
    def load_challenges(self):
        ''' 
        Loads all challenges in the challenge/challenges_init folder with an .ini ending.
        '''

        _,challenge_paths = self.challenge_name_path()
        

        
        for path in challenge_paths:
            parser = configparser.ConfigParser()
            parser.read(path)
            
            # Generate a challenge template
            challenge = Challenge(parser['challenge']['name'])

            
            # Load block types
            block_types = parser['challenge']['block_types']
            block_types = block_types.replace('[','')
            block_types = block_types.replace(']','')
            block_types = block_types.split(",")
            block_types = [i.lower() for i in block_types]
            
            #Load generall options
            options = {}
            keys = ['type','info_text']
            for key in keys:
                if parser.has_option('challenge',key):
                    option = parser['challenge'][key]
                else:
                    option = ''
                options[key] = option
            challenge.add_options(options)
               
            
            
            # Load block dimensions
            for block_type in parser['Block_Dimensions']:
                dim = parser['Block_Dimensions'][block_type]
                dim = dim.replace('[','')
                dim = dim.replace(']','')
                dim = dim.split(',')
                dim = [int(i) for i in dim]
                block_type = block_type.lower()
                if block_type in block_types:
                    challenge.add_block_type(BlockType(block_type,dim))
                    #Debug.msg("Added Dimensions: {} type: {}".format(dim,block_type))
                else:
                    Debug.error("There is no block type {} (see [Block_Dimensions] in {})".format(block_type,path))
            challenge.init[2] = True
                


            
            # Load start positions
            for key in parser['Start_Position']:
                start_pos = parser['Start_Position'][key]
                start_pos= start_pos.replace('[','')
                start_pos = start_pos.replace(']','')
                start_pos = start_pos.split(",")
                # ToDo: Check i
                if len(start_pos) == 4:
                    angle = 0
                elif len(start_pos) == 5:
                    angle = int(start_pos[3])
                else:
                    print("Wrong start_position length. Example name = [x,y,z],rotation,type] or name = [x,y,z], type ")
                if challenge.valid_block_type(start_pos[-1]): 
                    dim = challenge.get_dim(start_pos[-1])
                    #print('dim:' ,dim)
                    challenge.add_start_position(Block([int(i) for i in start_pos[:3]],start_pos[-1].lower(),dim,_rotation=angle))
                    #Debug.msg("Added start positions: {} type: {}".format(start_pos[:3],start_pos[3].lower()))
                else:
                    Debug.error("invalid key of start block: {}".format(start_pos[3] ))
            challenge.reset()
            challenge.init[0] = True
                
                
            # Load final positions
            for key in parser['Final_Position']:
                final_pos = parser['Final_Position'][key]
                final_pos= final_pos.replace('[','')
                final_pos = final_pos.replace(']','')
                final_pos = final_pos.split(",")
                if len(final_pos) == 4:
                    angle = 0
                elif len(final_pos) == 5:
                    angle = int(final_pos[3])
                else:
                    print("Wrong start_position length. Example name = [x,y,z],rotation,type] or name = [x,y,z], type ")
                if challenge.valid_block_type(final_pos[-1]):
                    dim = challenge.get_dim(final_pos[-1])
                    #print('dim:' ,dim)
                    challenge.add_final_position(Block([int(i) for i in final_pos[:3]],final_pos[-1].lower(),dim,_rotation=angle))
                    #Debug.msg("Added final positions: {} type: {}".format(final_pos[:3],final_pos[3].lower()))
                else:
                    Debug.error("invalid key of final block: {}".format(final_pos[3] ))
            challenge.init[1] = True
            
            #challenge.debug_function() #Prints out the current blocks: only for debugging
            #Add the new challenge to the class
            self.__all_challenges.append(challenge)
Пример #3
0
    def challenge_name_path():
        
        ''' Returns the path and other basics of the challenge config file '''
        parent_path = os.path.dirname(os.path.dirname( os.path.abspath(__file__)))
        path = os.path.join(parent_path,"challenges/challenges_init")
        path_challenges = []
        file_ending = 'ini'
        challenge_names_unsorted = [] # names of the challenge
        challenge_names = []
        sample_text_unsorted = [] # text in the challenge textbox
        sample_text = []
        description_unsorted = [] # Info in the challenge description
        description = []
        priorities = [] # Priority of the challenge in the dropdown menu: A low number means that the challenge is higher up.
        for file in os.listdir(path):
            if file.endswith(file_ending):
                path_challenge = os.path.join(path,file)
                path_challenges.append(path_challenge)
                parser = configparser.ConfigParser()
                parser.read(path_challenge)
                if parser['CONFIG']['filetype'] == "challenge":
                    challenge_names_unsorted.append(parser['challenge']['name'])
                    if parser.has_option('challenge','sample_text'):
                        text = parser['challenge']['sample_text']
                        text = text.split('\\n')
                        new_text = ''
                        for line in text:
                            new_text = new_text + line + '\n'
                        sample_text_unsorted.append(new_text)
                    else:
                        sample_text_unsorted.append('')
                    if parser.has_option('challenge','description'):
                        text = parser['challenge']['description']
                        text = text.split('\\n')
                        new_text = ''
                        for line in text:
                            new_text = new_text + line + '\n'
                        description_unsorted.append(new_text)
                    else:
                        description_unsorted.append('')
                    if parser.has_option('challenge','priority'):
                        priorities.append(parser['challenge']['priority'])
                    else:
                        priorities.append(9999)
                   
                    
                        
                else:
                    Debug.error(path_challenge, 'is no valid challenge file. The file is not loaded. Please add challenge to the name in the challenge section.')
        
        
        #Sorting the lists   
        index_of_list = list(range(len(priorities)))  # This will be used as a sorted index list
        range_len = len(priorities)-1
        while range_len > 0:
            for idx in  range(range_len):
                if priorities[idx] > priorities[idx+1]:
                    tmp_priority = priorities[idx]
                    tmp_idx= index_of_list[idx]
                    priorities[idx] = priorities[idx+1]
                    index_of_list[idx] = index_of_list[idx+1]
                    priorities[idx+1] = tmp_priority
                    index_of_list[idx+1] = tmp_idx
            range_len -= 1
                    
        new_list_position = index_of_list # For clarity, we are now sorting the list according to the priorities
        

        for idx in new_list_position:
            challenge_names.append(challenge_names_unsorted[idx])
            description.append(description_unsorted[idx])
            sample_text.append(sample_text_unsorted[idx])
            
            
            
        challenge_infos = {'names':challenge_names,'sample_text' : sample_text, 'description' : description, 'priorities' : priorities}

        
        return [challenge_infos,path_challenges]
Пример #4
0
 def z(self):
      if self.pos_center[2] == None:
          Debug.error("Coordinate x not set yet")
          return False
      else:
          return self.pos_center[2]