File: PanexU.c

package info (click to toggle)
xpuzzles 5.4.3-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 3,576 kB
  • ctags: 3,879
  • sloc: ansic: 34,831; makefile: 1,815; sh: 1,526
file content (252 lines) | stat: -rw-r--r-- 5,681 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*-
# X-BASED PANEX(tm)
#
#  PanexU.c
#
###
#
#  Copyright (c) 1996 - 97	David Albert Bagley, bagleyd@bigfoot.com
#
#                   All Rights Reserved
#
#  Permission to use, copy, modify, and distribute this software and
#  its documentation for any purpose and without fee is hereby granted,
#  provided that the above copyright notice appear in all copies and
#  that both that copyright notice and this permission notice appear in
#  supporting documentation, and that the name of the author not be
#  used in advertising or publicity pertaining to distribution of the
#  software without specific, written prior permission.
#
#  This program is distributed in the hope that it will be "playable",
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
*/

/* Undo algorithm */

#include <stdio.h>
#include <stdlib.h>
#include <X11/IntrinsicP.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/CoreP.h>
#include "PanexP.h"

typedef struct _MoveRecord {
	/* int from, to; */
	unsigned char packed;	/* This makes assumptions on the data. */
} MoveRecord;

typedef struct _MoveStack {
	MoveRecord  move;
	struct _MoveStack *previous, *next;
} MoveStack;

static MoveStack *currMove, *lastMove, *firstMove;
static int  count;
PanexLoc   *startLoc[MAXSTACKS - 1] =
{NULL, NULL};

static void InitStack(void);
static void PushStack(MoveRecord move);
static void PopStack(MoveRecord * move);
static int  EmptyStack(void);
static void FlushStack(void);

static void
InitStack(void)
{
	if (!(lastMove = (MoveStack *) malloc(sizeof (MoveStack))))
		XtError("Not enough memory, exiting.");
	if (!(firstMove = (MoveStack *) malloc(sizeof (MoveStack))))
		XtError("Not enough memory, exiting.");
	firstMove->previous = lastMove->next = NULL;
	firstMove->next = lastMove;
	lastMove->previous = firstMove;
	count = 0;
}

static void
PushStack(MoveRecord move)
{
	if (!(currMove = (MoveStack *) malloc(sizeof (MoveStack))))
		XtError("Not enough memory, exiting.");
	lastMove->previous->next = currMove;
	currMove->previous = lastMove->previous;
	currMove->next = lastMove;
	lastMove->previous = currMove;
	currMove->move = move;
	count++;
}

static void
PopStack(MoveRecord * move)
{
	*move = lastMove->previous->move;
	currMove = lastMove->previous;
	lastMove->previous->previous->next = lastMove;
	lastMove->previous = lastMove->previous->previous;
	(void) free((void *) currMove);
	count--;
}

static int
EmptyStack(void)
{
	return (lastMove->previous == firstMove);
}

static void
FlushStack(void)
{
	while (lastMove->previous != firstMove) {
		currMove = lastMove->previous;
		lastMove->previous->previous->next = lastMove;
		lastMove->previous = lastMove->previous->previous;
		(void) free((void *) currMove);
	}
	count = 0;
}

/**********************************/

void
InitMoves(void)
{
	InitStack();
}

static void
WriteMove(MoveRecord * move, int from, int to)
{
	/* move->from = from; move->to = to; */
	move->packed = ((from & 0xF) << 4) + (to & 0xF);
}

static void
ReadMove(int *from, int *to, MoveRecord move)
{
	/* *from = move.from; *to = move.to; */
	*to = move.packed & 0xF;
	*from = (move.packed >> 4) & 0xF;
}

void
PutMove(int from, int to)
{
	MoveRecord  move;

	WriteMove(&move, from, to);
	PushStack(move);
}

void
GetMove(int *from, int *to)
{
	MoveRecord  move;

	PopStack(&move);
	ReadMove(from, to, move);
}

int
MadeMoves(void)
{
	return !EmptyStack();
}

void
FlushMoves(PanexWidget w)
{
	int         i, j;

	FlushStack();
	for (i = 0; i <= w->panex.mode; i++)
		for (j = 0; j < w->panex.tiles; j++)
			startLoc[i][j] = w->panex.positionOfTile[i][j];
}

int
NumMoves(void)
{
	return count;
}

void
ScanMoves(FILE * fp, PanexWidget w, int moves)
{
	int         fromStack, fromPosition, toStack, l;
	char        c;

	for (l = 0; l < moves; l++) {
		while ((c = getc(fp)) != EOF && c != SYMBOL);
		(void) fscanf(fp, "%d %d", &fromStack, &toStack);
		if ((fromPosition = TopOfStack(w, fromStack)) < 0 ||
		    MovePanex(w, fromStack, fromPosition, toStack) < 0)
			(void) printf("%d move from %d to %d can not be made.",
				      l, fromStack, toStack);
	}
}

void
PrintMoves(FILE * fp)
{
	int         from, to, counter = 0;

	currMove = firstMove->next;
	(void) fprintf(fp, "moves\tfrom\tto\n");
	while (currMove != lastMove) {
		ReadMove(&from, &to, currMove->move);
		(void) fprintf(fp, "%d%c\t%d\t%d\n", ++counter, SYMBOL, from, to);
		currMove = currMove->next;
	}
}

void
ScanStartPosition(FILE * fp, PanexWidget w)
{
	int         i, j;
	char        c;

	while ((c = getc(fp)) != EOF && c != SYMBOL);
	for (i = 0; i <= w->panex.mode; i++)
		for (j = 0; j < w->panex.tiles; j++)
			(void) fscanf(fp, "%d %d",
			     &(startLoc[i][j].stack), &(startLoc[i][j].loc));
}

void
PrintStartPosition(FILE * fp, PanexWidget w)
{
	int         i, j;

	(void) fprintf(fp, "\nstartingPosition%c\n", SYMBOL);
	for (i = 0; i <= w->panex.mode; i++) {
		for (j = 0; j < w->panex.tiles; j++) {
			(void) fprintf(fp, "%3d %3d  ", startLoc[i][j].stack,
				       startLoc[i][j].loc);
		}
		(void) fprintf(fp, "\n");
	}
	(void) fprintf(fp, "\n");
}

void
SetStartPosition(PanexWidget w)
{
	int         i, j;

	for (i = 0; i < MAXSTACKS; i++)
		for (j = 0; j <= w->panex.tiles; j++)
			w->panex.tileOfPosition[i][j].stack = -1;
	for (i = 0; i <= w->panex.mode; i++)
		for (j = 0; j < w->panex.tiles; j++) {
			w->panex.positionOfTile[i][j] = startLoc[i][j];
			w->panex.tileOfPosition[startLoc[i][j].stack][startLoc[i][j].loc].stack =
				i;
			w->panex.tileOfPosition[startLoc[i][j].stack][startLoc[i][j].loc].loc =
				j;
		}
	DrawAllTiles(w, w->panex.tileGC, w->panex.borderGC);
}