Shuffle
Adapted from NCURSES Programming HOWTO, by Pradeep Padala. The program comes from the HOWTO’s example archive,
JustForFun/shuffle.c, maintained by Thomas E. Dickey.
A sliding-tile game, if you have time to kill. Slide the tiles with the arrow keys until they’re in order, or press F1 to give up. The board’s order is the program’s argument, and the Run button runs it with 4.
The program deals the tiles in a random order without checking that the result can be solved, and only half of all arrangements of a sliding-tile puzzle can be. In an unsolvable deal, such as one where everything is in order except two swapped tiles, no sequence of moves finishes the puzzle; press F1 and run it again.
Example: Shuffle
#include <stdlib.h>#include <string.h>#include <time.h>#include <curses.h>
#define STARTX 9#define STARTY 3#define WIDTH 6#define HEIGHT 4
#define BLANK 0
typedef struct _tile { int x; int y;} tile;
enum { LEFT, RIGHT, UP, DOWN};
static voidmove_blank(int direction, int **s_board, int n, tile *blank){ int temp;
switch (direction) { case LEFT: { if (blank->x != 0) { --blank->x; temp = s_board[blank->x][blank->y]; s_board[blank->x + 1][blank->y] = temp; s_board[blank->x][blank->y] = BLANK; } } break; case RIGHT: { if (blank->x != n - 1) { ++blank->x; temp = s_board[blank->x][blank->y]; s_board[blank->x - 1][blank->y] = temp; s_board[blank->x][blank->y] = BLANK; } } break; if (blank->y != 0) { --blank->y; temp = s_board[blank->x][blank->y]; s_board[blank->x][blank->y + 1] = temp; s_board[blank->x][blank->y] = BLANK; } } break; case DOWN: { if (blank->y != n - 1) { ++blank->y; temp = s_board[blank->x][blank->y]; s_board[blank->x][blank->y - 1] = temp; s_board[blank->x][blank->y] = BLANK; } } break; }}
static intcheck_win(int **s_board, int n, tile *blank){ int i, j;
s_board[blank->x][blank->y] = n * n; for (i = 0; i < n; ++i) for (j = 0; j < n; ++j) if (s_board[i][j] != j * n + i + 1) { s_board[blank->x][blank->y] = BLANK; }
s_board[blank->x][blank->y] = BLANK;}
static voidinit_board(int **s_board, int n, tile *blank){ int i, j, k; int *temp_board;
if ((temp_board = (int *) calloc((size_t) (n * n), sizeof(int))) == NULL) { perror("calloc"); exit(EXIT_FAILURE); }
srand((unsigned) time(NULL)); memset(blank, 0, sizeof(*blank)); for (i = 0; i < n * n; ++i) { repeat: k = rand() % (n * n); for (j = 0; j <= i - 1; ++j) { if (k == temp_board[j]) goto repeat; else temp_board[i] = k; } } k = 0; for (i = 0; i < n; ++i) { for (j = 0; j < n; ++j, ++k) { if (temp_board[k] == 0) { blank->x = i; blank->y = j; } s_board[i][j] = temp_board[k]; } } free(temp_board);}
static void int tile_width, int tile_height){ int endy, endx, i, j;
endy = starty + lines * tile_height; endx = startx + cols * tile_width;
for (j = starty; j <= endy; j += tile_height) for (i = startx; i <= endx; ++i) for (i = startx; i <= endx; i += tile_width) for (j = starty; j <= endy; ++j) for (j = starty + tile_height; j <= endy - tile_height; j += tile_height) { for (i = startx + tile_width; i <= endx - tile_width; i += tile_width) } for (i = startx + tile_width; i <= endx - tile_width; i += tile_width) { }}
static voidshuffle_board(int **s_board, int n){ int i, j, deltax, deltay; int startx, starty;
deltay = HEIGHT / 2; deltax = WIDTH / 2; for (j = 0; j < n; ++j) for (i = 0; i < n; ++i) if (s_board[i][j] != BLANK) startx + i * WIDTH + deltax, "%-2d", s_board[i][j]);}
intmain(int argc, char *argv[]){ int **s_board; int n, i, ch; tile blank;
if (argc != 2) { printf("Usage: %s <shuffle board order>\n", argv[0]); return EXIT_FAILURE; } n = atoi(argv[1]);
if ((s_board = (int **) calloc((size_t) n, sizeof(int *))) == NULL) { perror("s_board"); return EXIT_FAILURE; } for (i = 0; i < n; ++i) { if ((s_board[i] = (int *) calloc((size_t) n, sizeof(int))) == NULL) { perror("s_board[]"); return EXIT_FAILURE; } } init_board(s_board, n, &blank); shuffle_board(s_board, n); switch (ch) { move_blank(RIGHT, s_board, n, &blank); break; move_blank(LEFT, s_board, n, &blank); break; move_blank(DOWN, s_board, n, &blank); break; break; } shuffle_board(s_board, n); break; } } return EXIT_SUCCESS;}