static gboolean
move_puzzle (GtkWidget *grid, int dx, int dy)
{
GtkWidget *pos, *next;
GdkPaintable *piece;
guint next_x, next_y;
/* We don't move anything if the puzzle is solved */ if (solved) returnFALSE;
/* Return FALSE if we can't move to where the call *wantsustomove.
*/ if ((dx < 0 && pos_x < -dx) ||
dx + pos_x >= width ||
(dy < 0 && pos_y < -dy) ||
dy + pos_y >= height) returnFALSE;
/* Compute the new position */
next_x = pos_x + dx;
next_y = pos_y + dy;
/* Get the current and next image */
pos = gtk_grid_get_child_at (GTK_GRID (grid), pos_x, pos_y);
next = gtk_grid_get_child_at (GTK_GRID (grid), next_x, next_y);
/* Update the current position */
pos_x = next_x;
pos_y = next_y;
/* Return TRUE because we successfully moved the piece */ returnTRUE;
}
staticvoid
shuffle_puzzle (GtkWidget *grid)
{
guint i, n_steps;
/* Do this many random moves */
n_steps = width * height * 50;
for (i = 0; i < n_steps; i++)
{ /* Get a random number for the direction to move in */ switch (g_random_int_range (0, 4))
{ case0: /* left */
move_puzzle (grid, -1, 0); break;
case1: /* up */
move_puzzle (grid, 0, -1); break;
case2: /* right */
move_puzzle (grid, 1, 0); break;
case3: /* down */
move_puzzle (grid, 0, 1); break;
/* Nothing to check if the puzzle is already solved */ if (solved) returnTRUE;
/* If the empty cell isn't in the bottom right,
* the puzzle is obviously not solved */ if (pos_x != width - 1 ||
pos_y != height - 1) returnFALSE;
/* Check that all pieces are in the right position */ for (y = 0; y < height; y++)
{ for (x = 0; x < width; x++)
{
picture = gtk_grid_get_child_at (GTK_GRID (grid), x, y);
piece = gtk_picture_get_paintable (GTK_PICTURE (picture));
/* empty cell */ if (piece == NULL) continue;
if (gtk_puzzle_piece_get_x (GTK_PUZZLE_PIECE (piece)) != x ||
gtk_puzzle_piece_get_y (GTK_PUZZLE_PIECE (piece)) != y) returnFALSE;
}
}
/* We solved the puzzle!
*/
solved = TRUE;
/* Fill the empty cell to show that we're done.
*/
picture = gtk_grid_get_child_at (GTK_GRID (grid), 0, 0);
piece = gtk_picture_get_paintable (GTK_PICTURE (picture));
if (!move_puzzle (grid, dx, dy))
{ /* Make the error sound and then return TRUE. *Wehandledthiskey,eventhoughwedidn't *doanythingtothepuzzle.
*/
gtk_widget_error_bell (grid); returnTRUE;
}
check_solved (grid);
returnTRUE;
}
staticvoid
puzzle_button_pressed (GtkGestureClick *gesture, int n_press, double x, double y,
GtkWidget *grid)
{
GtkWidget *child; int l, t, i; int pos;
child = gtk_widget_pick (grid, x, y, GTK_PICK_DEFAULT);
if (!child)
{
gtk_widget_error_bell (grid); return;
}
/* add a picture for every cell */ for (y = 0; y < height; y++)
{ for (x = 0; x < width; x++)
{
GdkPaintable *piece;
/* Don't paint anything for the lsiding part of the video */ if (x == pos_x && y == pos_y)
piece = NULL; else
piece = gtk_puzzle_piece_new (paintable,
x, y,
width, height);
picture = gtk_picture_new_for_paintable (piece);
gtk_picture_set_content_fit (GTK_PICTURE (picture), GTK_CONTENT_FIT_FILL);
gtk_grid_attach (GTK_GRID (grid),
picture,
x, y, 1, 1);
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.