Exemplo n.º 1
0
    void fill(OutputIterator first, OutputIterator last, command_queue &queue)
    {
        const buffer &buffer = first.get_buffer();
        const size_t size = detail::iterator_range_size(first, last);

        kernel fill_kernel(m_program, "fill");
        fill_kernel.set_arg(0, m_state_buffer);
        fill_kernel.set_arg(1, buffer);

        size_t p = 0;

        for(;;){
            size_t count = 0;
            if(size - p >= n)
                count = n;
            else
                count = size - p;

            fill_kernel.set_arg(2, static_cast<uint_>(p));
            queue.enqueue_1d_range_kernel(fill_kernel, 0, count, 0);

            p += n;

            if(p >= size)
                break;

            generate_state(queue);
        }
    }
    void generate(OutputIterator first, OutputIterator last, command_queue &queue)
    {
        const size_t size = detail::iterator_range_size(first, last);

        kernel fill_kernel(m_program, "fill");
        fill_kernel.set_arg(0, m_state_buffer);
        fill_kernel.set_arg(2, first.get_buffer());

        size_t offset = 0;
        size_t &p = m_state_index;

        for(;;){
            size_t count = 0;
            if(size > n){
                count = n;
            }
            else {
                count = size;
            }
            fill_kernel.set_arg(1, static_cast<const uint_>(p));
            fill_kernel.set_arg(3, static_cast<const uint_>(offset));
            queue.enqueue_1d_range_kernel(fill_kernel, 0, count, 0);

            p += count;
            offset += count;

            if(offset >= size){
                break;
            }

            generate_state(queue);
            p = 0;
        }
    }
Exemplo n.º 3
0
//this function is gonna generate a solution of the puzzle
void frog_jumping_game_model::generate_all_states()
{
	generate_state(Root);
}
Exemplo n.º 4
0
void frog_jumping_game_model::generate_state(Node *state)
{
	//if this node is null return
	if(state == NULL)
	{
		return;
	}
    
	int win_flag = SOLUION;
    
	//  check if this node is a solution
	for(int i = 0; i < MAX_CHILD_NUM; i++)
	{
		if(state->current_state[i] != solution_array[i])
		{
			win_flag = NOT_A_SOLUION;
			break;
		}
	}
    
	//if this node is the solution, mark it and return
	if(win_flag == SOLUION)
	{
		state->is_solution = SOLUION;
		return ;
	}
    
	//fisrt of all, find empty stoen index to decide how many legal move we can move
	int empty_stone_index = 0;
	for(int i = 0; i < FROG_NUMBER; i++)
	{
		if(state->current_state[i] == ES)
		{
			empty_stone_index = i;
			break;
		}
	}
    
	//check all possible moves
	if(empty_stone_index == (FROG_NUMBER - 1))
	{
		//we have one move and one jump
		Node * move_node ;
		move_node = create_node();
        
		//copy all frogs from the above state to the new state then make the move
		for(int i = 0; i < FROG_NUMBER; i++)
		{
			move_node->current_state [i] = state->current_state [i];
		}
        
		//note that at this index, we have one move and one jump, we need to move fisrt then check if it legal
		//or not, if if legal move, then you can connect it to the state(make the state it's parent;
        
		int legal_or_illegal_move_check_variable = ILLEGAL_MOVE;
        
		//now move to left
		legal_or_illegal_move_check_variable = move_frog(move_node, (FROG_NUMBER - 1) - 1);
        
		//now we can connect it to the parnnt
		if(legal_or_illegal_move_check_variable == LEGAL_MOVE)
		{
			//find the index of the fisrt available child of the node
			store_node_on_state_node_child(move_node, state);
            
			//call the function recursivelly
			generate_state(move_node);
		}
        
		//the same thing must be here, but this time jump
        
		Node * jump_node;
		jump_node = create_node();
        
		//copy all frogs from the above state to the new state then make the move
		for(int i = 0; i < FROG_NUMBER; i++)
		{
			jump_node->current_state [i] = state->current_state[i];
		}
        
		int legal_or_illegal_jump_check_variable = ILLEGAL_MOVE;
        
		//now move to left
		legal_or_illegal_jump_check_variable = move_frog(jump_node, (FROG_NUMBER - 1) - 2);
        
		if(legal_or_illegal_jump_check_variable == LEGAL_MOVE)
		{
			store_node_on_state_node_child(jump_node, state);
            
			//call the function recussively
			generate_state(jump_node);
		}
	}
	else if(empty_stone_index == 0)
	{
		//we have one move and one jump
		Node * move_node ;
		move_node = create_node();
        
		//copy all frogs from the above state to the new state then make the move
		for(int i = 0; i < FROG_NUMBER; i++)
		{
			move_node->current_state [i] = state->current_state [i];
		}
        
		//note that at this index, we have one move and one jump, we need to move fisrt then check if it legal
		//or not, if if legal move, then you can connect it to the state(make the state it's parent;
        
		int legal_or_illegal_move_check_variable = ILLEGAL_MOVE;
        
		//now move to left
		legal_or_illegal_move_check_variable = move_frog(move_node, 1);
        
		//now we can connect it to the parnnt
		if(legal_or_illegal_move_check_variable == LEGAL_MOVE)
		{
			//find the index of the fisrt available child of the node
			store_node_on_state_node_child(move_node, state);
            
			//call the function recursivelly
			generate_state(move_node);
		}
        
		//the same thing must be here, but this time jump
        
		Node * jump_node;
		jump_node = create_node();
        
		//copy all frogs from the above state to the new state then make the move
		for(int i = 0; i < FROG_NUMBER; i++)
		{
			jump_node->current_state [i] = state->current_state[i];
		}
        
		int legal_or_illegal_jump_check_variable = ILLEGAL_MOVE;
        
		//now move to left
		legal_or_illegal_jump_check_variable = move_frog(jump_node, 2);
        
		if(legal_or_illegal_jump_check_variable == LEGAL_MOVE)
		{
			store_node_on_state_node_child(jump_node, state);
            
			//call the function recussively
			generate_state(jump_node);
		}
        
	}
	else
	{
		Node * first_move;
		first_move = create_node();
        
		//copy all frogs from the above state to the new state then make the move
		for(int i = 0; i < FROG_NUMBER; i++)
		{
			first_move->current_state [i] = state->current_state[i];
		}
        
		int legal_or_illegal_move_flag = ILLEGAL_MOVE;
		legal_or_illegal_move_flag = move_frog(first_move, empty_stone_index - 1);
        
		if(legal_or_illegal_move_flag == LEGAL_MOVE)
		{
			store_node_on_state_node_child(first_move, state);
            
			generate_state(first_move);
		}
		else
		{
			//free(fisrt_move);
		}
        
        
		Node * second_move;
		second_move = create_node();
        
		//copy all frogs from the above state to the new state then make the move
		for(int i = 0; i < FROG_NUMBER; i++)
		{
			second_move->current_state [i] = state->current_state[i];
		}
        
		legal_or_illegal_move_flag = ILLEGAL_MOVE;
		legal_or_illegal_move_flag = move_frog(second_move, empty_stone_index + 1);
        
		if(legal_or_illegal_move_flag == LEGAL_MOVE)
		{
			store_node_on_state_node_child(second_move, state);
            
			generate_state(second_move);
		}
        
		Node * fisrt_jump;
		fisrt_jump = create_node();
        
		//copy all frogs from the above state to the new state then make the move
		for(int i = 0; i < FROG_NUMBER; i++)
		{
			fisrt_jump->current_state [i] = state->current_state[i];
		}
        
		legal_or_illegal_move_flag = ILLEGAL_MOVE;
		legal_or_illegal_move_flag = move_frog(fisrt_jump, empty_stone_index - 2);
        
		if(legal_or_illegal_move_flag == LEGAL_MOVE)
		{
			store_node_on_state_node_child(fisrt_jump, state);
            
			generate_state(fisrt_jump);
		}
        
		Node * second_jump;
		second_jump = create_node();
        
		//copy all frogs from the above state to the new state then make the move
		for(int i = 0; i < FROG_NUMBER; i++)
		{
			second_jump->current_state [i] = state->current_state[i];
		}
        
		legal_or_illegal_move_flag = ILLEGAL_MOVE;
		legal_or_illegal_move_flag = move_frog(second_jump, empty_stone_index + 2);
        
		if(legal_or_illegal_move_flag == LEGAL_MOVE)
		{
			store_node_on_state_node_child(second_jump, state);
            
			generate_state(second_jump);
		}
        
		//we have two moves and two jumps
		return;
	}
}