Пример #1
0
void Table::UpdateRequisitions() {
    // Reset requisitions and expand flags, at first.
    for( std::size_t column_index = 0; column_index < m_columns.size(); ++column_index ) {
        m_columns[column_index].requisition = 0.f;
        m_columns[column_index].allocation = 0.f;
        m_columns[column_index].expand = false;
    }

    for( std::size_t row_index = 0; row_index < m_rows.size(); ++row_index ) {
        m_rows[row_index].requisition = 0.f;
        m_rows[row_index].allocation = 0.f;
        m_rows[row_index].expand = false;
    }

    // Iterate over children and add requisitions to columns and rows.
    TableCellList::iterator cell_iter( m_cells.begin() );
    TableCellList::iterator cell_iter_end( m_cells.end() );

    for( ; cell_iter != cell_iter_end; ++cell_iter ) {
        float col_requisition = (
                                    cell_iter->child->GetRequisition().x / static_cast<float>( cell_iter->rect.width ) +
                                    2 * cell_iter->padding.x
                                );
        sf::Uint32 col_bound = cell_iter->rect.left + cell_iter->rect.width;

        for( sf::Uint32 col_idx = cell_iter->rect.left; col_idx < col_bound; ++col_idx ) {
            m_columns[col_idx].requisition = std::max(
                                                 m_columns[col_idx].requisition,
                                                 col_requisition + (col_idx + 1 < m_columns.size() ? m_columns[col_idx].spacing : 0) // Add spacing if not last column.
                                             );

            // Set expand flag.
            if( (cell_iter->x_options & EXPAND) == EXPAND ) {
                m_columns[col_idx].expand = true;
            }
        }

        float row_requisition = (
                                    cell_iter->child->GetRequisition().y / static_cast<float>( cell_iter->rect.height ) +
                                    2 * cell_iter->padding.y
                                );
        sf::Uint32 row_bound = cell_iter->rect.top + cell_iter->rect.height;

        for( sf::Uint32 row_idx = cell_iter->rect.top; row_idx < row_bound; ++row_idx ) {
            m_rows[row_idx].requisition = std::max(
                                              m_rows[row_idx].requisition,
                                              row_requisition + (row_idx + 1 < m_rows.size() ? m_rows[row_idx].spacing : 0) // Add spacing if not last row.
                                          );

            // Set expand flag.
            if( (cell_iter->y_options & EXPAND) == EXPAND ) {
                m_rows[row_idx].expand = true;
            }
        }
    }

    AllocateChildren();
}
Пример #2
0
void Box::HandleRemove( const Widget::Ptr& child ) {
	ChildrenCont::iterator iter( std::find( m_children.begin(), m_children.end(), child ) );

	if( iter != m_children.end() ) {
		m_children.erase( iter );
	}

	AllocateChildren();
}
Пример #3
0
void Box::ReorderChild( const Widget::Ptr& widget, std::size_t position ) {
    ChildrenCont::iterator iter( std::find( m_children.begin(), m_children.end(), widget ) );

    if( iter == m_children.end() ) {
        return;
    }

    position = std::min( position, m_children.size() - 1 );

    ChildrenCont::iterator insertion_point( m_children.begin() );
    std::advance( insertion_point, position );
    m_children.insert( insertion_point, *iter );
    m_children.erase( iter );

    Refresh();
    AllocateChildren();
}
Пример #4
0
void Box::HandleAdd( const Widget::Ptr& child ) {
    Container::HandleAdd( child );

    ChildrenCont::const_iterator iter( std::find( m_children.begin(), m_children.end(), child ) );

    // If there's no ChildInfo present for the widget, the user added the widget
    // manually, which is not allowed for this class.
    if( iter == m_children.end() ) {

#ifdef SFGUI_DEBUG
        std::cerr << "SFGUI warning: Child must be added via Pack() for sfg::Box widgets.\n";
#endif

        Remove( child );
    }

    Refresh();
    AllocateChildren();
}
Пример #5
0
void Table::HandleSizeChange() {
    AllocateChildren();
}
Пример #6
0
void Table::HandleRequisitionChange() {
    AllocateChildren();
}
Пример #7
0
/*alphabeta pruning,  player.c's core, accurate description is inside the paper*/
static int  alphabeta(NODE **T, int depth, int a, int b, int maximizingPlayer) {


	if (depth == 0 || GetWin(*T)) { //if leaf...

		((*T)->player == MAX) ? ((*T)->value = -GetWin(*T)) : ((*T)->value = GetWin(*T)); //I assign a value to the node, -1 if opponent's win, +1 if AI win
		return (*T)->value;
	}

	if (maximizingPlayer == MAX) { //only if minimizing node

		NODE *tmp;
		unsigned short *v = NULL;
		unsigned int x, count;


		if (!AllocateChildren(T, depth, 0, 0, MIN, NULL)) { //first I allocate 1 child without giving any value, I do this to keep everything in one for-cycle

			printf("\nError");
			exit(0);
		}

		tmp = (*T)->leftchild;
		v = copyv((*T)->transboard); //I take the node's board and...
		for (x = 0, count = 0; x < M*N; x++) { //...I check every possibile move from that certain board state



			if (v[x] != 0) //if it's already occupied it's not an acceptable move so I go on
				continue;

			else {
				v[x] = maximizingPlayer; //I temporarily make a move on the free box (then it'll become empty once again)
				if (count == 0) { //means that I'm working with the first child, hence its address comes from the father and not from the brothers
					tmp->i = x / N; //saving the current move in the node
					tmp->j = x - N*(x / N);
					tmp->transboard = copyv(v); //and also the board
					count++; //increasing count in order not to belong to this case (first child) again
				}

				else { //if I already handled the first child

					if (!AllocateChildren(T, depth, x / N, x - N*(x / N), MIN, copyv(v))) { //I allocate the space for the brothers and their current values (board, i,j etc)

						printf("\nError");
						exit(0);
					}

					tmp = tmp->rightbrothers;
					count++;
				}

				tmp->value = Max(tmp->value, alphabeta(&tmp, depth - 1, a, b, maximizingPlayer == 1 ? 2 : 1)); //recursive call that goes to the other part (minimizing part) of the function (if father is maximizing then the children are minimizing because between father and children the player changes) 

				a = Max(a, tmp->value); //a is the best possible move for player1, so I have to choose the one with highest value
				v[x] = 0;
				if (a >= b) //pruning: if I see that the best possible move for player 1 has the same value of the best possibile move of player 2 I don't need to waste time to find another move nor to allocate more brothers
					break;



			}


		}

		return a;
	}




	else if (maximizingPlayer == MIN) { //same as above, just we're working with minimizing nodes

		NODE *tmp;
		unsigned short *v = NULL;
		unsigned int x, count;


		if (!AllocateChildren(T, depth, 0, 0, MAX, NULL)) {

			printf("\nError");
			exit(0);
		}

		tmp = (*T)->leftchild;
		v = copyv((*T)->transboard);
		for (x = 0, count = 0; x < M*N; x++) { //for every possible move...



			if (v[x] != 0)
				continue;

			else {
				v[x] = maximizingPlayer;
				if (count == 0) {
					tmp->i = x / N;
					tmp->j = x - N*(x / N);
					tmp->transboard = copyv(v);
					count++;
				}

				else {

					if (!AllocateChildren(T, depth, x / N, x - N*(x / N), MAX, copyv(v))) {

						printf("\nError");
						exit(0);
					}

					tmp = tmp->rightbrothers;
					count++;
				}

				tmp->value = Min(tmp->value, alphabeta(&tmp, depth - 1, a, b, maximizingPlayer == 1 ? 2 : 1)); //again, here the recursive call inverts minimizing with maximizing

				b = Min(b, tmp->value); //b instead chooses the minimum value because it has to pick the move that helps player1 less (=best move for player2 = optimal strategy)
				v[x] = 0;
				if (a >= b) //as before
					break;



			}


		}

		return b;
	}


}
Пример #8
0
void Box::HandleSizeChange() {
    AllocateChildren();
}
Пример #9
0
void Box::HandleAllocationChange( const sf::FloatRect& /*old_allocation*/ ) {
	AllocateChildren();
}