コード例 #1
0
int btree_delete_ex (PBTREE btree, int node_idx, long target_key) {
// target is just a package for the key value.  the reference does not
// provide the address of the Elem instance to be deleted.

    // first find the node contain the Elem instance with the given key
    int parent_index_this = BTREE_INVALID_ELEMENT_IDX;
    PBTREE_ELEMENT found;
    int last_visted_node_idx;
    if (node_idx == BTREE_INVALID_NODE_IDX)
        node_idx = btree->root_node_idx;
    last_visted_node_idx = node_idx;
    found = btree_search_ex (btree, &last_visted_node_idx, target_key);
    if (!found)
        return 0;

    if (is_leaf(btree, last_visted_node_idx) && key_count(btree, last_visted_node_idx) > btree_minimum_keys())
        return vector_delete (btree, last_visted_node_idx, target_key);
    else if (is_leaf(btree, last_visted_node_idx)) {
        vector_delete (btree, last_visted_node_idx, target_key);
        // loop invariant: if _node_ is not null_ptr, it points to a node
        // that has lost an element and needs to import one from a sibling
        // or merge with a sibling and import one from its parent.
        // after an iteration of the loop, _node_ may become null or
        // it may point to its parent if an element was imported from the
        // parent and this caused the parent to fall below the minimum
        // element count.
        while (BTREE_IS_VALID_NODE_IDX(last_visted_node_idx)) {
            int right, left;
            // NOTE: the "this" pointer may no longer be valid after the first
            // iteration of this loop!!!
            if (last_visted_node_idx == find_root(btree) && is_leaf(btree, last_visted_node_idx))
                break;
            if (last_visted_node_idx == find_root(btree) && !is_leaf(btree, last_visted_node_idx)) // sanity check
                return 0;
            // is an extra element available from the right sibling (if any)
            right = right_sibling(btree, last_visted_node_idx, &parent_index_this);
            if (BTREE_IS_VALID_NODE_IDX(right) && key_count(btree, right) > btree_minimum_keys())
                last_visted_node_idx = rotate_from_right(btree, last_visted_node_idx, parent_index_this);
            else {
                // is an extra element available from the left sibling (if any)
                left = left_sibling(btree, last_visted_node_idx, &parent_index_this);
                if (BTREE_IS_VALID_NODE_IDX(left) && key_count(btree, left) > btree_minimum_keys())
                    last_visted_node_idx = rotate_from_left(btree, last_visted_node_idx, parent_index_this);
                else if (BTREE_IS_VALID_NODE_IDX(right))
                    last_visted_node_idx = merge_right(btree, last_visted_node_idx, parent_index_this);
                else if (BTREE_IS_VALID_NODE_IDX(left))
                    last_visted_node_idx = merge_left(btree, last_visted_node_idx, parent_index_this);
            }
        }
    }
    else {
        PBTREE_ELEMENT smallest_in_subtree = smallest_key_in_subtree(btree, found->subtree_node_idx);
        found->key = smallest_in_subtree->key;
        found->data_entry_idx = smallest_in_subtree->data_entry_idx;
        btree_delete_ex (btree, found->subtree_node_idx, smallest_in_subtree->key);
    }
    return 1;
}
コード例 #2
0
ファイル: 2048.c プロジェクト: ZeroKelvinKeyboard/MikeOS-Apps
int can_move_right(board *b)
{
	board *tmp;

	tmp = dup_board(b);

	shift_right(tmp);
	merge_right(tmp);
	shift_right(tmp);

	if (cmp_board(b, tmp)) {
		free(tmp);
		return 0;
	} else {
		free(tmp);
		return 1;
	}
}
コード例 #3
0
void motor_control(void)	//Different scenerios that will light up the LEDs in a certain way,
//and the motor reactions to the scenarios.
{
    if (!SeeLine.b.Left&&!SeeLine.b.CntLeft&&SeeLine.b.Center&&!SeeLine.b.CntRight&&!SeeLine.b.Right) straight_fwd();	//00100
    else if (SeeLine.b.Left&&!SeeLine.b.CntLeft&&!SeeLine.b.Center&&!SeeLine.b.CntRight&&!SeeLine.b.Right) spin_left();	//10000
    else if (SeeLine.b.Left&&SeeLine.b.CntLeft&&!SeeLine.b.Center&&!SeeLine.b.CntRight&&!SeeLine.b.Right) spin_left();	//11000
    else if (!SeeLine.b.Left&&SeeLine.b.CntLeft&&!SeeLine.b.Center&&!SeeLine.b.CntRight&&!SeeLine.b.Right) turn_left();	//01000
    else if (!SeeLine.b.Left&&SeeLine.b.CntLeft&&SeeLine.b.Center&&!SeeLine.b.CntRight&&!SeeLine.b.Right) turn_left();	//01100
    else if (!SeeLine.b.Left&&!SeeLine.b.CntLeft&&!SeeLine.b.Center&&SeeLine.b.CntRight&&!SeeLine.b.Right) turn_right();//00010
    else if (!SeeLine.b.Left&&!SeeLine.b.CntLeft&&SeeLine.b.Center&&SeeLine.b.CntRight&&!SeeLine.b.Right) turn_right();	//00110
    else if (!SeeLine.b.Left&&!SeeLine.b.CntLeft&&!SeeLine.b.Center&&!SeeLine.b.CntRight&&SeeLine.b.Right) spin_right();//00001
    else if (!SeeLine.b.Left&&!SeeLine.b.CntLeft&&!SeeLine.b.Center&&SeeLine.b.CntRight&&SeeLine.b.Right) spin_right();	//00011
    else if ( (SeeLine.B ) == 0b00000u) Check_Gaps();	//00000	//all LEDs OFF
    else if (!SeeLine.b.Left&&!SeeLine.b.CntLeft&&SeeLine.b.Center&&SeeLine.b.CntRight&&SeeLine.b.Right) right_right_angle_turns();	//00111
    else if (SeeLine.b.Left&&SeeLine.b.CntLeft&&SeeLine.b.Center&&!SeeLine.b.CntRight&&!SeeLine.b.Right) left_right_angle_turns();	//11100
    else if (!SeeLine.b.Left&&!SeeLine.b.CntLeft&&SeeLine.b.Center&&!SeeLine.b.CntRight&&SeeLine.b.Right) sharp_right_turns();		//00101
    else if (SeeLine.b.Left&&!SeeLine.b.CntLeft&&SeeLine.b.Center&&!SeeLine.b.CntRight&&!SeeLine.b.Right) sharp_left_turns();		//10100
    else if (SeeLine.b.Left&&!SeeLine.b.CntLeft&&!SeeLine.b.Center&&SeeLine.b.CntRight&&!SeeLine.b.Right) merge_right();			//10010
    else if (!SeeLine.b.Left&&SeeLine.b.CntLeft&&!SeeLine.b.Center&&!SeeLine.b.CntRight&&SeeLine.b.Right) merge_left();				//01001
    else if (SeeLine.b.Left&&SeeLine.b.CntLeft&&!SeeLine.b.Center&&SeeLine.b.CntRight&&!SeeLine.b.Right) merge_right();				//11010
    else if (!SeeLine.b.Left&&SeeLine.b.CntLeft&&!SeeLine.b.Center&&SeeLine.b.CntRight&&SeeLine.b.Right) merge_left();				//01011
    else if (SeeLine.b.Left&&SeeLine.b.CntLeft&&SeeLine.b.Center&&SeeLine.b.CntRight&&SeeLine.b.Right) final_ends();				//11111	//all LEDs ON
}
コード例 #4
0
ファイル: 2048.c プロジェクト: ZeroKelvinKeyboard/MikeOS-Apps
void play_game()
{
	board *b = &grid;

	do {

		switch (os_wait_for_key()) {
			case ESC_KEY:
				return;

			case UP_KEY:
				if (can_move_up(b)) {
					shift_up(b);
					merge_up(b);
					shift_up(b);
					add_big_int(&score, &merge_score);
					break;
				} else {
					continue;
				}

			case DOWN_KEY:
				if (can_move_down(b)) {
					shift_down(b);
					merge_down(b);
					shift_down(b);
					add_big_int(&score, &merge_score);
					break;
				} else {
					continue;
				}

			case LEFT_KEY:
				if (can_move_left(b)) {
					shift_left(b);
					merge_left(b);
					shift_left(b);
					add_big_int(&score, &merge_score);
					break;
				} else {
					continue;
				}

			case RIGHT_KEY:
				if (can_move_right(b)) {
					shift_right(b);
					merge_right(b);
					shift_right(b);
					add_big_int(&score, &merge_score);
					break;
				} else {
					continue;
				}

			default:
				continue;
		}

		add_tile(b);
		display_board(b);

	} while (!game_is_lost());
}