mirror of
git://git.tartarus.org/simon/puzzles.git
synced 2025-04-21 08:01:30 -07:00
Initial checkin of a portable framework for writing small GUI puzzle
games. [originally from svn r4138]
This commit is contained in:
4
.cvsignore
Normal file
4
.cvsignore
Normal file
@ -0,0 +1,4 @@
|
||||
Makefile*
|
||||
net cube
|
||||
*.exe *.obj *.o
|
||||
*notes
|
21
Recipe
Normal file
21
Recipe
Normal file
@ -0,0 +1,21 @@
|
||||
# -*- makefile -*-
|
||||
#
|
||||
# This file describes which puzzle binaries are made up from which
|
||||
# object and resource files. It is processed into the various
|
||||
# Makefiles by means of a Perl script. Makefile changes should
|
||||
# really be made by editing this file and/or the Perl script, not
|
||||
# by editing the actual Makefiles.
|
||||
|
||||
!name puzzles
|
||||
|
||||
!makefile gtk Makefile
|
||||
#!makefile vc Makefile.vc
|
||||
|
||||
COMMON = midend malloc
|
||||
NET = net random tree234
|
||||
|
||||
net : [X] gtk COMMON NET
|
||||
#cube : [X] gtk COMMON CUBE
|
||||
|
||||
#net : [G] windows COMMON NET
|
||||
#cube : [G] windows COMMON CUBE
|
23
gtk.c
Normal file
23
gtk.c
Normal file
@ -0,0 +1,23 @@
|
||||
/*
|
||||
* gtk.c: GTK front end for my puzzle collection.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "puzzles.h"
|
||||
|
||||
void fatal(char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
fprintf(stderr, "fatal error: ");
|
||||
|
||||
va_start(ap, fmt);
|
||||
vfprintf(stderr, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
fprintf(stderr, "\n");
|
||||
exit(1);
|
||||
}
|
52
malloc.c
Normal file
52
malloc.c
Normal file
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* malloc.c: safe wrappers around malloc, realloc, free, strdup
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "puzzles.h"
|
||||
|
||||
/*
|
||||
* smalloc should guarantee to return a useful pointer - Halibut
|
||||
* can do nothing except die when it's out of memory anyway.
|
||||
*/
|
||||
void *smalloc(int size) {
|
||||
void *p;
|
||||
p = malloc(size);
|
||||
if (!p)
|
||||
fatal("out of memory");
|
||||
return p;
|
||||
}
|
||||
|
||||
/*
|
||||
* sfree should guaranteeably deal gracefully with freeing NULL
|
||||
*/
|
||||
void sfree(void *p) {
|
||||
if (p) {
|
||||
free(p);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* srealloc should guaranteeably be able to realloc NULL
|
||||
*/
|
||||
void *srealloc(void *p, int size) {
|
||||
void *q;
|
||||
if (p) {
|
||||
q = realloc(p, size);
|
||||
} else {
|
||||
q = malloc(size);
|
||||
}
|
||||
if (!q)
|
||||
fatal("out of memory");
|
||||
return q;
|
||||
}
|
||||
|
||||
/*
|
||||
* dupstr is like strdup, but with the never-return-NULL property
|
||||
* of smalloc (and also reliably defined in all environments :-)
|
||||
*/
|
||||
char *dupstr(char *s) {
|
||||
char *r = smalloc(1+strlen(s));
|
||||
strcpy(r,s);
|
||||
return r;
|
||||
}
|
6
midend.c
Normal file
6
midend.c
Normal file
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* midend.c: general middle fragment sitting between the
|
||||
* platform-specific front end and game-specific back end.
|
||||
* Maintains a move list, takes care of Undo and Redo commands, and
|
||||
* processes standard keystrokes for undo/redo/new/restart/quit.
|
||||
*/
|
624
net.c
Normal file
624
net.c
Normal file
@ -0,0 +1,624 @@
|
||||
/*
|
||||
* net.c: Net game.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "puzzles.h"
|
||||
#include "tree234.h"
|
||||
|
||||
/* Direction bitfields */
|
||||
#define R 0x01
|
||||
#define U 0x02
|
||||
#define L 0x04
|
||||
#define D 0x08
|
||||
#define LOCKED 0x10
|
||||
|
||||
/* Rotations: Anticlockwise, Clockwise, Flip, general rotate */
|
||||
#define A(x) ( (((x) & 0x07) << 1) | (((x) & 0x08) >> 3) )
|
||||
#define C(x) ( (((x) & 0x0E) >> 1) | (((x) & 0x01) << 3) )
|
||||
#define F(x) ( (((x) & 0x0C) >> 2) | (((x) & 0x03) << 2) )
|
||||
#define ROT(x, n) ( ((n)&3) == 0 ? (x) : \
|
||||
((n)&3) == 1 ? A(x) : \
|
||||
((n)&3) == 2 ? F(x) : C(x) )
|
||||
|
||||
/* X and Y displacements */
|
||||
#define X(x) ( (x) == R ? +1 : (x) == L ? -1 : 0 )
|
||||
#define Y(x) ( (x) == D ? +1 : (x) == U ? -1 : 0 )
|
||||
|
||||
/* Bit count */
|
||||
#define COUNT(x) ( (((x) & 0x08) >> 3) + (((x) & 0x04) >> 2) + \
|
||||
(((x) & 0x02) >> 1) + ((x) & 0x01) )
|
||||
|
||||
#define TILE_SIZE 32
|
||||
#define TILE_BORDER 1
|
||||
#define WINDOW_OFFSET 16
|
||||
|
||||
struct game_params {
|
||||
int width;
|
||||
int height;
|
||||
int wrapping;
|
||||
float barrier_probability;
|
||||
};
|
||||
|
||||
struct game_state {
|
||||
int width, height, wrapping, completed;
|
||||
unsigned char *tiles;
|
||||
unsigned char *barriers;
|
||||
};
|
||||
|
||||
#define OFFSET(x2,y2,x1,y1,dir,state) \
|
||||
( (x2) = ((x1) + (state)->width + X((dir))) % (state)->width, \
|
||||
(y2) = ((y1) + (state)->height + Y((dir))) % (state)->height)
|
||||
|
||||
#define index(state, a, x, y) ( a[(y) * (state)->width + (x)] )
|
||||
#define tile(state, x, y) index(state, (state)->tiles, x, y)
|
||||
#define barrier(state, x, y) index(state, (state)->barriers, x, y)
|
||||
|
||||
struct xyd {
|
||||
int x, y, direction;
|
||||
};
|
||||
|
||||
static int xyd_cmp(void *av, void *bv) {
|
||||
struct xyd *a = (struct xyd *)av;
|
||||
struct xyd *b = (struct xyd *)bv;
|
||||
if (a->x < b->x)
|
||||
return -1;
|
||||
if (a->x > b->x)
|
||||
return +1;
|
||||
if (a->y < b->y)
|
||||
return -1;
|
||||
if (a->y > b->y)
|
||||
return +1;
|
||||
if (a->direction < b->direction)
|
||||
return -1;
|
||||
if (a->direction > b->direction)
|
||||
return +1;
|
||||
return 0;
|
||||
};
|
||||
|
||||
static struct xyd *new_xyd(int x, int y, int direction)
|
||||
{
|
||||
struct xyd *xyd = snew(struct xyd);
|
||||
xyd->x = x;
|
||||
xyd->y = y;
|
||||
xyd->direction = direction;
|
||||
return xyd;
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
* Randomly select a new game seed.
|
||||
*/
|
||||
|
||||
char *new_game_seed(game_params *params)
|
||||
{
|
||||
/*
|
||||
* The full description of a Net game is far too large to
|
||||
* encode directly in the seed, so by default we'll have to go
|
||||
* for the simple approach of providing a random-number seed.
|
||||
*
|
||||
* (This does not restrict me from _later on_ inventing a seed
|
||||
* string syntax which can never be generated by this code -
|
||||
* for example, strings beginning with a letter - allowing me
|
||||
* to type in a precise game, and have new_game detect it and
|
||||
* understand it and do something completely different.)
|
||||
*/
|
||||
char buf[40];
|
||||
sprintf(buf, "%d", rand());
|
||||
return dupstr(buf);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
* Construct an initial game state, given a seed and parameters.
|
||||
*/
|
||||
|
||||
game_state *new_game(game_params *params, char *seed)
|
||||
{
|
||||
random_state *rs;
|
||||
game_state *state;
|
||||
tree234 *possibilities, *barriers;
|
||||
int w, h, x, y, nbarriers;
|
||||
|
||||
assert(params->width > 2);
|
||||
assert(params->height > 2);
|
||||
|
||||
/*
|
||||
* Create a blank game state.
|
||||
*/
|
||||
state = snew(game_state);
|
||||
w = state->width = params->width;
|
||||
h = state->height = params->height;
|
||||
state->wrapping = params->wrapping;
|
||||
state->completed = FALSE;
|
||||
state->tiles = snewn(state->width * state->height, unsigned char);
|
||||
memset(state->tiles, 0, state->width * state->height);
|
||||
state->barriers = snewn(state->width * state->height, unsigned char);
|
||||
memset(state->barriers, 0, state->width * state->height);
|
||||
|
||||
/*
|
||||
* Set up border barriers if this is a non-wrapping game.
|
||||
*/
|
||||
if (!state->wrapping) {
|
||||
for (x = 0; x < state->width; x++) {
|
||||
barrier(state, x, 0) |= U;
|
||||
barrier(state, x, state->height-1) |= D;
|
||||
}
|
||||
for (y = 0; y < state->height; y++) {
|
||||
barrier(state, y, 0) |= L;
|
||||
barrier(state, y, state->width-1) |= R;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Seed the internal random number generator.
|
||||
*/
|
||||
rs = random_init(seed, strlen(seed));
|
||||
|
||||
/*
|
||||
* Construct the unshuffled grid.
|
||||
*
|
||||
* To do this, we simply start at the centre point, repeatedly
|
||||
* choose a random possibility out of the available ways to
|
||||
* extend a used square into an unused one, and do it. After
|
||||
* extending the third line out of a square, we remove the
|
||||
* fourth from the possibilities list to avoid any full-cross
|
||||
* squares (which would make the game too easy because they
|
||||
* only have one orientation).
|
||||
*
|
||||
* The slightly worrying thing is the avoidance of full-cross
|
||||
* squares. Can this cause our unsophisticated construction
|
||||
* algorithm to paint itself into a corner, by getting into a
|
||||
* situation where there are some unreached squares and the
|
||||
* only way to reach any of them is to extend a T-piece into a
|
||||
* full cross?
|
||||
*
|
||||
* Answer: no it can't, and here's a proof.
|
||||
*
|
||||
* Any contiguous group of such unreachable squares must be
|
||||
* surrounded on _all_ sides by T-pieces pointing away from the
|
||||
* group. (If not, then there is a square which can be extended
|
||||
* into one of the `unreachable' ones, and so it wasn't
|
||||
* unreachable after all.) In particular, this implies that
|
||||
* each contiguous group of unreachable squares must be
|
||||
* rectangular in shape (any deviation from that yields a
|
||||
* non-T-piece next to an `unreachable' square).
|
||||
*
|
||||
* So we have a rectangle of unreachable squares, with T-pieces
|
||||
* forming a solid border around the rectangle. The corners of
|
||||
* that border must be connected (since every tile connects all
|
||||
* the lines arriving in it), and therefore the border must
|
||||
* form a closed loop around the rectangle.
|
||||
*
|
||||
* But this can't have happened in the first place, since we
|
||||
* _know_ we've avoided creating closed loops! Hence, no such
|
||||
* situation can ever arise, and the naive grid construction
|
||||
* algorithm will guaranteeably result in a complete grid
|
||||
* containing no unreached squares, no full crosses _and_ no
|
||||
* closed loops. []
|
||||
*/
|
||||
possibilities = newtree234(xyd_cmp);
|
||||
add234(possibilities, new_xyd(w/2, h/2, R));
|
||||
add234(possibilities, new_xyd(w/2, h/2, U));
|
||||
add234(possibilities, new_xyd(w/2, h/2, L));
|
||||
add234(possibilities, new_xyd(w/2, h/2, D));
|
||||
|
||||
while (count234(possibilities) > 0) {
|
||||
int i;
|
||||
struct xyd *xyd;
|
||||
int x1, y1, d1, x2, y2, d2, d;
|
||||
|
||||
/*
|
||||
* Extract a randomly chosen possibility from the list.
|
||||
*/
|
||||
i = random_upto(rs, count234(possibilities));
|
||||
xyd = delpos234(possibilities, i);
|
||||
x1 = xyd->x;
|
||||
y1 = xyd->y;
|
||||
d1 = xyd->direction;
|
||||
sfree(xyd);
|
||||
|
||||
OFFSET(x2, y2, x1, y1, d1, state);
|
||||
d2 = F(d1);
|
||||
#ifdef DEBUG
|
||||
printf("picked (%d,%d,%c) <-> (%d,%d,%c)\n",
|
||||
x1, y1, "0RU3L567D9abcdef"[d1], x2, y2, "0RU3L567D9abcdef"[d2]);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Make the connection. (We should be moving to an as yet
|
||||
* unused tile.)
|
||||
*/
|
||||
tile(state, x1, y1) |= d1;
|
||||
assert(tile(state, x2, y2) == 0);
|
||||
tile(state, x2, y2) |= d2;
|
||||
|
||||
/*
|
||||
* If we have created a T-piece, remove its last
|
||||
* possibility.
|
||||
*/
|
||||
if (COUNT(tile(state, x1, y1)) == 3) {
|
||||
struct xyd xyd1, *xydp;
|
||||
|
||||
xyd1.x = x1;
|
||||
xyd1.y = y1;
|
||||
xyd1.direction = 0x0F ^ tile(state, x1, y1);
|
||||
|
||||
xydp = find234(possibilities, &xyd1, NULL);
|
||||
|
||||
if (xydp) {
|
||||
#ifdef DEBUG
|
||||
printf("T-piece; removing (%d,%d,%c)\n",
|
||||
xydp->x, xydp->y, "0RU3L567D9abcdef"[xydp->direction]);
|
||||
#endif
|
||||
del234(possibilities, xydp);
|
||||
sfree(xydp);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Remove all other possibilities that were pointing at the
|
||||
* tile we've just moved into.
|
||||
*/
|
||||
for (d = 1; d < 0x10; d <<= 1) {
|
||||
int x3, y3, d3;
|
||||
struct xyd xyd1, *xydp;
|
||||
|
||||
OFFSET(x3, y3, x2, y2, d, state);
|
||||
d3 = F(d);
|
||||
|
||||
xyd1.x = x3;
|
||||
xyd1.y = y3;
|
||||
xyd1.direction = d3;
|
||||
|
||||
xydp = find234(possibilities, &xyd1, NULL);
|
||||
|
||||
if (xydp) {
|
||||
#ifdef DEBUG
|
||||
printf("Loop avoidance; removing (%d,%d,%c)\n",
|
||||
xydp->x, xydp->y, "0RU3L567D9abcdef"[xydp->direction]);
|
||||
#endif
|
||||
del234(possibilities, xydp);
|
||||
sfree(xydp);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Add new possibilities to the list for moving _out_ of
|
||||
* the tile we have just moved into.
|
||||
*/
|
||||
for (d = 1; d < 0x10; d <<= 1) {
|
||||
int x3, y3;
|
||||
|
||||
if (d == d2)
|
||||
continue; /* we've got this one already */
|
||||
|
||||
if (!state->wrapping) {
|
||||
if (d == U && y2 == 0)
|
||||
continue;
|
||||
if (d == D && y2 == state->height-1)
|
||||
continue;
|
||||
if (d == L && x2 == 0)
|
||||
continue;
|
||||
if (d == R && x2 == state->width-1)
|
||||
continue;
|
||||
}
|
||||
|
||||
OFFSET(x3, y3, x2, y2, d, state);
|
||||
|
||||
if (tile(state, x3, y3))
|
||||
continue; /* this would create a loop */
|
||||
|
||||
#ifdef DEBUG
|
||||
printf("New frontier; adding (%d,%d,%c)\n",
|
||||
x2, y2, "0RU3L567D9abcdef"[d]);
|
||||
#endif
|
||||
add234(possibilities, new_xyd(x2, y2, d));
|
||||
}
|
||||
}
|
||||
/* Having done that, we should have no possibilities remaining. */
|
||||
assert(count234(possibilities) == 0);
|
||||
freetree234(possibilities);
|
||||
|
||||
/*
|
||||
* Now compute a list of the possible barrier locations.
|
||||
*/
|
||||
barriers = newtree234(xyd_cmp);
|
||||
for (y = 0; y < state->height - (!state->wrapping); y++) {
|
||||
for (x = 0; x < state->width - (!state->wrapping); x++) {
|
||||
|
||||
if (!(tile(state, x, y) & R))
|
||||
add234(barriers, new_xyd(x, y, R));
|
||||
if (!(tile(state, x, y) & D))
|
||||
add234(barriers, new_xyd(x, y, D));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Now shuffle the grid.
|
||||
*/
|
||||
for (y = 0; y < state->height - (!state->wrapping); y++) {
|
||||
for (x = 0; x < state->width - (!state->wrapping); x++) {
|
||||
int orig = tile(state, x, y);
|
||||
int rot = random_upto(rs, 4);
|
||||
tile(state, x, y) = ROT(orig, rot);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* And now choose barrier locations. (We carefully do this
|
||||
* _after_ shuffling, so that changing the barrier rate in the
|
||||
* params while keeping the game seed the same will give the
|
||||
* same shuffled grid and _only_ change the barrier locations.
|
||||
* Also the way we choose barrier locations, by repeatedly
|
||||
* choosing one possibility from the list until we have enough,
|
||||
* is designed to ensure that raising the barrier rate while
|
||||
* keeping the seed the same will provide a superset of the
|
||||
* previous barrier set - i.e. if you ask for 10 barriers, and
|
||||
* then decide that's still too hard and ask for 20, you'll get
|
||||
* the original 10 plus 10 more, rather than getting 20 new
|
||||
* ones and the chance of remembering your first 10.)
|
||||
*/
|
||||
nbarriers = params->barrier_probability * count234(barriers);
|
||||
assert(nbarriers >= 0 && nbarriers <= count234(barriers));
|
||||
|
||||
while (nbarriers > 0) {
|
||||
int i;
|
||||
struct xyd *xyd;
|
||||
int x1, y1, d1, x2, y2, d2;
|
||||
|
||||
/*
|
||||
* Extract a randomly chosen barrier from the list.
|
||||
*/
|
||||
i = random_upto(rs, count234(barriers));
|
||||
xyd = delpos234(barriers, i);
|
||||
|
||||
assert(xyd != NULL);
|
||||
|
||||
x1 = xyd->x;
|
||||
y1 = xyd->y;
|
||||
d1 = xyd->direction;
|
||||
sfree(xyd);
|
||||
|
||||
OFFSET(x2, y2, x1, y1, d1, state);
|
||||
d2 = F(d1);
|
||||
|
||||
barrier(state, x1, y1) |= d1;
|
||||
barrier(state, x2, y2) |= d2;
|
||||
|
||||
nbarriers--;
|
||||
}
|
||||
|
||||
/*
|
||||
* Clean up the rest of the barrier list.
|
||||
*/
|
||||
{
|
||||
struct xyd *xyd;
|
||||
|
||||
while ( (xyd = delpos234(barriers, 0)) != NULL)
|
||||
sfree(xyd);
|
||||
|
||||
freetree234(barriers);
|
||||
}
|
||||
|
||||
random_free(rs);
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
game_state *dup_game(game_state *state)
|
||||
{
|
||||
game_state *ret;
|
||||
|
||||
ret = snew(game_state);
|
||||
ret->width = state->width;
|
||||
ret->height = state->height;
|
||||
ret->wrapping = state->wrapping;
|
||||
ret->completed = state->completed;
|
||||
ret->tiles = snewn(state->width * state->height, unsigned char);
|
||||
memcpy(ret->tiles, state->tiles, state->width * state->height);
|
||||
ret->barriers = snewn(state->width * state->height, unsigned char);
|
||||
memcpy(ret->barriers, state->barriers, state->width * state->height);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void free_game(game_state *state)
|
||||
{
|
||||
sfree(state->tiles);
|
||||
sfree(state->barriers);
|
||||
sfree(state);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
* Utility routine.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Compute which squares are reachable from the centre square, as a
|
||||
* quick visual aid to determining how close the game is to
|
||||
* completion. This is also a simple way to tell if the game _is_
|
||||
* completed - just call this function and see whether every square
|
||||
* is marked active.
|
||||
*/
|
||||
static unsigned char *compute_active(game_state *state)
|
||||
{
|
||||
unsigned char *active;
|
||||
tree234 *todo;
|
||||
struct xyd *xyd;
|
||||
|
||||
active = snewn(state->width * state->height, unsigned char);
|
||||
memset(active, 0, state->width * state->height);
|
||||
|
||||
/*
|
||||
* We only store (x,y) pairs in todo, but it's easier to reuse
|
||||
* xyd_cmp and just store direction 0 every time.
|
||||
*/
|
||||
todo = newtree234(xyd_cmp);
|
||||
add234(todo, new_xyd(state->width / 2, state->height / 2, 0));
|
||||
|
||||
while ( (xyd = delpos234(todo, 0)) != NULL) {
|
||||
int x1, y1, d1, x2, y2, d2;
|
||||
|
||||
x1 = xyd->x;
|
||||
y1 = xyd->y;
|
||||
sfree(xyd);
|
||||
|
||||
for (d1 = 1; d1 < 0x10; d1 <<= 1) {
|
||||
OFFSET(x2, y2, x1, y1, d1, state);
|
||||
d2 = F(d1);
|
||||
|
||||
/*
|
||||
* If the next tile in this direction is connected to
|
||||
* us, and there isn't a barrier in the way, and it
|
||||
* isn't already marked active, then mark it active and
|
||||
* add it to the to-examine list.
|
||||
*/
|
||||
if ((tile(state, x1, y1) & d1) &&
|
||||
(tile(state, x2, y2) & d2) &&
|
||||
!(barrier(state, x1, y1) & d1) &&
|
||||
!index(state, active, x2, y2)) {
|
||||
index(state, active, x2, y2) = 1;
|
||||
add234(todo, new_xyd(x2, y2, 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Now we expect the todo list to have shrunk to zero size. */
|
||||
assert(count234(todo) == 0);
|
||||
freetree234(todo);
|
||||
|
||||
return active;
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
* Process a move.
|
||||
*/
|
||||
game_state *make_move(game_state *state, int x, int y, int button)
|
||||
{
|
||||
game_state *ret;
|
||||
int tx, ty, orig;
|
||||
|
||||
/*
|
||||
* All moves in Net are made with the mouse.
|
||||
*/
|
||||
if (button != LEFT_BUTTON &&
|
||||
button != MIDDLE_BUTTON &&
|
||||
button != RIGHT_BUTTON)
|
||||
return NULL;
|
||||
|
||||
/*
|
||||
* The button must have been clicked on a valid tile.
|
||||
*/
|
||||
x -= WINDOW_OFFSET;
|
||||
y -= WINDOW_OFFSET;
|
||||
if (x < 0 || y < 0)
|
||||
return NULL;
|
||||
tx = x / TILE_SIZE;
|
||||
ty = y / TILE_SIZE;
|
||||
if (tx >= state->width || ty >= state->height)
|
||||
return NULL;
|
||||
if (tx % TILE_SIZE >= TILE_SIZE - TILE_BORDER ||
|
||||
ty % TILE_SIZE >= TILE_SIZE - TILE_BORDER)
|
||||
return NULL;
|
||||
|
||||
/*
|
||||
* The middle button locks or unlocks a tile. (A locked tile
|
||||
* cannot be turned, and is visually marked as being locked.
|
||||
* This is a convenience for the player, so that once they are
|
||||
* sure which way round a tile goes, they can lock it and thus
|
||||
* avoid forgetting later on that they'd already done that one;
|
||||
* and the locking also prevents them turning the tile by
|
||||
* accident. If they change their mind, another middle click
|
||||
* unlocks it.)
|
||||
*/
|
||||
if (button == MIDDLE_BUTTON) {
|
||||
ret = dup_game(state);
|
||||
tile(ret, tx, ty) ^= LOCKED;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* The left and right buttons have no effect if clicked on a
|
||||
* locked tile.
|
||||
*/
|
||||
if (tile(state, tx, ty) & LOCKED)
|
||||
return NULL;
|
||||
|
||||
/*
|
||||
* Otherwise, turn the tile one way or the other. Left button
|
||||
* turns anticlockwise; right button turns clockwise.
|
||||
*/
|
||||
ret = dup_game(state);
|
||||
orig = tile(ret, tx, ty);
|
||||
if (button == LEFT_BUTTON)
|
||||
tile(ret, tx, ty) = A(orig);
|
||||
else
|
||||
tile(ret, tx, ty) = C(orig);
|
||||
|
||||
/*
|
||||
* Check whether the game has been completed.
|
||||
*/
|
||||
{
|
||||
unsigned char *active = compute_active(ret);
|
||||
int x1, y1;
|
||||
int complete = TRUE;
|
||||
|
||||
for (x1 = 0; x1 < ret->width; x1++)
|
||||
for (y1 = 0; y1 < ret->height; y1++)
|
||||
if (!index(ret, active, x1, y1)) {
|
||||
complete = FALSE;
|
||||
goto break_label; /* break out of two loops at once */
|
||||
}
|
||||
break_label:
|
||||
|
||||
sfree(active);
|
||||
|
||||
if (complete)
|
||||
ret->completed = TRUE;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
* Routines for drawing the game position on the screen.
|
||||
*/
|
||||
|
||||
#ifndef TESTMODE /* FIXME: should be #ifdef */
|
||||
|
||||
int main(void)
|
||||
{
|
||||
game_params params = { 13, 11, TRUE, 0.1 };
|
||||
char *seed;
|
||||
game_state *state;
|
||||
unsigned char *active;
|
||||
|
||||
seed = "123";
|
||||
state = new_game(¶ms, seed);
|
||||
active = compute_active(state);
|
||||
|
||||
{
|
||||
int x, y;
|
||||
|
||||
printf("\033)0\016");
|
||||
for (y = 0; y < state->height; y++) {
|
||||
for (x = 0; x < state->width; x++) {
|
||||
if (index(state, active, x, y))
|
||||
printf("\033[1;32m");
|
||||
else
|
||||
printf("\033[0;31m");
|
||||
putchar("~``m`qjv`lxtkwua"[tile(state, x, y)]);
|
||||
}
|
||||
printf("\033[m\n");
|
||||
}
|
||||
printf("\017");
|
||||
}
|
||||
|
||||
free_game(state);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
61
puzzles.h
Normal file
61
puzzles.h
Normal file
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* puzzles.h: header file for my puzzle collection
|
||||
*/
|
||||
|
||||
#ifndef PUZZLES_PUZZLES_H
|
||||
#define PUZZLES_PUZZLES_H
|
||||
|
||||
#ifndef TRUE
|
||||
#define TRUE 1
|
||||
#endif
|
||||
#ifndef FALSE
|
||||
#define FALSE 0
|
||||
#endif
|
||||
|
||||
#define lenof(array) ( sizeof(array) / sizeof(*(array)) )
|
||||
|
||||
enum {
|
||||
LEFT_BUTTON = 0x1000,
|
||||
MIDDLE_BUTTON,
|
||||
RIGHT_BUTTON
|
||||
};
|
||||
|
||||
/*
|
||||
* Platform routines
|
||||
*/
|
||||
void fatal(char *fmt, ...);
|
||||
|
||||
/*
|
||||
* malloc.c
|
||||
*/
|
||||
void *smalloc(int size);
|
||||
void *srealloc(void *p, int size);
|
||||
void sfree(void *p);
|
||||
char *dupstr(char *s);
|
||||
#define snew(type) \
|
||||
( (type *) smalloc (sizeof (type)) )
|
||||
#define snewn(number, type) \
|
||||
( (type *) smalloc ((number) * sizeof (type)) )
|
||||
#define sresize(array, number, type) \
|
||||
( (type *) srealloc ((array), (len) * sizeof (type)) )
|
||||
|
||||
/*
|
||||
* random.c
|
||||
*/
|
||||
typedef struct random_state random_state;
|
||||
random_state *random_init(char *seed, int len);
|
||||
unsigned long random_upto(random_state *state, unsigned long limit);
|
||||
void random_free(random_state *state);
|
||||
|
||||
/*
|
||||
* Game-specific routines
|
||||
*/
|
||||
typedef struct game_params game_params;
|
||||
typedef struct game_state game_state;
|
||||
char *new_game_seed(game_params *params);
|
||||
game_state *new_game(game_params *params, char *seed);
|
||||
game_state *dup_game(game_state *state);
|
||||
void free_game(game_state *state);
|
||||
game_state *make_move(game_state *from, int x, int y, int button);
|
||||
|
||||
#endif /* PUZZLES_PUZZLES_H */
|
282
random.c
Normal file
282
random.c
Normal file
@ -0,0 +1,282 @@
|
||||
/*
|
||||
* random.c: Internal random number generator, guaranteed to work
|
||||
* the same way on all platforms. Used when generating an initial
|
||||
* game state from a random game seed; required to ensure that game
|
||||
* seeds can be exchanged between versions of a puzzle compiled for
|
||||
* different platforms.
|
||||
*
|
||||
* The generator is based on SHA-1. This is almost certainly
|
||||
* overkill, but I had the SHA-1 code kicking around and it was
|
||||
* easier to reuse it than to do anything else!
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "puzzles.h"
|
||||
|
||||
typedef unsigned long uint32;
|
||||
|
||||
typedef struct {
|
||||
uint32 h[5];
|
||||
unsigned char block[64];
|
||||
int blkused;
|
||||
uint32 lenhi, lenlo;
|
||||
} SHA_State;
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
* Core SHA algorithm: processes 16-word blocks into a message digest.
|
||||
*/
|
||||
|
||||
#define rol(x,y) ( ((x) << (y)) | (((uint32)x) >> (32-y)) )
|
||||
|
||||
static void SHA_Core_Init(uint32 h[5])
|
||||
{
|
||||
h[0] = 0x67452301;
|
||||
h[1] = 0xefcdab89;
|
||||
h[2] = 0x98badcfe;
|
||||
h[3] = 0x10325476;
|
||||
h[4] = 0xc3d2e1f0;
|
||||
}
|
||||
|
||||
static void SHATransform(uint32 * digest, uint32 * block)
|
||||
{
|
||||
uint32 w[80];
|
||||
uint32 a, b, c, d, e;
|
||||
int t;
|
||||
|
||||
for (t = 0; t < 16; t++)
|
||||
w[t] = block[t];
|
||||
|
||||
for (t = 16; t < 80; t++) {
|
||||
uint32 tmp = w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16];
|
||||
w[t] = rol(tmp, 1);
|
||||
}
|
||||
|
||||
a = digest[0];
|
||||
b = digest[1];
|
||||
c = digest[2];
|
||||
d = digest[3];
|
||||
e = digest[4];
|
||||
|
||||
for (t = 0; t < 20; t++) {
|
||||
uint32 tmp =
|
||||
rol(a, 5) + ((b & c) | (d & ~b)) + e + w[t] + 0x5a827999;
|
||||
e = d;
|
||||
d = c;
|
||||
c = rol(b, 30);
|
||||
b = a;
|
||||
a = tmp;
|
||||
}
|
||||
for (t = 20; t < 40; t++) {
|
||||
uint32 tmp = rol(a, 5) + (b ^ c ^ d) + e + w[t] + 0x6ed9eba1;
|
||||
e = d;
|
||||
d = c;
|
||||
c = rol(b, 30);
|
||||
b = a;
|
||||
a = tmp;
|
||||
}
|
||||
for (t = 40; t < 60; t++) {
|
||||
uint32 tmp = rol(a,
|
||||
5) + ((b & c) | (b & d) | (c & d)) + e + w[t] +
|
||||
0x8f1bbcdc;
|
||||
e = d;
|
||||
d = c;
|
||||
c = rol(b, 30);
|
||||
b = a;
|
||||
a = tmp;
|
||||
}
|
||||
for (t = 60; t < 80; t++) {
|
||||
uint32 tmp = rol(a, 5) + (b ^ c ^ d) + e + w[t] + 0xca62c1d6;
|
||||
e = d;
|
||||
d = c;
|
||||
c = rol(b, 30);
|
||||
b = a;
|
||||
a = tmp;
|
||||
}
|
||||
|
||||
digest[0] += a;
|
||||
digest[1] += b;
|
||||
digest[2] += c;
|
||||
digest[3] += d;
|
||||
digest[4] += e;
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
* Outer SHA algorithm: take an arbitrary length byte string,
|
||||
* convert it into 16-word blocks with the prescribed padding at
|
||||
* the end, and pass those blocks to the core SHA algorithm.
|
||||
*/
|
||||
|
||||
static void SHA_Init(SHA_State * s)
|
||||
{
|
||||
SHA_Core_Init(s->h);
|
||||
s->blkused = 0;
|
||||
s->lenhi = s->lenlo = 0;
|
||||
}
|
||||
|
||||
static void SHA_Bytes(SHA_State * s, void *p, int len)
|
||||
{
|
||||
unsigned char *q = (unsigned char *) p;
|
||||
uint32 wordblock[16];
|
||||
uint32 lenw = len;
|
||||
int i;
|
||||
|
||||
/*
|
||||
* Update the length field.
|
||||
*/
|
||||
s->lenlo += lenw;
|
||||
s->lenhi += (s->lenlo < lenw);
|
||||
|
||||
if (s->blkused && s->blkused + len < 64) {
|
||||
/*
|
||||
* Trivial case: just add to the block.
|
||||
*/
|
||||
memcpy(s->block + s->blkused, q, len);
|
||||
s->blkused += len;
|
||||
} else {
|
||||
/*
|
||||
* We must complete and process at least one block.
|
||||
*/
|
||||
while (s->blkused + len >= 64) {
|
||||
memcpy(s->block + s->blkused, q, 64 - s->blkused);
|
||||
q += 64 - s->blkused;
|
||||
len -= 64 - s->blkused;
|
||||
/* Now process the block. Gather bytes big-endian into words */
|
||||
for (i = 0; i < 16; i++) {
|
||||
wordblock[i] =
|
||||
(((uint32) s->block[i * 4 + 0]) << 24) |
|
||||
(((uint32) s->block[i * 4 + 1]) << 16) |
|
||||
(((uint32) s->block[i * 4 + 2]) << 8) |
|
||||
(((uint32) s->block[i * 4 + 3]) << 0);
|
||||
}
|
||||
SHATransform(s->h, wordblock);
|
||||
s->blkused = 0;
|
||||
}
|
||||
memcpy(s->block, q, len);
|
||||
s->blkused = len;
|
||||
}
|
||||
}
|
||||
|
||||
static void SHA_Final(SHA_State * s, unsigned char *output)
|
||||
{
|
||||
int i;
|
||||
int pad;
|
||||
unsigned char c[64];
|
||||
uint32 lenhi, lenlo;
|
||||
|
||||
if (s->blkused >= 56)
|
||||
pad = 56 + 64 - s->blkused;
|
||||
else
|
||||
pad = 56 - s->blkused;
|
||||
|
||||
lenhi = (s->lenhi << 3) | (s->lenlo >> (32 - 3));
|
||||
lenlo = (s->lenlo << 3);
|
||||
|
||||
memset(c, 0, pad);
|
||||
c[0] = 0x80;
|
||||
SHA_Bytes(s, &c, pad);
|
||||
|
||||
c[0] = (lenhi >> 24) & 0xFF;
|
||||
c[1] = (lenhi >> 16) & 0xFF;
|
||||
c[2] = (lenhi >> 8) & 0xFF;
|
||||
c[3] = (lenhi >> 0) & 0xFF;
|
||||
c[4] = (lenlo >> 24) & 0xFF;
|
||||
c[5] = (lenlo >> 16) & 0xFF;
|
||||
c[6] = (lenlo >> 8) & 0xFF;
|
||||
c[7] = (lenlo >> 0) & 0xFF;
|
||||
|
||||
SHA_Bytes(s, &c, 8);
|
||||
|
||||
for (i = 0; i < 5; i++) {
|
||||
output[i * 4] = (s->h[i] >> 24) & 0xFF;
|
||||
output[i * 4 + 1] = (s->h[i] >> 16) & 0xFF;
|
||||
output[i * 4 + 2] = (s->h[i] >> 8) & 0xFF;
|
||||
output[i * 4 + 3] = (s->h[i]) & 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
static void SHA_Simple(void *p, int len, unsigned char *output)
|
||||
{
|
||||
SHA_State s;
|
||||
|
||||
SHA_Init(&s);
|
||||
SHA_Bytes(&s, p, len);
|
||||
SHA_Final(&s, output);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
* The random number generator.
|
||||
*/
|
||||
|
||||
struct random_state {
|
||||
unsigned char seedbuf[40];
|
||||
unsigned char databuf[20];
|
||||
int pos;
|
||||
};
|
||||
|
||||
random_state *random_init(char *seed, int len)
|
||||
{
|
||||
random_state *state;
|
||||
|
||||
state = snew(random_state);
|
||||
|
||||
SHA_Simple(seed, len, state->seedbuf);
|
||||
SHA_Simple(state->seedbuf, 20, state->seedbuf + 20);
|
||||
SHA_Simple(state->seedbuf, 40, state->databuf);
|
||||
state->pos = 0;
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
unsigned long random_bits(random_state *state, int bits)
|
||||
{
|
||||
int ret = 0;
|
||||
int n;
|
||||
|
||||
for (n = 0; n < bits; n += 8) {
|
||||
if (state->pos >= 20) {
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 20; i++) {
|
||||
if (state->seedbuf[i] != 0xFF) {
|
||||
state->seedbuf[i]++;
|
||||
break;
|
||||
} else
|
||||
state->seedbuf[i] = 0;
|
||||
}
|
||||
SHA_Simple(state->seedbuf, 40, state->databuf);
|
||||
state->pos = 0;
|
||||
}
|
||||
ret = (ret << 8) | state->databuf[state->pos++];
|
||||
}
|
||||
|
||||
ret &= (1 << bits) - 1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
unsigned long random_upto(random_state *state, unsigned long limit)
|
||||
{
|
||||
int bits = 0;
|
||||
unsigned long max, divisor, data;
|
||||
|
||||
while ((limit >> bits) != 0)
|
||||
bits++;
|
||||
|
||||
bits += 3;
|
||||
assert(bits < 32);
|
||||
|
||||
max = 1 << bits;
|
||||
divisor = max / limit;
|
||||
max = limit * divisor;
|
||||
|
||||
do {
|
||||
data = random_bits(state, bits);
|
||||
} while (data >= max);
|
||||
|
||||
return data / divisor;
|
||||
}
|
||||
|
||||
void random_free(random_state *state)
|
||||
{
|
||||
sfree(state);
|
||||
}
|
202
tree234.h
Normal file
202
tree234.h
Normal file
@ -0,0 +1,202 @@
|
||||
/*
|
||||
* tree234.h: header defining functions in tree234.c.
|
||||
*
|
||||
* This file is copyright 1999-2001 Simon Tatham.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person
|
||||
* obtaining a copy of this software and associated documentation
|
||||
* files (the "Software"), to deal in the Software without
|
||||
* restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following
|
||||
* conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL SIMON TATHAM BE LIABLE FOR
|
||||
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
||||
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef TREE234_H
|
||||
#define TREE234_H
|
||||
|
||||
/*
|
||||
* This typedef is opaque outside tree234.c itself.
|
||||
*/
|
||||
typedef struct tree234_Tag tree234;
|
||||
|
||||
typedef int (*cmpfn234)(void *, void *);
|
||||
|
||||
typedef void *(*copyfn234)(void *state, void *element);
|
||||
|
||||
/*
|
||||
* Create a 2-3-4 tree. If `cmp' is NULL, the tree is unsorted, and
|
||||
* lookups by key will fail: you can only look things up by numeric
|
||||
* index, and you have to use addpos234() and delpos234().
|
||||
*/
|
||||
tree234 *newtree234(cmpfn234 cmp);
|
||||
|
||||
/*
|
||||
* Free a 2-3-4 tree (not including freeing the elements).
|
||||
*/
|
||||
void freetree234(tree234 *t);
|
||||
|
||||
/*
|
||||
* Add an element e to a sorted 2-3-4 tree t. Returns e on success,
|
||||
* or if an existing element compares equal, returns that.
|
||||
*/
|
||||
void *add234(tree234 *t, void *e);
|
||||
|
||||
/*
|
||||
* Add an element e to an unsorted 2-3-4 tree t. Returns e on
|
||||
* success, NULL on failure. (Failure should only occur if the
|
||||
* index is out of range or the tree is sorted.)
|
||||
*
|
||||
* Index range can be from 0 to the tree's current element count,
|
||||
* inclusive.
|
||||
*/
|
||||
void *addpos234(tree234 *t, void *e, int index);
|
||||
|
||||
/*
|
||||
* Look up the element at a given numeric index in a 2-3-4 tree.
|
||||
* Returns NULL if the index is out of range.
|
||||
*
|
||||
* One obvious use for this function is in iterating over the whole
|
||||
* of a tree (sorted or unsorted):
|
||||
*
|
||||
* for (i = 0; (p = index234(tree, i)) != NULL; i++) consume(p);
|
||||
*
|
||||
* or
|
||||
*
|
||||
* int maxcount = count234(tree);
|
||||
* for (i = 0; i < maxcount; i++) {
|
||||
* p = index234(tree, i);
|
||||
* assert(p != NULL);
|
||||
* consume(p);
|
||||
* }
|
||||
*/
|
||||
void *index234(tree234 *t, int index);
|
||||
|
||||
/*
|
||||
* Find an element e in a sorted 2-3-4 tree t. Returns NULL if not
|
||||
* found. e is always passed as the first argument to cmp, so cmp
|
||||
* can be an asymmetric function if desired. cmp can also be passed
|
||||
* as NULL, in which case the compare function from the tree proper
|
||||
* will be used.
|
||||
*
|
||||
* Three of these functions are special cases of findrelpos234. The
|
||||
* non-`pos' variants lack the `index' parameter: if the parameter
|
||||
* is present and non-NULL, it must point to an integer variable
|
||||
* which will be filled with the numeric index of the returned
|
||||
* element.
|
||||
*
|
||||
* The non-`rel' variants lack the `relation' parameter. This
|
||||
* parameter allows you to specify what relation the element you
|
||||
* provide has to the element you're looking for. This parameter
|
||||
* can be:
|
||||
*
|
||||
* REL234_EQ - find only an element that compares equal to e
|
||||
* REL234_LT - find the greatest element that compares < e
|
||||
* REL234_LE - find the greatest element that compares <= e
|
||||
* REL234_GT - find the smallest element that compares > e
|
||||
* REL234_GE - find the smallest element that compares >= e
|
||||
*
|
||||
* Non-`rel' variants assume REL234_EQ.
|
||||
*
|
||||
* If `rel' is REL234_GT or REL234_LT, the `e' parameter may be
|
||||
* NULL. In this case, REL234_GT will return the smallest element
|
||||
* in the tree, and REL234_LT will return the greatest. This gives
|
||||
* an alternative means of iterating over a sorted tree, instead of
|
||||
* using index234:
|
||||
*
|
||||
* // to loop forwards
|
||||
* for (p = NULL; (p = findrel234(tree, p, NULL, REL234_GT)) != NULL ;)
|
||||
* consume(p);
|
||||
*
|
||||
* // to loop backwards
|
||||
* for (p = NULL; (p = findrel234(tree, p, NULL, REL234_LT)) != NULL ;)
|
||||
* consume(p);
|
||||
*/
|
||||
enum {
|
||||
REL234_EQ, REL234_LT, REL234_LE, REL234_GT, REL234_GE
|
||||
};
|
||||
void *find234(tree234 *t, void *e, cmpfn234 cmp);
|
||||
void *findrel234(tree234 *t, void *e, cmpfn234 cmp, int relation);
|
||||
void *findpos234(tree234 *t, void *e, cmpfn234 cmp, int *index);
|
||||
void *findrelpos234(tree234 *t, void *e, cmpfn234 cmp, int relation,
|
||||
int *index);
|
||||
|
||||
/*
|
||||
* Delete an element e in a 2-3-4 tree. Does not free the element,
|
||||
* merely removes all links to it from the tree nodes.
|
||||
*
|
||||
* delpos234 deletes the element at a particular tree index: it
|
||||
* works on both sorted and unsorted trees.
|
||||
*
|
||||
* del234 deletes the element passed to it, so it only works on
|
||||
* sorted trees. (It's equivalent to using findpos234 to determine
|
||||
* the index of an element, and then passing that index to
|
||||
* delpos234.)
|
||||
*
|
||||
* Both functions return a pointer to the element they delete, for
|
||||
* the user to free or pass on elsewhere or whatever. If the index
|
||||
* is out of range (delpos234) or the element is already not in the
|
||||
* tree (del234) then they return NULL.
|
||||
*/
|
||||
void *del234(tree234 *t, void *e);
|
||||
void *delpos234(tree234 *t, int index);
|
||||
|
||||
/*
|
||||
* Return the total element count of a tree234.
|
||||
*/
|
||||
int count234(tree234 *t);
|
||||
|
||||
/*
|
||||
* Split a tree234 into two valid tree234s.
|
||||
*
|
||||
* splitpos234 splits at a given index. If `before' is TRUE, the
|
||||
* items at and after that index are left in t and the ones before
|
||||
* are returned; if `before' is FALSE, the items before that index
|
||||
* are left in t and the rest are returned.
|
||||
*
|
||||
* split234 splits at a given key. You can pass any of the
|
||||
* relations used with findrel234, except for REL234_EQ. The items
|
||||
* in the tree that satisfy the relation are returned; the
|
||||
* remainder are left.
|
||||
*/
|
||||
tree234 *splitpos234(tree234 *t, int index, int before);
|
||||
tree234 *split234(tree234 *t, void *e, cmpfn234 cmp, int rel);
|
||||
|
||||
/*
|
||||
* Join two tree234s together into a single one.
|
||||
*
|
||||
* All the elements in t1 are placed to the left of all the
|
||||
* elements in t2. If the trees are sorted, there will be a test to
|
||||
* ensure that this satisfies the ordering criterion, and NULL will
|
||||
* be returned otherwise. If the trees are unsorted, there is no
|
||||
* restriction on the use of join234.
|
||||
*
|
||||
* The tree returned is t1 (join234) or t2 (join234r), if the
|
||||
* operation is successful.
|
||||
*/
|
||||
tree234 *join234(tree234 *t1, tree234 *t2);
|
||||
tree234 *join234r(tree234 *t1, tree234 *t2);
|
||||
|
||||
/*
|
||||
* Make a complete copy of a tree234. Element pointers will be
|
||||
* reused unless copyfn is non-NULL, in which case it will be used
|
||||
* to copy each element. (copyfn takes two `void *' parameters; the
|
||||
* first is private state and the second is the element. A simple
|
||||
* copy routine probably won't need private state.)
|
||||
*/
|
||||
tree234 *copytree234(tree234 *t, copyfn234 copyfn, void *copyfnstate);
|
||||
|
||||
#endif /* TREE234_H */
|
Reference in New Issue
Block a user