Exemple #1
0
 /*  
     Teleport a piece to random location. If the piece is rotatable,
     rotate the piece in random direction
 */
 void Piece::teleportRandom()
 {
     pos.row = rand() % 9;
     pos.col = rand() % 9;
     int num_rot = rand() % 4;
     if (can_rotate())
         do { rotate_c(); } while (num_rot-- > 0); 
 }
Exemple #2
0
pair *map_set(map *m, void *key, void *value) {
	map_node *y, *z; /* Top node to update balance factor, and parent. */
	map_node *p, *q; /* Iterator, and parent. */
	map_node *n;     /* Newly inserted node. */
	map_node *w;     /* New root of rebalanced subtree. */
	unsigned char dir = 0;			/* Direction to descend. */
	unsigned char ds[MAX_HEIGHT];	/* Cached comparison results. */
	int i = 0;						/* Number of cached results. */

	z = (map_node*)&m->x;
	y = m->x;
	for (q = z, p = y; p != NULL; q = p, p = p->children[dir]) {
		int c = m->_cmp(key, p->x->a);
		if (c == 0)
			return p->x;
		if (p->balance != 0) {
			z = q;
			y = p;
			i = 0;
		}
		ds[i++] = dir = c > 0;
	}

	n = q->children[dir] = malloc(sizeof(map_node));
	if (n == NULL)
		return NULL;

	m->size++;
	n->x = create_pair(key, value);
	n->left = n->right = NULL;
	n->balance = 0;
	if (y == NULL)
		return n->x;

	for (p = y, i = 0; p != n; p = p->children[ds[i]], i++) {
		if (ds[i] == 0)
			p->balance--;
		else
			p->balance++;
	}

	if (y->balance == -2) {
		w = (y->left->balance == -1) ? rotate_c(y->left, y) : rotate_a(y->left, y);
	} else if (y->balance == +2) {
		w = (y->right->balance == +1) ? rotate_d(y->right, y) : rotate_b(y->right, y);
	} else
		return n->x;
	z->children[y != z->left] = w;
	return n->x;
}