Skip to content

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 void
move_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;
case UP: {
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 int
check_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;
return FALSE;
}
s_board[blank->x][blank->y] = BLANK;
return TRUE;
}
static void
init_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
board(WINDOW *win, int starty, int startx, int lines, int cols,
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)
mvwaddch(win, j, i, ACS_HLINE);
for (i = startx; i <= endx; i += tile_width)
for (j = starty; j <= endy; ++j)
mvwaddch(win, j, i, ACS_VLINE);
mvwaddch(win, starty, startx, ACS_ULCORNER);
mvwaddch(win, endy, startx, ACS_LLCORNER);
mvwaddch(win, starty, endx, ACS_URCORNER);
mvwaddch(win, endy, endx, ACS_LRCORNER);
for (j = starty + tile_height; j <= endy - tile_height;
j += tile_height) {
mvwaddch(win, j, startx, ACS_LTEE);
mvwaddch(win, j, endx, ACS_RTEE);
for (i = startx + tile_width; i <= endx - tile_width;
i += tile_width)
mvwaddch(win, j, i, ACS_PLUS);
}
for (i = startx + tile_width; i <= endx - tile_width; i += tile_width) {
mvwaddch(win, starty, i, ACS_TTEE);
mvwaddch(win, endy, i, ACS_BTEE);
}
wrefresh(win);
}
static void
shuffle_board(int **s_board, int n)
{
int i, j, deltax, deltay;
int startx, starty;
starty = (LINES - n * HEIGHT) / 2;
startx = (COLS - n * WIDTH) / 2;
clear();
mvprintw(LINES - 1, 0, "Press F1 to Exit");
board(stdscr, starty, startx, n, n, WIDTH, HEIGHT);
deltay = HEIGHT / 2;
deltax = WIDTH / 2;
for (j = 0; j < n; ++j)
for (i = 0; i < n; ++i)
if (s_board[i][j] != BLANK)
mvprintw(starty + j * HEIGHT + deltay,
startx + i * WIDTH + deltax,
"%-2d", s_board[i][j]);
}
int
main(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);
cbreak();
shuffle_board(s_board, n);
while ((ch = getch()) != KEY_F(1)) {
switch (ch) {
case KEY_LEFT:
move_blank(RIGHT, s_board, n, &blank);
break;
case KEY_RIGHT:
move_blank(LEFT, s_board, n, &blank);
break;
case KEY_UP:
move_blank(DOWN, s_board, n, &blank);
break;
case KEY_DOWN:
move_blank(UP, s_board, n, &blank);
break;
}
shuffle_board(s_board, n);
if (check_win(s_board, n, &blank) == TRUE) {
mvprintw(LINES - 1, 0, "You Win !!!\n");
break;
}
}
endwin();
return EXIT_SUCCESS;
}