Ejemplo n.º 1
0
void Map::informed_bff_search(Map &copy_map){
    std::priority_queue<node*,std::vector<node*>,comparator_functor> search_list; //list of current search nodes.
    std::queue<node*> neighbohrs;                       //results from add_all_possible
    node start(man,diamond_pos);
    wave(copy_map,man,diamond_pos);
    search_list.push(&start);                            //set first target
    closed_set.clear();
    std::string start_node_index = to_string(start.diamonds,start.man);
    std::string hash_index;
    node *current_node;
    std::unordered_map<std::string,node*>::iterator hash_ptr;
    while( !search_list.empty()) {
        current_node = search_list.top();
        if(game_complete(current_node)){
            std::cout << "//" << " found the goal. Number of nodes: " << closed_set.size() + search_list.size() << "\n";
            std::cout << "//" << " move length: " << current_node->path_length << "\n";
            //print the path as a string or as C code
            //print_path(copy_map,current_node);
            print_path_as_C_code(copy_map,current_node);
            clear_hashtable(closed_set,start_node_index);
            while(!search_list.empty()){
                current_node = search_list.top();
                delete current_node;
                search_list.pop();
            }
            return;
        }
        search_list.pop();

        hash_index = to_string(current_node->diamonds,current_node->man);
        if( closed_set.emplace(hash_index,current_node).second){         //if the configuration has not been visited
            neighbohrs = add_all_possible_paths(current_node,copy_map);  //this gives the possible paths
            while( !neighbohrs.empty() ) {                               //append these nodes to the list.
                current_node = neighbohrs.front();
                neighbohrs.pop();

                hash_index = to_string(current_node->diamonds,current_node->man);
                hash_ptr = closed_set.find(hash_index); ;

                if( hash_ptr != closed_set.end() ){ //if already visited
                    delete current_node;
                } else {
                    search_list.push(current_node);
                }
            }
        } else { //if already visited
            delete current_node;
        }
    }
    std::cout << "no solution\n";
    return;
}
Ejemplo n.º 2
0
void app_timer_event()
{
	static int completionTimer = 0;
	
	if (game_state == GameState_RUNNING)
	{
		for (int i = 0 ; i < GrabType_NBR_ELT ; i++)
		{
			if (grabbers[i].tick >= 0)
			{
				grabbers[i].tick++;
				
				if (grabbers[i].tick > GRABBER_TICKS)
				{
					grabbers[i].tick = 0;
					
					switch (grabbers[i].state)
					{
						case GrabberState_STOPPED:
						{
							break;
						}
						case GrabberState_EXTENDING:
						{
							ProgressResult result = can_progress((GrabType)i);
							if (result == ProgressResult_OK)
							{
								grabbers[i].length++;
							}
							else if (result == ProgressResult_COMPLETE)
							{
								score(grabbers[i].type);
								
								if (((scores[GrabType_TOP] >= 8) ||
									  (scores[GrabType_BOTTOM] >= 8) ||
									  (scores[GrabType_LEFT] >= 8) ||
									  (scores[GrabType_RIGHT] >= 8)))
								{
									/* Nb. This -- a return function in the middle of a function -- is horrible.
									I'll refactor at some point in the future! */
									
									game_complete(grabbers[i].type);
									
									return;
								}
								
								setup_nugget();
								grabbers[i].state = GrabberState_RETRACTING;
								grabbers[i].tick = GRABBER_TICKS;
							}
							else
							{
								grabbers[i].state = GrabberState_RETRACTING;
								grabbers[i].tick = GRABBER_TICKS;
							}
							break;
						}
						case GrabberState_RETRACTING:
						{
							if (grabbers[i].length > 0)
							{
								grabbers[i].length--;
							}
							
							if (grabbers[i].length == 0)
							{
								grabbers[i].state = GrabberState_STOPPED;
								grabbers[i].tick = -1;
							}
							break;
						}
						case GrabberState_RETRACTING_RESTART:
						{
							if (grabbers[i].length > 0)
							{
								grabbers[i].length--;
							}
							
							if (grabbers[i].length == 0)
							{
								grabbers[i].index = grabbers[i].next_index;
								grabbers[i].state = GrabberState_EXTENDING;
							}
							break;
						}
					}
					
					update_cells();
					update_LEDs();
				}
			}
		}
	}
	else if (game_state == GameState_INIT)
	{
		reset_cells();
		setup_nugget();
	}
	else if (game_state == GameState_GAME_COMPLETE)
	{
		completionTimer ++;
		
		if (completionTimer > COMPLETION_TICKS)
		{
			reset_game();
			update_LEDs();
			update_score_LEDs();
			completionTimer = 0;
		}
	}
}