void transpose2Tiled(float *m, int n, int s1, int s2) { int i, j; if (s1<=s2) { printf("Tilesize s1 must be greater than tilesize s2\n"); exit(0); } if (n<=s1) { transpose(m, n, n); } else { for (i=0; i<n; i+=s1) { for (j=i; j<n; j+=s1) { //transpose 1st tile using sub-tiling. transpose1Tiled(M(m,n,i,j), n, s1, s2); if (i==j) { continue; } //transpose 2nd tile using sub-tiling. transpose1Tiled(M(m,n,j,i), n, s1, s2); //swap the tiles. swapTiles(M(m,n,i,j), M(m,n,j,i), s1, n); } } } }
/** * (i1, j1) is the position of the nxn sub-matrix in an NxN matrix. * s is the tilesize. * It is important to have the right tile size for optimal performance. * Find this using (?) */ void transpose1Tiled(float *m, int N, int n, int s) { int i, j; if (n<=s) { transpose(m, n, n); } else { for (i=0; i<n; i+=s) { for (j=i; j<n; j+=s) { //transpose 1st tile //A(i, j)(i+s, j+s) is the 1st tile. transpose(M(m,N,i,j), s, N); if (i==j) { //only the above transpose needed since this is a diagonal tile. //only tiles above/below the diagonal need swapping and transposing. continue; } //transpose 2nd tile //A(j, i)(j+s, i+s) is the 2nd tile. transpose(M(m,N,j,i), s, N); //swap the tiles swapTiles(M(m,N,i,j), M(m,N,j,i), s, N); } } } }
static void activate(int val) { if (self->mental != -3) { if (val == 5) { self->health = 1 - self->health; } else if (self->health == 0) { switch (val) { case 1: self->endY -= TILE_SIZE; break; case 2: self->endY += TILE_SIZE; break; case 3: self->endX -= TILE_SIZE; break; default: self->endX += TILE_SIZE; break; } if (self->endX < self->startX) { self->endX = self->startX; } else if (self->endX >= self->startX + self->w) { self->endX = self->startX + self->w - TILE_SIZE; } else if (self->endY < self->startY) { self->endY = self->startY; } else if (self->endY >= self->startY + self->h) { self->endY = self->startY + self->h - TILE_SIZE; } } else if (self->health == 1) { swapTiles(self->endX, self->endY, val); } } }
/** * Initializes the game's board with tiles numbered 1 through d*d - 1, * (i.e., fills board with values but does not actually print them), * whereby board[i][j] represents row i and column j. */ void init(void) { for (int i = 0; i < d; i++) for (int j = 0; j < d; j++) board[i][j] = getReverseTile(i, j); if (d % 2 == 0) swapTiles(1, 2); }
void transposeCacheOblivious(float *m, int N, int n, int i, int j) { int temp; // printf("N= %d, n=%d, i=%d, j=%d\n", N, n, i, j); if (n==2) { //base case: if matrix size is 2x2, swap m(0,1) with m(1,0) // printf("Swapping %d, %d\n", *M(m,N,i,j+1), *M(m,N,i+1,j)); // printf("at cell number %d and %d\n", N*i+j+1, N*(i+1)+j); temp = *M(m,N,i,j+1); *M(m,N,i,j+1) = *M(m,N,i+1,j); *M(m,N,i+1,j) = temp; return; } transposeCacheOblivious(m, N, n/2, i, j); transposeCacheOblivious(m, N, n/2, i, j+n/2); transposeCacheOblivious(m, N, n/2, i+n/2, j); transposeCacheOblivious(m, N, n/2, i+n/2, j+n/2); swapTiles(M(m,N,i,j+n/2), M(m,N,i+n/2,j), n/2, N); }
PRIVATE void slideTiles(MAKE_THIS(MainWindow), int xDirection, int yDirection){ SELFREF_INIT; int traversX[4], traversY[4], i, y, j, x; struct coords farthestCoords; BOOL moved = FALSE; for (i = 0; i < 4; i++){ traversX[i] = xDirection < 0 ? i : 3 - i; traversY[i] = yDirection < 0 ? i : 3 - i; } for (i = 0, y = traversY[i]; i < 4; i++, y = traversY[i]){ for (j = 0, x = traversX[j]; j < 4; j++, x = traversX[j]){ if (this->tiles[y][x]){ farthestCoords = findFarthest(this, this->tiles[y][x], xDirection, yDirection); if (farthestCoords.x != x || farthestCoords.y != y){ if (this->tiles[farthestCoords.y][farthestCoords.x]){ /* We need a merger! */ deleteTile(this->tiles[farthestCoords.y][farthestCoords.x]); this->tiles[farthestCoords.y][farthestCoords.x] = NULL; this->tiles[y][x]->merged = TRUE; $(this->tiles[y][x])_incNumber(); } $(this->tiles[y][x])_moveTile(farthestCoords.y, farthestCoords.x); swapTiles(this->tiles, y, x, farthestCoords.y, farthestCoords.x); moved = TRUE; } } } } if (moved){ for (i = 0; i < 4; i++) for (j = 0; j < 4; j++) if (this->tiles[i][j]) this->tiles[i][j]->merged = FALSE; addRandomTile(this); } flushMessageQueue(); }
static void randomize() { int i, *tiles, boardWidth, tileWidth, x, y, dir; Entity *e; tileWidth = self->target->w; boardWidth = self->w; tiles = malloc(9 * sizeof(int)); if (tiles == NULL) { showErrorAndExit("Failed to allocate a whole %d bytes for the Jigsaw Puzzle display", (int)sizeof(int) * 9); } for (i=0;i<9;i++) { tiles[i] = i * tileWidth; } i = 0; for (e=self->target;e!=NULL;e=e->target) { e->targetX = self->x + (tiles[i] % boardWidth); e->targetY = self->y + (tiles[i] / boardWidth) * tileWidth; e->x = e->targetX; e->y = e->targetY; setEntityAnimationByID(e, i); e->health = i; i++; } if (self->mental != -3) { for (i=0;i<9;i++) { tiles[i] = i; } for (i=0;i<9;i++) { swap(&tiles[i], &tiles[prand() % 9]); } i = 0; self->mental = -10; for (i=0;i<10000;i++) { x = self->x + (TILE_SIZE * (prand() % 3)); y = self->y + (TILE_SIZE * (prand() % 3)); dir = 1 + (prand() % 4); swapTiles(x, y, dir); } self->endX = self->x; self->endY = self->y; self->mental = 1; } }